根据给定条件从圆形容器打印给定字符串所需的最短时间
给定一个圆形容器,该容器由从“a”到“z”的小写字母、最初指向字母“a”的指针和字符串str组成,任务是找到从循环中打印给定字符串str所需的最短时间容器,基于可以对每个字符执行的以下操作:
- 单位时间内将指针逆时针或顺时针移动一个字符
- 在一个单位时间内打印字符,然后将指针移动到字符串的下一个索引处
例子:
Input: str = “zcd”
Output: 8
Explanation: The steps are as follows:
- Move the pointer anti-clockwise to ‘z’ in 1 second
- Type the character ‘z’ in 1 second
- Move the pointer clockwise to ‘c’ in 3 seconds
- Type the character ‘c’ in 1 second
- Move the pointer clockwise to ‘d’ in 1 seconds
- Type the character ‘d’ in 1 second.
Input: str =”zjpc”
Output: 34
Explanation: The steps are as follows:
- Move the pointer anti-clockwise to ‘z’ in 1 second
- Type the character ‘z’ in 1 second
- Move the pointer clockwise to ‘j’ in 10 seconds
- Type the character ‘j’ in 1 second
- Move the pointer clockwise to ‘p’ in 6 seconds
- Type the character ‘p’ in 1 second
- Move the pointer anti-clockwise to ‘c’ in 13 seconds.
方法:可以按照以下步骤解决给定的问题:
- 计算从当前指针索引在两个方向上到达字符索引所需的时间:
- 顺时针时间是通过取两个指数的绝对差来计算的
- 逆时针时间是通过从 26 中减去顺时针时间来计算的
- 将上一步中获得的两次时间的最小值添加到答案中
- 然后打印字符,一个单位时间也被添加到答案中。
最后将得到的答案打印为所需的最短时间。
下面是上述方法的实现:
C++
// C++ implementation for the above approach
#include
using namespace std;
// Function to calculate minimum time to
// print all characters in the string
void minTime(string word)
{
int ans = 0;
// Current element where the
// pointer is pointing
int curr = 0;
for (int i = 0; i < word.length(); i++) {
// Find index of that element
int k = word[i] - 'a';
// Calculate absolute difference
// between pointer index and character
// index as clockwise distance
int a = abs(curr - k);
// Subtract clockwise time from
// 26 to get anti-clockwise time
int b = 26 - abs(curr - k);
// Add minimum of both times to
// the answer
ans += min(a, b);
// Add one unit time to print
// the character
ans++;
curr = word[i] - 'a';
}
// Print the final answer
cout << ans;
}
// Driver code
int main()
{
// Given string word
string str = "zjpc";
// Function call
minTime(str);
return 0;
}
Java
// Java implementation for the above approach
class GFG{
// Function to calculate minimum time to
// print all characters in the string
static void minTime(String word)
{
int ans = 0;
// Current element where the
// pointer is pointing
int curr = 0;
for (int i = 0; i < word.length(); i++) {
// Find index of that element
int k = (int)word.charAt(i) - 97;
// Calculate absolute difference
// between pointer index and character
// index as clockwise distance
int a = Math.abs(curr - k);
// Subtract clockwise time from
// 26 to get anti-clockwise time
int b = 26 - Math.abs(curr - k);
// Add minimum of both times to
// the answer
ans += Math.min(a, b);
// Add one unit time to print
// the character
ans++;
curr = (int)word.charAt(i) - 97;
}
// Print the final answer
System.out.print(ans);
}
// Driver code
public static void main(String[] args)
{
// Given string word
String str = "zjpc";
// Function call
minTime(str);
}
}
// This code is contributed by AnkThon
Python3
# Python 3 implementation for the above approach
# Function to calculate minimum time to
# print all characters in the string
def minTime(word):
ans = 0
# Current element where the
# pointer is pointing
curr = 0
for i in range(len(word)):
# Find index of that element
k = ord(word[i]) - 97
# Calculate absolute difference
# between pointer index and character
# index as clockwise distance
a = abs(curr - k)
# Subtract clockwise time from
# 26 to get anti-clockwise time
b = 26 - abs(curr - k)
# Add minimum of both times to
# the answer
ans += min(a, b)
# Add one unit time to print
# the character
ans += 1
curr = ord(word[i]) - 97
# Print the final answer
print(ans)
# Driver code
if __name__ == '__main__':
# Given string word
str = "zjpc"
# Function call
minTime(str)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# implementation for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate minimum time to
// print all characters in the string
static void minTime(string word)
{
int ans = 0;
// Current element where the
// pointer is pointing
int curr = 0;
for (int i = 0; i < word.Length; i++) {
// Find index of that element
int k = (int)word[i] - 97;
// Calculate absolute difference
// between pointer index and character
// index as clockwise distance
int a = Math.Abs(curr - k);
// Subtract clockwise time from
// 26 to get anti-clockwise time
int b = 26 - Math.Abs(curr - k);
// Add minimum of both times to
// the answer
ans += Math.Min(a, b);
// Add one unit time to print
// the character
ans++;
curr = (int)word[i] - 97;
}
// Print the final answer
Console.Write(ans);
}
// Driver code
public static void Main()
{
// Given string word
string str = "zjpc";
// Function call
minTime(str);
}
}
// This code is contributed by ipg2016107.
Javascript
输出
34
时间复杂度: O(N),其中 N 是给定字符串str 的长度
辅助空间: O(1)