Skip to content

Commit

Permalink
June 1: Distribute candies I [E, M]
Browse files Browse the repository at this point in the history
Math problem in senior high
  • Loading branch information
aucker committed Jun 1, 2024
1 parent 2ed15bf commit a67ca17
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions daily/June1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;

class Solution {
int c2(int n) { return n > 1 ? n * (n - 1) / 2 : 0; }

public:
/**
* LC: 2928: Distribute Candies among children I
* MATH
* Time/Space: O(1)
*/
int distributeCandies(int n, int limit) {
return c2(n + 2) - 3 * c2(n - limit + 1) + 3 * c2(n - 2 * limit) -
c2(n - 3 * limit - 1);
}
};

0 comments on commit a67ca17

Please sign in to comment.