-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1707 lines (817 loc) · 124 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 pisces use-motion">
<head>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />
<meta name="description" content="Every Day in Life Is a First Time">
<meta property="og:type" content="website">
<meta property="og:title" content="Lotp's Blog">
<meta property="og:url" content="http://blog.lotp.xyz/index.html">
<meta property="og:site_name" content="Lotp's Blog">
<meta property="og:description" content="Every Day in Life Is a First Time">
<meta property="og:locale" content="en">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Lotp's Blog">
<meta name="twitter:description" content="Every Day in Life Is a First Time">
<script type="text/javascript" id="hexo.configuration">
var NexT = window.NexT || {};
var CONFIG = {
scheme: 'Pisces',
sidebar: {"position":"left","display":"post"},
fancybox: true,
motion: true,
duoshuo: {
userId: 0,
author: 'Author'
}
};
</script>
<link rel="canonical" href="http://blog.lotp.xyz/"/>
<title> Lotp's Blog </title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="en">
<!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container one-collumn 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-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">Lotp's Blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<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-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-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
Tags
</a>
</li>
</ul>
</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">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2021/01/01/LeetCode-887-Super-Egg-Drop/" itemprop="url">
An Articulation of the O(KN) Solution to 'Leetcode 887: Super Egg Drop'
</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 itemprop="dateCreated" datetime="2021-01-01T20:00:00+08:00" content="2021-01-01">
2021-01-01
</time>
</span>
<span class="post-comments-count">
|
<a href="/2021/01/01/LeetCode-887-Super-Egg-Drop/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2021/01/01/LeetCode-887-Super-Egg-Drop/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p><img src="/images/2020/superEggDrop/cover.png" alt="Super Egg Drop"></p>
<p><a href="https://leetcode.com/problems/super-egg-drop/" target="_blank" rel="noopener">‘Super Egg Drop’</a> is a very classic <strong>DP</strong> problem. Good articles explaining the O(KN*N) and the O(KN*logN) solutions can be easily found from the internet. Better than that, there is a solution of O(KN). However, there isn’t much about the O(KN) solution and the rare posts about it aren’t very clear. The article you are reading now is an endeavor to fill this gap.</p>
<h1 id="Check-Out-The-Problem"><a href="#Check-Out-The-Problem" class="headerlink" title="Check Out The Problem"></a>Check Out The Problem</h1><p>You are given <code>K</code> eggs, and you have access to a building with <code>N</code> floors from 1 to <code>N</code> </p>
<p>Each egg is identical in function, and if an egg breaks, you cannot drop it again.</p>
<p>You know that there exists a floor F with <code>0 <= F <= N</code> such that any egg dropped at a floor higher than <code>F</code> will break, and any egg dropped at or below floor <code>F</code> will not break.</p>
<p>Each move, you may take an egg (if you have an unbroken one) and drop it from any floor <code>X (with 1 <= X <= N)</code>. </p>
<p>Your goal is to know with certainty what the value of <code>F</code> is.</p>
<p>What is the minimum number of moves that you need to know with certainty what <code>F</code> is, regardless of the initial value of <code>F</code></p>
<h5 id="Example"><a href="#Example" class="headerlink" title="Example:"></a>Example:</h5><blockquote>
<p>Input: K = 1, N = 2<br>Output: 2<br>Explanation:<br>Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.<br>Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1.<br>If it didn’t break, then we know with certainty F = 2.<br>Hence, we needed 2 moves in the worst case to know what F is with certainty.</p>
</blockquote>
<h1 id="Analysis"><a href="#Analysis" class="headerlink" title="Analysis"></a>Analysis</h1><p>Firstly Let’s define dp(i,j) as the maximum number of floors we can surely find <strong>F</strong> from with i moves and j eggs. With this definition, the original question can be transformed into finding the minimum m whose <code>dp(m,K)</code> is larger than or equal to N.</p>
<p>Secondly, it can be easily observed that the maximum number of floors we can find F from is equal to the number of moves we can take if there is only 1 egg. In a formula, <code>dp(i,1)=i</code>.</p>
<p>Assuming that to get <code>dp(m,K)</code>, the first floor where we drop the egg is <code>dp(m-1, K-1)+1</code> floor (We will explain why choose this floor later). With this assumption in mind, there will be two different cases to analyse:</p>
<ul>
<li><h4 id="The-first-egg-breaks"><a href="#The-first-egg-breaks" class="headerlink" title="The first egg breaks"></a>The first egg breaks</h4>From the breaking of the first egg, we know that the elusive <strong>F</strong> must be within first <code>dp(m-1,K-1)</code> floors. And with the poor broken first egg lost and the first move taken, we now can move <strong>m-1</strong> times with <strong>K-1</strong> eggs which is just enough for us to find <strong>F</strong> from the first <code>dp(m-1,K-1)</code> floors. At this moment, we can confidently say we could always find target <strong>F</strong> no matter how large the number <strong>N</strong> is.</li>
<li><h4 id="The-first-egg-remains-intact"><a href="#The-first-egg-remains-intact" class="headerlink" title="The first egg remains intact"></a>The first egg remains intact</h4>Since the egg won’t break from <code>dp(m-1,K-1)+1</code> floor we can conclude that <strong>F</strong> is not within the first <code>dp(m-1,K-1)</code> floors. With the initial move taken and the egg stay un-broken, we now have m-1 moves to make with K eggs. That is to say, we can check another at most<code>dp(m-1,K)</code> floors above the first <code>dp(m-1,K-1)+1</code> floors. At this case, the maximum number of floors we can find from is <code>dp(m-1,K-1)+1+dp(m-1,K)</code>.</li>
</ul>
<p>The proplem states we have to find the answer that we can be 100 percent confident about. In another word, we have to take the worst case into consideration ,i.e. the case of un-broken first egg. After all these analyses, we get the state transforming formula: <code>dp(m,K)=dp(m-1,K-1)+1+dp(m-1,K)</code>.</p>
<p>Finally, let’s write the code:<br><figure class="highlight python"><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></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span><span class="params">(object)</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">superEggDrop</span><span class="params">(self, K, N)</span>:</span></span><br><span class="line"> m = <span class="number">1</span></span><br><span class="line"> dp = [<span class="number">1</span>]*(K+<span class="number">1</span>)</span><br><span class="line"> <span class="keyword">while</span> dp[K] < N:</span><br><span class="line"> m += <span class="number">1</span></span><br><span class="line"> tmp = [m]*(K+<span class="number">1</span>)</span><br><span class="line"> <span class="keyword">for</span> k <span class="keyword">in</span> range(<span class="number">2</span>, K+<span class="number">1</span>):</span><br><span class="line"> tmp[k] = dp[k<span class="number">-1</span>]+dp[k]+<span class="number">1</span></span><br><span class="line"> <span class="keyword">if</span> tmp[k] >= N:</span><br><span class="line"> tmp[K] = tmp[k]</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> dp = tmp</span><br><span class="line"> <span class="keyword">return</span> m</span><br></pre></td></tr></table></figure></p>
<h3 id="Why-Drop-The-Egg-From-the-dp-m-1-K-1-1-Floor-At-The-First-Place"><a href="#Why-Drop-The-Egg-From-the-dp-m-1-K-1-1-Floor-At-The-First-Place" class="headerlink" title="Why Drop The Egg From the dp(m-1,K-1)+1 Floor At The First Place?"></a>Why Drop The Egg From the <code>dp(m-1,K-1)+1</code> Floor At The First Place?</h3><p>To answer this question, we will have two scenarios to analyse:</p>
<ul>
<li><h4 id="Drop-the-first-egg-from-floor-higher-than-dp-m-1-K-1-1"><a href="#Drop-the-first-egg-from-floor-higher-than-dp-m-1-K-1-1" class="headerlink" title="Drop the first egg from floor higher than dp(m-1, K-1)+1"></a>Drop the first egg from floor higher than <code>dp(m-1, K-1)+1</code></h4>Let’s say the first egg is dropped from <code>dp(m-1,K-1)+2</code> floor and it breaks. Then we will have to find <strong>F</strong> from <code>dp(m-1,K-1)+1</code> floors. Unfortunately, the remaining m-1 moves and K-1 eggs only allow us to find <strong>F</strong> from <code>dp(m-1, K-1)</code> floors. At the this case, we will fail.</li>
<li><h4 id="Drop-the-first-egg-from-floor-lower-than-dp-m-1-K-1-1"><a href="#Drop-the-first-egg-from-floor-lower-than-dp-m-1-K-1-1" class="headerlink" title="Drop the first egg from floor lower than dp(m-1,K-1)+1"></a>Drop the first egg from floor lower than <code>dp(m-1,K-1)+1</code></h4>Assuming we drop the first egg at floor <code>dp(m-1,K-1)</code> and it doesn’t break. Then the final maximum number of floors we will reach is <code>dp(m-1,K-1)-1+1+dp(m-1,K)=dp(m-1,K-1)+dp(m-1,K)</code> which is smaller than the number <code>dp(m-1,K-1)+1+dp(m-1,K)</code> we can get from the <code>dp(m-1,K-1)+1</code> floor.</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2020/05/25/LeetCode-233-Number-of-Digit-One/" itemprop="url">
[LeetCode 233, AC] Number of Digit One Done With 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 itemprop="dateCreated" datetime="2020-05-25T19:39:16+08:00" content="2020-05-25">
2020-05-25
</time>
</span>
<span class="post-comments-count">
|
<a href="/2020/05/25/LeetCode-233-Number-of-Digit-One/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/05/25/LeetCode-233-Number-of-Digit-One/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<pre><code>Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
Example:
Input: 13
Output: 6
Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
</code></pre><p>Let’s try to solve <a href="https://leetcode.com/problems/number-of-digit-one/" target="_blank" rel="noopener">this problem</a> by dividing it into smaller quizzes:</p>
<p>For example, to caculate the result for 2954:</p>
<ul>
<li>count(2954)=count(2000)+count(954)</li>
<li>count(954)=count(900)+count(54)</li>
<li>count(54)=count(50)+count(4)</li>
</ul>
<p>Now we got the conclusion that <code>count(2954)=count(2000)+count(900)+count(50)+count(4)</code>, which is leading us to the question of how to get <code>count(i*10^j)</code></p>
<p>To calculate <code>count(i*10^j)</code>, we separate the ‘1’s we are looking for into two groups:</p>
<ul>
<li>1s appearing in the highest digit, like the first ‘1’ in the number 1111.<ul>
<li>For numbers starting with 1, this part is the lower part number plus one. Let’s take 1599 as an example. From 1000 to 1599 we got 599+1=600 ‘1’s at the highest digit.</li>
<li>For numbers not starting with 1, this part is always <code>10^math.floor(log10(number))</code>. Like for 2000, from 1000 to 1999 we got 1000 ‘1’ which is equal to 10^3. </li>
</ul>
</li>
<li>1s appearing in the lower digits, like the last 3 ‘1’s in the number 1111.<br> The formula to calulate this part is: <code>(highest digit) * count(10^math.floor(log10(number)-1))</code>. Like for 3000, the result is 3*count(999)</li>
</ul>
<p>At this point, the last puzzle we need to solve is to find a way to calculate count(10^n-1). Let’s take a look at some examples:</p>
<ul>
<li>For number ranging from 1-99 we got 1 one.</li>
<li>For number ranging from 1-999 we got 20 ones.</li>
<li>For number ranging from 1-9999 we got 300 ones.</li>
</ul>
<p>After some observations, we can see <code>count(10^n-1)=count(10^(n-1)-1)*10+10^(n-1)</code>. Combining this with an initial case of <code>count(9)=1</code>, we can extrapolate that <code>count(10^n-1)=n*10^(n-1)</code>.</p>
<p>With all these deductions in mind, we can finally solve the original problem with DP.<br>I implemente this idea with the help of stack to avoid recursion and below is the code</p>
<figure class="highlight python"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> math <span class="keyword">import</span> log10</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span><span class="params">(object)</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">countDigitOne</span><span class="params">(self, n)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> n <= <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line"> ord_0, res, t = ord(<span class="string">'0'</span>), <span class="number">0</span>, [</span><br><span class="line"> (i+<span class="number">1</span>)*<span class="number">10</span>**i <span class="keyword">for</span> i <span class="keyword">in</span> xrange(<span class="number">0</span>, int(log10(n)))]</span><br><span class="line"> <span class="keyword">while</span> n:</span><br><span class="line"> te, tem = list(str(n)), int(log10(n))<span class="number">-1</span></span><br><span class="line"> tmp, te[<span class="number">0</span>] = ord(te[<span class="number">0</span>])-ord_0, <span class="string">'0'</span></span><br><span class="line"> n = int(<span class="string">''</span>.join(te))</span><br><span class="line"> <span class="keyword">if</span> tem >= <span class="number">0</span>:</span><br><span class="line"> res += tmp*t[tem]</span><br><span class="line"> res += n+<span class="number">1</span> <span class="keyword">if</span> tmp == <span class="number">1</span> <span class="keyword">else</span> <span class="number">10</span>**(tem+<span class="number">1</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> res += <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> res</span><br></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2020/01/17/Reading-Notes-How-Google-Works/" itemprop="url">
Reading Notes: How Google Works
</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 itemprop="dateCreated" datetime="2020-01-17T20:30:00+08:00" content="2020-01-17">
2020-01-17
</time>
</span>
<span class="post-comments-count">
|
<a href="/2020/01/17/Reading-Notes-How-Google-Works/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2020/01/17/Reading-Notes-How-Google-Works/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Definitions"><a href="#Definitions" class="headerlink" title="Definitions"></a>Definitions</h2><ul>
<li><p>Technical insights</p>
<p>A technical insight is a new way of applying technology or design that either drives down the cost or increases the functions and usability of the product by a significant factor.</p>
</li>
<li><p>What is a good product</p>
<p>The best products are still the ones that are based on technical insights, those unique ideas that apply one or more technologies in a new way to solve big problems.</p>
</li>
<li><p>A Successful Product Is The One Fails To Fail</p>
</li>
<li><p>Elevator Pitch</p>
<p>Elevator pitch” is venture capital–speak for “you have thirty seconds to impress me with your business idea.”</p>
<p>Elevator pitch should explain what you are working on, the technical insight that’s driving it, how you are measuring your success (particularly customer benefit), and perhaps how it fits into the big picture.</p>
</li>
<li><p>The 70/20/10 Rule</p>
<p>70 percent of resources dedicated to the core business, 20 percent on emerging, and 10 percent on new.</p>
</li>
<li><p>Jobs attractive to the Smart Creatives</p>
<p>Flying an F-16 at Mach 2 over a boulder-strewn landscape, two meters off the ground. Plus, if you crash it’s just like a video game at the arcade, and we have lots of quarters.”</p>
</li>
<li><p>Hippo(the Highest-Paid Person’s Opinion)</p>
</li>
</ul>
<h2 id="Suggestions-From-Google"><a href="#Suggestions-From-Google" class="headerlink" title="Suggestions From Google"></a>Suggestions From Google</h2><ul>
<li><p>Process Is A Friend To Small Companies But An Enemy To Big Ones</p>
<p>Process is a great thing. It is the art and science of taking something that needs to be done and reducing it to a documented set of steps, often supported by information tools and systems. With process comes scale; you absolutely need process to grow a company profitably.</p>
<p>At some point, though, process starts to take over. It becomes so entrenched that it can trump common sense and cause executives to, as our head of business operations, Kristen Gil, says, “lose muscle memory.” People stop thinking and instead just depend on the process to make decisions for them.</p>
<p>The antidote to these issues is to break your own processes. This usually requires “air cover” from a senior leader in the company, someone who says it’s OK not to follow the rules and has the power and authority to fend off the antibodies who will attack the rule-breaker.</p>
</li>
<li><p>BU(Business Unit) is not a good thing</p>
<p>We believe in staying functionally organized—with separate departments such as engineering, products, finance, and sales reporting directly to the CEO—as long as possible, because organizing around business divisions or product lines can lead to the formation of silos, which usually stifle the free flow of information and people. Having separate P&Ls seems like a good way to measure performance, but it can have the unfortunate side effect of skewing behavior:<br>The leaders of a business unit are motivated to prioritize their unit’s P&L over the company’s.</p>
</li>
<li><p>Googles’ overall strategy</p>
<p>Bet on technical insights that help solve a big problem in a novel way, optimize for scale, not for revenue, and let great products grow the market for everyone.</p>
</li>
<li><p>Hire no incorrect people rather than hiring more correct people</p>
<p>This is why we would rather our hiring process generate more false negatives (people we should have hired but didn’t) than false positives (we shouldn’t have hired, but did).</p>
</li>
<li><p>Checklist for Meeting Organizers</p>
<ul>
<li>Every Meeting need a owner</li>
<li>Every meeting should have a clear purpose</li>
<li>Meeting need a clear time-box</li>
<li>Owners’ responsibilities before the metting are to schedue the time and let all attendances know in advance. After the meeting the owner is expected to sumaarize the decisions or conclusions reached at the metting.</li>
</ul>
</li>
<li>How to End an Argument: <ul>
<li>Say that You are both right</li>
<li>Then, after reassuring the argument’s losers and articulating what needs to be done, the decision-maker must ensure that every loser does one of two things: disagree but commit, or escalate publicly.</li>
</ul>
</li>
</ul>
<h2 id="Mottos"><a href="#Mottos" class="headerlink" title="Mottos"></a>Mottos</h2><ul>
<li><p>Never stop learning</p>
<p>Henry Ford said that “anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.”</p>
</li>
<li><p>Incrementalism Leads To Irrelevance</p>
<p>It’s also true that many companies get comfortable doing what they have always done, with a few incremental changes. This kind of incrementalism leads to irrelevance over time, especially in technology, because change tends to be revolutionary not evolutionary.</p>
</li>
<li><p>Googlers also work on the weekend like when doing 20 percent project</p>
</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/01/20/Reading-Notes-The-Checklist-Manifesto/" itemprop="url">
Reading Notes: The Checklist Manifesto
</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 itemprop="dateCreated" datetime="2019-01-20T16:30:00+08:00" content="2019-01-20">
2019-01-20
</time>
</span>
<span class="post-comments-count">
|
<a href="/2019/01/20/Reading-Notes-The-Checklist-Manifesto/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2019/01/20/Reading-Notes-The-Checklist-Manifesto/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p><a href="https://www.amazon.com/Checklist-Manifesto-How-Things-Right/dp/0312430000" target="_blank" rel="noopener">The Checklist Manifesto</a> is a book from <a href="https://en.wikipedia.org/wiki/Atul_Gawande" target="_blank" rel="noopener">Atul Gawande</a>. I learned the following three points from this book.</p>
<h4 id="All-Tasks-Can-Be-Divided-into-3-Groups"><a href="#All-Tasks-Can-Be-Divided-into-3-Groups" class="headerlink" title="All Tasks Can Be Divided into 3 Groups"></a>All Tasks Can Be Divided into 3 Groups</h4><ul>
<li><p>simple ones:</p>
<p> Simple problems are ones like baking a cake from a mix. There is a recipe. Sometimes there are a few basic techniques to learn. But once these are mastered, following the recipe brings a high likelihood of success.</p>
</li>
<li><p>complicated ones:</p>
<p> Ones like launcing a rocket. They can sometimes be broken down into a series of simple problems. Once you learn how to send a rocket to the moon, you can repeat the process with other rockets and perfect it. </p>
</li>
<li><p>complex problems:</p>
<p>Like raising a child. Although raising one child may provide experience, it does not guarantee success with the next child. Expertise is valuable but most certainly not sufficient. Indeed, the next child may require an entirely different approach from the previous one.</p>
</li>
</ul>
<h4 id="How-to-Categorize-Checklists"><a href="#How-to-Categorize-Checklists" class="headerlink" title="How to Categorize Checklists"></a>How to Categorize Checklists</h4><ul>
<li>With a DO-CONFIRM checklist: we perform our jobs from memory and experience, often separately. But then we stop. We pause to run the checklist and confirm that everything that was supposed to be done was done. </li>
<li>With a READ-DO checklist, on the other hand, we carry out the tasks as we check them off it is more like a recipe.</li>
</ul>
<h4 id="What-Makes-a-Good-Checklists"><a href="#What-Makes-a-Good-Checklists" class="headerlink" title="What Makes a Good Checklists"></a>What Makes a Good Checklists</h4><ul>
<li>Five to nine items at most for a single check list.</li>
<li>The wording should be simple and exact</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/08/12/A-Simple-But-Effective-Spaced-Repitition-Algorithm-MS/" itemprop="url">
A Simple but Effective Learning Algorithm: MS Syntax
</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 itemprop="dateCreated" datetime="2018-08-12T08:56:00+08:00" content="2018-08-12">
2018-08-12
</time>
</span>
<span class="post-comments-count">
|
<a href="/2018/08/12/A-Simple-But-Effective-Spaced-Repitition-Algorithm-MS/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2018/08/12/A-Simple-But-Effective-Spaced-Repitition-Algorithm-MS/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p><img src="/images/2018/08/learnFast.jpg" alt="Sample Image Added via Markdown"></p>
<p>Personally I have been using spaced repetition algorithm in my study for many years. There are many different kinds of this algorithm and the one I have used is invented by BlueRaja called <a href="http://www.blueraja.com/blog/477/a-better-spaced-repetition-learning-algorithm-sm2" target="_blank" rel="noopener">SM2+</a>. Based on this algorithm I have written a lib called <a href="https://github.com/lo-tp/sm2-plus" target="_blank" rel="noopener">sm2-plus</a> and I liked it a lot. However, SM2+ has its own problem.</p>
<p>In this post I will show you what problems SM2+ algorithm has and provide a new algorithm named Memory Scheduler that I find quite helpful in my learning.</p>
<h3 id="Problem-of-SM2"><a href="#Problem-of-SM2" class="headerlink" title="Problem of SM2+"></a>Problem of SM2+</h3><ul>
<li>It’s a little bit complex.</li>
<li>It’s not compatible with the forgetting curve theory. </li>
</ul>
<h3 id="Memory-Scheduler-Algorithm"><a href="#Memory-Scheduler-Algorithm" class="headerlink" title="Memory Scheduler Algorithm"></a>Memory Scheduler Algorithm</h3><p>To learn with this algorithm, two arguments have to be fed to it:</p>
<ul>
<li>intervals([int]): intervals between each study session.</li>
<li>scroreToProgressChange([int]): how to update the progress based on the score the user gives when reviewing items</li>
</ul>
<p>For one item we want to learn, store two data:</p>
<ul>
<li>dueDate(int): the next day scheduled to review this item.</li>
<li>progress(int): How many times continuously the user has correctly answered this item.</li>
</ul>
<p>To reivew an item, the user is asked to chose a score based on how confident he is with the item(the larger, the more confident).</p>
<p>The answer is deemed as correct only when the score is equal to the length of scroreToProgressChange, and in this circumstance the nextDute is intervals[progress] days after today.</p>
<p>Otherwise, the answer is deemed as incorrect and the next review is scheduled at tomorrow.</p>
<p>In both cases, progress should be updated in this way: <code>progress+=scroreToProgressChange[score]</code>.</p>
<p>Following is a javascript implementation of this algorithm and I have also published it to npm as <a href="https://www.npmjs.com/package/memory-scheduler" target="_blank" rel="noopener">memory-scheduler</a>. If you are not satisfied with the this implementation, send me a PR at this <a href="https://github.com/lo-tp/memory-scheduler" target="_blank" rel="noopener">repo</a>.</p>
<figure class="highlight js"><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><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">MS</span> </span>{</span><br><span class="line"> <span class="keyword">constructor</span>(</span><br><span class="line"> intervals = [1, 2, 3, 8, 17],</span><br><span class="line"> scroreToProgressChange = [-3, -1, 1]</span><br><span class="line"> ) {</span><br><span class="line"> <span class="keyword">this</span>.intervals = intervals;</span><br><span class="line"> <span class="keyword">this</span>.scroreToProgressChange = scroreToProgressChange;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> get maxProgress() {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.intervals.length;</span><br><span class="line"> }</span><br><span class="line"> get correctScore() {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.scroreToProgressChange.length - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> calculate(s, { progress }, now) {</span><br><span class="line"> <span class="keyword">const</span> correct = s === <span class="keyword">this</span>.scroreToProgressChange.length - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">const</span> newProgress = progress + <span class="keyword">this</span>.scroreToProgressChange[s];</span><br><span class="line"> <span class="keyword">let</span> dueDate = now + <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> (correct && progress < <span class="keyword">this</span>.maxProgress) {</span><br><span class="line"> dueDate = now + <span class="keyword">this</span>.intervals[progress];</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> dueDate,</span><br><span class="line"> progress: newProgress < <span class="number">0</span> ? <span class="number">0</span> : newProgress</span><br><span class="line"> };</span><br><span class="line"> }</span><br><span class="line"> getInitialRecord(now) {</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> progress: <span class="number">0</span>,</span><br><span class="line"> dueDate: now</span><br><span class="line"> };</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>That’s all I want to say about Memory Scheduler algorithm. As you can see, it’s pretty simple to use. I believe with this algorithm, we can learn better in a faster pace.</p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/01/05/My-First-Try-at-TDD-How-I-Test-Asynchronous-Code-in-Mocha-With-async-await-Syntax/" itemprop="url">
My First Try at TDD: How I Test Asynchronous Code in Mocha With async/await Syntax
</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 itemprop="dateCreated" datetime="2017-01-05T13:56:47+08:00" content="2017-01-05">
2017-01-05
</time>
</span>
<span class="post-comments-count">
|
<a href="/2017/01/05/My-First-Try-at-TDD-How-I-Test-Asynchronous-Code-in-Mocha-With-async-await-Syntax/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/01/05/My-First-Try-at-TDD-How-I-Test-Asynchronous-Code-in-Mocha-With-async-await-Syntax/" itemprop="commentsCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Every body knows that writing unit test is very important during programming.<br>However, it is not an easy job, especially when it comes to <code>asynchronous code</code>.</p>
<p>Initially, callback has to be used to test <code>asynchronous code</code>:<br><figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line">describe(<span class="string">'User Profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> it(<span class="string">'can get user profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">done</span>)</span>{</span><br><span class="line"> getProfile(<span class="string">'bulkan'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">err, result</span>)</span>{</span><br><span class="line"> <span class="keyword">if</span>(err) {</span><br><span class="line"> done(err)</span><br><span class="line"> }</span><br><span class="line"> assert.equal(result.userName, <span class="string">'bulkan'</span>);</span><br><span class="line"> done();</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<p>This kind of test could easily turned out to be a [callback hell][ch] when the logic become complex.<br>Thank god, with the advent of <code>promise</code>, testing <code>asynchronous code</code> is not that much depressing now.</p>
<figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line">describe(<span class="string">'User Profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> it(<span class="string">'can get user profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">done</span>)</span>{</span><br><span class="line"> getProfile(<span class="string">'bulkan'</span>)</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> assert.equal(result.userName, <span class="string">'bulkan'</span>);</span><br><span class="line"> done();</span><br><span class="line"> })</span><br><span class="line"> .catch(<span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>{</span><br><span class="line"> done(err);</span><br><span class="line"> });</span><br><span class="line"> });</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<p>Actually manually calling <code>done</code> is not necessary any more in newer version of mocha, as mocha takes care of promise itself pretty well.<br>So the above code can be written as:<br><figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line">describe(<span class="string">'User Profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> it(<span class="string">'can get user profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> getProfile(<span class="string">'bulkan'</span>)</span><br><span class="line"> .then(<span class="function"><span class="keyword">function</span>(<span class="params">result</span>)</span>{</span><br><span class="line"> assert.equal(result.userName, <span class="string">'bulkan'</span>);</span><br><span class="line"> })</span><br><span class="line"> });</span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<p>Now this looks good.<br>However, we won’t be satisfied with this, will we?<br>Take a look at how this code could be further improved with <code>async/await</code>.<br><figure class="highlight javascript"><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></pre></td><td class="code"><pre><span class="line">describe(<span class="string">'User Profile'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> it(<span class="string">'can get user profile'</span>, <span class="keyword">async</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">const</span> result = <span class="keyword">await</span> getProfile(<span class="string">'bulkan'</span>);</span><br><span class="line"> assert.equal(result.userName, <span class="string">'bulkan'</span>);</span><br><span class="line"> });</span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<p>In the above snippet, we are taking advantage of of one lovely feature of Async functions:</p>
<blockquote>
<p>Async functions always return a promise, no matter whether or not await is invoked.<br>The promise, returned by async function, would resolve with whatever the async function returns, or rejects with whatever the async function throws.<br>With the <code>async/await</code>, testing asynchronous code is nearly identical to testing synchronous code.<br>Cheer!!!</p>
</blockquote>
<hr>
<h5 id="Caveat"><a href="#Caveat" class="headerlink" title="Caveat"></a>Caveat</h5><p>Every thing comes at a price.<br>Although it is very trendy now, we still need a transpiler like <code>bable</code> to use <code>async/await</code>.<br>What a pity.<br>[ch]:<a href="http://callbackhell.com/" target="_blank" rel="noopener">http://callbackhell.com/</a></p>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/12/13/LeetCode-417-AC-Pacific-Atlantic-Water-Flow/" itemprop="url">
[LeetCode 417, AC] Pacific Atlantic Water Flow
</a>
</h1>
<div class="post-meta">
<span class="post-time">