通过删除或附加任何数字来最小化将 N 转换为 K 的幂的操作
给定一个数字N ,任务是找到将给定整数转换为K的任意幂的最小操作数,其中在每个操作中,可以删除任何数字,或者可以将任何数字附加到后面整数。
例子:
Input: N = 247, K = 3
Output: 1
Explanation: In the 1st operation, digit 4 can be removed. Hence, N = 27 which is a power of K.
Input: N = 5, K = 2
Output: 2
Explanation: In the 1st operation, digit 5 can be removed and in the 2nd operation, digit 2 can be appended in the end. Hence, N = 2 which is a power of K.
方法:给定问题可以通过将K的所有幂存储在一个向量中并计算将给定整数N转换为当前幂所需的操作数来解决。以下是要遵循的步骤:
- 将K的所有幂存储在向量幂中。
- 现在遍历向量幂并计算将给定整数N转换为当前幂所需的操作数,可按如下方式完成:
- 将给定的整数转换为字符串s1和s2 。
- 初始化两个变量i = 0和j = 0 。
- 如果s1[i]等于s2[j] ,则增加i和j 。否则,只增加j 。
- 所需的操作数将为S1.length + S2.length – (2 * i) 。
- 在所有值上保持所需操作的最小值,这是所需的答案。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
#define int long long
// Function to find all powers of
// K in the range [1, 10^18]
vector findPowers(int K)
{
// Stores the powers of K
vector powers;
int val = 1;
// Loop to iterate over all
// powers in the range
while (val <= 1e18) {
powers.push_back(val);
val *= K;
}
// Return answer
return powers;
}
// Function to find minimum operations to
// convert given integer into a power of K
int minOperations(int N, int K)
{
// Store all the powers of K
vector powers = findPowers(K);
// Stores the final result
int res = INT_MAX;
// Loop to iterate through all
// the powers of K
for (int x = 0; x < powers.size(); x++) {
// Convert integers to strings
// for easier comparison
string s1 = to_string(powers[x]);
string s2 = to_string(N);
int i = 0, j = 0;
// Loop to calculate operations
// required to convert s1 to s2
while (i < s1.size() && j < s2.size()) {
// Count no of equal character
if (s1[i] == s2[j]) {
i++;
}
// Increment j by 1
j++;
}
// Update res
res = min(res,
(int)(s1.size()
+ s2.size() - 2 * i));
}
// Return answer;
return res;
}
// Driver Code
int32_t main()
{
int N = 247;
int K = 3;
cout << minOperations(N, K);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to find all powers of
// K in the range [1, 10^18]
static Vector findPowers(int K)
{
// Stores the powers of K
Vector powers = new Vector();
int val = 1;
// Loop to iterate over all
// powers in the range
while (val <= (int)(9999999999L)) {
powers.add(val);
val *= K;
}
// Return answer
return powers;
}
// Function to find minimum operations to
// convert given integer into a power of K
static int minOperations(int N, int K)
{
// Store all the powers of K
Vector powers = findPowers(K);
// Stores the final result
int res = Integer.MAX_VALUE;
// Loop to iterate through all
// the powers of K
for (int x = 0; x < powers.size(); x++) {
// Convert integers to Strings
// for easier comparison
String s1 = String.valueOf(powers.get(x));
String s2 = String.valueOf(N);
int i = 0, j = 0;
// Loop to calculate operations
// required to convert s1 to s2
while (i < s1.length() && j < s2.length()) {
// Count no of equal character
if (s1.charAt(i) == s2.charAt(j)) {
i++;
}
// Increment j by 1
j++;
}
// Update res
res = Math.min(res,
(int)(s1.length()
+ s2.length() - 2 * i));
}
// Return answer;
return res;
}
// Driver Code
public static void main(String[] args)
{
int N = 247;
int K = 3;
System.out.print(minOperations(N, K));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 code for the above approach
import sys
# Function to find all powers of
# K in the range [1, 10^18]
def findPowers(K):
# Stores the powers of K
powers = []
val = 1
# Loop to iterate over all
# powers in the range
while (val <= 1e18):
powers.append(val)
val *= K
# Return answer
return powers
# Function to find minimum operations to
# convert given integer into a power of K
def minOperations(N, K):
# Store all the powers of K
powers = findPowers(K)
# Stores the final result
res = sys.maxsize
# Loop to iterate through all
# the powers of K
for x in range(len(powers)):
# Convert integers to strings
# for easier comparison
s1 = str(powers[x])
s2 = str(N)
i = 0
j = 0
# Loop to calculate operations
# required to convert s1 to s2
while (i < len(s1) and j < len(s2)):
# Count no of equal character
if (s1[i] == s2[j]):
i += 1
# Increment j by 1
j += 1
# Update res
res = min(res,
(int)(len(s1)
+ len(s2) - 2 * i))
# Return answer;
return res
# Driver Code
if __name__ == "__main__":
N = 247
K = 3
print(minOperations(N, K))
# This code is contributed by ukasp.
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find all powers of
// K in the range [1, 10^18]
static List findPowers(int K)
{
// Stores the powers of K
List powers = new List();
int val = 1;
// Loop to iterate over all
// powers in the range
while (val <= ((int)(999999999))) {
powers.Add(val);
val *= K;
}
// Return answer
return powers;
}
// Function to find minimum operations to
// convert given integer into a power of K
static int minOperations(int N, int K)
{
// Store all the powers of K
List powers = findPowers(K);
// Stores the readonly result
int res = int.MaxValue;
// Loop to iterate through all
// the powers of K
for (int x = 0; x < powers.Count; x++) {
// Convert integers to Strings
// for easier comparison
String s1 = String.Join("",powers[x]);
String s2 = String.Join("",N);
int i = 0, j = 0;
// Loop to calculate operations
// required to convert s1 to s2
while (i < s1.Length && j < s2.Length) {
// Count no of equal character
if (s1[i] == s2[j]) {
i++;
}
// Increment j by 1
j++;
}
// Update res
res = Math.Min(res,
(int)(s1.Length
+ s2.Length - 2 * i));
}
// Return answer;
return res;
}
// Driver Code
public static void Main(String[] args)
{
int N = 247;
int K = 3;
Console.Write(minOperations(N, K));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
1
时间复杂度: O((log N) 2 )
辅助空间: O(1)