Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions combination-sum/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# idea: brute force
# Complexity: O(len(candidates)^(target / min(candidates)))

class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
result = []
nums = []

def dfs(start: int, total: int):
if total > target:
return
if total == target:
result.append(nums.copy())
return
for i in range(start, len(candidates)):
num = candidates[i]
if total + num > target:
break
nums.append(num)
dfs(i, total + num)
nums.pop()
dfs(0, 0)
return result



19 changes: 19 additions & 0 deletions decode-ways/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# idea : Tree
class Solution:
def numDecodings(self, s: str) -> int:
# recurisve : top-down
# DP : bottom-up can be another way to solve it, but it is not clear for me.
memo = {len(s):1}
def dfs(start):
if start in memo:
return memo[start]
if s[start] == '0':
memo[start] = 0
elif start+1 < len(s) and int(s[start:start+2]) < 27:
memo[start] = dfs(start+1) + dfs(start+2)
else:
memo[start] = dfs(start+1)
return memo[start]
return dfs(0)


21 changes: 21 additions & 0 deletions maximum-subarray/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# idea : DP
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# The solution must run in O(N^2) or better.
'''
1. Idea: Sorting + Two Pointers (x) — This problem does not allow changing the original order of the array.
2. Subarrays (TLE) — O(N^3), too slow.
3. DP (o)
'''
max_total = nums[0]
total = nums[0]
# dp = [0]*len(nums)
# dp[0] = nums[0]
for i in range(1,len(nums)):
# dp[i] = max(nums[i], dp[i-1]+nums[i])
total = max(nums[i], total+nums[i])
max_total = max(total, max_total)
return max_total



10 changes: 10 additions & 0 deletions number-of-1-bits/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# idea: -

from collections import Counter
class Solution:
def hammingWeight(self, n: int) -> int:
bits = bin(n).split('0b')[-1]
return Counter(bits)['1']



11 changes: 11 additions & 0 deletions valid-palindrome/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# idea : regex
import re

class Solution:
def isPalindrome(self, s: str) -> bool:
alphabet = re.sub(r'[^A-Za-z0-9]', '', s).lower()

return alphabet == alphabet[::-1]