2 Keys Keyboard LeetCode solution in Java
1class Solution {2public int minSteps(int n) {3int rem = n - 1, copied = 0, ans = 0, onScreen = 1;45while (rem > 0) {6if (rem % onScreen == 0) {7ans++; // copy operation8copied = onScreen;9}10rem -= copied;11ans++; // paste operation12onScreen = n - rem; // no. of characters on screen currently that can be copied in next copy13// operation14}1516return ans;17}18}