从任一端到给定字符串中最大和最小字符的最小跳转
给定一个字符串str ,任务是找到达到字典上最大和最小字符所需的最小移动次数。在一次移动中,可以从给定字符串的最左侧或最右侧进行跳跃。
例子:
Input: str = AEDCB, N = 5
Output: 2
Explanation: take two steps from leftmost side to reach A and E
Input: str = BACDEFHG, N = 8
Output: 4
Explanation: take two steps from leftmost side to reach A and 2 steps from rightmost side to reach H(2+2=4)
Input: str = CDBA, N = 4
Output: 3
Explanation: take three steps from rightmost side to reach A and then D
方法:这个问题是基于实现的。请按照以下步骤解决给定的问题。
- 查找字符串中最大和最小元素的索引
- 计算这些索引的最小值和最大值,找到可以采取的min_steps和max_steps
- 只有三种可能的方式来达到这两个元素
- 从头开始遍历并覆盖两个元素,即 min_steps+1
- 从最后一个遍历并覆盖两个元素,即n-max_steps
- 从开始和结束都遍历,即min_steps+1+n-max_steps
- 最终的答案是所有可能的三种遍历方式中的最小值。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Minimum number of moves required to
// reach and largest and smallest ASCII values
int min_moves(string s, int n)
{
int maxpos = 0, minpos = 0;
// Finding index of maximum
// and minimum element in string
for (int i = 0; i < n; i++) {
if (s[i] > s[maxpos])
maxpos = i;
if (s[i] < s[minpos])
minpos = i;
}
// Calculating minimum ans maximum steps
// that can be taken
int min_steps = min(maxpos, minpos);
int max_steps = max(maxpos, minpos);
// Only three possible ways
// to reach both elements
int ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
int result;
// Minimum steps in all three ways
result = min(ans1, min(ans2, ans3));
// Return the final result
return result;
}
// Driver code
int main()
{
string str = "BACDEFHG";
int N = str.length();
cout << min_moves(str, N);
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG
{
// Minimum number of moves required to
// reach and largest and smallest ASCII values
static int min_moves(String s, int n)
{
int maxpos = 0, minpos = 0;
// Finding index of maximum
// and minimum element in string
for (int i = 0; i < n; i++) {
if (s.charAt(i) > s.charAt(maxpos))
maxpos = i;
if (s.charAt(i) < s.charAt(minpos))
minpos = i;
}
// Calculating minimum ans maximum steps
// that can be taken
int min_steps = Math.min(maxpos, minpos);
int max_steps = Math.max(maxpos, minpos);
// Only three possible ways
// to reach both elements
int ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
int result;
// Minimum steps in all three ways
result = Math.min(ans1, Math.min(ans2, ans3));
// Return the final result
return result;
}
// Driver code
public static void main (String[] args) {
String str = "BACDEFHG";
int N = str.length();
System.out.println(min_moves(str, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python code for the above approach
# Minimum number of moves required to
# reach and largest and smallest ASCII values
def min_moves(s, n):
maxpos = 0;
minpos = 0;
# Finding index of maximum
# and minimum element in string
for i in range(n):
if (s[i] > s[maxpos]):
maxpos = i;
if (s[i] < s[minpos]):
minpos = i;
# Calculating minimum ans maximum steps
# that can be taken
min_steps = min(maxpos, minpos);
max_steps = max(maxpos, minpos);
# Only three possible ways
# to reach both elements
ans1, ans2, ans3 = 0,0,0;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
result=0;
# Minimum steps in all three ways
result = min(ans1, min(ans2, ans3));
# Return the final result
return result;
# Driver code
if __name__ == '__main__':
str = "BACDEFHG";
N = len(str);
print(min_moves(str, N));
# This code is contributed by shikhasingrajput
C#
// C# program for above approach
using System;
class GFG{
// Minimum number of moves required to
// reach and largest and smallest ASCII values
static int min_moves(string s, int n)
{
int maxpos = 0, minpos = 0;
// Finding index of maximum
// and minimum element in string
for(int i = 0; i < n; i++)
{
if (s[i] > s[maxpos])
maxpos = i;
if (s[i] < s[minpos])
minpos = i;
}
// Calculating minimum ans maximum steps
// that can be taken
int min_steps = Math.Min(maxpos, minpos);
int max_steps = Math.Max(maxpos, minpos);
// Only three possible ways
// to reach both elements
int ans1, ans2, ans3;
ans1 = n - min_steps;
ans2 = max_steps + 1;
ans3 = min_steps + 1 + n - max_steps;
int result;
// Minimum steps in all three ways
result = Math.Min(ans1, Math.Min(ans2, ans3));
// Return the final result
return result;
}
// Driver code
public static void Main(string[] args)
{
String str = "BACDEFHG";
int N = str.Length;
Console.WriteLine(min_moves(str, N));
}
}
// This code is contributed by ukasp
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(1)