From c2558ef7111c280e2ddf163d120765bdca8e117d Mon Sep 17 00:00:00 2001 From: K Rishabh <90962594+k-rishabh6172@users.noreply.github.com> Date: Wed, 14 Jun 2023 01:29:31 +0530 Subject: [PATCH] Update Make It Equal.cpp The problem was failing a test: 33 18 37 17 11 26 17 33 36 80 84 56 4 17 4 96 78 10 23 47 13 49 59 9 13 0 96 23 51 52 1 32 76 33 43 Expected: 64 Output: 63 Changed in the judge as well --- Coding Exercise Solutions/Make It Equal.cpp | 40 ++++++++++----------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/Coding Exercise Solutions/Make It Equal.cpp b/Coding Exercise Solutions/Make It Equal.cpp index 2dab7ac..f013ae8 100644 --- a/Coding Exercise Solutions/Make It Equal.cpp +++ b/Coding Exercise Solutions/Make It Equal.cpp @@ -1,30 +1,28 @@ #include using namespace std; -#define maxn 100001 -int solve(int n,int k,vector h){ - int cnt[maxn]={0}; - for(int i=0;i h){ + int maxi = *max_element(h.begin(), h.end()) + 1; + vector cnt(maxi, 0); + for(int i = 0 ; i < n ; i++){ cnt[0]++; cnt[h[i]]--; } - for(int i=1;i=0;i--){ - if(cnt[i]==n){ - ans+=(sum>0); - break; - } - if(sum+ cnt[i]>k) - { - ans++; - sum=cnt[i]; - continue; + + int slices = 0, curr = 0; + for(int i = maxi-1; i >= 0; i--){ + if(cnt[i] == n) break; + if(curr + cnt[i] <= k){ + curr += cnt[i]; + }else{ + slices++; + curr = cnt[i]; } - sum+=cnt[i]; } - return ans; -} \ No newline at end of file + if(curr != 0) slices++; + return slices; +}