Greedy


  • 0
    R
    class Solution {
    public:
        int minSteps(int n) {
            int tmp_n = n-1;
            int size = 1;
            int step = 0;
            while(tmp_n != 0){
                if(tmp_n%(n-tmp_n) == 0){
                    step +=2;
                    size = n-tmp_n;
                    tmp_n -= size;
                }else{
                    step += 1;
                    tmp_n -= size;
                }
            }
            return step;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.