通过基于给定命令在 X 轴上移动来最大化从原点的绝对位移
给定一个长度为N的字符串S ,其中字符串的每个字符都等于“L”、“R”或“?” ,任务是通过从原点(0, 0)开始在 X 轴上按照给定命令移动来找到从原点的最大绝对位移:
- 'L':在负 X 方向移动一个单位。
- 'R':沿 X 正方向移动一个单位。
- '?':可以在负 X 方向或正 X 方向移动一个单位。
例子:
Input: S = “LL??R”
Output: 3
Explanation:
One of the possible way to move is:
- S[0] = ‘L’, move one unit in -ve X direction, so displacement becomes equal to -1.
- S[1] = ‘L’, move one unit in -ve X direction, so displacement becomes equal to -2.
- S[2] = ‘?’, move one unit in -ve X direction, so displacement becomes equal to -3.
- S[3] = ‘?’, move one unit in -ve X direction, so displacement becomes equal to -4.
- S[4] = ‘R’, move one unit in +ve X direction, so displacement becomes equal to -3.
Therefore, the absolute displacement is abs(-3)=3, and also it is the maximum absolute displacement possible.
Input: S = “?RRR?”
Output: 5
天真的方法:解决问题的最简单方法是尝试替换“?”使用递归使用“L”或“R” ,然后打印获得的最大绝对位移。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Recursive function to find the maximum
// absolute displacement from origin by
// performing the given set of moves
int DistRecursion(string S, int i, int dist)
{
// If i is equal to N
if (i == S.length())
return abs(dist);
// If S[i] is equal to 'L'
if (S[i] == 'L')
return DistRecursion(S, i + 1, dist - 1);
// If S[i] is equal to 'R'
if (S[i] == 'R')
return DistRecursion(S, i + 1, dist + 1);
// If S[i] is equal to '?'
return max(DistRecursion(S, i + 1, dist - 1),
DistRecursion(S, i + 1, dist + 1));
}
// Function to find the maximum absolute
// displacement from the origin
int maxDistance(string S)
{
// Return the maximum absolute
// displacement
return DistRecursion(S, 0, 0);
}
// Driver Code
int main()
{
// Input
string S = "?RRR?";
// Function call
cout << maxDistance(S);
return 0;
}
// This code is contributed by lokesh potta.
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Recursive function to find the maximum
// absolute displacement from origin by
// performing the given set of moves
static int DistRecursion(String S, int i, int dist)
{
char[] ch = S.toCharArray();
// If i is equal to N
if (i == ch.length)
return Math.abs(dist);
// If S[i] is equal to 'L'
if (ch[i] == 'L')
return DistRecursion(S, i + 1, dist - 1);
// If S[i] is equal to 'R'
if (ch[i] == 'R')
return DistRecursion(S, i + 1, dist + 1);
// If S[i] is equal to '?'
return Math.max(DistRecursion(S, i + 1,
dist - 1),
DistRecursion(S, i + 1, dist + 1));
}
// Function to find the maximum absolute
// displacement from the origin
static int maxDistance(String S)
{
// Return the maximum absolute
// displacement
return DistRecursion(S, 0, 0);
}
// Driver Code
public static void main(String[] args)
{
// Input
String S = "?RRR?";
// Function call
System.out.print(maxDistance(S));
}
}
// This code is contributed by ukasp.
Python3
# Python3 program for the above approach
# Recursive function to find the maximum
# absolute displacement from origin by
# performing the given set of moves
def DistRecursion(S, i, dist):
# If i is equal to N
if i == len(S):
return abs(dist)
# If S[i] is equal to 'L'
if S[i] == 'L':
return DistRecursion(S, i + 1, dist-1)
# If S[i] is equal to 'R'
if S[i] == 'R':
return DistRecursion(S, i + 1, dist + 1)
# If S[i] is equal to '?'
return max(DistRecursion(S, i + 1, dist-1),
DistRecursion(S, i + 1, dist + 1))
# Function to find the maximum absolute
# displacement from the origin
def maxDistance(S):
# Return the maximum absolute
# displacement
return DistRecursion(S, 0, 0)
# Driver Code
# Input
S = "?RRR?"
# Function call
print(maxDistance(S))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Recursive function to find the maximum
// absolute displacement from origin by
// performing the given set of moves
static int DistRecursion(string S, int i, int dist)
{
// If i is equal to N
if (i == S.Length)
return Math.Abs(dist);
// If S[i] is equal to 'L'
if (S[i] == 'L')
return DistRecursion(S, i + 1, dist - 1);
// If S[i] is equal to 'R'
if (S[i] == 'R')
return DistRecursion(S, i + 1, dist + 1);
// If S[i] is equal to '?'
return Math.Max(DistRecursion(S, i + 1,
dist - 1),
DistRecursion(S, i + 1, dist + 1));
}
// Function to find the maximum absolute
// displacement from the origin
static int maxDistance(string S)
{
// Return the maximum absolute
// displacement
return DistRecursion(S, 0, 0);
}
// Driver Code
public static void Main()
{
// Input
string S = "?RRR?";
// Function call
Console.Write(maxDistance(S));
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
int count(string s, char c) {
int ans = 0;
for(int i = 0; i < s.length(); i++)
{
if (c == s[i])
{
ans++;
}
}
return ans;
}
// Function to find the maximum absolute
// displacement from the origin
int maxDistance(string S) {
// Stores the count of 'L'
int l = count(S, 'L');
// Stores the count of 'R'
int r = count(S, 'R');
// Stores the length of S
int N = S.length();
// Return the answer
return abs(N - min(l, r));
}
int main()
{
// Input
string S = "?RRR?";
// Function call
cout << maxDistance(S);
return 0;
}
// This code is contributed by divyesh072019.
Java
// Java program for the above approach
// Function to find the maximum absolute
// displacement from the origin
class GFG {
static int maxDistance(String S) {
// Stores the count of 'L'
int l = count(S, 'L');
// Stores the count of 'R'
int r = count(S, 'R');
// Stores the length of S
int N = S.length();
// Return the answer
return Math.abs(N - Math.min(l, r));
}
private static int count(String s, char c) {
int ans = 0;
for (char i : s.toCharArray())
if (c == i)
ans++;
return ans;
}
// Driver Code
public static void main(String[] args) {
// Input
String S = "?RRR?";
// Function call
System.out.println(maxDistance(S));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the maximum absolute
# displacement from the origin
def maxDistance(S):
# Stores the count of 'L'
l = S.count('L')
# Stores the count of 'R'
r = S.count('R')
# Stores the length of S
N = len(S)
# Return the answer
return abs(N - min(l, r))
# Driver Code
# Input
S = "?RRR?"
# Function call
print(maxDistance(S))
C#
// C# program for the above approach
// Function to find the maximum absolute
// displacement from the origin
using System;
public class GFG {
static int maxDistance(String S) {
// Stores the count of 'L'
int l = count(S, 'L');
// Stores the count of 'R'
int r = count(S, 'R');
// Stores the length of S
int N = S.Length;
// Return the answer
return Math.Abs(N - Math.Min(l, r));
}
private static int count(String s, char c) {
int ans = 0;
foreach (char i in s.ToCharArray())
if (c == i)
ans=ans+1;
return ans;
}
// Driver Code
public static void Main(String[] args) {
// Input
String S = "?RRR?";
// Function call
Console.WriteLine(maxDistance(S));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
5
时间复杂度: O(2 N )
辅助空间: O(1)
有效方法:上述方法可以根据观察得到优化,即在“?”时将获得最大绝对位移。替换为最大出现字符。请按照以下步骤解决问题:
- 在S中查找字符'L'的计数,并将其存储在变量l中。
- 在S中查找字符'R'的计数,并将其存储在变量 say r中。
- 将答案打印为N – min(l, r)
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
int count(string s, char c) {
int ans = 0;
for(int i = 0; i < s.length(); i++)
{
if (c == s[i])
{
ans++;
}
}
return ans;
}
// Function to find the maximum absolute
// displacement from the origin
int maxDistance(string S) {
// Stores the count of 'L'
int l = count(S, 'L');
// Stores the count of 'R'
int r = count(S, 'R');
// Stores the length of S
int N = S.length();
// Return the answer
return abs(N - min(l, r));
}
int main()
{
// Input
string S = "?RRR?";
// Function call
cout << maxDistance(S);
return 0;
}
// This code is contributed by divyesh072019.
Java
// Java program for the above approach
// Function to find the maximum absolute
// displacement from the origin
class GFG {
static int maxDistance(String S) {
// Stores the count of 'L'
int l = count(S, 'L');
// Stores the count of 'R'
int r = count(S, 'R');
// Stores the length of S
int N = S.length();
// Return the answer
return Math.abs(N - Math.min(l, r));
}
private static int count(String s, char c) {
int ans = 0;
for (char i : s.toCharArray())
if (c == i)
ans++;
return ans;
}
// Driver Code
public static void main(String[] args) {
// Input
String S = "?RRR?";
// Function call
System.out.println(maxDistance(S));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to find the maximum absolute
# displacement from the origin
def maxDistance(S):
# Stores the count of 'L'
l = S.count('L')
# Stores the count of 'R'
r = S.count('R')
# Stores the length of S
N = len(S)
# Return the answer
return abs(N - min(l, r))
# Driver Code
# Input
S = "?RRR?"
# Function call
print(maxDistance(S))
C#
// C# program for the above approach
// Function to find the maximum absolute
// displacement from the origin
using System;
public class GFG {
static int maxDistance(String S) {
// Stores the count of 'L'
int l = count(S, 'L');
// Stores the count of 'R'
int r = count(S, 'R');
// Stores the length of S
int N = S.Length;
// Return the answer
return Math.Abs(N - Math.Min(l, r));
}
private static int count(String s, char c) {
int ans = 0;
foreach (char i in s.ToCharArray())
if (c == i)
ans=ans+1;
return ans;
}
// Driver Code
public static void Main(String[] args) {
// Input
String S = "?RRR?";
// Function call
Console.WriteLine(maxDistance(S));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
5
时间复杂度: O(N)
辅助空间: O(1)