Last active
December 6, 2017 12:51
-
-
Save freelze/ee41a064a6f21133b962a30ff9eb076c to your computer and use it in GitHub Desktop.
Runtime: 3 ms , Your runtime beats 61.34 % of cpp submissions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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