Skip to content

Commit 2dbcd1c

Browse files
authored
Merge pull request #20 from tiationg-kho/update
update
2 parents 953637c + b42db5f commit 2dbcd1c

15 files changed

+838
-521
lines changed

README.md

Lines changed: 519 additions & 519 deletions
Large diffs are not rendered by default.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
res = 0
4+
left, right = 0, len(height) - 1
5+
while left < right:
6+
if height[left] < height[right]:
7+
res = max(res, height[left] * (right - left))
8+
left += 1
9+
else:
10+
res = max(res, height[right] * (right - left))
11+
right -= 1
12+
return res
13+
14+
# time O(n)
15+
# space O(1)
16+
# using array and two pointers opposite direction and shrink type and greedy
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
left, right = 0, len(s) - 1
4+
while left < right:
5+
if not s[left].isalnum():
6+
left += 1
7+
elif not s[right].isalnum():
8+
right -= 1
9+
elif s[left].lower() == s[right].lower():
10+
left += 1
11+
right -= 1
12+
else:
13+
return False
14+
return True
15+
16+
# time O(n), due to traverse
17+
# space O(1)
18+
# using array and two pointers opposite direction and shrink type
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
4+
nums.sort()
5+
6+
res = []
7+
for i in range(len(nums) - 2):
8+
if i - 1 >= 0 and nums[i - 1] == nums[i]:
9+
continue
10+
left, right = i + 1, len(nums) - 1
11+
target = - nums[i]
12+
while left < right:
13+
cur = nums[left] + nums[right]
14+
if cur == target:
15+
res.append([nums[i], nums[left], nums[right]])
16+
left += 1
17+
while left < right and nums[left - 1] == nums[left]:
18+
left += 1
19+
elif cur > target:
20+
right -= 1
21+
while left < right and nums[right + 1] == nums[right]:
22+
right -= 1
23+
else:
24+
left += 1
25+
while left < right and nums[left - 1] == nums[left]:
26+
left += 1
27+
return res
28+
29+
# time O(n**2)
30+
# space O(1), or due to built in sort's cost
31+
# using array and two pointers opposite direction and shrink type and sort
32+
'''
33+
1. be aware of handling duplicate
34+
'''
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
4+
chars = list(' ' + s + ' ')
5+
6+
def rev(left, right):
7+
while left < right:
8+
chars[left], chars[right] = chars[right], chars[left]
9+
left += 1
10+
right -= 1
11+
12+
rev(0, len(chars) - 1)
13+
14+
word = False
15+
left = 0
16+
for right in range(len(chars)):
17+
if chars[right] == ' ' and word:
18+
rev(left, right)
19+
word = False
20+
elif chars[right] != ' ' and not word:
21+
left = right
22+
word = True
23+
24+
res = ''
25+
for c in chars:
26+
if c != ' ':
27+
res += c
28+
elif res and res[- 1] != ' ':
29+
res += ' '
30+
return res.rstrip()
31+
32+
# time O(n)
33+
# space O(n)
34+
# using array and two pointers opposite direction and shrink type
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def threeSumClosest(self, nums: List[int], target: int) -> int:
3+
4+
nums.sort()
5+
res_diff = float('inf')
6+
res = None
7+
8+
for i in range(len(nums) - 2):
9+
left, right = i + 1, len(nums) - 1
10+
11+
small_three = nums[i] + nums[left] + nums[left + 1]
12+
if small_three > target:
13+
if abs(small_three - target) < res_diff:
14+
res_diff = abs(small_three - target)
15+
res = small_three
16+
break
17+
large_three = nums[i] + nums[right - 1] + nums[right]
18+
if large_three < target:
19+
if abs(large_three - target) < res_diff:
20+
res_diff = abs(large_three - target)
21+
res = large_three
22+
continue
23+
24+
while left < right:
25+
cur = nums[i] + nums[left] + nums[right]
26+
if abs(cur - target) < res_diff:
27+
res_diff = abs(cur - target)
28+
res = cur
29+
if cur == target:
30+
return target
31+
elif cur > target:
32+
right -= 1
33+
else:
34+
left += 1
35+
return res
36+
37+
# time O(n**2)
38+
# space O(1), not count built in sort's cost
39+
# using array and two pointers opposite direction and shrink type and sort and prune
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
3+
left, right = 0, len(numbers) - 1
4+
while left < right:
5+
if numbers[left] + numbers[right] == target:
6+
break
7+
elif numbers[left] + numbers[right] > target:
8+
right -= 1
9+
else:
10+
left += 1
11+
return [left + 1, right + 1]
12+
13+
# time O(n), due to traverse
14+
# space O(1)
15+
# using array and two pointers opposite direction and shrink type
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
3+
nums.sort()
4+
res = []
5+
for i in range(len(nums) - 3):
6+
if i - 1 >= 0 and nums[i - 1] == nums[i]:
7+
continue
8+
for j in range(i + 1, len(nums) - 2):
9+
if j - 1 >= i + 1 and nums[j - 1] == nums[j]:
10+
continue
11+
left, right = j + 1, len(nums) - 1
12+
while left < right:
13+
cur = nums[i] + nums[j] + nums[left] + nums[right]
14+
if cur == target:
15+
res.append([nums[i], nums[j], nums[left], nums[right]])
16+
left += 1
17+
while left < right and nums[left - 1] == nums[left]:
18+
left += 1
19+
elif cur > target:
20+
right -= 1
21+
while left < right and nums[right + 1] == nums[right]:
22+
right -= 1
23+
else:
24+
left += 1
25+
while left < right and nums[left - 1] == nums[left]:
26+
left += 1
27+
return res
28+
29+
# time O(n**3)
30+
# space O(1), not count output list
31+
# using array and two pointers opposite direction and shrink type and sort
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def reverseString(self, s: List[str]) -> None:
3+
"""
4+
Do not return anything, modify s in-place instead.
5+
"""
6+
left, right = 0, len(s) - 1
7+
while left < right:
8+
s[left], s[right] = s[right], s[left]
9+
left += 1
10+
right -= 1
11+
12+
# time O(n)
13+
# space O(1)
14+
# using array and two pointers opposite direction and shrink type
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def trap(self, height: List[int]) -> int:
3+
res = 0
4+
left, right = 0, len(height) - 1
5+
left_wall, right_wall = height[left], height[right]
6+
while left < right:
7+
left_wall = max(left_wall, height[left])
8+
right_wall = max(right_wall, height[right])
9+
if left_wall < right_wall:
10+
res += left_wall - height[left]
11+
left += 1
12+
else:
13+
res += right_wall - height[right]
14+
right -= 1
15+
return res
16+
17+
# tiem O(n), due to traverse
18+
# space O(1)
19+
# using array and two pointers opposite direction and shrink type and greedy
20+
'''
21+
1. count the water with lower wall's side first
22+
'''
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> str:
3+
4+
res_len = 0
5+
res = - 1
6+
7+
for i in range(len(s)):
8+
9+
left, right = i, i
10+
while left >= 0 and right < len(s):
11+
if s[left] == s[right]:
12+
if right - left + 1 > res_len:
13+
res_len = right - left + 1
14+
res = left
15+
left -= 1
16+
right += 1
17+
else:
18+
break
19+
20+
left, right = i, i + 1
21+
while left >= 0 and right < len(s):
22+
if s[left] == s[right]:
23+
if right - left + 1 > res_len:
24+
res_len = right - left + 1
25+
res = left
26+
left -= 1
27+
right += 1
28+
else:
29+
break
30+
31+
return s[res: res + res_len]
32+
33+
# time O(n**2), traversal costs O(n), and each time need to using two pointers to expand
34+
# space O(1)
35+
# using array and two pointers opposite direction and expand type
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def validPalindrome(self, s: str) -> bool:
3+
4+
def helper(left, right, count):
5+
while left < right:
6+
if s[left] != s[right]:
7+
if count == 0:
8+
return helper(left + 1, right, 1) or helper(left, right - 1, 1)
9+
return False
10+
left += 1
11+
right -= 1
12+
return True
13+
14+
return helper(0, len(s) - 1, 0)
15+
16+
# time O(n), due to traverse
17+
# space O(1)
18+
# using array and two pointers opposite direction and shrink type
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def bagOfTokensScore(self, tokens: List[int], power: int) -> int:
3+
4+
tokens.sort()
5+
6+
res = 0
7+
score = 0
8+
left, right = 0, len(tokens) - 1
9+
while left <= right:
10+
if power >= tokens[left]:
11+
power -= tokens[left]
12+
score += 1
13+
left += 1
14+
res = max(res, score)
15+
elif score >= 1:
16+
power += tokens[right]
17+
score -= 1
18+
right -= 1
19+
else:
20+
break
21+
return res
22+
23+
# time O(nlogn)
24+
# space O(1), or due to built in sort
25+
# using array and two pointers opposite direction and shrink type and sort and greedy
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def sortedSquares(self, nums: List[int]) -> List[int]:
3+
res = []
4+
left, right = 0, len(nums) - 1
5+
while left <= right:
6+
if nums[left] ** 2 < nums[right] ** 2:
7+
res.append(nums[right] ** 2)
8+
right -= 1
9+
else:
10+
res.append(nums[left] ** 2)
11+
left += 1
12+
return res[:: - 1]
13+
14+
# time O(n)
15+
# space O(n)
16+
# using array and two pointers opposite direction and shrink type

[H]array/array.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ two pointers opposite direction
353353
- could use the attribute of sorted order
354354
- if not sorted
355355
- we should greedily think move which side’s ptr will be the current best choice
356-
- or think which side’s ptr’s result is already the final result
356+
- or consider which side’s ptr’s result is already the final result
357357
- record it if need, then move this side’s ptr
358358
- or the both side shrink together
359359
- sometimes can use when we want to reverse array
@@ -429,7 +429,7 @@ two pointers opposite direction
429429
quick_sort(0, len(nums) - 1)
430430
return nums
431431

432-
# time O(n**2), when the list is almost sorted, and the average time is O(nlogn)
432+
# time O(n**2), worst-case happens due to bad pivot choice or nearly sorted array, and the average time is O(nlogn)
433433
# space O(n), due to recursion layers, O(logn) in average
434434
# using quick sort
435435
# unstable

0 commit comments

Comments
 (0)