From a67ca17a67b03fc7235e9dd4a0de0b48f1985f1b Mon Sep 17 00:00:00 2001 From: aucker Date: Sat, 1 Jun 2024 09:39:29 +0800 Subject: [PATCH] June 1: Distribute candies I [E, M] Math problem in senior high --- daily/June1.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 daily/June1.cpp diff --git a/daily/June1.cpp b/daily/June1.cpp new file mode 100644 index 0000000..f7d9013 --- /dev/null +++ b/daily/June1.cpp @@ -0,0 +1,17 @@ +#include +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); + } +};