Skip to content

Commit 6af6d8b

Browse files
committed
update 11.3.2024
1 parent fb4d45e commit 6af6d8b

12 files changed

+414
-27
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
# BY MY SELF
3+
def twoSum(self, numbers, target):
4+
"""
5+
:type numbers: List[int]
6+
:type target: int
7+
:rtype: List[int]
8+
"""
9+
10+
n = len(numbers)
11+
first = 0
12+
second = n - 1
13+
while True :
14+
current = numbers[first] + numbers[second]
15+
16+
if current == target :
17+
return [first + 1, second + 1]
18+
19+
if current < target :
20+
first += 1
21+
22+
else : # current > target
23+
second -= 1

209. Minimum Size Subarray Sum.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Solution(object):
2+
def minSubArrayLen(self, target, nums):
3+
"""
4+
:type target: int
5+
:type nums: List[int]
6+
:rtype: int
7+
"""
8+
n = len(nums)
9+
min_len = n
10+
first = 0
11+
second = 0
12+
current_sum = nums[0]
13+
if sum(nums) < target :
14+
return 0
15+
while first <= n-1 :
16+
# Now current_sum is the minimun possible sum start from first pointer
17+
18+
# loop through second
19+
while True :
20+
if current_sum >= target :
21+
# satisfy the problem
22+
current_len = second - first + 1
23+
if current_sum < min_len :
24+
min_len = current_len
25+
26+
# now break the while loop
27+
break
28+
29+
# ELSE current_sum < target, need more element in subarray
30+
if second <= n-2 :
31+
second += 1
32+
current_sum += nums[second]
33+
current_len = second - first + 1
34+
continue
35+
36+
# ELSE :
37+
break
38+
39+
# Check last time
40+
if current_sum >= target and current_len < min_len :
41+
min_len = second - first + 1
42+
43+
# move to the next index
44+
current_sum -= nums[first]
45+
first += 1
46+
47+
return min_len
48+
49+
50+
51+
52+
53+
54+
target = 11
55+
nums = [1,2,3,4,5]
56+
obj = Solution()
57+
print(obj.minSubArrayLen(target, nums))
58+
59+

27. Remove Element.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
def minSubArrayLen(self, target, nums):
3+
"""
4+
:type target: int
5+
:type nums: List[int]
6+
:rtype: int
7+
"""
8+
first = 0
9+
second = len(nums) - 1
10+
current_sum = sum(nums)
11+
if current_sum < target :
12+
return 0
13+
while True :
14+
if current_sum - nums[first] - nums[second] >= target :
15+
current_sum = current_sum - nums[first] - nums[second]
16+
first += 1
17+
second -= 1
18+
19+
elif current_sum - nums[first] >= target :
20+
current_sum = current_sum - nums[first]
21+
first += 1
22+
23+
elif current_sum - nums[second] >= target :
24+
current_sum = current_sum - nums[second]
25+
second -= 1
26+
27+
else :
28+
return second - first + 1
29+
30+
31+
32+
33+
nums = [2,3,1,2,4,3]
34+
target = 7
35+
36+
obj = Solution()
37+
38+
print(obj.minSubArrayLen(target, nums))

498. Diagonal Traverse.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution(object):
2+
def findDiagonalOrder(self, mat):
3+
"""
4+
:type mat: List[List[int]]
5+
:rtype: List[int]
6+
"""
7+
m = len(mat)
8+
n = len(mat[0])
9+
max_diagonal = m - 1 + n - 1
10+
travel_order = []
11+
12+
13+
14+
for line in range(max_diagonal + 1) :
15+
16+
# ODD LINE, then column decrease
17+
if line % 2 == 1 :
18+
# PROVE:
19+
# 0 <= row <= m - 1
20+
# 0 >= -row >= - (m - 1)
21+
# line >= line - row = column >= line - (m-1)
22+
# Then we iterate column from min(line, n - 1) to max(0, line - (m - 1) )
23+
for column in range(min(line, n - 1), max(0, line - (m-1)) - 1, -1) :
24+
row = line - column
25+
ele = mat[row][column]
26+
27+
travel_order.append(ele)
28+
29+
else :
30+
# EVEN LINE, then row decrease
31+
for row in range(min(line, m - 1), max(0, line - (n-1)) - 1, -1) :
32+
column = line - row
33+
ele = mat[row][column]
34+
travel_order.append(ele)
35+
36+
return travel_order
37+

54. Spiral Matrix.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution(object):
2+
def spiralOrder(self, matrix):
3+
"""
4+
:type matrix: List[List[int]]
5+
:rtype: List[int]
6+
"""
7+
# IDEA: When travel 1 round with 4 edge of matrix, the remaining way is the same as the full way of the (m-2) * (n-2) matrix
8+
9+
# Then we have 4 base case: 1 row, n-m + 1 column; 2 row, n-m+2 column; m-n+1 row, 1 column; m-n+2 row, 2 column
10+
m = len(matrix)
11+
n = len(matrix[0])
12+
13+
if m == 1 :
14+
return matrix[0]
15+
elif m == 2 :
16+
return matrix[0] + list(reversed(matrix[1]))
17+
18+
elif n == 1 : # 1 column
19+
element = [i[0] for i in matrix]
20+
return element
21+
22+
elif n == 2 : # and m > 2
23+
first_row = [i[0] for i in matrix]
24+
second_row = [i[1] for i in matrix]
25+
26+
return [first_row[0], second_row[0]] + second_row[1: -1] + [second_row[-1], first_row[-1]] + list(reversed(first_row[1: -1]) )
27+
28+
else :
29+
last_row = [i[-1] for i in matrix]
30+
first_row = [i[0] for i in matrix]
31+
32+
outer_element = matrix[0] + last_row[1: -1] + list(reversed(matrix[-1]) )+ list(reversed(first_row[1: -1]) )
33+
34+
del matrix[0]
35+
del matrix[-1]
36+
37+
for r in matrix :
38+
del r[0]
39+
del r[-1]
40+
41+
return outer_element + self.spiralOrder(matrix)
42+

561. Array Partition.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def arrayPairSum(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
sorted_num = sorted(nums)
8+
9+
return sum(sorted_num[0: len(nums) : 2])
10+
11+
# PROVE:
12+
# 2n number (a1,b1),...,(an, bn), such that ai <= bi
13+
# So maximize sum(ai) means minimize sum (bi-ai) due to sum of 2n nums is constant, then
14+
# 2 sum(ai) = sum (ai + ai) = sum (ai + bi - (bi - ai) ) = const - sum(bi -ai)
15+
# Then we can prove that the optimal sum is as code above

622. Design Circular Queue.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class MyCircularQueue(object):
2+
3+
def __init__(self, k):
4+
"""
5+
:type k: int
6+
"""
7+
self.data = [None]* k # k-element list
8+
self.k = k
9+
self.head = -1
10+
self.tail = -1
11+
# When both head and tail = -1, mean empty Queue
12+
13+
14+
def enQueue(self, value):
15+
"""
16+
:type value: int
17+
:rtype: bool
18+
"""
19+
if self.isFull() :
20+
return False
21+
if self.isEmpty() :
22+
self.tail = 0
23+
self.head = 0
24+
self.data[0] = value
25+
return True
26+
#ELSE
27+
self.tail = (self.tail + 1) % self.k
28+
self.data[self.tail] = value
29+
30+
return True
31+
32+
33+
def deQueue(self):
34+
"""
35+
:rtype: bool
36+
"""
37+
if self.isEmpty() :
38+
return False
39+
40+
#ELSE
41+
42+
if self.head == self.tail :
43+
# After removing, become empty queue
44+
self.tail = -1
45+
self.head = -1
46+
return True
47+
48+
self.head = (self.head + 1) % self.k
49+
return True
50+
51+
52+
53+
54+
55+
def Front(self):
56+
"""
57+
:rtype: int
58+
"""
59+
if self.isEmpty() :
60+
return -1
61+
return self.data[self.head]
62+
63+
64+
def Rear(self):
65+
"""
66+
:rtype: int
67+
"""
68+
if self.isEmpty() :
69+
return -1
70+
return self.data[self.tail]
71+
72+
73+
def isEmpty(self):
74+
"""
75+
:rtype: bool
76+
"""
77+
return (self.head == -1 and self.tail == -1)
78+
79+
80+
def isFull(self):
81+
"""
82+
:rtype: bool
83+
"""
84+
return (self.head == (self.tail + 1) % self.k)
85+
86+
87+

66. Plus One.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def plusOne(self, digits):
3+
"""
4+
:type digits: List[int]
5+
:rtype: List[int]
6+
"""
7+
n = len(digits)
8+
if len(digits) == 1 :
9+
if digits[0] == 9 :
10+
return [1, 0]
11+
else :
12+
return [digits[0] + 1]
13+
14+
if digits[-1] != 9 :
15+
16+
digits[-1] = digits[-1] + 1
17+
return digits
18+
else :
19+
return self.plusOne(digits[:-1]) + [0]
20+

0 commit comments

Comments
 (0)