Skip to content

Commit 1b793a6

Browse files
authored
Test (#1)
* add 322 and 509 * add more problems * add 752 * add more problems * add more problems * add more problems * add stocks * add stocks * add roober * add more problems * add more problems
1 parent b998db8 commit 1b793a6

27 files changed

+1647
-48
lines changed

easy/1.two_sum.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
2929
return [result[res], index]
3030
else:
3131
result[value] = index
32-
33-
34-
# two-pass hash table [Accepted]
32+
#
33+
#
34+
# # two-pass hash table [Accepted]
3535
class Solution:
3636
def twoSum(self, nums: List[int], target: int) -> List[int]:
3737
# generate a dict which key is element, value is index
@@ -41,12 +41,38 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
4141
res = target - nums[index]
4242
if res in nums_dict and nums_dict[res] != index:
4343
return [index, nums_dict[res]]
44-
45-
46-
# Bruce Force [Not Accepted, Time exceeded]
44+
#
45+
#
46+
# # Bruce Force [Not Accepted, Time exceeded]
4747
class Solution:
4848
def twoSum(self, nums: List[int], target: int) -> List[int]:
4949
for idx1 in range(len(nums)):
5050
for idx2 in range(idx1 + 1, len(nums)):
5151
if nums[idx1] + nums[idx2] == target:
5252
return [idx1, idx2]
53+
54+
# two pointers
55+
class Solution:
56+
def twoSum(self, nums: List[int], target: int) -> List[int]:
57+
sorted_nums = sorted(nums)
58+
lo = 0
59+
hi = len(nums) -1
60+
while lo < hi:
61+
total = sorted_nums[lo] + sorted_nums[hi]
62+
left = sorted_nums[lo]
63+
right = sorted_nums[hi]
64+
if total > target:
65+
while lo < hi and sorted_nums[hi] == right:
66+
hi -= 1
67+
elif total < target:
68+
while lo < hi and sorted_nums[lo] == left:
69+
lo += 1
70+
elif total == target:
71+
res = []
72+
for index, item in enumerate(nums):
73+
if item in [left, right]:
74+
res.append(index)
75+
return res
76+
return []
77+
78+

easy/121.best_time_to_buy_and_sell_stock.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,66 @@
3535
# [End of Description]:
3636

3737
# brute force(Not Accepted, Timeout)
38-
# In formal terms, we need to find max(prices[j]−prices[i]), for every i and j such that j > i.
39-
class Solution:
40-
def maxProfit(self, prices: List[int]) -> int:
41-
n = len(prices)
42-
max_profit = 0
43-
for i in range(n):
44-
for j in range(i + 1, n):
45-
if prices[j] - prices[i] > max_profit:
46-
max_profit = prices[j] - prices[i]
47-
return max_profit
38+
# # In formal terms, we need to find max(prices[j]−prices[i]), for every i and j such that j > i.
39+
# class Solution:
40+
# def maxProfit(self, prices: List[int]) -> int:
41+
# n = len(prices)
42+
# max_profit = 0
43+
# for i in range(n):
44+
# for j in range(i + 1, n):
45+
# if prices[j] - prices[i] > max_profit:
46+
# max_profit = prices[j] - prices[i]
47+
# return max_profit
48+
#
49+
#
50+
# # one pass(We need to find the largest peak following the smallest valley)
51+
# class Solution:
52+
# def maxProfit(self, prices: List[int]) -> int:
53+
# n = len(prices)
54+
# max_profit = 0
55+
# min_price = float("inf")
56+
# for i in range(n):
57+
# if prices[i] < min_price:
58+
# min_price = prices[i]
59+
# else:
60+
# max_profit = max(max_profit, prices[i] - min_price)
61+
# return max_profit
4862

63+
# dp template
64+
# class Solution:
65+
# def maxProfit(self, prices: List[int]) -> int:
66+
# n = len(prices)
67+
# # initial dp array
68+
# dp = []
69+
# for _ in range(n):
70+
# inner = []
71+
# for i in range(2):
72+
# inner.append(i)
73+
# dp.append(inner)
74+
# # base case
75+
# for i in range(n):
76+
# if i - 1 == -1:
77+
# dp[i][0] = 0
78+
# # dp[i][0] =
79+
# # max(dp[-1][0], dp[-1][1] + price[0])
80+
# # max(0, -inf + price[0]) = 0
81+
# dp[i][1] = -prices[i]
82+
# # dp[i][1] =
83+
# # max(dp[-1][1], -price[i])
84+
# # max(-inf, -price[i]) = -price[i]
85+
# continue
86+
# dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i])
87+
# dp[i][1] = max(dp[i-1][1], -prices[i])
88+
# return dp[n-1][0]
4989

50-
# one pass(We need to find the largest peak following the smallest valley)
90+
# actually we just need to store the previous two value
91+
# no need to use a two-dimension array
5192
class Solution:
5293
def maxProfit(self, prices: List[int]) -> int:
5394
n = len(prices)
54-
max_profit = 0
55-
min_price = float("inf")
95+
dp_i_0 = 0
96+
dp_i_1 = -float("inf")
5697
for i in range(n):
57-
if prices[i] < min_price:
58-
min_price = prices[i]
59-
else:
60-
max_profit = max(max_profit, prices[i] - min_price)
61-
return max_profit
98+
dp_i_0 = max(dp_i_0, dp_i_1 + prices[i])
99+
dp_i_1 = max(dp_i_1, -prices[i])
100+
return dp_i_0

easy/122.best_time_to_buy_and_sell_stock_ii.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,27 @@ def maxProfit(self, prices: List[int]) -> int:
7171
return maxProfit
7272

7373

74-
# a mathmatics trick here
75-
# peak1 - valley1 + peak2 - valley2
76-
# p1-p0+p2-p1+p3-p2+...+p(n)-p(n-1) = p(n) - p0
74+
## a mathmatics trick here
75+
## peak1 - valley1 + peak2 - valley2
76+
## p1-p0+p2-p1+p3-p2+...+p(n)-p(n-1) = p(n) - p0
7777
class Solution:
7878
def maxProfit(self, prices: List[int]) -> int:
7979
maxProfit = 0
8080
for i in range(1, len(prices)):
8181
if prices[i] > prices[i - 1]:
8282
maxProfit += prices[i] - prices[i - 1]
8383
return maxProfit
84+
85+
86+
class Solution:
87+
def maxProfit(self, prices: List[int]) -> int:
88+
dp_i_0 = 0
89+
dp_i_1 = -float("inf")
90+
n = len(prices)
91+
for i in range(n):
92+
# markdown the previous one
93+
# which is different from the second dp_i_0
94+
temp = dp_i_0
95+
dp_i_0 = max(dp_i_0, dp_i_1 + prices[i])
96+
dp_i_1 = max(dp_i_1, temp - prices[i])
97+
return dp_i_0
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Best Time to Buy and Sell Stock III
2+
#
3+
# [Hard] [AC:38.8% 247.6K of 638.4K] [filetype:python3]
4+
#
5+
# Say you have an array for which the ith element is the price of a given stock on day i.
6+
#
7+
# Design an algorithm to find the maximum profit. You may complete at most two transactions.
8+
#
9+
# Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy
10+
# again).
11+
#
12+
# Example 1:
13+
#
14+
# Input: prices = [3,3,5,0,0,3,1,4]
15+
#
16+
# Output: 6
17+
#
18+
# Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
19+
#
20+
# Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
21+
#
22+
# Example 2:
23+
#
24+
# Input: prices = [1,2,3,4,5]
25+
#
26+
# Output: 4
27+
#
28+
# Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
29+
#
30+
# Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the
31+
# same time. You must sell before buying again.
32+
#
33+
# Example 3:
34+
#
35+
# Input: prices = [7,6,4,3,1]
36+
#
37+
# Output: 0
38+
#
39+
# Explanation: In this case, no transaction is done, i.e. max profit = 0.
40+
#
41+
# Example 4:
42+
#
43+
# Input: prices = [1]
44+
#
45+
# Output: 0
46+
#
47+
# Constraints:
48+
#
49+
# 1 <= prices.length <= 105
50+
#
51+
# 0 <= prices[i] <= 105
52+
#
53+
# [End of Description]:
54+
55+
# stock template
56+
class Solution:
57+
def maxProfit(self, prices: List[int]) -> int:
58+
dp_i10 = dp_i20 = 0
59+
dp_i11 = dp_i21 = -float("inf")
60+
for price in prices:
61+
dp_i20 = max(dp_i20, dp_i21 + price)
62+
dp_i21 = max(dp_i21, dp_i10 - price)
63+
dp_i10 = max(dp_i10, dp_i11 + price)
64+
dp_i11 = max(dp_i11, -price)
65+
66+
return dp_i20

easy/15.3sum.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 3Sum
2+
#
3+
# [Medium] [AC:27.1% 1M of 3.8M] [filetype:python3]
4+
#
5+
# Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique
6+
# triplets in the array which gives the sum of zero.
7+
#
8+
# Notice that the solution set must not contain duplicate triplets.
9+
#
10+
# Example 1:
11+
#
12+
# Input: nums = [-1,0,1,2,-1,-4]
13+
#
14+
# Output: [[-1,-1,2],[-1,0,1]]
15+
#
16+
# Example 2:
17+
#
18+
# Input: nums = []
19+
#
20+
# Output: []
21+
#
22+
# Example 3:
23+
#
24+
# Input: nums = [0]
25+
#
26+
# Output: []
27+
#
28+
# Constraints:
29+
#
30+
# 0 <= nums.length <= 3000
31+
#
32+
# -105 <= nums[i] <= 105
33+
#
34+
# [End of Description]:
35+
class Solution:
36+
def threeSum(self, nums: List[int]) -> List[List[int]]:
37+
sorted_nums = sorted(nums)
38+
return self.nSumTarget(sorted_nums, 3, 0, 0)
39+
40+
def nSumTarget(self, nums: List[int], n: int, start: int, target: int) -> List[List[int]]:
41+
sz = len(nums)
42+
res: List[List[int]] = []
43+
if n < 2 or sz < n:
44+
return res
45+
if n == 2:
46+
lo = start
47+
hi = sz -1
48+
while lo < hi:
49+
total = nums[lo] + nums[hi]
50+
left = nums[lo]
51+
right = nums[hi]
52+
if total == target:
53+
res.append([left, right])
54+
while lo < hi and nums[lo] == left:
55+
lo += 1
56+
while lo < hi and nums[hi] == right:
57+
hi -= 1
58+
elif total < target:
59+
while lo < hi and nums[lo] == left:
60+
lo += 1
61+
elif total > target:
62+
while lo < hi and nums[hi] == right:
63+
hi -= 1
64+
else:
65+
i = start
66+
while i < sz:
67+
sub = self.nSumTarget(nums, n - 1, i + 1, target - nums[i])
68+
for item in sub:
69+
item.append(nums[i])
70+
res.append(item)
71+
while (i < sz - 1) and (nums[i] == nums[i + 1]):
72+
i += 1
73+
i += 1
74+
return [sorted(item) for item in res]
75+
76+
77+
78+

easy/18.4sum.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 4Sum
2+
#
3+
# [Medium] [AC:33.9% 352.6K of 1M] [filetype:python3]
4+
#
5+
# Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that
6+
# a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
7+
#
8+
# Note:
9+
#
10+
# The solution set must not contain duplicate quadruplets.
11+
#
12+
# Example:
13+
#
14+
# Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
15+
#
16+
# A solution set is:
17+
#
18+
# [
19+
#
20+
# [-1, 0, 0, 1],
21+
#
22+
# [-2, -1, 1, 2],
23+
#
24+
# [-2, 0, 0, 2]
25+
#
26+
# ]
27+
#
28+
# [End of Description]:
29+
class Solution:
30+
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
31+
sorted_nums = sorted(nums)
32+
return self.nSumTarget(sorted_nums, 4, 0, target)
33+
34+
def nSumTarget(self, nums: List[int], n: int, start: int, target: int) -> List[List[int]]:
35+
sz = len(nums)
36+
res = []
37+
if n < 2 or sz < n:
38+
return res
39+
if n == 2:
40+
lo = start
41+
hi = sz - 1
42+
while lo < hi:
43+
total = nums[lo] + nums[hi]
44+
left = nums[lo]
45+
right = nums[hi]
46+
if total > target:
47+
while lo < hi and nums[hi] == right:
48+
hi -= 1
49+
elif total < target:
50+
while lo < hi and nums[lo] == left:
51+
lo += 1
52+
elif total == target:
53+
res.append([left, right])
54+
while lo < hi and nums[lo] == left:
55+
lo += 1
56+
while lo < hi and nums[hi] == right:
57+
hi -= 1
58+
else:
59+
i = start
60+
while i < sz:
61+
sub = self.nSumTarget(nums, n - 1, i + 1, target - nums[i])
62+
for item in sub:
63+
item.append(nums[i])
64+
res.append(item)
65+
while (i < sz - 1) and (nums[i] == nums[i + 1]):
66+
i += 1
67+
i += 1
68+
return [sorted(item) for item in res]
69+
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)