leetcode_OJ WC143 Report and some notes
These days I usually write the solution report on leetcode official website more than my own github website, serving as a path to communicate with pro algorithmic coders and exchange the ideas throughout the world.
PA. 1103. Distribute Candies to People
- Thought: Intuitive implmentation
- Analysis:
- Time complexity: O(log(N_candies)) since 1 + 2 + 3... + k >= candies, then this is an arithmetic sequence, up to logN
- Space complexity: O(N) for storing the results
class Solution { public: vector<int> distributeCandies(int candies, int num_people) { vector<int> res(num_people); int idx = 0, cnt = 1; while(candies) { res[idx % num_people] += (candies > cnt) ? cnt : candies; candies = (candies > cnt) ? candies - cnt : 0; cnt++; idx++; } return res; } };