通过将 N 替换为 N/M 或将 M 增加 1 将 N 减少到 0 所需的最少操作
给定两个整数N和M ,任务是使用以下操作计算将N减少到0所需的最小操作数:
- 将N替换为(N/M) 。
- 将M的值增加1 。
例子:
Input: N = 9, M = 2
Output: 4
Explanation: The given example can be solved by following the below sequence of operations:
- In 1st operation, replace N with (N/M), i.e, N = 9/2 = 4.
- In 2nd operation, again replace N with N/M, i.e, N = 4/2 = 2.
- In 3rd operation, increment M by 1, i.e, M = M+1 = 2+1 = 3.
- In 4th operation, replace N with N/M, i.e, N = 2/3 = 0.
Hence, the number of required operations is 4 which is the minimum possible.
Input: N = 15, M = 1
Output: 5
方法:给定问题可以通过观察以下事实来解决:最佳操作选择是将M的值增加x次,然后将N的值减小到N / (M+x)直到它变为0 。要找到最佳情况,请使用变量i遍历[0, √N]范围内的所有x值,并通过将其除以(M+i)来计算将N减少到0所需的步骤数。跟踪变量ans中(M+i)的所有可能值的最小操作次数,这是必需的值。
下面是上述方法的实现:
C++
// C++ Program of the above approach
#include
using namespace std;
// Function to find the minimum count of
// operations to reduce N to 0 using M
int findMinimum(int N, int M)
{
// If N is already 0
if (N == 0) {
return 0;
}
// Stores the minimum count of operations
int ans = INT_MAX;
// Loop to iterate in the range [0, √N]
for (int i = 0; i * i <= N; i++) {
// Edge case to prevent infinite looping
if (M == 1 && i == 0) {
continue;
}
// Stores the current count of moves
int count = i;
int tempN = N;
// Number of operations required to
// reduce N to 0 by dividing by M + i
while (tempN != 0) {
tempN /= (M + i);
count++;
}
// Update the final count
ans = min(count, ans);
}
// Return answer
return ans;
}
// Driver code
int main()
{
int N = 9;
int M = 2;
cout << findMinimum(N, M);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static int findMinimum(int N, int M)
{
// If N is already 0
if (N == 0) {
return 0;
}
// Stores the minimum count of operations
int ans = 1000000007;
// Loop to iterate in the range [0, √N]
for (int i = 0; i * i <= N; i++) {
// Edge case to prevent infinite looping
if (M == 1 && i == 0) {
continue;
}
// Stores the current count of moves
int count = i;
int tempN = N;
// Number of operations required to
// reduce N to 0 by dividing by M + i
while (tempN != 0) {
tempN /= (M + i);
count++;
}
// Update the final count
if(count < ans){
ans = count;
}
}
// Return answer
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 9;
int M = 2;
System.out.println(findMinimum(N, M));
}
}
// This code is contributed by maddler.
Python3
# Python Program to implement
# the above approach
# Function to find the minimum count of
# operations to reduce N to 0 using M
def findMinimum(N, M):
# If N is already 0
if (N == 0):
return 0
# Stores the minimum count of operations
ans = 10**9
# Loop to iterate in the range[0, √N]
i = 0
while(i * i <= N):
i += 1
# Edge case to prevent infinite looping
if (M == 1 and i == 0):
continue
# Stores the current count of moves
count = i
tempN = N
# Number of operations required to
# reduce N to 0 by dividing by M + i
while (tempN != 0):
tempN = tempN // (M + i)
count += 1
# Update the final count
ans = min(count, ans)
# Return answer
return ans
# Driver code
N = 9
M = 2
print(findMinimum(N, M))
# This code is contributed by Saurabh Jaiswal
C#
/*package whatever //do not write package name here */
using System;
class GFG
{
public static int findMinimum(int N, int M)
{
// If N is already 0
if (N == 0)
{
return 0;
}
// Stores the minimum count of operations
int ans = 1000000007;
// Loop to iterate in the range [0, √N]
for (int i = 0; i * i <= N; i++)
{
// Edge case to prevent infinite looping
if (M == 1 && i == 0)
{
continue;
}
// Stores the current count of moves
int count = i;
int tempN = N;
// Number of operations required to
// reduce N to 0 by dividing by M + i
while (tempN != 0)
{
tempN /= (M + i);
count++;
}
// Update the final count
if (count < ans)
{
ans = count;
}
}
// Return answer
return ans;
}
// Driver code
public static void Main()
{
int N = 9;
int M = 2;
Console.WriteLine(findMinimum(N, M));
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
4
时间复杂度: O( √ N*log N )
辅助空间: O(1)