Skip to content

Instantly share code, notes, and snippets.

@freelze
Last active December 6, 2017 12:51
Show Gist options
  • Save freelze/ee41a064a6f21133b962a30ff9eb076c to your computer and use it in GitHub Desktop.
Save freelze/ee41a064a6f21133b962a30ff9eb076c to your computer and use it in GitHub Desktop.
Runtime: 3 ms , Your runtime beats 61.34 % of cpp submissions.
/*
Question: https://leetcode.com/problems/2-keys-keyboard/description/
*/
class Solution {
public:
int minSteps(int n) {
int n_temp = n;
if(n == 1 ) return 0;
int result = 0;
while(n%2 == 0)
{
result+=2;
n/=2;
}
for(int i=3; i < n_temp; i+=2)
{
while(n%i == 0)
{
result+=i;
n/=i;
}
}
if(n != 1) return n;
else return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment