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+
'''

0 commit comments

Comments
 (0)