-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2859 lines (1056 loc) · 108 KB
/
index.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
<!DOCTYPE html>
<html class="theme-next mist use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">
<link rel="stylesheet" href="/css/main.css?v=7.1.2">
<script id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Mist',
version: '7.1.2',
sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
back2top: true,
back2top_sidebar: false,
fancybox: false,
fastclick: false,
lazyload: false,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="IAM.CZH">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="IAM.CZH">
<meta property="og:locale" content="default">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="IAM.CZH">
<link rel="canonical" href="http://yoursite.com/">
<script id="page.configurations">
CONFIG.page = {
sidebar: "",
};
</script>
<title>IAM.CZH</title>
<noscript>
<style>
.use-motion .motion-element,
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-title { opacity: initial; }
.use-motion .logo,
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="default">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">IAM.CZH</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
</div>
<div class="site-nav-toggle">
<button aria-label="Toggle navigation bar">
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home menu-item-active">
<a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>Home</a>
</li>
<li class="menu-item menu-item-me">
<a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>Me</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>Tags</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>Categories</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>Archives</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br>Search</a>
</li>
</ul>
<div class="site-search">
<div class="popup search-popup local-search-popup">
<div class="local-search-header clearfix">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
<div class="local-search-input-wrapper">
<input autocomplete="off" placeholder="Searching..." spellcheck="false" type="text" id="local-search-input">
</div>
</div>
<div id="local-search-result"></div>
</div>
</div>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/02/19/algorithm/Minimum-Number-of-Refueling-Stops/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="CHEN ZHIHANG">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="IAM.CZH">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2021/02/19/algorithm/Minimum-Number-of-Refueling-Stops/" class="post-title-link" itemprop="url">从Minimum Number of Refueling Stops谈DP降维</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2021-02-19 22:49:40 / Modified: 23:35:02" itemprop="dateCreated datePublished" datetime="2021-02-19T22:49:40+09:00">2021-02-19</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Algorithm/" itemprop="url" rel="index"><span itemprop="name">Algorithm</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h4><p><a href="https://leetcode.com/problems/minimum-number-of-refueling-stops/" target="_blank" rel="noopener">Link</a></p>
<p>给定一个<code>target</code>和<code>stations</code>,求最少加几次油可以到达目的地。</p>
<h4 id="Answer"><a href="#Answer" class="headerlink" title="Answer"></a>Answer</h4><p>这是一道DP题</p>
<p><code>dp[i][j]</code>: 经过<code>i</code>个加油站加<code>j</code>次油最多能走多少公里。这里有个限制<code>j <= i</code></p>
<p>当经过第<code>i</code>个加油站有两个选择,只有加油或者不加油两种选择。</p>
<ol>
<li>如果不加油的话, <code>dp[i][j] = dp[i-1][j]</code></li>
<li>如果加油的话,<code>dp[i][j] = dp[i-1][j-1] + station[i-1][1]</code></li>
</ol>
<p>最后我们遍历<code>j</code>, 代表经过所有车站最多加<code>j</code>次油可以跑多少公里。如果大于等于<code>target</code>那么就返回<code>j</code>. 如果没有结果的话就返回<code>-1</code>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> {</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">minRefuelStops</span><span class="params">(<span class="keyword">int</span> target, <span class="keyword">int</span> startFuel, <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">int</span>>>& s)</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> n = s.size();</span><br><span class="line"> <span class="comment">// dp[i][j] 经过第i个加油站,加j次油,可以跑的最远距离</span></span><br><span class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">long</span>>> dp(n+<span class="number">1</span>, <span class="built_in">vector</span><<span class="keyword">long</span>>(n+<span class="number">1</span>, <span class="number">0</span>));</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i <= n; ++i) {</span><br><span class="line"> dp[i][<span class="number">0</span>] = startFuel;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; ++i) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j<= i; ++j) {</span><br><span class="line"> <span class="comment">// 不加油</span></span><br><span class="line"> <span class="keyword">if</span> (dp[i<span class="number">-1</span>][j] >= s[i<span class="number">-1</span>][<span class="number">0</span>]) {</span><br><span class="line"> dp[i][j] = dp[i<span class="number">-1</span>][j];</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 加油</span></span><br><span class="line"> <span class="keyword">if</span> (dp[i<span class="number">-1</span>][j<span class="number">-1</span>] >= s[i<span class="number">-1</span>][<span class="number">0</span>]) {</span><br><span class="line"> dp[i][j] = max(dp[i][j], dp[i<span class="number">-1</span>][j<span class="number">-1</span>] + s[i<span class="number">-1</span>][<span class="number">1</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i <= n; ++i) {</span><br><span class="line"> <span class="keyword">if</span> (dp[n][i] >= target) <span class="keyword">return</span> i;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
<h4 id="Follow-Up"><a href="#Follow-Up" class="headerlink" title="Follow Up"></a>Follow Up</h4><p>这题用了二维数组,我们观察到<code>dp</code>在更新的时候,只跟前有限个状态有关 </p>
<ul>
<li><p><code>dp[i][j] <=> dp[i-1][j]</code></p>
</li>
<li><p><code>dp[i][j] <=> dp[i-1][j-1]</code></p>
</li>
</ul>
<p>我们看到<code>dp[i][*]</code>总是和前一个状态<code>dp[i-1][*]</code>有关,因此我们可以降低一个维度,也就是把第一维也删去。变为</p>
<ul>
<li><p><code>dp[j] <=> dp[j]</code></p>
</li>
<li><p><code>dp[j] <=> dp[j-1]</code></p>
</li>
</ul>
<p>但是我们要注意<code>dp[j] = dp[j-1]</code>. 如果按顺序更新的话<code>dp[1] <=> dp[0]</code>… <code>dp[2] <=> dp[1]</code>. 在更新<code>dp[2]</code>的时候使用的<code>dp[1]</code>是本轮更新过的,会导致答案错误,因此我们应该尝试倒序更新。<code>dp[2] <=> dp[1]</code>…<code>dp[1] <=> dp[0]</code>. </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> {</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">minRefuelStops</span><span class="params">(<span class="keyword">int</span> target, <span class="keyword">int</span> startFuel, <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">int</span>>>& s)</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> n = s.size();</span><br><span class="line"> <span class="comment">// dp[i] 加i次油,可以跑的最远距离</span></span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">long</span>> dp(n+<span class="number">1</span>);</span><br><span class="line"> dp[<span class="number">0</span>] = startFuel;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">1</span>; i <= n; ++i) {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = i; j >= <span class="number">1</span>; --j) {</span><br><span class="line"> <span class="comment">// 不加油</span></span><br><span class="line"> <span class="keyword">if</span> (dp[j] >= s[i<span class="number">-1</span>][<span class="number">0</span>]) {</span><br><span class="line"> dp[j] = dp[j];</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 加油</span></span><br><span class="line"> <span class="keyword">if</span> (dp[j<span class="number">-1</span>] >= s[i<span class="number">-1</span>][<span class="number">0</span>]) {</span><br><span class="line"> dp[j] = max(dp[j], dp[j<span class="number">-1</span>] + s[i<span class="number">-1</span>][<span class="number">1</span>]);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i <= n; ++i) {</span><br><span class="line"> <span class="keyword">if</span> (dp[i] >= target) <span class="keyword">return</span> i;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/02/14/leetcode/Domino-and-Tromino-Tiling/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="CHEN ZHIHANG">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="IAM.CZH">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2021/02/14/leetcode/Domino-and-Tromino-Tiling/" class="post-title-link" itemprop="url">Domino and Tromino Tiling</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2021-02-14 14:59:54" itemprop="dateCreated datePublished" datetime="2021-02-14T14:59:54+09:00">2021-02-14</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-02-19 23:34:55" itemprop="dateModified" datetime="2021-02-19T23:34:55+09:00">2021-02-19</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Leetcode/" itemprop="url" rel="index"><span itemprop="name">Leetcode</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h4><p><a href="https://leetcode.com/problems/domino-and-tromino-tiling/" target="_blank" rel="noopener">Link</a></p>
<p>很有趣的一道题,给定一个<code>2*N</code>的格子,一共有2种形状,问有几种拼法。</p>
<h4 id="Answer"><a href="#Answer" class="headerlink" title="Answer"></a>Answer</h4><p>虽然只有2种形状,一种是<code>domino</code>, 一种是<code>tromino</code>, 但是由于可以旋转,所以拼的时候一共有<code>domino</code>2种+<code>tromino</code>4种=6种。最开始也是想用<code>dp</code>做。但是对于<code>tromino</code>就不适用了。论坛有个人定一个三个<code>dp</code>来做。<br><a href="https://leetcode.com/problems/domino-and-tromino-tiling/" target="_blank" rel="noopener">https://leetcode.com/problems/domino-and-tromino-tiling/</a></p>
<p>时间: $O(N)$<br>空间: $O(3N)$<br>由于只需要最多前两种的状态,所以我们也可以用常数来保存状态使得空间复杂度只有$O(1)$</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> {</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">numTilings</span><span class="params">(<span class="keyword">int</span> N)</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> (N == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> mod = <span class="number">1e9</span>+<span class="number">7</span>;</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">long</span>> dp(N+<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">long</span>> dpUp(N+<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"> <span class="built_in">vector</span><<span class="keyword">long</span>> dpDown(N+<span class="number">1</span>, <span class="number">0</span>);</span><br><span class="line"> </span><br><span class="line"> dp[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"> </span><br><span class="line"> dp[<span class="number">2</span>] = <span class="number">2</span>;</span><br><span class="line"> dpUp[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line"> dpDown[<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">3</span>; i <= N; ++i) {</span><br><span class="line"> dp[i] = (dp[i<span class="number">-1</span>] + dp[i<span class="number">-2</span>] + dpUp[i<span class="number">-1</span>] + dpDown[i<span class="number">-1</span>]) % mod;</span><br><span class="line"> </span><br><span class="line"> dpUp[i] = (dp[i<span class="number">-2</span>] + dpDown[i<span class="number">-1</span>]) % mod;</span><br><span class="line"> </span><br><span class="line"> dpDown[i] = (dp[i<span class="number">-2</span>] + dpUp[i<span class="number">-1</span>]) % mod;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> dp[N];</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/02/14/leetcode/3Sum-With-Multiplicity/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="CHEN ZHIHANG">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="IAM.CZH">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2021/02/14/leetcode/3Sum-With-Multiplicity/" class="post-title-link" itemprop="url">3Sum With Multiplicity</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2021-02-14 14:35:31" itemprop="dateCreated datePublished" datetime="2021-02-14T14:35:31+09:00">2021-02-14</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-02-19 23:35:01" itemprop="dateModified" datetime="2021-02-19T23:35:01+09:00">2021-02-19</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Leetcode/" itemprop="url" rel="index"><span itemprop="name">Leetcode</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h4><p><a href="https://leetcode.com/problems/3sum-with-multiplicity/" target="_blank" rel="noopener">Link</a></p>
<p><code>3Sum</code>的升级版,给定一个数列,然后求所有加起来和是<code>target</code>的子数列的个数。</p>
<h4 id="Answer"><a href="#Answer" class="headerlink" title="Answer"></a>Answer</h4><p>由于答案不是单一的,肯定根每个数字出现的次数有关,所以用一个<code>map</code>来储存所有数字的个数。然后我们分3种情况。假设<code>nums[i]+nums[j]+nums[k]=target</code></p>
<ol>
<li><p>假设三个数字都是一样的。即<code>nums[i] == nums[j] == nums[k]</code>, 那么就是组合问题,从<code>N</code>个数字里面挑出3个。</p>
</li>
<li><p>假设两个数字一样。即<code>nums[i] == nums[j] != nums[k]</code>, 也就是从<code>N</code>个数字里面挑出2个乘以<code>nums[k]</code>出现的次数。</p>
</li>
<li><p>假设三个数字都不一样。这种比较复杂。嫁入给定的数组是<code>[1,2,3]</code>, <code>target=6</code>, 如果我们双循环数组,我们会得到<code>[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]</code>. 但是其实他们只能算一种。所以我们应该做个简单限制,当<code>nums[i] < nums[j] < nums[k]</code>或者<code>nums[i] > nums[j] > nums[k]</code>时候,我们才计算。</p>
</li>
</ol>
<p>时间复杂度: $O(N^2)$<br>空间复杂度: $O(N)$</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> {</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">threeSumMulti</span><span class="params">(<span class="built_in">vector</span><<span class="keyword">int</span>>& A, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="built_in">unordered_map</span><<span class="keyword">int</span>,<span class="keyword">long</span>> m;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> c: A) {</span><br><span class="line"> m[c]++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> it1: m){</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">auto</span> it2: m) {</span><br><span class="line"> <span class="keyword">int</span> i = it1.first, j = it2.first, k = target - i - j;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> (!m.count(k)) <span class="keyword">continue</span>;</span><br><span class="line"> <span class="keyword">if</span> (i == j && j == k) {</span><br><span class="line"> res += m[i] * (m[i]<span class="number">-1</span>) * (m[i]<span class="number">-2</span>) / <span class="number">6</span>;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span> (i == j && i != k) {</span><br><span class="line"> res += m[i] * (m[i]<span class="number">-1</span>) / <span class="number">2</span> * m[k];</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span> (i < j && j < k) {</span><br><span class="line"> res += m[i] * m[j] * m[k];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res % <span class="keyword">int</span>(<span class="number">1e9</span>+<span class="number">7</span>);</span><br><span class="line"> }</span><br><span class="line">};</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/02/14/leetcode/Minimum-Limit-of-Balls-in-a-Bag/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="CHEN ZHIHANG">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="IAM.CZH">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2021/02/14/leetcode/Minimum-Limit-of-Balls-in-a-Bag/" class="post-title-link" itemprop="url">Minimum Limit of Balls in a Bag</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2021-02-14 14:24:01" itemprop="dateCreated datePublished" datetime="2021-02-14T14:24:01+09:00">2021-02-14</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2021-02-19 23:34:58" itemprop="dateModified" datetime="2021-02-19T23:34:58+09:00">2021-02-19</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Leetcode/" itemprop="url" rel="index"><span itemprop="name">Leetcode</span></a></span>
</span>