650. 2 Keys Keyboard
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of operations to produce exactly n copies of the character 'A' starting from a single 'A'. We have two operations: "Copy All" and "Paste".
Solution
2 Keys Keyboard in Python
class Solution:
def minSteps(self, n: int) -> int:
# The problem is equivalent to finding the sum of the prime factors of n.
# Each prime factor p corresponds to a sequence of 1 Copy and p-1 Paste operations,
# which totals p steps, to multiply the current count of 'A's by p.
ans = 0
d = 2
# Iterate through potential factors `d` up to sqrt(n).
while d * d <= n:
# While d is a factor of n, add d to the answer
# and divide n by d. This extracts all occurrences
# of the prime factor d.
while n % d == 0:
ans += d
n //= d
d += 1
# If n is still greater than 1 after the loop,
# it means the remaining n is a prime factor itself.
# For example, if n was 14, the loop handles the factor 2, ans becomes 2, n becomes 7.
# The loop terminates as 3*3 > 7. The remaining n=7 is prime and must be added.
if n > 1:
ans += n
return ans
2 Keys Keyboard in Java
class Solution {
public int minSteps(int n) {
// The problem is equivalent to finding the sum of the prime factors of n.
// Each prime factor p corresponds to a sequence of 1 Copy and p-1 Paste operations,
// which totals p steps, to multiply the current count of 'A's by p.
if (n == 1) {
return 0; // Base case: already at 1 'A', so 0 steps.
}
int ans = 0;
// Iterate through potential factors `d`.
for (int d = 2; d * d <= n; d++) {
// While d is a factor of n, add d to the total steps (ans)
// and divide n by d. This extracts all occurrences of the prime factor d.
while (n % d == 0) {
ans += d;
n /= d;
}
}
// After the loop, if n is still greater than 1, it must be a prime factor itself.
// For example, if n was 14, the loop handles the factor 2, ans becomes 2, n becomes 7.
// The loop terminates as 3*3 > 7. The remaining n=7 is prime and must be added.
if (n > 1) {
ans += n;
}
return ans;
}
}
2 Keys Keyboard in C++
class Solution {
public:
int minSteps(int n) {
// The problem is equivalent to finding the sum of prime factors of n.
// Each prime factor p requires p operations (1 copy, p-1 pastes) to multiply the current number of 'A's by p.
if (n == 1) {
return 0; // Already have 1 'A', 0 steps needed.
}
int ans = 0;
// Find all factors of 2.
while (n % 2 == 0) {
ans += 2;
n /= 2;
}
// Find all odd factors.
// We iterate from 3 up to sqrt(n).
for (int i = 3; i * i <= n; i += 2) {
// While i is a factor of n, add it to the answer and divide n by it.
while (n % i == 0) {
ans += i;
n /= i;
}
}
// If n is still greater than 1 after the loop, it means the remaining
// n is a prime factor itself (and it's > sqrt(original_n)).
if (n > 1) {
ans += n;
}
return ans;
}
};
2 Keys Keyboard in TypeScript
function minSteps(n: number): number {
// The problem is equivalent to finding the sum of the prime factors of n.
// Each prime factor p corresponds to a sequence of 1 Copy and p-1 Paste operations,
// which totals p steps, to multiply the current count of 'A's by p.
let ans = 0;
let d = 2;
// Iterate through potential factors `d` up to sqrt(n).
while (d * d <= n) {
// While d is a factor of n, add d to the total steps (ans)
// and divide n by d. This extracts all occurrences of the prime factor d.
while (n % d === 0) {
ans += d;
n /= d;
}
d++;
}
// After the loop, if n is still greater than 1, it must be a prime factor itself.
// For example, if n was 14, the loop handles the factor 2, ans becomes 2, n becomes 7.
// The loop terminates as 3*3 > 7. The remaining n=7 is prime and must be added.
if (n > 1) {
ans += n;
}
return ans;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.