给定一个数字N 、一个数组arr[]和一个目标数字K ,任务是通过选择任何数组元素并将每个元素中的N更改为(N + arr[i]) mod 100000来找到达到K的最小移动次数移动。
例子:
Input: N = 99880, K = 89, arr = {100, 3}
Output: 5
Explanation: Number “100” is used 2 times and the number “3” is pushed 3 times, resulting in K from N
Input: N = 10000, K = 10004, arr = {1}
Output: 4
方法:上述问题可以用动态规划解决,因为它有重叠的子问题和最优子结构。初始化dp表,其中dp[i]存储在 N 上达到状态i所需的最小移动,而dp[target]将是所需的答案。从每个arr[i] 中,找到所有可达状态并最小化dp值。请按照以下步骤操作:
- 初始化一个数组,比如dp[]来存储达到状态i所需的最小移动。
- 迭代数组 arr 并检查当前数字 N 的每个位置为x :
- 如果dp[x]等于-1,则继续。
- 否则,迭代 while 循环直到dp[next]等于-1或dp[next]大于dp[x] + 1并更新next为(x+buttons[i])%100000和dp[next]为dp[ x]+ 1 。
- 最后,返回dp[target]的值。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the minimum moves
// to reach K from N
int minPushes(int N, int K, vector arr)
{
// Intialization of dp vector
vector dp(100000, -1);
// dp[i] = minimum pushes
// required to reach i
dp[N] = 0;
// Traversing through the buttons
for (int i = 0; i < arr.size(); i++) {
// Iterating through all the positions
for (int xx = 0; xx < 100000; xx++) {
int x = xx;
// If not visited
if (dp[x] == -1)
continue;
// Next status of lock
int next = (x + arr[i]) % 100000;
while (dp[next] == -1
|| dp[next] > dp[x] + 1) {
dp[next] = dp[x] + 1;
// Advance to next state
x = next;
next = (next + arr[i]) % 100000;
}
}
}
// Return the final dp[target]
return dp[K];
}
// Driver function
int main()
{
// Given Input
int N = 99880, K = 89;
vector arr{ 100, 3 };
// Function Call
cout << minPushes(N, K, arr);
return 0;
}
Java
// Java implementation of the above approach
class GFG{
// Function to find the minimum moves
// to reach K from N
static int minPushes(int N, int K, int[] arr)
{
// Intialization of dp vector
int[] dp = new int[100000];
for (int i = 0; i < dp.length; i++)
dp[i] = -1;
// dp[i] = minimum pushes
// required to reach i
dp[N] = 0;
// Traversing through the buttons
for (int i = 0; i < arr.length; i++) {
// Iterating through all the positions
for (int xx = 0; xx < 100000; xx++) {
int x = xx;
// If not visited
if (dp[x] == -1)
continue;
// Next status of lock
int next = (x + arr[i]) % 100000;
while (dp[next] == -1
|| dp[next] > dp[x] + 1) {
dp[next] = dp[x] + 1;
// Advance to next state
x = next;
next = (next + arr[i]) % 100000;
}
}
}
// Return the final dp[target]
return dp[K];
}
// Driver function
public static void main(String[] args)
{
// Given Input
int N = 99880, K = 89;
int[] arr = { 100, 3 };
// Function Call
System.out.print(minPushes(N, K, arr));
}
}
// This code is contributed by shikhasingrajput
Javascript
C#
// C# implementation of the above approach
using System;
public class GFG{
// Function to find the minimum moves
// to reach K from N
static int minPushes(int N, int K, int[] arr)
{
// Intialization of dp vector
int[] dp = new int[100000];
for (int i = 0; i < dp.Length; i++)
dp[i] = -1;
// dp[i] = minimum pushes
// required to reach i
dp[N] = 0;
// Traversing through the buttons
for (int i = 0; i < arr.Length; i++) {
// Iterating through all the positions
for (int xx = 0; xx < 100000; xx++) {
int x = xx;
// If not visited
if (dp[x] == -1)
continue;
// Next status of lock
int next = (x + arr[i]) % 100000;
while (dp[next] == -1
|| dp[next] > dp[x] + 1) {
dp[next] = dp[x] + 1;
// Advance to next state
x = next;
next = (next + arr[i]) % 100000;
}
}
}
// Return the readonly dp[target]
return dp[K];
}
// Driver function
public static void Main(String[] args)
{
// Given Input
int N = 99880, K = 89;
int[] arr = { 100, 3 };
// Function Call
Console.Write(minPushes(N, K, arr));
}
}
// This code is contributed by 29AjayKumar
输出
5
时间复杂度: O(N*M)
辅助空间: O(N)