We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Cut the sticks
Cut the sticks
+ 0 comments C++
vector<int> res; map<int,int> m1; int total = arr.size(); for(int i = 0 ; i < arr.size() ; i++) { m1[arr[i]]++; } for(auto i : m1){ res.push_back(total); total = total - i.second; } return res;
+ 0 comments Grouping sticks by length in C#
public static List<int> cutTheSticks(List<int> arr) { int n = arr.Count; List<int> sticksCut = new List<int>(n); var grps = arr.OrderBy(l => l).GroupBy(l => l); foreach (var grp in grps) { sticksCut.Add(n); n -= grp.Count(); } return sticksCut; }
+ 0 comments def cutTheSticks(arr): counter = [len(arr)] while len(arr) > 0: cut = min(arr) min_counter = arr.count(cut) sticks_left = len(arr)-min_counter if sticks_left == 0: break counter.append(sticks_left) for _ in range(min_counter): arr.remove(cut) for length in range(sticks_left): arr[length] -= cut return counter
+ 0 comments def cutTheSticks(arr): # Write your code here if all(item==arr[0] for item in arr): return [len(arr)] length=[] length.append(len(arr)) while 1: min_=min(arr) arr=list(map(lambda x:x-min_,arr)) temp=[] for item in arr: if item!=0: temp.append(item) arr=temp len_=len(arr) length.append(len_) if all(item==arr[0] for item in arr): break return length
+ 0 comments Java, I tried to condense this into one big stream, but I dont know if it can be done:
ArrayList<Integer> answer = new ArrayList<>(); while(!arr.isEmpty()){ int shortStick = Collections.min(arr); answer.add(arr.size()); arr = arr.stream().map(e -> e - shortStick) .filter(e -> e > 0) .collect(Collectors.toList()); } return answer;
Load more conversations
Sort 1995 Discussions, By:
Please Login in order to post a comment