[InterviewBit] Magician and Chocolates

Magician and Chocolates

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
int Solution::nchoc(int A, vector<int> &B) {
priority_queue<int> q;
for(auto b : B) q.push(b);
long long res = 0, mod = 1e9 + 7;
while(A-- and q.size()) {
int now = q.top(); q.pop();
res = (res + now) % mod;
now /= 2;
if(now) q.push(now);
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/17/PS/interviewbit/magician-and-chocolates/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.