Sort by

recency

|

2143 Discussions

|

  • + 0 comments

    O(log n) solution Python3 using frequency method

    O(log n) because of sorting of keys in dictionary

    def cutTheSticks(arr):
        
        freq = {}
        for i in arr:
            freq[i] = freq.get(i, 0) + 1
        ans = []
        sticks_left = len(arr)
        for num in sorted(freq):
                   
            ans.append(sticks_left)
            sticks_left -= freq[num]
        
        return ans
    
  • + 0 comments

    Simple Java Solution:

    public static List<Integer> cutTheSticks(List<Integer> arr) {
        // Write your code here
        
        List<Integer> list = new ArrayList<>();
        while(arr.size()>0){
            list.add(arr.size());
            Integer minValue = arr.stream().min(Integer::compare).orElse(null);
            for(int i=0;i<arr.size();i++){
                if(arr.get(i)-minValue == 0){
                    arr.set(i, 0);
                }
            }
            arr = arr.stream().filter(p->p!=0).collect(Collectors.toList());
        }
        return list;
        }
    
  • + 0 comments

    Python3 Solution

    def cutTheSticks(arr):
        # Write your code here
        arr = sorted(arr, reverse=True)
        
        res = [len(arr)]
        currVal = arr[-1]    
        while len(arr) > 0:
            if currVal != arr[-1]:
                res.append(len(arr))
                currVal = arr[-1]
            arr.pop(-1)
        
        return res
    
  • + 0 comments

    My code in python

    def cutTheSticks(arr):
    	out = []
    			while arr:
    					out.append(len(arr))
    					m = min(arr)
    					arr = [x - m for x in arr if x > m]
    			return out
    
  • + 0 comments

    Here is Cut the Sticks problem solution in python, java, c++, c and javascript - https://programmingoneonone.com/hackerrank-cut-the-sticks-problem-solution.html