Skip to content

Commit

Permalink
t status
Browse files Browse the repository at this point in the history
:Merge branch 'master' of https://github.com/yennanliu/CS_basics
  • Loading branch information
yennanliu committed Jul 3, 2022
2 parents 4b81b38 + 8b912e9 commit 6c63935
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
375| [Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/)| [Python](./leetcode_python//Dynamic_Programming/guess-number-higher-or-lower-ii.py) | _O(n^2)_ | _O(n^2)_ | Medium | | AGAIN (not start)
377| [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)| [Python](./leetcode_python//Dynamic_Programming/combination-sum-iv.py) | _O(nlogn + n * t)_| _O(t)_ | Medium |Curated Top 75, AGAIN, dp basic ,`fb`| AGAIN******** (3)
403| [Frog Jump](https://leetcode.com/problems/frog-jump/)| [Python](./leetcode_python//Dynamic_Programming/frog-jump.py) | _O(nlogn + n * t)_| _O(t)_ | Hard |dfs, bfs, Memoization dfs, dp, amazon, tik-tok, m$| AGAIN (not start)
416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](./leetcode_python//Dynamic_Programming/partition-equal-subset-sum.py) | _O(n * s)_ | _O(s)_ | Medium || AGAIN (not start)
416 | [Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/) | [Python](./leetcode_python//Dynamic_Programming/partition-equal-subset-sum.py) | _O(n * s)_ | _O(s)_ | Medium |dp, fb, google, apple, amazon| AGAIN** (not start) (1)
418 | [Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/) | [Python](./leetcode_python//Dynamic_Programming/sentence-screen-fitting.py) | _O(r + n * c)_ | _O(n)_ | Medium |🔒, `dp`, `google`| AGAIN (not start)
467 | [Unique Substrings in Wraparound String](https://leetcode.com/problems/unique-substrings-in-wraparound-string/) | [Python](./leetcode_python//Dynamic_Programming/unique-substrings-in-wraparound-string.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start)
471 | [Encode String with Shortest Length](https://leetcode.com/problems/encode-string-with-shortest-length/) | [Python](./leetcode_python//Dynamic_Programming/encode-string-with-shortest-length.py) | _O(n^3)_ on average | _O(n^2)_ | Medium |🔒, `dp`, `google`| AGAIN (not start)
Expand Down
1 change: 1 addition & 0 deletions doc/cheatsheet/bit_manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- ref
- [bit_manipulation.md](https://github.com/yennanliu/CS_basics/blob/master/doc/bit_manipulation.md)
- [python-operators.html](https://www.runoob.com/python/python-operators.html)
- [leetcode-easy-bitwise-xor-summary](https://steveyang.blog/2022/07/02/leetcode-easy-bitwise-xor-summary/)

<p align="center"><img src ="https://github.com/yennanliu/CS_basics/blob/master/doc/pic/bit_basic1.png" ></p>
<p align="center"><img src ="https://github.com/yennanliu/CS_basics/blob/master/doc/pic/bit_basic2.png" ></p>
Expand Down
3 changes: 1 addition & 2 deletions doc/progress.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Progress

# 2022-07-02
# 2022-07-03
- ================
- leetcode_company_V5/LC-goog.pdf
-
Expand Down Expand Up @@ -127,7 +127,6 @@
- 114
- 221
- 337
- 416
- 437
- ================
- LC general
Expand Down
114 changes: 110 additions & 4 deletions leetcode_python/Dynamic_Programming/partition-equal-subset-sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,113 @@
"""
416. Partition Equal Subset Sum
Medium
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 100
"""

# VO

# V1
# V1
# IDEA : brute force (TLE)
# https://leetcode.com/problems/partition-equal-subset-sum/solution/
class Solution:
def canPartition(self, nums: List[int]) -> bool:
def dfs(nums: List[int], n: int, subset_sum: int) -> bool:
# Base cases
if subset_sum == 0:
return True
if n == 0 or subset_sum < 0:
return False
result = (dfs(nums, n - 1, subset_sum - nums[n - 1])
or dfs(nums, n - 1, subset_sum))
return result

# find sum of array elements
total_sum = sum(nums)

# if total_sum is odd, it cannot be partitioned into equal sum subsets
if total_sum % 2 != 0:
return False

subset_sum = total_sum // 2
n = len(nums)
return dfs(nums, n - 1, subset_sum)

# V1'
# IDEA : Top Down Dynamic Programming - Memoization
# https://leetcode.com/problems/partition-equal-subset-sum/solution/
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@lru_cache(maxsize=None)
def dfs(nums: Tuple[int], n: int, subset_sum: int) -> bool:
# Base cases
if subset_sum == 0:
return True
if n == 0 or subset_sum < 0:
return False
result = (dfs(nums, n - 1, subset_sum - nums[n - 1])
or dfs(nums, n - 1, subset_sum))
return result

# find sum of array elements
total_sum = sum(nums)

# if total_sum is odd, it cannot be partitioned into equal sum subsets
if total_sum % 2 != 0:
return False

subset_sum = total_sum // 2
n = len(nums)
return dfs(tuple(nums), n - 1, subset_sum)

# V1''
# IDEA : Bottom Up Dynamic Programming
# https://leetcode.com/problems/partition-equal-subset-sum/solution/
class Solution:
def canPartition(self, nums: List[int]) -> bool:
# find sum of array elements
total_sum = sum(nums)

# if total_sum is odd, it cannot be partitioned into equal sum subsets
if total_sum % 2 != 0:
return False
subset_sum = total_sum // 2
n = len(nums)

# construct a dp table of size (n+1) x (subset_sum + 1)
dp = [[False] * (subset_sum + 1) for _ in range(n + 1)]
dp[0][0] = True
for i in range(1, n + 1):
curr = nums[i - 1]
for j in range(subset_sum + 1):
if j < curr:
dp[i][j] = dp[i - 1][j]
else:
dp[i][j] = dp[i - 1][j] or dp[i - 1][j - curr]
return dp[n][subset_sum]

# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/79787425
# DFS
class Solution:
Expand All @@ -24,7 +131,7 @@ def dfs(self, nums, index, target):
target[i] += nums[index]
return False

# V1'
# V1'''''
# https://blog.csdn.net/fuxuemingzhu/article/details/79787425
# dp[j] = dp[j] || dp[j - nums[i]]
class Solution(object):
Expand All @@ -44,7 +151,6 @@ def canPartition(self, nums):
# V2
# Time: O(n * s), s is the sum of nums
# Space: O(s)

class Solution(object):
def canPartition(self, nums):
"""
Expand All @@ -61,4 +167,4 @@ def canPartition(self, nums):
for i in reversed(xrange(1, len(dp))):
if num <= i:
dp[i] = dp[i] or dp[i - num]
return dp[-1]
return dp[-1]

0 comments on commit 6c63935

Please sign in to comment.