Skip to content

Commit 7775507

Browse files
author
aaron.liu
committed
updates
1 parent 6acebfa commit 7775507

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

Diff for: BinarySearch/LC658FindKClosestElements.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
class Solution:
4+
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
5+
lo, hi = 0, len(arr) - k
6+
while lo < hi:
7+
mid = (lo + hi) // 2
8+
if x - arr[mid] > arr[mid + k] - x:
9+
lo = mid + 1
10+
else:
11+
hi = mid
12+
return arr[lo:lo + k]

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# AlgoInPython
2-
Python Algo
2+
Algo in Python

Diff for: TwoPointers/LC11ContainerWithMostWater.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
'''
3+
复杂度
4+
时间 O(n) 空间(1)
5+
思路:双指针->相遇问题
6+
从数组两端走起,每次迭代时判断左pointer和右pointer指向的数字哪个大,如果左pointer小,
7+
意味着向左移动右pointer不可能使结果变得更好,因为瓶颈在左pointer,移动右pointer只会变小,所以这时候我们选择左pointer右移。
8+
反之,则选择右pointer左移。在这个过程中一直维护最大的那个容积
9+
'''
10+
from typing import List
11+
12+
class Solution:
13+
def maxArea(self, height: List[int]) -> int:
14+
if not height or len(height) <= 1:
15+
return 0
16+
17+
left, right = 0, len(height) - 1
18+
ret = 0
19+
while left < right:
20+
ret = max(ret, min(height[left], height[right]) * (right - left))
21+
if height[left] <= height[right]:
22+
left += 1
23+
else:
24+
right -= 1
25+
return ret

Diff for: TwoPointers/LC42TrappingRainWater.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
复杂度
3+
时间 O(n) 空间(1)
4+
思路:双指针->相遇问题
5+
从数组两端走起,每次迭代时判断左pointer和右pointer指向的数字哪个大,
6+
如果左pointer小,意味着向左移动右pointer不可能使结果变得更好,因为瓶颈在左pointer,
7+
移动右pointer只会变小,所以这时候我们选择左pointer右移。
8+
反之,则选择右pointer左移。在这个过程中一直维护最大的那个容积
9+
'''
10+
from typing import List
11+
12+
class Solution:
13+
def trap(self, height: List[int]) -> int:
14+
if not height or len(height) < 3:
15+
return 0
16+
17+
vol = 0
18+
left, right = 0, len(height) - 1
19+
while left < right:
20+
if height[left] <= height[right]:
21+
ptr = left + 1
22+
while ptr < right and height[ptr] < height[left]:
23+
vol += height[left] - height[ptr]
24+
ptr += 1
25+
left = ptr
26+
else:
27+
ptr = right - 1
28+
while ptr > left and height[ptr] < height[right]:
29+
vol += height[right] - height[ptr]
30+
ptr -= 1
31+
right = ptr
32+
return vol

Diff for: local_test.py

Whitespace-only changes.

0 commit comments

Comments
 (0)