You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
116
110
117
-
Return *the fewest number of coins that you need to make up that amount*. If that amount of money cannot be made up by any combination of the coins, return `-1`.
111
+
Return _the fewest number of coins that you need to make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `-1`.
118
112
119
113
You may assume that you have an infinite number of each kind of coin.
#### Why this cannot be solved by greedy algorithm?
167
157
168
158
Exception:
159
+
169
160
> But for some coin sets, there are sums for which the greedy algorithm fails. For example, for the set {1, 15, 25} and the sum 30, the greedy algorithm first chooses 25, leaving a remainder of 5, and then five 1s for a total of six coins. But the solution with the minimal number of coins is to choose 15 twice.
170
161
171
162
> In any case where there is no coin whose value, when added to the lowest denomination, is lower than twice that of the denomination immediately less than it, the greedy algorithm works.
172
-
i.e. {1,2,3} works because [1,3] and [2,2] add to the same value however {1, 15, 25} doesn't work because (for the change 30) 15+15>25+1
163
+
> i.e. {1,2,3} works because [1,3] and [2,2] add to the same value however {1, 15, 25} doesn't work because (for the change 30) 15+15>25+1
173
164
174
165
## 0931. Minimum Falling Path Sum
175
166
176
-
Given an `n x n` array of integers `matrix`, return *the**minimum sum** of any **falling path**through*`matrix`.
167
+
Given an `n x n` array of integers `matrix`, return _the**minimum sum** of any **falling path**through_`matrix`.
177
168
178
169
A **falling path** starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position `(row, col)` will be `(row + 1, col - 1)`, `(row + 1, col)`, or `(row + 1, col + 1)`.
Explanation: The falling path with a minimum sum is shown.
200
189
```
201
190
202
-
203
-
204
191
**Constraints:**
205
192
206
193
-`n == matrix.length == matrix[i].length`
@@ -238,9 +225,7 @@ The passes allow that many days of consecutive travel.
238
225
239
226
- For example, if we get a **7-day** pass on day `2`, then we can travel for `7` days: `2`, `3`, `4`, `5`, `6`, `7`, and `8`.
240
227
241
-
Return *the minimum number of dollars you need to travel every day in the given list of days*.
242
-
243
-
228
+
Return _the minimum number of dollars you need to travel every day in the given list of days_.
244
229
245
230
**Example 1:**
246
231
@@ -265,8 +250,6 @@ On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
265
250
In total, you spent $17 and covered all the days of your travel.
266
251
```
267
252
268
-
269
-
270
253
**Constraints:**
271
254
272
255
- `1 <= days.length <= 365`
@@ -296,9 +279,7 @@ There is only one character `'A'` on the screen of a notepad. You can perform tw
296
279
- Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).
297
280
- Paste: You can paste the characters which are copied last time.
298
281
299
-
Given an integer `n`, return *the minimum number of operations to get the character*`'A'`*exactly*`n`*times on the screen*.
300
-
301
-
282
+
Given an integer `n`, return _the minimum number of operations to get the character_`'A'`_exactly_`n`_times on the screen_.
302
283
303
284
**Example 1:**
304
285
@@ -318,8 +299,6 @@ Input: n = 1
318
299
Output: 0
319
300
```
320
301
321
-
322
-
323
302
**Constraints:**
324
303
325
304
-`1 <= n <= 1000`
@@ -356,12 +335,10 @@ return s;
356
335
357
336
## 0279. Perfect Squares
358
337
359
-
Given an integer `n`, return *the least number of perfect square numbers that sum to*`n`.
338
+
Given an integer `n`, return _the least number of perfect square numbers that sum to_`n`.
360
339
361
340
A **perfect square** is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, `1`, `4`, `9`, and `16` are perfect squares while `3` and `11` are not.
362
341
363
-
364
-
365
342
**Example 1:**
366
343
367
344
```
@@ -378,8 +355,6 @@ Output: 2
378
355
Explanation: 13 = 4 + 9.
379
356
```
380
357
381
-
382
-
383
358
**Constraints:**
384
359
385
360
-`1 <= n <= 10^4`
@@ -411,9 +386,7 @@ We are playing a game with the stones. On each turn, we choose any two stones an
411
386
412
387
At the end of the game, there is **at most one** stone left.
413
388
414
-
Return *the smallest possible weight of the left stone*. If there are no stones left, return `0`.
415
-
416
-
389
+
Return _the smallest possible weight of the left stone_. If there are no stones left, return `0`.
@@ -461,12 +432,10 @@ int lastStoneWeightII(vector<int>& stones) {
461
432
462
433
## 0120. Triangle
463
434
464
-
Given a `triangle` array, return *the minimum path sum from top to bottom*.
435
+
Given a `triangle` array, return _the minimum path sum from top to bottom_.
465
436
466
437
For each step, you may move to an adjacent number of the row below. More formally, if you are on index `i` on the current row, you may move to either index `i` or index `i + 1` on the next row.
**Follow up:** Could you do this using only `O(n)` extra space, where `n` is the total number of rows in the triangle?
502
467
503
468
dp[i]: min cost from bottom to i-th row
@@ -519,12 +484,10 @@ int minimumTotal(vector<vector<int>>& triangle) {
519
484
520
485
You are given an array of binary strings `strs` and two integers `m` and `n`.
521
486
522
-
Return *the size of the largest subset of `strs` such that there are **at most*** `m` `0`*'s and* `n` `1`*'s in the subset*.
487
+
Return \*the size of the largest subset of `strs` such that there are **at most\*** `m` `0`_'s and_ `n` `1`_'s in the subset_.
523
488
524
489
A set `x` is a **subset** of a set `y` if all elements of `x` are also elements of `y`.
525
490
526
-
527
-
528
491
**Example 1:**
529
492
530
493
```
@@ -543,8 +506,6 @@ Output: 2
543
506
Explanation: The largest subset is {"0", "1"}, so the answer is 2.
544
507
```
545
508
546
-
547
-
548
509
**Constraints:**
549
510
550
511
- `1 <= strs.length <= 600`
@@ -556,7 +517,7 @@ My dp[i][j] means with i zeros and j ones, what is the max strings to be chosen
556
517
557
518
1. choose current string means dp[i-# of zero for current string][j - # of one for current string] + 1.
558
519
2. not choose current string means dp[i][j] which means there is nothing changed as previous state.
559
-
Why it has to start from m, n and decrease to 1 (or making sure there is at least # of 0 or 1 spots left in our case)? Because it prevents invalid counting. As we can see, our dp[m][n] is going to be updated sz times, and before we calculate i - zero[k] and j - one[k], they has to be valid. If we start from 0 and increase to m, n, these values will never be updated beforehand.
520
+
Why it has to start from m, n and decrease to 1 (or making sure there is at least # of 0 or 1 spots left in our case)? Because it prevents invalid counting. As we can see, our dp[m][n] is going to be updated sz times, and before we calculate i - zero[k] and j - one[k], they has to be valid. If we start from 0 and increase to m, n, these values will never be updated beforehand.
560
521
561
522
```c
562
523
int findMaxForm(vector<string>& strs, int m, int n) {
@@ -585,9 +546,7 @@ int findMaxForm(vector<string>& strs, int m, int n) {
585
546
586
547
## 0221. Maximal Square
587
548
588
-
Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, *find the largest square containing only*`1`'s *and return its area*.
589
-
590
-
549
+
Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, _find the largest square containing only_`1`'s _and return its area_.
591
550
592
551
**Example 1:**
593
552
@@ -614,25 +573,21 @@ Input: matrix = [["0"]]
614
573
Output: 0
615
574
```
616
575
617
-
618
-
619
576
**Constraints:**
620
577
621
578
-`m == matrix.length`
622
579
-`n == matrix[i].length`
623
580
-`1 <= m, n <= 300`
624
581
-`matrix[i][j]` is `'0'` or `'1'`.
625
582
626
-
627
-
628
583
Here the`dp[i][j]`the same as `matrix[i][j]`, and it means the maximum width of the square that includes `matrix[i][j]` as the right down side of the resulting square. To expand the area to any directions (right, down), we check the `matrix[i][j-1]`, `matrix[i-1][j]` and `matrix[i-1][j-1]` and choose the minimal from them. The final answer is the maximum `matrix[i][j] * matrix[i][j]` from all the values we have filled.
629
584
630
585
```c++
631
586
for (int i = 0; i < m; ++i) {
632
587
for (int j = 0; j < n; ++j) {
633
588
if (matrix[i][j] == '0' || i == 0 || j == 0) continue;
0 commit comments