2 Keys Keyboard LeetCode solution in Java

1
class Solution {
2
public int minSteps(int n) {
3
int rem = n - 1, copied = 0, ans = 0, onScreen = 1;
4

5
while (rem > 0) {
6
if (rem % onScreen == 0) {
7
ans++; // copy operation
8
copied = onScreen;
9
}
10
rem -= copied;
11
ans++; // paste operation
12
onScreen = n - rem; // no. of characters on screen currently that can be copied in next copy
13
// operation
14
}
15

16
return ans;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0