Skip to content

Commit 05f7aac

Browse files
committed
add more problems
1 parent 9d73380 commit 05f7aac

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Longest Increasing Subsequence
2+
#
3+
# [Medium] [AC:42.8% 421.8K of 986.2K] [filetype:python3]
4+
#
5+
# Given an unsorted array of integers, find the length of longest increasing subsequence.
6+
#
7+
# Example:
8+
#
9+
# Input: [10,9,2,5,3,7,101,18]
10+
#
11+
# Output: 4
12+
#
13+
# Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
14+
#
15+
# Note:
16+
#
17+
# There may be more than one LIS combination, it is only necessary for you to return the length.
18+
#
19+
# Your algorithm should run in O(n2) complexity.
20+
#
21+
# Follow up: Could you improve it to O(n log n) time complexity?
22+
#
23+
# [End of Description]:
24+
class Solution:
25+
def lengthOfLIS(self, nums: List[int]) -> int:
26+
if not nums:
27+
return 0
28+
sz = len(nums)
29+
dp = [1] * sz
30+
for i in range(sz):
31+
for j in range(i):
32+
if nums[i] > nums[j]:
33+
dp[i] = max(dp[i], dp[j] + 1)
34+
return max(dp)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Partition Equal Subset Sum
2+
#
3+
# [Medium] [AC:44.0% 205.3K of 466.4K] [filetype:python3]
4+
#
5+
# Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such
6+
# that the sum of elements in both subsets is equal.
7+
#
8+
# Note:
9+
#
10+
# Each of the array element will not exceed 100.
11+
#
12+
# The array size will not exceed 200.
13+
#
14+
# Example 1:
15+
#
16+
# Input: [1, 5, 11, 5]
17+
#
18+
# Output: true
19+
#
20+
# Explanation: The array can be partitioned as [1, 5, 5] and [11].
21+
#
22+
# Example 2:
23+
#
24+
# Input: [1, 2, 3, 5]
25+
#
26+
# Output: false
27+
#
28+
# Explanation: The array cannot be partitioned into equal sum subsets.
29+
#
30+
# [End of Description]:
31+
# bag
32+
# class Solution:
33+
# def canPartition(self, nums: List[int]) -> bool:
34+
# total = sum(nums)
35+
# # sum cannot be divided by 2
36+
# if total % 2 != 0:
37+
# return False
38+
# capacity = sum(nums) // 2
39+
# sz = len(nums)
40+
# # initialize dp
41+
# dp = []
42+
# for _ in range(sz+1):
43+
# inner1 = []
44+
# for _ in range(capacity+1):
45+
# inner1.append(False)
46+
# dp.append(inner1)
47+
#
48+
# # base case
49+
# for i in range(sz+1):
50+
# dp[i][0] = True
51+
#
52+
# # since we use i - 1 to call nums, so we need to add one to the size
53+
# # i must start from 1
54+
# for i in range(1, sz+1):
55+
# # need to include capacity itself
56+
# # j must start from 1, since 0 is the base case
57+
# for j in range(1, capacity+1):
58+
# # judge if there is enough space for i
59+
# if j - nums[i - 1] >= 0:
60+
# # dp[i][j] = dp[i - 1][j]
61+
# # else:
62+
# dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]]
63+
# return dp[sz][capacity]
64+
65+
66+
class Solution:
67+
def canPartition(self, nums: List[int]) -> bool:
68+
total = sum(nums)
69+
if total % 2 != 0:
70+
return False
71+
capacity = sum(nums) // 2
72+
sz = len(nums)
73+
dp = [False] * (capacity + 1)
74+
# base case
75+
dp[0] = True
76+
77+
for i in range(sz):
78+
for j in range(capacity, -1, -1):
79+
if j - nums[i] >= 0:
80+
dp[j] = dp[j] or dp[j - nums[i]]
81+
return dp[capacity]

easy/518.coin_change_2.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Coin Change 2
2+
#
3+
# [Medium] [AC:50.6% 148.7K of 294.1K] [filetype:python3]
4+
#
5+
# You are given coins of different denominations and a total amount of money. Write a function to compute the number of
6+
# combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
7+
#
8+
# Example 1:
9+
#
10+
# Input: amount = 5, coins = [1, 2, 5]
11+
#
12+
# Output: 4
13+
#
14+
# Explanation: there are four ways to make up the amount:
15+
#
16+
# 5=5
17+
#
18+
# 5=2+2+1
19+
#
20+
# 5=2+1+1+1
21+
#
22+
# 5=1+1+1+1+1
23+
#
24+
# Example 2:
25+
#
26+
# Input: amount = 3, coins = [2]
27+
#
28+
# Output: 0
29+
#
30+
# Explanation: the amount of 3 cannot be made up just with coins of 2.
31+
#
32+
# Example 3:
33+
#
34+
# Input: amount = 10, coins = [10]
35+
#
36+
# Output: 1
37+
#
38+
# Note:
39+
#
40+
# You can assume that
41+
#
42+
# 0 <= amount <= 5000
43+
#
44+
# 1 <= coin <= 5000
45+
#
46+
# the number of coins is less than 500
47+
#
48+
# the answer is guaranteed to fit into signed 32-bit integer
49+
#
50+
# [End of Description]:
51+
# bag
52+
class Solution:
53+
def change(self, amount: int, coins: List[int]) -> int:
54+
sz = len(coins)
55+
dp = []
56+
for _ in range(sz + 1):
57+
inner = []
58+
for _ in range(amount + 1):
59+
inner.append(0)
60+
dp.append(inner)
61+
# base case
62+
for i in range(sz + 1):
63+
dp[i][0] = 1
64+
65+
for i in range(1, sz + 1):
66+
for j in range(1, amount + 1):
67+
if j - coins[i - 1] >= 0:
68+
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i - 1]]
69+
else:
70+
dp[i][j] = dp[i - 1][j]
71+
return dp[sz][amount]
72+
73+
74+
class Solution:
75+
def change(self, amount: int, coins: List[int]) -> int:
76+
sz = len(coins)
77+
dp = [0] * (amount + 1)
78+
# base case
79+
dp[0] = 1
80+
for i in range(sz):
81+
for j in range(1, amount + 1):
82+
if j - coins[i] >= 0:
83+
dp[j] = dp[j] + dp[j - coins[i]]
84+
return dp[amount]

0 commit comments

Comments
 (0)