|
| 1 | +## 1. Simulation |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def totalMoney(self, n: int) -> int: |
| 8 | + day, deposit = 0, 1 |
| 9 | + res = 0 |
| 10 | + |
| 11 | + while day < n: |
| 12 | + res += deposit |
| 13 | + deposit += 1 |
| 14 | + day += 1 |
| 15 | + |
| 16 | + if day % 7 == 0: |
| 17 | + deposit = 1 + day // 7 |
| 18 | + |
| 19 | + return res |
| 20 | +``` |
| 21 | + |
| 22 | +```java |
| 23 | +public class Solution { |
| 24 | + public int totalMoney(int n) { |
| 25 | + int day = 0, deposit = 1, res = 0; |
| 26 | + |
| 27 | + while (day < n) { |
| 28 | + res += deposit; |
| 29 | + deposit++; |
| 30 | + day++; |
| 31 | + |
| 32 | + if (day % 7 == 0) { |
| 33 | + deposit = 1 + day / 7; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return res; |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +```cpp |
| 43 | +class Solution { |
| 44 | +public: |
| 45 | + int totalMoney(int n) { |
| 46 | + int day = 0, deposit = 1, res = 0; |
| 47 | + |
| 48 | + while (day < n) { |
| 49 | + res += deposit; |
| 50 | + deposit++; |
| 51 | + day++; |
| 52 | + |
| 53 | + if (day % 7 == 0) { |
| 54 | + deposit = 1 + day / 7; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return res; |
| 59 | + } |
| 60 | +}; |
| 61 | +``` |
| 62 | + |
| 63 | +```javascript |
| 64 | +class Solution { |
| 65 | + /** |
| 66 | + * @param {number} n |
| 67 | + * @return {number} |
| 68 | + */ |
| 69 | + totalMoney(n) { |
| 70 | + let day = 0, deposit = 1, res = 0; |
| 71 | + |
| 72 | + while (day < n) { |
| 73 | + res += deposit; |
| 74 | + deposit++; |
| 75 | + day++; |
| 76 | + |
| 77 | + if (day % 7 === 0) { |
| 78 | + deposit = 1 + Math.floor(day / 7); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return res; |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +::tabs-end |
| 88 | + |
| 89 | +### Time & Space Complexity |
| 90 | + |
| 91 | +* Time complexity: $O(n)$ |
| 92 | +* Space complexity: $O(1)$ |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## 2. Math |
| 97 | + |
| 98 | +::tabs-start |
| 99 | + |
| 100 | +```python |
| 101 | +class Solution: |
| 102 | + def totalMoney(self, n: int) -> int: |
| 103 | + weeks = n // 7 |
| 104 | + low = 28 |
| 105 | + high = 28 + 7 * (weeks - 1) |
| 106 | + res = weeks * (low + high) // 2 |
| 107 | + |
| 108 | + monday = weeks + 1 |
| 109 | + for i in range(n % 7): |
| 110 | + res += i + monday |
| 111 | + |
| 112 | + return res |
| 113 | +``` |
| 114 | + |
| 115 | +```java |
| 116 | +public class Solution { |
| 117 | + public int totalMoney(int n) { |
| 118 | + int weeks = n / 7; |
| 119 | + int low = 28; |
| 120 | + int high = 28 + 7 * (weeks - 1); |
| 121 | + int res = weeks * (low + high) / 2; |
| 122 | + |
| 123 | + int monday = weeks + 1; |
| 124 | + for (int i = 0; i < n % 7; i++) { |
| 125 | + res += i + monday; |
| 126 | + } |
| 127 | + |
| 128 | + return res; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +```cpp |
| 134 | +class Solution { |
| 135 | +public: |
| 136 | + int totalMoney(int n) { |
| 137 | + int weeks = n / 7; |
| 138 | + int low = 28; |
| 139 | + int high = 28 + 7 * (weeks - 1); |
| 140 | + int res = weeks * (low + high) / 2; |
| 141 | + |
| 142 | + int monday = weeks + 1; |
| 143 | + for (int i = 0; i < n % 7; i++) { |
| 144 | + res += i + monday; |
| 145 | + } |
| 146 | + |
| 147 | + return res; |
| 148 | + } |
| 149 | +}; |
| 150 | +``` |
| 151 | + |
| 152 | +```javascript |
| 153 | +class Solution { |
| 154 | + /** |
| 155 | + * @param {number} n |
| 156 | + * @return {number} |
| 157 | + */ |
| 158 | + totalMoney(n) { |
| 159 | + const weeks = Math.floor(n / 7); |
| 160 | + const low = 28; |
| 161 | + const high = 28 + 7 * (weeks - 1); |
| 162 | + let res = weeks * (low + high) / 2; |
| 163 | + |
| 164 | + const monday = weeks + 1; |
| 165 | + for (let i = 0; i < n % 7; i++) { |
| 166 | + res += i + monday; |
| 167 | + } |
| 168 | + |
| 169 | + return res; |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +::tabs-end |
| 175 | + |
| 176 | +### Time & Space Complexity |
| 177 | + |
| 178 | +* Time complexity: $O(1)$ |
| 179 | +* Space complexity: $O(1)$ |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## 3. Math (Optimal) |
| 184 | + |
| 185 | +::tabs-start |
| 186 | + |
| 187 | +```python |
| 188 | +class Solution: |
| 189 | + def totalMoney(self, n: int) -> int: |
| 190 | + SUM = lambda x: (x * (x + 1)) >> 1 |
| 191 | + weeks = n // 7 |
| 192 | + res = SUM(weeks - 1) * 7 + weeks * SUM(7) |
| 193 | + res += SUM(n % 7) + weeks * (n % 7) |
| 194 | + return res |
| 195 | +``` |
| 196 | + |
| 197 | +```java |
| 198 | +public class Solution { |
| 199 | + public int totalMoney(int n) { |
| 200 | + int weeks = n / 7; |
| 201 | + int res = SUM(weeks - 1) * 7 + weeks * SUM(7); |
| 202 | + res += SUM(n % 7) + weeks * (n % 7); |
| 203 | + return res; |
| 204 | + } |
| 205 | + |
| 206 | + private int SUM(int n) { |
| 207 | + return (n * (n + 1)) / 2; |
| 208 | + } |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +```cpp |
| 213 | +class Solution { |
| 214 | +public: |
| 215 | + int totalMoney(int n) { |
| 216 | + auto SUM = [](int x) { return (x * (x + 1)) / 2; }; |
| 217 | + |
| 218 | + int weeks = n / 7; |
| 219 | + int res = SUM(weeks - 1) * 7 + weeks * SUM(7); |
| 220 | + res += SUM(n % 7) + weeks * (n % 7); |
| 221 | + return res; |
| 222 | + } |
| 223 | +}; |
| 224 | +``` |
| 225 | + |
| 226 | +```javascript |
| 227 | +class Solution { |
| 228 | + /** |
| 229 | + * @param {number} n |
| 230 | + * @return {number} |
| 231 | + */ |
| 232 | + totalMoney(n) { |
| 233 | + const SUM = x => (x * (x + 1)) / 2; |
| 234 | + |
| 235 | + const weeks = Math.floor(n / 7); |
| 236 | + let res = SUM(weeks - 1) * 7 + weeks * SUM(7); |
| 237 | + res += SUM(n % 7) + weeks * (n % 7); |
| 238 | + return res; |
| 239 | + } |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +::tabs-end |
| 244 | + |
| 245 | +### Time & Space Complexity |
| 246 | + |
| 247 | +* Time complexity: $O(1)$ |
| 248 | +* Space complexity: $O(1)$ |
0 commit comments