Sort by

recency

|

2117 Discussions

|

  • + 0 comments
    def cutTheSticks(arr):
        # Write your code here
        
        ans = []
        
        while len(arr) >= 1:
            
            ans.append(len(arr))
            
            min_val = min(arr)
            
            arr = [i-min_val for i in arr]
            
            arr = [i for i in arr if i!=0]
            
        return ans 
    
  • + 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

    Perl:

    sub cutTheSticks {
        my $arr = shift;
    
        my @res;
        my $n = scalar(@$arr);
        while ($n > 0) {
            push(@res, scalar(@$arr)) if scalar(@$arr) > 0;
            my $min = min(@$arr);
            @$arr = map {$_ - $min} @$arr;
            @$arr = grep {!/^0$/} @$arr;
            $n--;
        }
        return @res;
    }
    
  • + 0 comments
    def cutTheSticks(arr):
        arr.sort()
        result = [len(arr)]
        for i in range(1, len(arr)):
            if arr[i] != arr[i - 1]:
                result.append(len(arr) - i)
        return result
    
  • + 0 comments

    NOTE: There is an error in the problem specs.

    The Function Description says:

    ... return an array of integers representing the number of sticks before each cut operation

    The Returns specification says:

    int[]: the number of sticks after each iteration

    Guess which one is correct. (OK, fine, I'll tell you: The one before the other.)