给定三个整数N 、 K和M 分别代表框数(从 1 到N水平对齐)、允许跳跃的总数和可用硬币总数,任务是打印可以在[1, N]内遍历的路径通过在恰好K次跳跃中使用恰好M 个硬币。如果从位置X跳到位置Y,则使用abs(X – Y)硬币。如果在K次跳跃中不能使用M 个硬币,则打印 -1。
例子:
Input : N = 5, K = 4, M = 12
Output : 5 1 4 5
Explanation :
First jump: Box 1 -> Box 5. Hence, |1-5| = 4 coins used.
Second Jump: Box 5 -> Box 1 Hence, |5-1| = 4 coins used.
Third Jump: Box 1 -> Box 4 using 3 coins.
Fourth Jump: Box 4 -> Box 5 using 1 coin.
Hence, exactly 12 coins used in 4 jumps.
Input : N = 4, K = 3, M = 10
Output : -1
方法:
We can observe that the answer will be -1 for the following two cases:
- K > N-1 or
- K * (N-1) < M.
可以按照以下步骤使用贪婪方法解决该问题:
重复以下操作,直到K变为零。
- 找出N-1和M – K – 1的最小值。
- 基于上述最小值,根据可用性向左或向右移动减少 K。
- 重复上述步骤,直到 K 变为 0。
下面是上述方法的实现:
C++
// C++ program to print
// the path using exactly
// K jumps and M coins
#include
using namespace std;
// Function that print the path
// using exactly K jumps and M coins
void print_path(int N, int jump, int coin)
{
// If no path exists
if (jump > coin
|| jump * (N - 1) < coin) {
cout << "-1" << endl;
}
else {
int pos = 1;
while (jump > 0) {
// It decides which
// box to be jump
int tmp = min(N - 1,
coin - (jump - 1));
// It decides whether
// to jump on left side or
// to jump on right side
if (pos + tmp <= N) {
pos += tmp;
}
else {
pos -= tmp;
}
// Print the path
cout << pos << " ";
coin -= tmp;
jump -= 1;
}
}
}
// Driver Code
int main()
{
int N = 5, K = 4, M = 12;
// Function Call
print_path(N, K, M);
return 0;
}
Java
// Java program to print the path
// using exactly K jumps and M coins
import java.io.*;
class GFG{
// Function that print the path
// using exactly K jumps and M coins
static void print_path(int N, int jump,
int coin)
{
// If no path exists
if (jump > coin || jump * (N - 1) < coin)
{
System.out.println("-1");
}
else
{
int pos = 1;
while (jump > 0)
{
// It decides which
// box to be jump
int tmp = Math.min(N - 1,
coin - (jump - 1));
// It decides whether
// to jump on left side or
// to jump on right side
if (pos + tmp <= N)
{
pos += tmp;
}
else
{
pos -= tmp;
}
// Print the path
System.out.print(pos + " ");;
coin -= tmp;
jump -= 1;
}
}
}
// Driver Code
public static void main (String[] args)
{
int N = 5, K = 4, M = 12;
// Function Call
print_path(N, K, M);
}
}
// This code is contributed by shubhamsingh10
Python3
# Python3 program to print the path
# using exactly K jumps and M coins
# Function that pr the path
# using exactly K jumps and M coins
def print_path(N, jump, coin):
# If no path exists
if (jump > coin or
jump * (N - 1) < coin):
print("-1")
else:
pos = 1;
while (jump > 0):
# It decides which
# box to be jump
tmp = min(N - 1,
coin - (jump - 1));
# It decides whether
# to jump on left side or
# to jump on right side
if (pos + tmp <= N):
pos += tmp;
else:
pos -= tmp;
# Print the path
print(pos, end = " ")
coin -= tmp;
jump -= 1;
# Driver code
N = 5
K = 4
M = 12
# Function call
print_path(N, K, M);
# This code is contributed by grand_master
C#
// C# program to print the path
// using exactly K jumps and M coins
using System;
class GFG{
// Function that print the path
// using exactly K jumps and M coins
static void print_path(int N, int jump,
int coin)
{
// If no path exists
if (jump > coin || jump * (N - 1) < coin)
{
Console.WriteLine("-1");
}
else
{
int pos = 1;
while (jump > 0)
{
// It decides which
// box to be jump
int tmp = Math.Min(N - 1,
coin - (jump - 1));
// It decides whether
// to jump on left side or
// to jump on right side
if (pos + tmp <= N)
{
pos += tmp;
}
else
{
pos -= tmp;
}
// Print the path
Console.Write(pos + " ");
coin -= tmp;
jump -= 1;
}
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 5, K = 4, M = 12;
// Function Call
print_path(N, K, M);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
5 1 4 5
时间复杂度: O(K)
辅助空间: O(1)