-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.html
2549 lines (2261 loc) · 175 KB
/
leetcode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>leetcode</title>
<link rel="stylesheet" type="text/css" href="/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="./htmlize.css"/>
<link rel="stylesheet" type="text/css" href="../htmlize.css"/>
<link rel="stylesheet" type="text/css" href="../../htmlize.css"/>
<link rel="stylesheet" type="text/css" href="/readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="./readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="../readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="../../readtheorg.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
<link rel="stylesheet" type="text/css" href="/main.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../main.css" media="screen" />
<link rel="stylesheet" type="text/css" href="../../main.css" media="screen" />
<link rel="stylesheet" type="text/css" href="./main.css" media="screen" />
<link rel = "icon" href = "/icon.png" type = "image/x-icon">
<script>
window.MathJax = {
tex: {
ams: {
multlineWidth: '85%'
},
tags: 'ams',
tagSide: 'right',
tagIndent: '.8em'
},
chtml: {
scale: 1.0,
displayAlign: 'center',
displayIndent: '0em'
},
svg: {
scale: 1.0,
displayAlign: 'center',
displayIndent: '0em'
},
output: {
font: 'mathjax-modern',
displayOverflow: 'overflow'
}
};
</script>
<script
id="MathJax-script"
async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</head>
<body>
<div id="content" class="content">
<h1 class="title">leetcode</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org00000e3">1. leetcode</a>
<ul>
<li><a href="#org0000006">1.1. Backtracking</a>
<ul>
<li><a href="#org0000000">1.1.1. N-Queens</a></li>
<li><a href="#org0000003">1.1.2. Permutations II</a></li>
</ul>
</li>
<li><a href="#org000001c">1.2. Binary Search</a>
<ul>
<li><a href="#org000000a">1.2.1. Search a 2D Matrix II</a></li>
<li><a href="#org000000d">1.2.2. Find Minimum in Rotated Sorted Array</a></li>
<li><a href="#org0000010">1.2.3. Search in Rotated Sorted Array</a></li>
<li><a href="#org0000013">1.2.4. Sqrt</a></li>
<li><a href="#org0000016">1.2.5. Minimize Maximum of Array</a></li>
<li><a href="#org0000019">1.2.6. Single Element in a Sorted Array</a></li>
</ul>
</li>
<li><a href="#org0000030">1.3. knapsack</a>
<ul>
<li><a href="#org0000020">1.3.1. Partition Equal Subset Sum</a></li>
<li><a href="#org0000024">1.3.2. Coin Change</a></li>
<li><a href="#org0000027">1.3.3. Coin Change II</a></li>
<li><a href="#org000002a">1.3.4. Combination Sum IV</a></li>
<li><a href="#org000002d">1.3.5. Word Break</a></li>
</ul>
</li>
<li><a href="#org0000040">1.4. DP</a>
<ul>
<li><a href="#org0000034">1.4.1. Stone Game</a></li>
<li><a href="#org0000037">1.4.2. Maximum Sum Circular Subarray</a></li>
<li><a href="#org000003a">1.4.3. Champagne Tower</a></li>
<li><a href="#org000003d">1.4.4. Maximum Alternating Subsequence Sum</a></li>
</ul>
</li>
<li><a href="#org0000052">1.5. Graph</a>
<ul>
<li><a href="#org0000043">1.5.1. Find the City With the Smallest Number of Neighbors at a Threshold Distance</a></li>
<li><a href="#org0000046">1.5.2. Cheapest Flights Within K Stops</a></li>
<li><a href="#org0000049">1.5.3. Course Schedule</a></li>
<li><a href="#org000004c">1.5.4. Surrounded Regions</a></li>
<li><a href="#org000004f">1.5.5. 01 Matrix</a></li>
</ul>
</li>
<li><a href="#org0000058">1.6. Hash Map</a>
<ul>
<li><a href="#org0000055">1.6.1. Two Sum</a></li>
</ul>
</li>
<li><a href="#org0000061">1.7. Heap</a>
<ul>
<li><a href="#org000005b">1.7.1. Find K Pairs with Smallest Sums</a></li>
<li><a href="#org000005e">1.7.2. Find Median from Data Stream</a></li>
</ul>
</li>
<li><a href="#org0000067">1.8. Interval</a>
<ul>
<li><a href="#org0000064">1.8.1. Insert Interval</a></li>
</ul>
</li>
<li><a href="#org0000070">1.9. Linked List</a>
<ul>
<li><a href="#org000006a">1.9.1. LRU Cache</a></li>
<li><a href="#org000006d">1.9.2. Rotate List</a></li>
</ul>
</li>
<li><a href="#org000007c">1.10. LIS</a>
<ul>
<li><a href="#org0000073">1.10.1. Longest Increasing Subsequence</a></li>
<li><a href="#org0000076">1.10.2. Longest Ideal Subsequence</a></li>
<li><a href="#org0000079">1.10.3. Flip String to Monotone Increasing</a></li>
</ul>
</li>
<li><a href="#org0000082">1.11. Matrix</a>
<ul>
<li><a href="#org000007f">1.11.1. Rotate Image</a></li>
</ul>
</li>
<li><a href="#org000008e">1.12. Monotonic Stack</a>
<ul>
<li><a href="#org0000085">1.12.1. Daily Temperatures</a></li>
<li><a href="#org0000088">1.12.2. Sum of Subarray Minimums</a></li>
<li><a href="#org000008b">1.12.3. 132 Pattern</a></li>
</ul>
</li>
<li><a href="#org0000094">1.13. Recursive</a>
<ul>
<li><a href="#org0000091">1.13.1. Minimum Cost For Tickets</a></li>
</ul>
</li>
<li><a href="#org00000a6">1.14. Sliding Window</a>
<ul>
<li><a href="#org0000097">1.14.1. Substring with Concatenation of All Words</a></li>
<li><a href="#org000009a">1.14.2. Longest Repeating Character Replacement</a></li>
<li><a href="#org000009d">1.14.3. Longest Substring with At Least K Repeating Characters</a></li>
<li><a href="#org00000a0">1.14.4. Longest Nice Subarray</a></li>
<li><a href="#org00000a3">1.14.5. Count Complete Subarrays in an Array</a></li>
</ul>
</li>
<li><a href="#org00000ad">1.15. Stack</a>
<ul>
<li><a href="#org00000aa">1.15.1. Basic Calculator</a></li>
</ul>
</li>
<li><a href="#org00000b9">1.16. Tree</a>
<ul>
<li><a href="#org00000b0">1.16.1. BST Iterator</a></li>
<li><a href="#org00000b3">1.16.2. Symmetric Tree</a></li>
<li><a href="#org00000b6">1.16.3. Validate BST</a></li>
</ul>
</li>
<li><a href="#org00000cb">1.17. Trick</a>
<ul>
<li><a href="#org00000bc">1.17.1. Pow</a></li>
<li><a href="#org00000bf">1.17.2. Roman To Integer</a></li>
<li><a href="#org00000c2">1.17.3. Minimize Maximum of Array</a></li>
<li><a href="#org00000c5">1.17.4. Count Negative Numbers in a Sorted Matrix</a></li>
<li><a href="#org00000c8">1.17.5. Count of Smaller Numbers After Self</a></li>
</ul>
</li>
<li><a href="#org00000d1">1.18. Trie</a>
<ul>
<li><a href="#org00000ce">1.18.1. Word Search II</a></li>
</ul>
</li>
<li><a href="#org00000da">1.19. Two Pointers</a>
<ul>
<li><a href="#org00000d4">1.19.1. Heaters</a></li>
<li><a href="#org00000d7">1.19.2. Remove Duplicates From Sorted Array II</a></li>
</ul>
</li>
<li><a href="#org00000e0">1.20. Rolling Hash</a>
<ul>
<li><a href="#org00000dd">1.20.1. Repeated DNA Sequences</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org00000e3" class="outline-2">
<h2 id="org00000e3"><span class="section-number-2">1.</span> leetcode</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-org0000006" class="outline-3">
<h3 id="org0000006"><span class="section-number-3">1.1.</span> Backtracking</h3>
<div class="outline-text-3" id="text-1-1">
<p>
backtracking 的一般框架为:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">dfs</span>(curr, i):
<span class="org-keyword">if</span> ending_cond:
ans.append(curr)
<span class="org-keyword">return</span>
<span class="org-keyword">if</span> visited(i):
<span class="org-keyword">return</span>
visited.add(i)
<span class="org-keyword">for</span> x <span class="org-keyword">in</span> branches:
dfs(curr <span class="org-operator">+</span> x, ...)
visited.remove(i)
</pre>
</div>
<p>
它与普通 dfs 的区别主要是需要 `visited.remove` 而 dfs 通常不需要
</p>
</div>
<div id="outline-container-org0000000" class="outline-4">
<h4 id="org0000000"><span class="section-number-4">1.1.1.</span> N-Queens</h4>
<div class="outline-text-4" id="text-1-1-1">
<p>
<a href="https://leetcode.com/problems/n-queens/">https://leetcode.com/problems/n-queens/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">from</span> collections <span class="org-keyword">import</span> defaultdict
<span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">solveNQueens</span>(<span class="org-keyword">self</span>, n: <span class="org-builtin">int</span>) <span class="org-operator">-></span> List[List[<span class="org-builtin">str</span>]]:
<span class="org-variable-name">rows</span> <span class="org-operator">=</span> defaultdict(<span class="org-builtin">int</span>)
<span class="org-variable-name">cols</span> <span class="org-operator">=</span> defaultdict(<span class="org-builtin">int</span>)
<span class="org-variable-name">left</span> <span class="org-operator">=</span> defaultdict(<span class="org-builtin">int</span>)
<span class="org-variable-name">right</span> <span class="org-operator">=</span> defaultdict(<span class="org-builtin">int</span>)
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> []
<span class="org-keyword">def</span> <span class="org-function-name">put</span>(output, i, j):
<span class="org-variable-name">rows</span>[i] <span class="org-operator">=</span> 1
<span class="org-variable-name">cols</span>[j] <span class="org-operator">=</span> 1
<span class="org-variable-name">left</span>[i <span class="org-operator">+</span> j] <span class="org-operator">=</span> 1
<span class="org-variable-name">right</span>[i <span class="org-operator">-</span> j] <span class="org-operator">=</span> 1
output[i][j] <span class="org-operator">=</span> <span class="org-string">"Q"</span>
<span class="org-keyword">def</span> <span class="org-function-name">clear</span>(output, i, j):
<span class="org-variable-name">rows</span>[i] <span class="org-operator">=</span> 0
<span class="org-variable-name">cols</span>[j] <span class="org-operator">=</span> 0
<span class="org-variable-name">left</span>[i <span class="org-operator">+</span> j] <span class="org-operator">=</span> 0
<span class="org-variable-name">right</span>[i <span class="org-operator">-</span> j] <span class="org-operator">=</span> 0
output[i][j] <span class="org-operator">=</span> <span class="org-string">"."</span>
<span class="org-keyword">def</span> <span class="org-function-name">isValid</span>(i, j):
<span class="org-keyword">return</span> (
rows[i] <span class="org-operator">==</span> 0 <span class="org-keyword">and</span> cols[j] <span class="org-operator">==</span> 0 <span class="org-keyword">and</span> left[i <span class="org-operator">+</span> j] <span class="org-operator">==</span> 0 <span class="org-keyword">and</span> right[i <span class="org-operator">-</span> j] <span class="org-operator">==</span> 0
)
<span class="org-keyword">def</span> <span class="org-function-name">dfs</span>(output, i):
<span class="org-keyword">if</span> i <span class="org-operator">==</span> n:
ans.append([<span class="org-string">""</span>.join(x) <span class="org-keyword">for</span> x <span class="org-keyword">in</span> output])
<span class="org-keyword">return</span>
<span class="org-keyword">for</span> j <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n):
<span class="org-keyword">if</span> isValid(i, j):
put(output, i, j)
dfs(output, i <span class="org-operator">+</span> 1)
clear(output, i, j)
<span class="org-variable-name">output</span> <span class="org-operator">=</span> [[<span class="org-string">"."</span> <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n)] <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n)]
dfs(output, 0)
<span class="org-keyword">return</span> ans
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000003" class="outline-4">
<h4 id="org0000003"><span class="section-number-4">1.1.2.</span> Permutations II</h4>
<div class="outline-text-4" id="text-1-1-2">
<p>
<a href="https://leetcode.com/problems/permutations-ii/">https://leetcode.com/problems/permutations-ii/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">permuteUnique</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> List[List[<span class="org-builtin">int</span>]]:
<span class="org-variable-name">visited</span> <span class="org-operator">=</span> <span class="org-builtin">set</span>()
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> []
<span class="org-keyword">def</span> <span class="org-function-name">dfs</span>(curr):
<span class="org-keyword">if</span> <span class="org-builtin">len</span>(curr) <span class="org-operator">==</span> <span class="org-builtin">len</span>(nums):
ans.append(curr)
<span class="org-keyword">return</span>
<span class="org-variable-name">used</span> <span class="org-operator">=</span> <span class="org-builtin">set</span>()
<span class="org-keyword">for</span> i, x <span class="org-keyword">in</span> <span class="org-builtin">enumerate</span>(nums):
<span class="org-keyword">if</span> i <span class="org-keyword">in</span> visited:
<span class="org-keyword">continue</span>
<span class="org-keyword">if</span> x <span class="org-keyword">in</span> used:
<span class="org-keyword">continue</span>
used.add(x)
visited.add(i)
dfs(curr <span class="org-operator">+</span> [x])
visited.remove(i)
dfs([])
<span class="org-keyword">return</span> ans
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org000001c" class="outline-3">
<h3 id="org000001c"><span class="section-number-3">1.2.</span> Binary Search</h3>
<div class="outline-text-3" id="text-1-2">
</div>
<div id="outline-container-org000000a" class="outline-4">
<h4 id="org000000a"><span class="section-number-4">1.2.1.</span> Search a 2D Matrix II</h4>
<div class="outline-text-4" id="text-1-2-1">
<p>
<a href="https://leetcode.com/problems/search-a-2d-matrix-ii/">https://leetcode.com/problems/search-a-2d-matrix-ii/</a>
</p>
<p>
关键问题在于确定二维的 lo, hi 的值, 例如下图中比 e 小的范围为 abc, adg, 比 e 大的范围为 cfk, ghk. c, g 与 e 的大小并不确定
</p>
<pre class="example" id="org0000009">
a b c
d e f
g h k
</pre>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">searchMatrix</span>(<span class="org-keyword">self</span>, matrix: List[List[<span class="org-builtin">int</span>]], target: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">bool</span>:
<span class="org-variable-name">M</span>, <span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(matrix), <span class="org-builtin">len</span>(matrix[0])
<span class="org-keyword">def</span> <span class="org-function-name">solve</span>(lo, hi, target):
<span class="org-keyword">if</span> lo[0] <span class="org-operator">>=</span> hi[0] <span class="org-keyword">or</span> lo[1] <span class="org-operator">>=</span> hi[1]:
<span class="org-keyword">return</span> <span class="org-constant">False</span>
<span class="org-variable-name">midx</span>, <span class="org-variable-name">midy</span> <span class="org-operator">=</span> (lo[0] <span class="org-operator">+</span> hi[0]) <span class="org-operator">//</span> 2, (lo[1] <span class="org-operator">+</span> hi[1]) <span class="org-operator">//</span> 2
<span class="org-keyword">if</span> matrix[midx][midy] <span class="org-operator">==</span> target:
<span class="org-keyword">return</span> <span class="org-constant">True</span>
<span class="org-keyword">if</span> matrix[midx][midy] <span class="org-operator">></span> target:
<span class="org-keyword">return</span> solve(lo, (midx, hi[1]), target) <span class="org-keyword">or</span> solve(
lo, (hi[0], midy), target
)
<span class="org-keyword">return</span> solve((lo[0], midy <span class="org-operator">+</span> 1), hi, target) <span class="org-keyword">or</span> solve(
(midx <span class="org-operator">+</span> 1, lo[1]), hi, target
)
<span class="org-keyword">return</span> solve((0, 0), (M, N), target)
</pre>
</div>
<p>
或者采用非 bisect 的算法 (O(m+n)):
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">searchMatrix</span>(<span class="org-keyword">self</span>, matrix: List[List[<span class="org-builtin">int</span>]], target: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">bool</span>:
<span class="org-variable-name">M</span>, <span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(matrix), <span class="org-builtin">len</span>(matrix[0])
<span class="org-variable-name">x</span>, <span class="org-variable-name">y</span> <span class="org-operator">=</span> 0, N <span class="org-operator">-</span> 1
<span class="org-keyword">while</span> x <span class="org-operator"><</span> M <span class="org-keyword">and</span> y <span class="org-operator">>=</span> 0:
<span class="org-keyword">if</span> matrix[x][y] <span class="org-operator">==</span> target:
<span class="org-keyword">return</span> <span class="org-constant">True</span>
<span class="org-keyword">if</span> matrix[x][y] <span class="org-operator">></span> target:
<span class="org-variable-name">y</span> <span class="org-operator">-=</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">x</span> <span class="org-operator">+=</span> 1
<span class="org-keyword">return</span> <span class="org-constant">False</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org000000d" class="outline-4">
<h4 id="org000000d"><span class="section-number-4">1.2.2.</span> Find Minimum in Rotated Sorted Array</h4>
<div class="outline-text-4" id="text-1-2-2">
<p>
<a href="https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/">https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/</a>
</p>
<p>
这里使用 hi 为 len(nums)-1 而不是 len(nums). 正常的二分查找并不关注这个值, 但这个问题中 mid 本身要参与到下一次迭代: (lo,mid)/(mid+1, hi), 导致 N=2 时出现死循环.
</p>
<p>
假设 nums 为 [0,1], 若迭代公式为 (lo,mid)/(mid+1,hi), 若 mid 选 1, 则会死循环,
若 mid 选 0, 则没有问题.
</p>
<p>
同理, 若迭代公式为 (lo,mid-1)/(mid,hi), 若 mid 选 0, 则死循环, 若 mid 选 1, 则没问题.
</p>
<p>
使用 N 还是 N-1 实际上代表剩余两个元素 [0,1] 时 mid 选 0 还是 1.
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">findMin</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">lo</span>, <span class="org-variable-name">hi</span> <span class="org-operator">=</span> 0, <span class="org-builtin">len</span>(nums) <span class="org-operator">-</span> 1
<span class="org-keyword">while</span> lo <span class="org-operator"><</span> hi:
<span class="org-variable-name">mid</span> <span class="org-operator">=</span> (lo <span class="org-operator">+</span> hi) <span class="org-operator">//</span> 2
<span class="org-keyword">if</span> nums[lo] <span class="org-operator">></span> nums[mid]:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
<span class="org-keyword">elif</span> nums[mid] <span class="org-operator">></span> nums[hi]:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
<span class="org-keyword">return</span> nums[lo]
</pre>
</div>
<p>
如果不想考虑前面的问题, 也可以这样, 更简单一些:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">findMin</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">lo</span>, <span class="org-variable-name">hi</span> <span class="org-operator">=</span> 0, <span class="org-builtin">len</span>(nums)
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> 1 <span class="org-operator"><<</span> 31
<span class="org-keyword">while</span> lo <span class="org-operator"><</span> hi:
<span class="org-variable-name">mid</span> <span class="org-operator">=</span> (lo <span class="org-operator">+</span> hi) <span class="org-operator">//</span> 2
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> <span class="org-builtin">min</span>(ans, nums[mid])
<span class="org-keyword">if</span> nums[lo] <span class="org-operator">></span> nums[mid]:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
<span class="org-keyword">elif</span> nums[mid] <span class="org-operator">></span> nums[hi <span class="org-operator">-</span> 1]:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
<span class="org-keyword">return</span> ans
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000010" class="outline-4">
<h4 id="org0000010"><span class="section-number-4">1.2.3.</span> Search in Rotated Sorted Array</h4>
<div class="outline-text-4" id="text-1-2-3">
<p>
<a href="https://leetcode.com/problems/search-in-rotated-sorted-array/">https://leetcode.com/problems/search-in-rotated-sorted-array/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">search</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>], target: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">lo</span>, <span class="org-variable-name">hi</span> <span class="org-operator">=</span> 0, <span class="org-builtin">len</span>(nums)
<span class="org-keyword">while</span> lo <span class="org-operator"><</span> hi:
<span class="org-variable-name">mid</span> <span class="org-operator">=</span> (lo <span class="org-operator">+</span> hi) <span class="org-operator">//</span> 2
<span class="org-keyword">if</span> nums[mid] <span class="org-operator">==</span> target:
<span class="org-keyword">return</span> mid
<span class="org-keyword">if</span> nums[lo] <span class="org-operator"><</span> nums[mid]:
<span class="org-keyword">if</span> target <span class="org-operator">>=</span> nums[lo] <span class="org-keyword">and</span> target <span class="org-operator"><</span> nums[mid]:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
<span class="org-keyword">else</span>:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">else</span>:
<span class="org-keyword">if</span> target <span class="org-operator">></span> nums[mid] <span class="org-keyword">and</span> target <span class="org-operator"><=</span> nums[hi <span class="org-operator">-</span> 1]:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
<span class="org-keyword">return</span> <span class="org-operator">-</span>1
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000013" class="outline-4">
<h4 id="org0000013"><span class="section-number-4">1.2.4.</span> Sqrt</h4>
<div class="outline-text-4" id="text-1-2-4">
<p>
<a href="https://leetcode.com/problems/sqrtx/">https://leetcode.com/problems/sqrtx/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">mySqrt</span>(<span class="org-keyword">self</span>, x: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> 1
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> x
<span class="org-keyword">while</span> lo <span class="org-operator"><=</span> hi:
<span class="org-variable-name">mid</span> <span class="org-operator">=</span> (lo <span class="org-operator">+</span> hi) <span class="org-operator">//</span> 2
<span class="org-keyword">if</span> mid <span class="org-operator">*</span> mid <span class="org-operator"><=</span> x <span class="org-keyword">and</span> (mid <span class="org-operator">+</span> 1) <span class="org-operator">*</span> (mid <span class="org-operator">+</span> 1) <span class="org-operator">></span> x:
<span class="org-keyword">return</span> mid
<span class="org-keyword">if</span> mid <span class="org-operator">*</span> mid <span class="org-operator">></span> x:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid <span class="org-operator">-</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">return</span> 0
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000016" class="outline-4">
<h4 id="org0000016"><span class="section-number-4">1.2.5.</span> Minimize Maximum of Array</h4>
<div class="outline-text-4" id="text-1-2-5">
<p>
<a href="https://leetcode.com/problems/minimize-maximum-of-array/">https://leetcode.com/problems/minimize-maximum-of-array/</a>
</p>
<p>
与 Sqrt 问题类似, 需要在一个有限的解空间内寻找答案, 且寻找过程符合 bisect 的性质
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">minimizeArrayValue</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(nums)
<span class="org-keyword">def</span> <span class="org-function-name">ok</span>(n):
<span class="org-variable-name">t</span> <span class="org-operator">=</span> nums[::]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">reversed</span>(<span class="org-builtin">range</span>(1, N)):
<span class="org-keyword">if</span> t[i] <span class="org-operator">></span> n:
<span class="org-variable-name">t</span>[i <span class="org-operator">-</span> 1] <span class="org-operator">+=</span> t[i] <span class="org-operator">-</span> n
<span class="org-keyword">return</span> t[0] <span class="org-operator"><=</span> n
<span class="org-variable-name">lo</span>, <span class="org-variable-name">hi</span> <span class="org-operator">=</span> <span class="org-builtin">min</span>(nums), <span class="org-builtin">max</span>(nums)
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> 0
<span class="org-keyword">while</span> lo <span class="org-operator"><=</span> hi:
<span class="org-variable-name">mid</span> <span class="org-operator">=</span> (lo <span class="org-operator">+</span> hi) <span class="org-operator">//</span> 2
<span class="org-keyword">if</span> ok(mid):
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> mid
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid <span class="org-operator">-</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">return</span> ans
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000019" class="outline-4">
<h4 id="org0000019"><span class="section-number-4">1.2.6.</span> Single Element in a Sorted Array</h4>
<div class="outline-text-4" id="text-1-2-6">
<p>
<a href="https://leetcode.com/problems/single-element-in-a-sorted-array/">https://leetcode.com/problems/single-element-in-a-sorted-array/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">singleNonDuplicate</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> 0
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(nums)
<span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(nums)
<span class="org-keyword">while</span> lo <span class="org-operator"><</span> hi:
<span class="org-variable-name">mid</span> <span class="org-operator">=</span> (lo <span class="org-operator">+</span> hi) <span class="org-operator">//</span> 2
<span class="org-keyword">if</span> (mid <span class="org-operator">==</span> 0 <span class="org-keyword">or</span> nums[mid] <span class="org-operator">!=</span> nums[mid <span class="org-operator">-</span> 1]) <span class="org-keyword">and</span> (
mid <span class="org-operator">==</span> N <span class="org-operator">-</span> 1 <span class="org-keyword">or</span> nums[mid] <span class="org-operator">!=</span> nums[mid <span class="org-operator">+</span> 1]
):
<span class="org-keyword">return</span> nums[mid]
<span class="org-variable-name">pair</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1 <span class="org-keyword">if</span> mid <span class="org-operator">%</span> 2 <span class="org-operator">==</span> 0 <span class="org-keyword">else</span> mid <span class="org-operator">-</span> 1
<span class="org-keyword">if</span> nums[mid] <span class="org-operator">==</span> nums[pair]:
<span class="org-variable-name">lo</span> <span class="org-operator">=</span> mid <span class="org-operator">+</span> 1
<span class="org-keyword">else</span>:
<span class="org-variable-name">hi</span> <span class="org-operator">=</span> mid
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org0000030" class="outline-3">
<h3 id="org0000030"><span class="section-number-3">1.3.</span> knapsack</h3>
<div class="outline-text-3" id="text-1-3">
</div>
<div id="outline-container-org0000020" class="outline-4">
<h4 id="org0000020"><span class="section-number-4">1.3.1.</span> Partition Equal Subset Sum</h4>
<div class="outline-text-4" id="text-1-3-1">
<p>
<a href="https://leetcode.com/problems/partition-equal-subset-sum/">https://leetcode.com/problems/partition-equal-subset-sum/</a>
</p>
<p>
01 背包, sum 是背包, nums 是物品
</p>
<p>
其框架为:
</p>
<pre class="example" id="org000001f">
for i in range(N):
for k,v in dp[i-1].items():
dp[i][k+nums[i]]= ...
</pre>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">canPartition</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">bool</span>:
<span class="org-variable-name">total</span> <span class="org-operator">=</span> <span class="org-builtin">sum</span>(nums)
<span class="org-keyword">if</span> total <span class="org-operator">%</span> 2 <span class="org-operator">!=</span> 0:
<span class="org-keyword">return</span> <span class="org-constant">False</span>
<span class="org-variable-name">_dp</span> <span class="org-operator">=</span> [{} <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(<span class="org-builtin">len</span>(nums))]
_dp[0][0] <span class="org-operator">=</span> 1
_dp[0][nums[0]] <span class="org-operator">=</span> 1
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, <span class="org-builtin">len</span>(nums)):
<span class="org-keyword">for</span> k, v <span class="org-keyword">in</span> _dp[i <span class="org-operator">-</span> 1].items():
_dp[i][k <span class="org-operator">+</span> nums[i]] <span class="org-operator">=</span> 1
_dp[i][k] <span class="org-operator">=</span> 1
<span class="org-keyword">return</span> total <span class="org-operator">//</span> 2 <span class="org-keyword">in</span> _dp[<span class="org-operator">-</span>1]
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000024" class="outline-4">
<h4 id="org0000024"><span class="section-number-4">1.3.2.</span> Coin Change</h4>
<div class="outline-text-4" id="text-1-3-2">
<p>
<a href="https://leetcode.com/problems/coin-change/">https://leetcode.com/problems/coin-change/</a>
</p>
<p>
完全背包问题, 不限制 coins 的数量, amount 是背包, coins 是物品
</p>
<p>
其框架为:
</p>
<pre class="example" id="org0000023">
for i in range(amount):
for x in range(N):
dp[i]=f(x,dp[i-1])
</pre>
<p>
其中一般情况下外层循环是背包或物品并没有限制, 除非:
</p>
<ol class="org-ol">
<li>排列问题需要背包放在外层循环</li>
<li>组合问题需要背包放在内层循环</li>
</ol>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">coinChange</span>(<span class="org-keyword">self</span>, coins: List[<span class="org-builtin">int</span>], amount: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">_dp</span> <span class="org-operator">=</span> [0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(amount <span class="org-operator">+</span> 1)]
<span class="org-variable-name">MAX</span> <span class="org-operator">=</span> 1 <span class="org-operator"><<</span> 31
<span class="org-keyword">def</span> <span class="org-function-name">dp</span>(i):
<span class="org-keyword">if</span> i <span class="org-operator"><</span> 0:
<span class="org-keyword">return</span> MAX
<span class="org-keyword">return</span> _dp[i]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, amount <span class="org-operator">+</span> 1):
<span class="org-variable-name">tmp</span> <span class="org-operator">=</span> MAX
<span class="org-keyword">for</span> c <span class="org-keyword">in</span> coins:
<span class="org-variable-name">tmp</span> <span class="org-operator">=</span> <span class="org-builtin">min</span>(tmp, dp(i <span class="org-operator">-</span> c) <span class="org-operator">+</span> 1)
<span class="org-variable-name">_dp</span>[i] <span class="org-operator">=</span> tmp
<span class="org-keyword">if</span> _dp[<span class="org-operator">-</span>1] <span class="org-operator">==</span> MAX:
<span class="org-keyword">return</span> <span class="org-operator">-</span>1
<span class="org-keyword">return</span> _dp[<span class="org-operator">-</span>1]
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000027" class="outline-4">
<h4 id="org0000027"><span class="section-number-4">1.3.3.</span> Coin Change II</h4>
<div class="outline-text-4" id="text-1-3-3">
<p>
<a href="https://leetcode.com/problems/coin-change-ii/">https://leetcode.com/problems/coin-change-ii/</a>
</p>
<p>
完全背包, 但是是一个组合问题 (而不是排列问题), 需要把背包放在内层循环
</p>
<p>
<a href="https://leetcode.com/problems/coin-change-ii/solutions/5023598/beginner-mistake-permutations-vs-combination/">https://leetcode.com/problems/coin-change-ii/solutions/5023598/beginner-mistake-permutations-vs-combination/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">change</span>(<span class="org-keyword">self</span>, amount: <span class="org-builtin">int</span>, coins: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">_dp</span> <span class="org-operator">=</span> [0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(amount <span class="org-operator">+</span> 1)]
<span class="org-variable-name">_dp</span>[0] <span class="org-operator">=</span> 1
<span class="org-keyword">def</span> <span class="org-function-name">dp</span>(i):
<span class="org-keyword">if</span> i <span class="org-operator"><</span> 0:
<span class="org-keyword">return</span> 0
<span class="org-keyword">return</span> _dp[i]
<span class="org-keyword">for</span> c <span class="org-keyword">in</span> coins:
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, amount <span class="org-operator">+</span> 1):
<span class="org-variable-name">_dp</span>[i] <span class="org-operator">+=</span> dp(i <span class="org-operator">-</span> c)
<span class="org-keyword">return</span> _dp[<span class="org-operator">-</span>1]
</pre>
</div>
</div>
</div>
<div id="outline-container-org000002a" class="outline-4">
<h4 id="org000002a"><span class="section-number-4">1.3.4.</span> Combination Sum IV</h4>
<div class="outline-text-4" id="text-1-3-4">
<p>
<a href="https://leetcode.com/problems/combination-sum-iv/">https://leetcode.com/problems/combination-sum-iv/</a>
</p>
<p>
与 Coin Change II 基本相同, 但是是一个排列问题, 需要把背包放在外层循环
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">combinationSum4</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>], target: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">_dp</span> <span class="org-operator">=</span> [0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(target <span class="org-operator">+</span> 1)]
<span class="org-variable-name">_dp</span>[0] <span class="org-operator">=</span> 1
<span class="org-keyword">def</span> <span class="org-function-name">dp</span>(i):
<span class="org-keyword">if</span> i <span class="org-operator"><</span> 0:
<span class="org-keyword">return</span> 0
<span class="org-keyword">return</span> _dp[i]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, target <span class="org-operator">+</span> 1):
<span class="org-keyword">for</span> x <span class="org-keyword">in</span> nums:
<span class="org-variable-name">_dp</span>[i] <span class="org-operator">+=</span> dp(i <span class="org-operator">-</span> x)
<span class="org-keyword">return</span> _dp[<span class="org-operator">-</span>1]
</pre>
</div>
</div>
</div>
<div id="outline-container-org000002d" class="outline-4">
<h4 id="org000002d"><span class="section-number-4">1.3.5.</span> Word Break</h4>
<div class="outline-text-4" id="text-1-3-5">
<p>
<a href="https://leetcode.com/problems/word-break/">https://leetcode.com/problems/word-break/</a>
</p>
<p>
完全背包, 不限制 words 的数量, s 是背包, words 是物品
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">wordBreak</span>(<span class="org-keyword">self</span>, s: <span class="org-builtin">str</span>, wordDict: List[<span class="org-builtin">str</span>]) <span class="org-operator">-></span> <span class="org-builtin">bool</span>:
<span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(s)
<span class="org-variable-name">wordDict</span> <span class="org-operator">=</span> <span class="org-builtin">set</span>(wordDict)
<span class="org-variable-name">_dp</span> <span class="org-operator">=</span> [<span class="org-constant">False</span> <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N)]
<span class="org-keyword">def</span> <span class="org-function-name">dp</span>(i):
<span class="org-keyword">if</span> i <span class="org-operator">==</span> <span class="org-operator">-</span>1:
<span class="org-keyword">return</span> <span class="org-constant">True</span>
<span class="org-keyword">return</span> _dp[i]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N):
<span class="org-keyword">for</span> j <span class="org-keyword">in</span> <span class="org-builtin">range</span>(i <span class="org-operator">+</span> 1):
<span class="org-variable-name">x</span> <span class="org-operator">=</span> s[j : i <span class="org-operator">+</span> 1]
<span class="org-variable-name">tmp</span> <span class="org-operator">=</span> <span class="org-constant">False</span>
<span class="org-keyword">if</span> x <span class="org-keyword">in</span> wordDict:
<span class="org-variable-name">tmp</span> <span class="org-operator">|=</span> dp(j <span class="org-operator">-</span> 1)
<span class="org-keyword">if</span> tmp:
<span class="org-variable-name">_dp</span>[i] <span class="org-operator">=</span> <span class="org-constant">True</span>
<span class="org-keyword">break</span>
<span class="org-keyword">return</span> _dp[<span class="org-operator">-</span>1]
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org0000040" class="outline-3">
<h3 id="org0000040"><span class="section-number-3">1.4.</span> DP</h3>
<div class="outline-text-3" id="text-1-4">
</div>
<div id="outline-container-org0000034" class="outline-4">
<h4 id="org0000034"><span class="section-number-4">1.4.1.</span> Stone Game</h4>
<div class="outline-text-4" id="text-1-4-1">
<p>
<a href="https://leetcode.com/problems/stone-game/description/">https://leetcode.com/problems/stone-game/description/</a>
</p>
<p>
这个问题的子问题对应中间一段子区间, 这种形式的 DP 需要使用从小的 subarray 扩展到大的 subarray 的形式, 二维 DP 需要使用 distance 来迭代, 即:
</p>
<pre class="example" id="org0000033">
for d in range(1,N):
for i in range(N-d):
...
</pre>
<p>
且初始化条件为 d=0
</p>
<p>
类似的问题还有 <a href="https://leetcode.com/problems/palindromic-substrings/">https://leetcode.com/problems/palindromic-substrings/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">stoneGame</span>(<span class="org-keyword">self</span>, piles: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">bool</span>:
<span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(piles)
<span class="org-comment-delimiter"># </span><span class="org-comment">5 3 4 5</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">i j</span>
<span class="org-variable-name">dp</span> <span class="org-operator">=</span> [[0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N)] <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N)]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N):
dp[i][i] <span class="org-operator">=</span> piles[i]
<span class="org-keyword">for</span> d <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, N):
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N <span class="org-operator">-</span> d):
dp[i][i <span class="org-operator">+</span> d] <span class="org-operator">=</span> <span class="org-builtin">max</span>(
piles[i] <span class="org-operator">-</span> dp[i <span class="org-operator">+</span> 1][i <span class="org-operator">+</span> d], piles[i <span class="org-operator">+</span> d] <span class="org-operator">-</span> dp[i][i <span class="org-operator">+</span> d <span class="org-operator">-</span> 1]
)
<span class="org-keyword">return</span> dp[0][<span class="org-operator">-</span>1] <span class="org-operator">></span> 0
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000037" class="outline-4">
<h4 id="org0000037"><span class="section-number-4">1.4.2.</span> Maximum Sum Circular Subarray</h4>
<div class="outline-text-4" id="text-1-4-2">
<p>
<a href="https://leetcode.com/problems/maximum-sum-circular-subarray/">https://leetcode.com/problems/maximum-sum-circular-subarray/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">maxSubarraySumCircular</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-comment-delimiter"># </span><span class="org-comment">5 -3 5</span>
<span class="org-comment-delimiter"># </span><span class="org-comment">5 -3 5 5 -3 5</span>
<span class="org-keyword">def</span> <span class="org-function-name">getMax</span>(nums):
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> nums[0]
<span class="org-variable-name">dp</span> <span class="org-operator">=</span> nums[:]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, <span class="org-builtin">len</span>(nums)):
<span class="org-variable-name">dp</span>[i] <span class="org-operator">=</span> <span class="org-builtin">max</span>(dp[i <span class="org-operator">-</span> 1] <span class="org-operator">+</span> nums[i], nums[i])
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> <span class="org-builtin">max</span>(ans, dp[i])
<span class="org-keyword">return</span> ans
<span class="org-keyword">def</span> <span class="org-function-name">getMin</span>(nums):
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> nums[0]
<span class="org-variable-name">dp</span> <span class="org-operator">=</span> nums[:]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, <span class="org-builtin">len</span>(nums)):
<span class="org-variable-name">dp</span>[i] <span class="org-operator">=</span> <span class="org-builtin">min</span>(dp[i <span class="org-operator">-</span> 1] <span class="org-operator">+</span> nums[i], nums[i])
<span class="org-variable-name">ans</span> <span class="org-operator">=</span> <span class="org-builtin">min</span>(ans, dp[i])
<span class="org-keyword">return</span> ans
<span class="org-variable-name">a</span> <span class="org-operator">=</span> getMax(nums)
<span class="org-variable-name">b</span> <span class="org-operator">=</span> getMin(nums)
<span class="org-keyword">if</span> b <span class="org-operator">==</span> <span class="org-builtin">sum</span>(nums):
<span class="org-keyword">return</span> a
<span class="org-keyword">return</span> <span class="org-builtin">max</span>(a, <span class="org-builtin">sum</span>(nums) <span class="org-operator">-</span> b)
</pre>
</div>
</div>
</div>
<div id="outline-container-org000003a" class="outline-4">
<h4 id="org000003a"><span class="section-number-4">1.4.3.</span> Champagne Tower</h4>
<div class="outline-text-4" id="text-1-4-3">
<p>
<a href="https://leetcode.com/problems/champagne-tower/">https://leetcode.com/problems/champagne-tower/</a>
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">champagneTower</span>(<span class="org-keyword">self</span>, poured: <span class="org-builtin">int</span>, query_row: <span class="org-builtin">int</span>, query_glass: <span class="org-builtin">int</span>) <span class="org-operator">-></span> <span class="org-builtin">float</span>:
<span class="org-variable-name">_dp</span> <span class="org-operator">=</span> [[0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(query_row <span class="org-operator">+</span> 1)] <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(query_row <span class="org-operator">+</span> 1)]
_dp[0][0] <span class="org-operator">=</span> poured
<span class="org-keyword">def</span> <span class="org-function-name">get</span>(i, j):
<span class="org-keyword">if</span> j <span class="org-operator"><</span> 0 <span class="org-keyword">or</span> j <span class="org-operator">></span> i:
<span class="org-keyword">return</span> 0
<span class="org-keyword">return</span> (_dp[i][j] <span class="org-operator">-</span> 1) <span class="org-operator">/</span> 2 <span class="org-keyword">if</span> _dp[i][j] <span class="org-operator">>=</span> 1 <span class="org-keyword">else</span> 0
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, query_row <span class="org-operator">+</span> 1):
<span class="org-variable-name">goon</span> <span class="org-operator">=</span> <span class="org-constant">False</span>
<span class="org-keyword">for</span> j <span class="org-keyword">in</span> <span class="org-builtin">range</span>(i <span class="org-operator">+</span> 1):
_dp[i][j] <span class="org-operator">=</span> get(i <span class="org-operator">-</span> 1, j <span class="org-operator">-</span> 1) <span class="org-operator">+</span> get(i <span class="org-operator">-</span> 1, j)
<span class="org-keyword">if</span> _dp[i][j] <span class="org-operator">></span> 1:
<span class="org-variable-name">goon</span> <span class="org-operator">=</span> <span class="org-constant">True</span>
<span class="org-keyword">if</span> <span class="org-keyword">not</span> goon:
<span class="org-keyword">break</span>
<span class="org-keyword">return</span> <span class="org-builtin">min</span>(_dp[query_row][query_glass], 1)
</pre>
</div>
</div>
</div>
<div id="outline-container-org000003d" class="outline-4">
<h4 id="org000003d"><span class="section-number-4">1.4.4.</span> Maximum Alternating Subsequence Sum</h4>
<div class="outline-text-4" id="text-1-4-4">
<p>
<a href="https://leetcode.com/problems/maximum-alternating-subsequence-sum/">https://leetcode.com/problems/maximum-alternating-subsequence-sum/</a>
</p>
<p>
类似的问题还有:
<a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/">https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/</a>
</p>
<p>
该问题和`最大的子数组`类似, 但元素如何 take 取决于当前子序列长度是奇数还是偶数,
因此需要维护 even 和 odd 两个 sum
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">maxAlternatingSum</span>(<span class="org-keyword">self</span>, nums: List[<span class="org-builtin">int</span>]) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-comment-delimiter"># </span><span class="org-comment">4 2 5 3</span>
<span class="org-variable-name">N</span> <span class="org-operator">=</span> <span class="org-builtin">len</span>(nums)
<span class="org-comment-delimiter"># </span><span class="org-comment">subseq 中元素为偶数个</span>
<span class="org-variable-name">_dpEven</span> <span class="org-operator">=</span> [0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N)]
<span class="org-comment-delimiter"># </span><span class="org-comment">subseq 中元素为奇数个</span>
<span class="org-variable-name">_dpOdd</span> <span class="org-operator">=</span> [0 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(N)]
<span class="org-variable-name">_dpOdd</span>[0] <span class="org-operator">=</span> nums[0]
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(1, N):
<span class="org-variable-name">_dpEven</span>[i] <span class="org-operator">=</span> <span class="org-builtin">max</span>(_dpEven[i <span class="org-operator">-</span> 1], _dpOdd[i <span class="org-operator">-</span> 1] <span class="org-operator">-</span> nums[i])
<span class="org-variable-name">_dpOdd</span>[i] <span class="org-operator">=</span> <span class="org-builtin">max</span>(_dpOdd[i <span class="org-operator">-</span> 1], _dpEven[i <span class="org-operator">-</span> 1] <span class="org-operator">+</span> nums[i])
<span class="org-keyword">return</span> <span class="org-builtin">max</span>(_dpEven[<span class="org-operator">-</span>1], _dpOdd[<span class="org-operator">-</span>1])
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org0000052" class="outline-3">
<h3 id="org0000052"><span class="section-number-3">1.5.</span> Graph</h3>
<div class="outline-text-3" id="text-1-5">
</div>
<div id="outline-container-org0000043" class="outline-4">
<h4 id="org0000043"><span class="section-number-4">1.5.1.</span> Find the City With the Smallest Number of Neighbors at a Threshold Distance</h4>
<div class="outline-text-4" id="text-1-5-1">
<p>
<a href="https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/">https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/</a>
</p>
<p>
floyd warshall
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">class</span> <span class="org-type">Solution</span>:
<span class="org-keyword">def</span> <span class="org-function-name">findTheCity</span>(
<span class="org-keyword">self</span>, n: <span class="org-builtin">int</span>, edges: List[List[<span class="org-builtin">int</span>]], distanceThreshold: <span class="org-builtin">int</span>
) <span class="org-operator">-></span> <span class="org-builtin">int</span>:
<span class="org-variable-name">dist</span> <span class="org-operator">=</span> [[1 <span class="org-operator"><<</span> 31 <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n)] <span class="org-keyword">for</span> _ <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n)]
<span class="org-keyword">for</span> x, y, d <span class="org-keyword">in</span> edges:
dist[x][y] <span class="org-operator">=</span> d
dist[y][x] <span class="org-operator">=</span> d
<span class="org-keyword">for</span> k <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n):
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n):
<span class="org-keyword">for</span> j <span class="org-keyword">in</span> <span class="org-builtin">range</span>(n):
<span class="org-keyword">if</span> i <span class="org-operator">==</span> j:
<span class="org-keyword">continue</span>
dist[i][j] <span class="org-operator">=</span> <span class="org-builtin">min</span>(dist[i][j], dist[i][k] <span class="org-operator">+</span> dist[k][j])
<span class="org-variable-name">count</span> <span class="org-operator">=</span> [<span class="org-builtin">len</span>(<span class="org-builtin">list</span>(<span class="org-builtin">filter</span>(<span class="org-keyword">lambda</span> x: x <span class="org-operator"><=</span> distanceThreshold, x))) <span class="org-keyword">for</span> x <span class="org-keyword">in</span> dist]
<span class="org-variable-name">target</span> <span class="org-operator">=</span> <span class="org-builtin">min</span>(count)
<span class="org-keyword">for</span> i <span class="org-keyword">in</span> <span class="org-builtin">reversed</span>(<span class="org-builtin">range</span>(n)):
<span class="org-keyword">if</span> count[i] <span class="org-operator">==</span> target:
<span class="org-keyword">return</span> i
</pre>
</div>
</div>
</div>
<div id="outline-container-org0000046" class="outline-4">
<h4 id="org0000046"><span class="section-number-4">1.5.2.</span> Cheapest Flights Within K Stops</h4>
<div class="outline-text-4" id="text-1-5-2">
<p>
<a href="https://leetcode.com/problems/cheapest-flights-within-k-stops/">https://leetcode.com/problems/cheapest-flights-within-k-stops/</a>
</p>
<p>
dijkstra
</p>