Sort by

recency

|

2122 Discussions

|

  • + 0 comments

    This is my c++ solution of this problem, you can watch the explanation here : https://youtu.be/jZPhE_MTkVg

    vector<int> cutTheSticks(vector<int> arr) {
        sort(arr.begin(), arr.end());
        vector<int>result(1, arr.size());
        for(int i = 1; i < arr.size(); i++){
          if(arr[i-1] != arr[i]) result.push_back(arr.size() - i);
        }
        return result;
    }
    
  • + 0 comments

    python solution: def cutTheSticks(arr): # Write your code here res_op = [] for i in range(len(arr)): if len(arr) == 0: break else: res_op.append(len(arr)) min_val = min(arr) arr = [ (i- min_val) for i in arr if i - min_val > 0] return res_op

  • + 0 comments

    My python3 solution

    c = []
    l = len(arr)
    k = 0
    
    while ( l != 0  ):
    
        m = [x for x in (arr) if x != 0]
        if ( len(m) == 0):
            break
    
        l = min(m)
        k = 0
    
        for i in range(0,len(arr)):
            if ( arr[i] != 0 ):      
                arr[i] = arr[i] - l                
                k += 1            
        c.append(k)
    return(c)
    
  • + 0 comments

    My C++ Solution: (open for feedbacks)

    vector<int> cutTheSticks(vector<int> arr) {
        sort(arr.begin(),arr.end());
        int i = 0;
        int count = 0;
        vector<int> res;
        while(i < arr.size()){
            count=1;
            if(arr[i] == 0){
                i++; 
                continue;
            };
            for(int j=i+1;j<arr.size();j++){
                arr[j] -= arr[i];
                count++;
            }        
            res.push_back(count);
            i++;
        }
        return res;
    }
    
  • + 0 comments

    another solution

    public static List<Integer> cut(List<Integer> arr){
        List<Integer> listUpdated = new ArrayList<>(arr);
        List<Integer> res = new ArrayList<>();
        while (!listUpdated.isEmpty()){
           res.add(listUpdated.size());
            int mini = Integer.MAX_VALUE;
            for (int e : listUpdated){
                if (e < mini){
                    mini = e;
                }
            }
            Iterator<Integer> iterator = listUpdated.iterator();
            while (iterator.hasNext()){
                if (iterator.next() == mini){
                    iterator.remove();
                }
            }
        }
        return res;
    }