给定两个整数A和B表示第一象限中一个点的坐标,任务是找到到达原点所需的最少步数。从点(i, j) 开始的所有可能的移动是(i – 1, j)、(i, j – 1)或(i, j) (停留在同一位置)。
注意:不允许连续两次向同一方向移动。
例子:
Input: A = 4, B = 0
Output: 7
Explanation:
Below are the movements from the given points to origin:
(4, 0) → (3, 0) → (3, 0)(stays) → (2, 0) → (2, 0)(stays) → (1, 0) → (1, 0)(stays) → (0, 0).
Hence, 7 moves are required to reach origin.
Input: A = 3, B = 5
Output: 9
Explanation:
Below are the movements from the given points to origin:
(3, 5) → (3, 4) → (2, 4) → (2, 3) → (1, 3) → (1, 2) → (0, 2) → (0, 1) → (0, 1)(stays) → (0, 0).
Hence, 9 moves are required to reach origin.
朴素的方法:最简单的方法是递归。这个想法是递归地考虑从每个点开始的所有可能的移动,并为每个点计算到达原点所需的最小步数。
时间复杂度: O(3 max(A, B) )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是基于以下观察:如果x 和 y 坐标之间的绝对差为 1 或 0 ,则到达原点所需的最小步数为(a + b) 。否则,需要(2 * abs(a – b) – 1) 次移动才能到达(k, k) ,其中k是a, b的最小值。
Therefore, the minimum number of steps required to reach origin from (a, b) is equal to = (Steps required to reach (k, k) + Steps required to reach (0, 0) from (k, k)) = (2 * abs(a – b) – 1) + (2 * k)
请按照以下步骤解决问题:
- 初始化一个变量ans ,它存储从(a, b)到达原点所需的最少步数。
- 如果a和b的绝对差为1或0 ,则将ans更新为(a + b) 。
- 否则:
- 找到a, b的最小值,并将其存储在变量k 中。
- 使用公式,更新ans = (2 * abs(a – b) – 1) + (2 * k) 。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum moves
// required to reach origin from (a, b)
void findMinMoves(int a, int b)
{
// Stores the minimum number of moves
int ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || abs(a - b) == 1) {
ans = a + b;
}
else {
// Store the minimum of a, b
int k = min(a, b);
// Store the maximum of a, b
int j = max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
// Given co-ordinates
int a = 3, b = 5;
// Function Call
findMinMoves(a, b);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the minimum moves
// required to reach origin from (a, b)
static void findMinMoves(int a, int b)
{
// Stores the minimum number of moves
int ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || Math.abs(a - b) == 1)
{
ans = a + b;
}
else
{
// Store the minimum of a, b
int k = Math.min(a, b);
// Store the maximum of a, b
int j = Math.max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
System.out.print(ans);
}
// Driver Code
public static void main (String[] args)
{
// Given co-ordinates
int a = 3, b = 5;
// Function Call
findMinMoves(a, b);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# function to find the minimum moves
# required to reach origin from (a, b)
def findMinMoves(a, b):
# Stores the minimum number of moves
ans = 0
# Check if the absolute
# difference is 1 or 0
if (a == b or abs(a - b) == 1):
ans = a + b
else:
# Store the minimum of a, b
k = min(a, b)
# Store the maximum of a, b
j = max(a, b)
ans = 2 * k + 2 * (j - k) - 1
# Prthe answer
print (ans)
# Driver Code
if __name__ == '__main__':
# Given co-ordinates
a,b = 3, 5
# Function Call
findMinMoves(a, b)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the minimum moves
// required to reach origin from (a, b)
static void findMinMoves(int a, int b)
{
// Stores the minimum number of moves
int ans = 0;
// Check if the absolute
// difference is 1 or 0
if (a == b || Math.Abs(a - b) == 1)
{
ans = a + b;
}
else
{
// Store the minimum of a, b
int k = Math.Min(a, b);
// Store the maximum of a, b
int j = Math.Max(a, b);
ans = 2 * k + 2 * (j - k) - 1;
}
// Print the answer
Console.Write(ans);
}
// Driver Code
public static void Main()
{
// Given co-ordinates
int a = 3, b = 5;
// Function Call
findMinMoves(a, b);
}
}
// This code is contributed by chitranayal.
Javascript
9
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。