Sort by

recency

|

2105 Discussions

|

  • + 0 comments

    Javascript

    function cutTheSticks(arr) {
        let cArr = arr;
        let cMin = Math.min(...cArr);
        let lenList = [];
        while(cArr.length!=0)
        {
            lenList.push(cArr.length);
            let nArr = [];
            let nMin = 1001;
            for(var cVal of cArr)
            {
                let nVal = cVal - cMin;
                if(nVal!=0)
                {
                    nArr.push(nVal);
                    if(nVal<nMin) nMin = nVal;
                }
            }
            cArr = nArr;
            cMin = nMin;
        }
        return lenList;
    }
    
  • + 0 comments
    def cutTheSticks(arr):
        Res = []
        Min = min(arr)
        Sticks = len(arr)
        while len(arr) > 0:
            Cut = 0
            while Min in arr:
                arr.remove(Min)
            for i in range(len(arr)):
                Cut += 1
                arr[i] = arr[i] - Min
            Res.append(Cut)
            if not arr:
                break
            Min = min(arr)
        Res.insert(0, Sticks)
        Res.pop()
        return Res
    
  • + 0 comments
     p=[]
      while True:
        if arr==[]:
           break
        p.append(len(arr))
        arr=[arr[i]for i in range(len(arr)) if arr[i]!=min(arr)]
      return p
    	
    
  • + 0 comments

    You can make the script easier if you use a native sort method. That way you just shift the array each time.

    function cutTheSticks($arr) {
        sort($arr);
        $countedValues = array_count_values($arr);
        while($countedValues) {
            $results[] = array_sum($countedValues);
            array_shift($countedValues);
        }
        return $results;
    }
    
  • + 0 comments

    C# solution is shown below

    public static List<int> cutTheSticks(List<int> arr) {
            List<int> ret = new List<int>();
            while (arr.Count>0) {
                ret.Add(arr.Count);
                int min = arr.Min();
                List<int> tmp = new List<int>();
                for (int i = 0; i < arr.Count; i++) 
                    if (arr[i] > min) tmp.Add(arr[i] - min); 
                arr = tmp;
            }
            return ret;  
        }