给定一个表示一系列移动( L 、 R 、 U和D )的字符串S和一个表示圆心为原点(0, 0)的圆的半径的整数R ,任务是检查是否有可能通过从字符串S 中选择任何移动子序列,从原点到达给定圆的圆周上的任何点。如果可以到达圆周上的一点,则打印“是” 。否则,打印“否” 。
每个方向需要执行的操作如下:
- 如果当前字符是L ,则将x坐标减1 。
- 如果当前字符是R ,则将x坐标增加1 。
- 如果当前字符是U ,则将y坐标增加1 。
- 如果当前字符是D ,则将y坐标减1 。
例子:
Input: S = “LLRULD”, R = 3
Output: Yes
Explanation:
Selecting the subsequence “LLL”, the change in coordinates by performing the sequence of moves are:
(0, 0) → ( -1, 0) → (-2, 0) → (-3, 0)
Since (-3, 0) lies on the circumference of the given circle, it is possible to reach the
Input: S = “ULRDLD”, R = 6
Output: No
朴素的方法:最简单的方法是生成给定字符串S 的所有可能子序列,如果存在任何导致从原点(0, 0)到达圆圆周的移动子序列,则打印“是” 。否则,打印“否” 。
时间复杂度: O(2 N )
辅助空间: O(1)
有效的方法:可以根据以下观察优化上述方法:
- 由于需要考虑的路径从原点(0, 0) 开始,如果字符串中每个字符L 、 R 、 U和D之间的最大计数值超过R ,则从原点到圆的周长存在。
- 如果存在任意两个值x和y,使得X和Y的平方和是R 2,则存在从原点到圆的圆周的三角形路径。
请按照以下步骤解决给定的问题:
- 将给定字符串S中的字符L 、 R 、 U和D的计数存储在一个变量中,分别说是cntL 、 cntR 、 cntU和cntD 。
- 如果cntL 、 cntR 、 cntU和cntD 的最大值至少为R ,则存在从原点到圆周的直线路径。因此,打印“是” 。
- 如果最大CNTL和CNTR和最大cntU和CNTD的的平方是至少R 2,然后打印“是”,因为存在从原点到圆的圆周的三角形路径。
- 如果上述情况均未出现,则打印“No” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if it is possible
// to reach any point on circumference
// of the given circle from (0,0)
string isPossible(string S, int R, int N)
{
// Stores the count of 'L', 'R'
int cntl = 0, cntr = 0;
// Stores the count of 'U', 'D'
int cntu = 0, cntd = 0;
// Traverse the string S
for (int i = 0; i < N; i++) {
// Update the count of L
if (S[i] == 'L')
cntl++;
// Update the count of R
else if (S[i] == 'R')
cntr++;
// Update the count of U
else if (S[i] == 'U')
cntu++;
// Update the count of D
else
cntd++;
}
// Condition 1 for reaching
// the circumference
if (max(max(cntl, cntr), max(cntu, cntd)) >= R)
return "Yes";
unordered_map mp;
int r_square = R * R;
for (int i = 1; i * i <= r_square; i++) {
// Store the the value
// of (i * i) in the Map
mp[i * i] = i;
// Check if (r_square - i*i)
// already present in HashMap
if (mp.find(r_square - i * i) != mp.end()) {
// If it is possible to reach the
// point (± mp[r_square - i*i], ± i)
if (max(cntl, cntr)
>= mp[r_square - i * i]
&& max(cntu, cntd) >= i)
return "Yes";
// If it is possible to reach the
// point (±i, ±mp[r_square-i*i])
if (max(cntl, cntr) >= i
&& max(cntu, cntd)
>= mp[r_square - i * i])
return "Yes";
}
}
// If it is impossible to reach
return "No";
}
// Driver Code
int main()
{
string S = "RDLLDDLDU";
int R = 5;
int N = S.length();
cout << isPossible(S, R, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to check if it is possible
// to reach any point on circumference
// of the given circle from (0,0)
static String isPossible(String S, int R, int N)
{
// Stores the count of 'L', 'R'
int cntl = 0, cntr = 0;
// Stores the count of 'U', 'D'
int cntu = 0, cntd = 0;
// Traverse the string S
for(int i = 0; i < N; i++)
{
// Update the count of L
if (S.charAt(i) == 'L')
cntl++;
// Update the count of R
else if (S.charAt(i) == 'R')
cntr++;
// Update the count of U
else if (S.charAt(i) == 'U')
cntu++;
// Update the count of D
else
cntd++;
}
// Condition 1 for reaching
// the circumference
if (Math.max(Math.max(cntl, cntr),
Math.max(cntu, cntd)) >= R)
return "Yes";
HashMap mp = new HashMap<>();
int r_square = R * R;
for(int i = 1; i * i <= r_square; i++)
{
// Store the the value
// of (i * i) in the Map
mp.put(i * i, i);
// Check if (r_square - i*i)
// already present in HashMap
if (mp.containsKey(r_square - i * i))
{
// If it is possible to reach the
// point (± mp[r_square - i*i], ± i)
if (Math.max(cntl, cntr) >=
mp.get(r_square - i * i) &&
Math.max(cntu, cntd) >= i)
return "Yes";
// If it is possible to reach the
// point (±i, ±mp[r_square-i*i])
if (Math.max(cntl, cntr) >= i &&
Math.max(cntu, cntd) >=
mp.get(r_square - i * i))
return "Yes";
}
}
// If it is impossible to reach
return "No";
}
// Driver Code
public static void main(String[] args)
{
String S = "RDLLDDLDU";
int R = 5;
int N = S.length();
System.out.println(isPossible(S, R, N));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to check if it is possible
# to reach any point on circumference
# of the given circle from (0,0)
def isPossible(S, R, N):
# Stores the count of 'L', 'R'
cntl = 0
cntr = 0
# Stores the count of 'U', 'D'
cntu = 0
cntd = 0
# Traverse the string S
for i in range(N):
# Update the count of L
if (S[i] == 'L'):
cntl += 1
# Update the count of R
elif (S[i] == 'R'):
cntr += 1
# Update the count of U
elif (S[i] == 'U'):
cntu += 1
# Update the count of D
else:
cntd += 1
# Condition 1 for reaching
# the circumference
if (max(max(cntl, cntr), max(cntu, cntd)) >= R):
return "Yes"
mp = {}
r_square = R * R
i = 1
while i * i <= r_square:
# Store the the value
# of (i * i) in the Map
mp[i * i] = i
# Check if (r_square - i*i)
# already present in HashMap
if ((r_square - i * i) in mp):
# If it is possible to reach the
# point (± mp[r_square - i*i], ± i)
if (max(cntl, cntr) >= mp[r_square - i * i] and
max(cntu, cntd) >= i):
return "Yes"
# If it is possible to reach the
# point (±i, ±mp[r_square-i*i])
if (max(cntl, cntr) >= i and
max(cntu, cntd) >= mp[r_square - i * i]):
return "Yes"
i += 1
# If it is impossible to reach
return "No"
# Driver Code
if __name__ == "__main__":
S = "RDLLDDLDU"
R = 5
N = len(S)
print(isPossible(S, R, N))
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to check if it is possible
// to reach any point on circumference
// of the given circle from (0,0)
static string isPossible(string S, int R, int N)
{
// Stores the count of 'L', 'R'
int cntl = 0, cntr = 0;
// Stores the count of 'U', 'D'
int cntu = 0, cntd = 0;
// Traverse the string S
for(int i = 0; i < N; i++)
{
// Update the count of L
if (S[i] == 'L')
cntl++;
// Update the count of R
else if (S[i] == 'R')
cntr++;
// Update the count of U
else if (S[i] == 'U')
cntu++;
// Update the count of D
else
cntd++;
}
// Condition 1 for reaching
// the circumference
if (Math.Max(Math.Max(cntl, cntr),
Math.Max(cntu, cntd)) >= R)
return "Yes";
Dictionary mp = new Dictionary();
int r_square = R * R;
for(int i = 1; i * i <= r_square; i++)
{
// Store the the value
// of (i * i) in the Map
mp.Add(i * i, i);
// Check if (r_square - i*i)
// already present in HashMap
if (mp.ContainsKey(r_square - i * i))
{
// If it is possible to reach the
// point (± mp[r_square - i*i], ± i)
if (Math.Max(cntl, cntr) >=
mp[r_square - i * i] &&
Math.Max(cntu, cntd) >= i)
return "Yes";
// If it is possible to reach the
// point (±i, ±mp[r_square-i*i])
if (Math.Max(cntl, cntr) >= i &&
Math.Max(cntu, cntd) >=
mp[r_square - i * i])
return "Yes";
}
}
// If it is impossible to reach
return "No";
}
static public void Main ()
{
string S = "RDLLDDLDU";
int R = 5;
int N = S.Length;
Console.WriteLine(isPossible(S, R, N));
}
}
// This code is contributed by offbeat
Javascript
Yes
时间复杂度: O(N + R)
辅助空间: O(R 2 )
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。