From 8d057232a6a3aa65f3f0c7d9c02ba7d0cd500e01 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 25 Oct 2025 07:49:44 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1716 --- .../README.md | 66 ++++++++++++++++--- .../README_EN.md | 66 ++++++++++++++++--- .../Solution.cpp | 8 ++- .../Solution.go | 8 ++- .../Solution.java | 8 ++- .../Solution.py | 6 +- .../Solution.ts | 7 ++ 7 files changed, 140 insertions(+), 29 deletions(-) create mode 100644 solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.ts diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README.md b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README.md index 831af9485cd13..5ee42c37a6e36 100644 --- a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README.md +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README.md @@ -61,7 +61,35 @@ tags: -### 方法一 +### 方法一:数学 + +根据题目描述,每周的存款情况如下: + +``` +第 1 周:1, 2, 3, 4, 5, 6, 7 +第 2 周:2, 3, 4, 5, 6, 7, 8 +第 3 周:3, 4, 5, 6, 7, 8, 9 +... +第 k 周:k, k+1, k+2, k+3, k+4, k+5, k+6 +``` + +存款天数为 $n$,那么完整的周数为 $k = \lfloor n / 7 \rfloor$,剩余天数为 $b = n \mod 7$。 + +完整的 $k$ 周的存款总额,可以通过等差数列求和公式计算: + +$$ +S_1 = \frac{k}{2} \times (28 + 28 + 7 \times (k - 1)) +$$ + +剩余的 $b$ 天的存款总额,同样可以通过等差数列求和公式计算: + +$$ +S_2 = \frac{b}{2} \times (k + 1 + k + 1 + b - 1) +$$ + +最终的总存款金额为 $S = S_1 + S_2$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 @@ -70,8 +98,10 @@ tags: ```python class Solution: def totalMoney(self, n: int) -> int: - a, b = divmod(n, 7) - return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2 + k, b = divmod(n, 7) + s1 = (28 + 28 + 7 * (k - 1)) * k // 2 + s2 = (k + 1 + k + 1 + b - 1) * b // 2 + return s1 + s2 ``` #### Java @@ -79,8 +109,10 @@ class Solution: ```java class Solution { public int totalMoney(int n) { - int a = n / 7, b = n % 7; - return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2; + int k = n / 7, b = n % 7; + int s1 = (28 + 28 + 7 * (k - 1)) * k / 2; + int s2 = (k + 1 + k + 1 + b - 1) * b / 2; + return s1 + s2; } } ``` @@ -91,8 +123,10 @@ class Solution { class Solution { public: int totalMoney(int n) { - int a = n / 7, b = n % 7; - return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2; + int k = n / 7, b = n % 7; + int s1 = (28 + 28 + 7 * (k - 1)) * k / 2; + int s2 = (k + 1 + k + 1 + b - 1) * b / 2; + return s1 + s2; } }; ``` @@ -101,8 +135,22 @@ public: ```go func totalMoney(n int) int { - a, b := n/7, n%7 - return (28+28+7*(a-1))*a/2 + (a*2+b+1)*b/2 + k, b := n/7, n%7 + s1 := (28 + 28 + 7*(k-1)) * k / 2 + s2 := (k + 1 + k + 1 + b - 1) * b / 2 + return s1 + s2 +} +``` + +#### TypeScript + +```ts +function totalMoney(n: number): number { + const k = (n / 7) | 0; + const b = n % 7; + const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0; + const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0; + return s1 + s2; } ``` diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README_EN.md b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README_EN.md index ef5648bc9a8ae..61f6443e63415 100644 --- a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README_EN.md +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/README_EN.md @@ -62,7 +62,35 @@ tags: -### Solution 1 +### Solution 1: Math + +According to the problem description, the deposit situation for each week is as follows: + +```bash +Week 1: 1, 2, 3, 4, 5, 6, 7 +Week 2: 2, 3, 4, 5, 6, 7, 8 +Week 3: 3, 4, 5, 6, 7, 8, 9 +... +Week k: k, k+1, k+2, k+3, k+4, k+5, k+6 +``` + +Given $n$ days of deposits, the number of complete weeks is $k = \lfloor n / 7 \rfloor$, and the remaining days is $b = n \mod 7$. + +The total deposit for the complete $k$ weeks can be calculated using the arithmetic sequence sum formula: + +$$ +S_1 = \frac{k}{2} \times (28 + 28 + 7 \times (k - 1)) +$$ + +The total deposit for the remaining $b$ days can also be calculated using the arithmetic sequence sum formula: + +$$ +S_2 = \frac{b}{2} \times (k + 1 + k + 1 + b - 1) +$$ + +The final total deposit amount is $S = S_1 + S_2$. + +The time complexity is $O(1)$ and the space complexity is $O(1)$. @@ -71,8 +99,10 @@ tags: ```python class Solution: def totalMoney(self, n: int) -> int: - a, b = divmod(n, 7) - return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2 + k, b = divmod(n, 7) + s1 = (28 + 28 + 7 * (k - 1)) * k // 2 + s2 = (k + 1 + k + 1 + b - 1) * b // 2 + return s1 + s2 ``` #### Java @@ -80,8 +110,10 @@ class Solution: ```java class Solution { public int totalMoney(int n) { - int a = n / 7, b = n % 7; - return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2; + int k = n / 7, b = n % 7; + int s1 = (28 + 28 + 7 * (k - 1)) * k / 2; + int s2 = (k + 1 + k + 1 + b - 1) * b / 2; + return s1 + s2; } } ``` @@ -92,8 +124,10 @@ class Solution { class Solution { public: int totalMoney(int n) { - int a = n / 7, b = n % 7; - return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2; + int k = n / 7, b = n % 7; + int s1 = (28 + 28 + 7 * (k - 1)) * k / 2; + int s2 = (k + 1 + k + 1 + b - 1) * b / 2; + return s1 + s2; } }; ``` @@ -102,8 +136,22 @@ public: ```go func totalMoney(n int) int { - a, b := n/7, n%7 - return (28+28+7*(a-1))*a/2 + (a*2+b+1)*b/2 + k, b := n/7, n%7 + s1 := (28 + 28 + 7*(k-1)) * k / 2 + s2 := (k + 1 + k + 1 + b - 1) * b / 2 + return s1 + s2 +} +``` + +#### TypeScript + +```ts +function totalMoney(n: number): number { + const k = (n / 7) | 0; + const b = n % 7; + const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0; + const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0; + return s1 + s2; } ``` diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.cpp b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.cpp index bd48ce50c5819..a1434e92aa473 100644 --- a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.cpp +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.cpp @@ -1,7 +1,9 @@ class Solution { public: int totalMoney(int n) { - int a = n / 7, b = n % 7; - return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2; + int k = n / 7, b = n % 7; + int s1 = (28 + 28 + 7 * (k - 1)) * k / 2; + int s2 = (k + 1 + k + 1 + b - 1) * b / 2; + return s1 + s2; } -}; \ No newline at end of file +}; diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.go b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.go index 01755853181e6..ce10a0f10e723 100644 --- a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.go +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.go @@ -1,4 +1,6 @@ func totalMoney(n int) int { - a, b := n/7, n%7 - return (28+28+7*(a-1))*a/2 + (a*2+b+1)*b/2 -} \ No newline at end of file + k, b := n/7, n%7 + s1 := (28 + 28 + 7*(k-1)) * k / 2 + s2 := (k + 1 + k + 1 + b - 1) * b / 2 + return s1 + s2 +} diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.java b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.java index 34292e0a9f7d9..63c7a276073e0 100644 --- a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.java +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.java @@ -1,6 +1,8 @@ class Solution { public int totalMoney(int n) { - int a = n / 7, b = n % 7; - return (28 + 28 + 7 * (a - 1)) * a / 2 + (a * 2 + b + 1) * b / 2; + int k = n / 7, b = n % 7; + int s1 = (28 + 28 + 7 * (k - 1)) * k / 2; + int s2 = (k + 1 + k + 1 + b - 1) * b / 2; + return s1 + s2; } -} \ No newline at end of file +} diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.py b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.py index a5d56f4455c58..37827828137e4 100644 --- a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.py +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.py @@ -1,4 +1,6 @@ class Solution: def totalMoney(self, n: int) -> int: - a, b = divmod(n, 7) - return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2 + k, b = divmod(n, 7) + s1 = (28 + 28 + 7 * (k - 1)) * k // 2 + s2 = (k + 1 + k + 1 + b - 1) * b // 2 + return s1 + s2 diff --git a/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.ts b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.ts new file mode 100644 index 0000000000000..a38f1f181897c --- /dev/null +++ b/solution/1700-1799/1716.Calculate Money in Leetcode Bank/Solution.ts @@ -0,0 +1,7 @@ +function totalMoney(n: number): number { + const k = (n / 7) | 0; + const b = n % 7; + const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0; + const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0; + return s1 + s2; +}