Skip to content

Commit

Permalink
June 3: distribute candies [E]
Browse files Browse the repository at this point in the history
simple traverse the vector and save res to vector
  • Loading branch information
aucker committed Jun 3, 2024
1 parent 7ed4e54 commit ef09607
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions daily/June3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <vector>
using namespace std;

class Solution {
public:
/**
* LC:1103: distribute candies II [E]
* Time: O(N + sqrt(candies)), Space: O(1)
*/
vector<int> distributeCandies(int candies, int num_people) {
vector<int> ans(num_people);
for (int i = 1; candies > 0; i++) {
ans[(i - 1) % num_people] += min(i, candies);
candies -= i;
}

return ans;
}
};

0 comments on commit ef09607

Please sign in to comment.