给定两个长度相同的数字X和Y ,任务是找到需要与X 的所有数字相加以使其大于Y的最小数字d 。
例子:
Input: X = 123, Y = 222
Output: 1
Explanation:
Add 1 to all digits of X
Then X becomes {2, 3, 4} which is
lexicographically larger than {2, 2, 2}.
Input: X = 4512, Y = 2998
Output: 0
Explanation:
X is already lexicographically larger than Y
so the answer will be 0.
方法:这个问题可以很容易的解决,分成三种情况
- 案例 1:查找 X 是否已经在字典序上大于 Y。如果是,那么我们不需要做任何事情。
- 情况 2:否则将(Y[0] – X[0]) 添加到 X 的所有元素,然后检查 X 是否按字典顺序大于 Y。
- 情况 3:如果它仍然不大于,那么答案将是(Y[0] – X[0]) + 1,因为 X 的第一个元素变得大于 Y 的第一个元素意味着 X[0] > Y[0] .
下面是上述方法的实现:
C++
// C++ program to find Minimum number to be added
// to all digits of X to make X > Y
#include
using namespace std;
// Function to check if X
// is lexicographically larger Y
bool IsLarger(int X[], int Y[], int n)
{
for (int i = 0; i < n; i++) {
// It is lexicographically larger
if (X[i] < Y[i]) {
return false;
}
}
return true;
}
// Utility function to check
// minimum value of d
int solve(int X[], int Y[], int n)
{
int ans = 0;
// If X is already larger
// do not need to add anything
if (IsLarger(X, Y, n)) {
ans = 0;
}
else {
// Adding d to all elements of X
int d = Y[0] - X[0];
for (int i = 0; i < n; i++) {
X[i] += d;
}
// If X is larger now
// print d
if (IsLarger(X, Y, n)) {
ans = d;
}
// else print d + 1
else {
ans = d + 1;
}
}
return ans;
}
// Driver Code
int main()
{
// Taking the numbers as sequences
int X[] = { 2, 3, 6, 9 };
int Y[] = { 3, 4, 8, 1 };
int n = sizeof(X) / sizeof(X[0]);
cout << solve(X, Y, n);
return 0;
}
Java
// Java program to find Minimum number to be added
// to all digits of X to make X > Y
import java.util.*;
class GFG
{
// Function to check if X
// is lexicographically larger Y
static boolean IsLarger(int[] X,
int[] Y, int n)
{
for (int i = 0; i < n; i++)
{
// It is lexicographically larger
if (X[i] < Y[i])
{
return false;
}
}
return true;
}
// Utility function to check
// minimum value of d
static int solve(int X[], int Y[], int n)
{
int ans = 0;
// If X is already larger
// do not need to add anything
if (IsLarger(X, Y, n))
ans = 0;
else
{
// Adding d to all elements of X
int d = Y[0] - X[0];
for (int i = 0; i < n; i++)
X[i] += d;
// If X is larger now
// print d
if (IsLarger(X, Y, n))
ans = d;
// else print d + 1
else
{
ans = d + 1;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Taking the numbers as sequences
int X[] = { 2, 3, 6, 9 };
int Y[] = { 3, 4, 8, 1 };
int n = X.length;
System.out.println(solve(X, Y, n));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 program to find Minimum number to be added
# to all digits of X to make X > Y
# Function to check if X
# is lexicographically larger Y
def IsLarger(X, Y, n) :
for i in range(n) :
# It is lexicographically larger
if (X[i] < Y[i]) :
return False;
return True;
# Utility function to check
# minimum value of d
def solve(X, Y, n) :
ans = 0;
# If X is already larger
# do not need to add anything
if (IsLarger(X, Y, n)) :
ans = 0;
else :
# Adding d to all elements of X
d = Y[0] - X[0];
for i in range(n) :
X[i] += d;
# If X is larger now
# print d
if (IsLarger(X, Y, n)) :
ans = d;
# else print d + 1
else :
ans = d + 1;
return ans;
# Driver Code
if __name__ == "__main__" :
# Taking the numbers as sequences
X = [ 2, 3, 6, 9 ];
Y = [ 3, 4, 8, 1 ];
n = len(X);
print(solve(X, Y, n));
# This code is contributed by AnkitRai01
C#
// C# program to find Minimum number to be.Added
// to all digits of X to make X > Y
using System;
class GFG
{
// Function to check if X
// is lexicographically larger Y
static bool IsLarger(int[] X,
int[] Y, int n)
{
for (int i = 0; i < n; i++)
{
// It is lexicographically larger
if (X[i] < Y[i])
{
return false;
}
}
return true;
}
// Utility function to check
// minimum value of d
static int solve(int []X, int []Y, int n)
{
int ans = 0;
// If X is already larger
// do not need to.Add anything
if (IsLarger(X, Y, n))
ans = 0;
else
{
// Adding d to all elements of X
int d = Y[0] - X[0];
for (int i = 0; i < n; i++)
X[i] += d;
// If X is larger now
// print d
if (IsLarger(X, Y, n))
ans = d;
// else print d + 1
else
{
ans = d + 1;
}
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Taking the numbers as sequences
int []X = { 2, 3, 6, 9 };
int []Y = { 3, 4, 8, 1 };
int n = X.Length;
Console.WriteLine(solve(X, Y, n));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
2
时间复杂度: ,其中 N 是 X 或 Y 的长度
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。