Skip to content

Commit 3412c01

Browse files
committed
[LeetCode Sync] Runtime - 1 ms (68.00%), Memory - 13.4 MB (16.38%)
1 parent 94aeab3 commit 3412c01

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<p>You are given an integer array <code>gifts</code> denoting the number of gifts in various piles. Every second, you do the following:</p>
2+
3+
<ul>
4+
<li>Choose the pile with the maximum number of gifts.</li>
5+
<li>If there is more than one pile with the maximum number of gifts, choose any.</li>
6+
<li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li>
7+
</ul>
8+
9+
<p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
16+
<strong>Output:</strong> 29
17+
<strong>Explanation:</strong>
18+
The gifts are taken in the following way:
19+
- In the first second, the last pile is chosen and 10 gifts are left behind.
20+
- Then the second pile is chosen and 8 gifts are left behind.
21+
- After that the first pile is chosen and 5 gifts are left behind.
22+
- Finally, the last pile is chosen again and 3 gifts are left behind.
23+
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> gifts = [1,1,1,1], k = 4
30+
<strong>Output:</strong> 4
31+
<strong>Explanation:</strong>
32+
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
33+
That is, you can&#39;t take any pile with you.
34+
So, the total gifts remaining are 4.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= gifts.length &lt;= 10<sup>3</sup></code></li>
42+
<li><code>1 &lt;= gifts[i] &lt;= 10<sup>9</sup></code></li>
43+
<li><code>1 &lt;= k &lt;= 10<sup>3</sup></code></li>
44+
</ul>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
long long pickGifts(vector<int>& gifts, int k) {
4+
priority_queue<int> h(gifts.begin(), gifts.end());
5+
6+
for (int i = 0; i < k; i++) {
7+
int maxElement = h.top();
8+
h.pop();
9+
10+
h.push(sqrt(maxElement)); // heapify
11+
}
12+
13+
long long res = 0;
14+
while (!h.empty()) {
15+
res += h.top();
16+
h.pop();
17+
}
18+
19+
return res;
20+
}
21+
};

0 commit comments

Comments
 (0)