Skip to content

Commit 9b6a9af

Browse files
Added all the summaries in the world
1 parent 5d5bf10 commit 9b6a9af

93 files changed

Lines changed: 4773 additions & 821 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1-D Dynamic Programming/100-palindromeSubstring.py

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def countSubstrings(self, s: str) -> int:
3+
res = ""
4+
resLen = 0
5+
count_palyndrome = 0
6+
for i in range(len(s)):
7+
8+
l, r = i, i
9+
while l >= 0 and r < len(s) and s[l] == s[r]:
10+
count_palyndrome += 1
11+
l -= 1
12+
r += 1
13+
14+
l, r = i, i + 1
15+
while l >= 0 and r < len(s) and s[l] == s[r]:
16+
count_palyndrome += 1
17+
l -= 1
18+
r += 1
19+
return count_palyndrome

1-D Dynamic Programming/101-numDecoding.py renamed to 1-D_Dynamic_Programming/101-numDecoding.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,45 @@ def numDecodings(self, s: str) -> int:
55
if s[i] == "0":
66
dp[i] = 0
77
else:
8-
dp[i] = dp[i+1]
8+
dp[i] = dp[i + 1]
99

10-
if i+1 < len(s) and (
10+
if i + 1 < len(s) and (
1111
(s[i] == "1") or s[i] == "2" and s[i + 1] in "0123456"
1212
):
13-
dp[i] += dp[i+2]
13+
dp[i] += dp[i + 2]
1414

1515
return dp[0]
16-
17-
18-
1916

2017
def numDecodings_(self, s: str) -> int:
2118

2219
if s.startswith("0"):
2320
return 0
24-
25-
keys = set([str(i) for i in range(1,27)])
21+
22+
keys = set([str(i) for i in range(1, 27)])
2623
count = 1
2724
preMade = 0
2825

2926
for i in range(len(s)):
3027
if s[i] in keys:
3128
pass
32-
33-
if i>=1 and s[i] == "0":
34-
if int(s[i-1:i+1])>26:
29+
30+
if i >= 1 and s[i] == "0":
31+
if int(s[i - 1 : i + 1]) > 26:
3532
return 0
3633
else:
3734
print("here")
3835
count -= 1
3936
if preMade:
40-
count -=1
37+
count -= 1
4138
preMade = 0
4239

43-
if i>=1 and s[i-1:i+1] in keys:
44-
print("Add", s[i-1:i+1])
45-
count+=1
46-
if preMade>=2:
40+
if i >= 1 and s[i - 1 : i + 1] in keys:
41+
print("Add", s[i - 1 : i + 1])
42+
count += 1
43+
if preMade >= 2:
4744
preMade
48-
count+=1
45+
count += 1
4946
preMade = 1
5047
else:
5148
preMade = False
52-
return max(count,0)
49+
return max(count, 0)
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# TO REDO
22

3+
34
class Solution:
45
def coinChange(self, coins: List[int], amount: int) -> int:
5-
dp = [amount + 1]*(amount + 1)
6+
dp = [amount + 1] * (amount + 1)
67
dp[0] = 0
78

89
for a in range(1, amount + 1):
910
for c in coins:
1011
if a - c >= 0:
11-
dp[a] = min(dp[a], dp[a-c] + 1)
12+
dp[a] = min(dp[a], dp[a - c] + 1)
1213

1314
print(dp)
1415
return dp[amount] if dp[amount] != amount + 1 else -1
15-
16-
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution:
22
def maxProduct(self, nums: List[int]) -> int:
33
res = nums[0]
4-
curr_min, curr_max = 1,1
4+
curr_min, curr_max = 1, 1
55

66
for n in nums:
7-
temp = n*curr_max
8-
curr_max = max(temp, n*curr_min, n)
9-
curr_min = min(temp, n*curr_min, n)
7+
temp = n * curr_max
8+
curr_max = max(temp, n * curr_min, n)
9+
curr_min = min(temp, n * curr_min, n)
1010
res = max(curr_max, res)
1111

1212
return res
13-
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ def wordBreak(self, s: str, wordDict: List[str]) -> bool:
55

66
# I have to get used to this structure. I had some
77
# "Math issues" with the indexes here
8-
dp = [0]*(len(s) + 1)
9-
for i in range(len(s) +1):
8+
dp = [0] * (len(s) + 1)
9+
for i in range(len(s) + 1):
1010
for word in wordDict:
1111
if len(word) < i + 1:
12-
if word != s[i-len(word):i]:
12+
if word != s[i - len(word) : i]:
1313
continue
14-
15-
if len(word) == i :
14+
15+
if len(word) == i:
1616
dp[i] = 1
17-
elif dp[i - len(word) ]:
17+
elif dp[i - len(word)]:
1818
dp[i] = 1
1919
# print(dp)
2020
return dp[-1] == 1
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
class Solution:
22
def lengthOfLIS(self, nums: List[int]) -> int:
3-
dp = [1]* len(nums)
3+
dp = [1] * len(nums)
44

5-
for i in range(1,len(nums)):
5+
for i in range(1, len(nums)):
66
j = i - 1
77
currMax = 0
88
while j >= 0:
99
if nums[i] > nums[j]:
1010
currMax = max(dp[j], currMax)
1111
# break
12-
j-=1
12+
j -= 1
1313
dp[i] += currMax
1414
# print(dp)
1515
return max(dp)
16-
17-
18-

1-D Dynamic Programming/106-CanPartition.py renamed to 1-D_Dynamic_Programming/106-CanPartition.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
class Solution:
33
def canPartition(self, nums: List[int]) -> bool:
44
# I am dumb for not thinking about this...
5-
sums = sum(nums)
6-
if sums%2:
5+
sums = sum(nums)
6+
if sums % 2:
77
return False
88

9-
target = sum(nums)//2
9+
target = sum(nums) // 2
1010

1111
dp = set()
1212
dp.add(0)
13-
for i in range (len(nums)):
13+
for i in range(len(nums)):
1414
nextDP = set()
15-
# Super simple when you thik about it since we just
15+
# Super simple when you thik about it since we just
1616
# need to explore the begining of the state
1717
for t in dp:
18-
if (t+ nums[i]) == target:
18+
if (t + nums[i]) == target:
1919
return True
2020
nextDP.add(t + nums[i])
2121
nextDP.add(t)
2222
dp = nextDP
23-
24-
return False
2523

26-
24+
return False

1-D Dynamic Programming/95-Climbing_Stairs.py renamed to 1-D_Dynamic_Programming/95-Climbing_Stairs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# give the right solution.
33
# Could also use a dfs if memory was not an issue and the depth of the tree was limited.
44

5+
56
class Solution:
67
def climbStairs(self, n: int) -> int:
78

8-
if n<=3:
9+
if n <= 3:
910
return n
1011

1112
n1, n2 = 2, 3
@@ -15,4 +16,4 @@ def climbStairs(self, n: int) -> int:
1516
n1 = n2
1617
n2 = temp
1718

18-
return n1 + n2
19+
return n1 + n2
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
21
class Solution:
32
min_cost = 10_000_000
4-
def minCostClimbingStairs(self, cost: List[int]) -> int:
53

6-
def climbSteps(step_num: int, current_cost:int):
4+
def minCostClimbingStairs(self, cost: List[int]) -> int:
5+
def climbSteps(step_num: int, current_cost: int):
76
if step_num > len(cost):
87
# print("here")
98
# current_cost += cost[val - 1]
109
# print(step_num, cost, current_cost)
11-
if self.min_cost>current_cost:
10+
if self.min_cost > current_cost:
1211
self.min_cost = current_cost
1312

1413
elif step_num <= len(cost):
1514
current_cost += cost[step_num - 1]
1615
# print(step_num, current_cost)
1716
climbSteps(step_num + 1, current_cost)
1817
climbSteps(step_num + 2, current_cost)
19-
20-
climbSteps(1,0)
21-
climbSteps(2,0)
22-
return self.min_cost
18+
19+
climbSteps(1, 0)
20+
climbSteps(2, 0)
21+
return self.min_cost

0 commit comments

Comments
 (0)