Skip to content

Commit 6dcab71

Browse files
author
Yan Cao
committed
Updated LRU and added Recover rotated sorted array
1 parent 06989a6 commit 6dcab71

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

Interviews/Recover_Rotated_Sorted_Array.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,21 @@
66
77
NC Class 2, slides 32
88
Solution three way reverse
9-
"""
9+
"""
10+
11+
def recover_rotated_sorted_array(A):
12+
N = len(A)
13+
rotate_start = -1
14+
for i in range(1, N):
15+
if A[i-1] > A[i]:
16+
rotate_start = i
17+
break
18+
first_part = A[:rotate_start]
19+
second_part = A[rotate_start:]
20+
total = first_part[::-1] + second_part[::-1]
21+
return total[::-1]
22+
23+
A = [4, 5, 1, 2, 3]
24+
print recover_rotated_sorted_array(A)
25+
26+
# I really have no idea why I need to reverse every time, but basically this works

Leetcode/LRU_Cache.py

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def set(self, key, value):
3030
elif key in self.cache:
3131
del self.cache[key]
3232
self.cache[key] = value
33+
34+
# Keep a note here, always delete the item. Cause update the key won't update the order
3335
"""
3436
# @param capacity, an integer
3537
def __init__(self, capacity):

coding_index.md

+6-28
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,12 @@ __去想关于数据结构的题目的时候, 只需要考虑数据结构里处
658658
* [ ] Rotate Image
659659
* [ ] Gray Code
660660
* [ ] Candy
661-
* [ ] LRU
661+
* [x] LRU
662+
* [x] Surrounded Regions
662663
* [ ] Gas Station
663664
* [ ] Permutation Sequence
664665
* [ ] Next Permutation
666+
* [ ] Maximum Subwindow
665667

666668

667669
* 还有前面tree的一些
@@ -679,33 +681,9 @@ __去想关于数据结构的题目的时候, 只需要考虑数据结构里处
679681

680682

681683
##New
682-
683-
* [ ] min(|a-c| + |b-c| + |c-1|)
684-
* [ ] max product of subarray
685-
O(n1+n2+n3) n1,n2,n3 是三个数组的长度
686-
687-
|a-b|+|b-c|+|c-a| 其实就是求 2*(最大数-最小数) (eg. 如果a>b>c, |a-b|+|b-c|+|
688-
c-a| = 2(a-c) )
689-
690-
三个指针i, j, k
691-
从头开始扫, 总是移动最小的那个指针 更新当前最小的 2*(最大数-最小数) 即可
692-
693-
694-
证明正确性:
695-
在扫的过程中, 对于三个数组中的任意一个数, 分三种情况讨论 (下面假设取出的三个
696-
数 a>b>c)
697-
698-
1. 如果他作为b, 那么永远不影响最后结果
699-
700-
2. 如果他作为a, 在他作为a的时候, 由于一直在移动另两个指针并接近a, 肯定能扫到
701-
那个对于a而言最大的c
702-
703-
3. 如果他作为c, 假设 a或者b不是自己数组中比c大的最小值, 那么肯定有在c数组还
704-
没扫到c的时候有移动a,b数组指针的情况, 但这和假设矛盾
705-
706-
707-
证明写的有点乱 求大神更好更清楚的证明
708-
684+
* [x] [Absolut Minimum](./Interviews/Absolute_Minimum.py)
685+
* [ ] Max Product of Subarray
709686

710687
##Some Note
711688
1. 一定要看清题,比如这次就被问了find all palindrome,但是理解成palindrome partitioning了,所以错了
689+
2. 再仔细确认下怎么算recursion的big O

0 commit comments

Comments
 (0)