-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd74215
commit 803e164
Showing
14 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def waysToChange(self, n: int) -> int: | ||
dp = [0]*(n+1) | ||
coins=[1,5,10,25] | ||
dp[0]=1 | ||
for coin in coins: | ||
for i in range(1,n+1): | ||
if i-coin>=0: | ||
dp[i]+=(dp[i-coin]) | ||
return dp[-1]%1000000007 | ||
s = Solution() | ||
print(s.waysToChange(10)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def lastStoneWeightII(self, stones: List[int]) -> int: | ||
stones_sum=sum(stones) | ||
bag_size=(stones_sum+1)//2 | ||
dp=[[0]*len(stones) for _ in range(bag_size+1)] | ||
for i in range(1,bag_size+1): | ||
if i>=stones[0]: | ||
dp[i][0]=stones[0] | ||
for j in range(0,len(stones)): | ||
if stones[j]==1: | ||
dp[1][j]=1 | ||
for i in range(1,bag_size+1): | ||
for j in range(1,len(stones)): | ||
if i>=stones[j]: | ||
dp[i][j]=max(dp[i-stones[j]][j-1]+stones[j],dp[i][j-1]) | ||
else: | ||
dp[i][j]=dp[i][j-1] | ||
return abs(stones_sum-2*dp[-1][-1]) | ||
s = Solution() | ||
print(s.lastStoneWeightII([57,32,40,27,35,61])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def minHeightShelves(self, books: List[List[int]], shelf_width: int) -> int: | ||
dp=[[0]*(shelf_width+1) for _ in range(len(books))] | ||
dp[0][books[0][0]]=books[0][1] | ||
for i in range(len(books)): | ||
for j in range(1,shelf_width+1): | ||
if j-books[i][0]>=0: | ||
pre=max(dp[i-1][j-books[i][0]]) | ||
else: | ||
pre=float('inf') | ||
dp[i][j]=min(pre,dp[i-1][j]+books[i][1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def minHeightShelves(self, books: List[List[int]], shelf_width: int) -> int: | ||
pre_not_new_row_x=float('inf') | ||
pre_not_new_row_y=float('inf') | ||
pre_new_row_x,pre_new_row_y=books[0] | ||
for i in range(1,len(books)): | ||
book_x,book_y=books[i] | ||
cur_not_new_row_x=float('inf') | ||
cur_not_new_row_y=float('inf') | ||
if book_x+pre_not_new_row_x<=shelf_width: | ||
cur_not_new_row_x=pre_not_new_row_x+book_x | ||
cur_not_new_row_y=pre_not_new_row_y+book_y-books[i-1][1] if book_y-books[i-1][1]>=0 else pre_not_new_row_y | ||
if book_x+pre_new_row_x<=shelf_width and max(pre_new_row_y,pre_new_row_y+book_y-books[i-1][1])<=cur_not_new_row_y: | ||
cur_not_new_row_x=pre_new_row_x+book_x | ||
cur_not_new_row_y=max(pre_new_row_y,pre_new_row_y+book_y-books[i-1][1]) | ||
cur_new_row_x=book_x | ||
cur_new_row_y=min(pre_not_new_row_y,pre_new_row_y)+book_y | ||
pre_not_new_row_x=cur_not_new_row_x | ||
pre_not_new_row_y=cur_not_new_row_y | ||
pre_new_row_x, pre_new_row_y=cur_new_row_x,cur_new_row_y | ||
return min(pre_not_new_row_y,pre_new_row_y) | ||
s = Solution() | ||
print(s.minHeightShelves(books =[[9,9],[5,4],[3,1],[1,5],[7,3]], shelf_width = 10)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def minHeightShelves(self, books: List[List[int]], shelf_width: int) -> int: | ||
if len(books)==0: | ||
return 0 | ||
dp=[float('inf')]*len(books) | ||
dp[0]=books[0][1] | ||
for i in range(1,len(books)): | ||
book_x,book_y = books[i] | ||
dp[i]=dp[i-1]+book_y | ||
temp_x=book_x | ||
temp_y=book_y | ||
for j in range(i-1,-1,-1): | ||
if temp_x+books[j][0]<=shelf_width: | ||
temp_y=max(temp_y,books[j][1]) | ||
temp_x=temp_x+books[j][0] | ||
if j-1>=0: | ||
dp[i]=min(dp[i],dp[j-1]+temp_y) | ||
else: | ||
dp[i] = min(dp[i],temp_y) | ||
else: | ||
break | ||
return dp[-1] | ||
s = Solution() | ||
print(s.minHeightShelves(books =[[1,3],[2,4],[3,2]], shelf_width = 6)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def minimumTotal(self, triangle: List[List[int]]) -> int: | ||
if len(triangle)==0: | ||
return 0 | ||
dp=[[float('inf')]*len(triangle[i]) for i in range(len(triangle))] | ||
dp[0][0]=triangle[0][0] | ||
for i in range(1,len(triangle)): | ||
for j in range(len(triangle[i])): | ||
v= triangle[i][j] | ||
if j-1>=0: | ||
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+v) | ||
if j<len(triangle[i-1]): | ||
dp[i][j]=min(dp[i][j],dp[i-1][j]+v) | ||
return min(dp[-1]) | ||
s = Solution() | ||
print(s.minimumTotal([ | ||
[2], | ||
[3,4], | ||
[6,5,7], | ||
[4,1,8,3] | ||
])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import List | ||
|
||
import copy | ||
class Solution: | ||
def probabilityOfHeads(self, prob: List[float], target: int) -> float: | ||
|
||
self.total_p=0 | ||
def back_track(index,current_p,left): | ||
if len(prob)-1-index>=left-1: | ||
if left==0: | ||
for i in range(index,len(prob)): | ||
current_p*=(1-prob[i]) | ||
self.total_p+=current_p | ||
else: | ||
next=current_p*prob[index] | ||
back_track(index+1,next,left-1) | ||
next=current_p*(1-prob[index]) | ||
back_track(index+1,next,left) | ||
back_track(0,1,target) | ||
return self.total_p | ||
s =Solution() | ||
# print(s.probabilityOfHeads(prob = | ||
# [0.3,0.4,0.5,0.6], target = 3)) | ||
|
||
print(s.probabilityOfHeads(prob = | ||
[0.5, 0.5, 0.5, 0.5, 0.5], target = 1)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import List | ||
|
||
import copy | ||
class Solution: | ||
def shoppingOffers(self, price: List[int], special: List[List[int]], needs: List[int]) -> int: | ||
self.dp = {} | ||
def back_track(left_needs,current_price): | ||
result=float('inf') | ||
for i in range(len(special)): | ||
items=special[i] | ||
can_buy=True | ||
left_needs_copy=copy.deepcopy(left_needs) | ||
for j in range(len(items)-1): | ||
n = items[j] | ||
if left_needs[j]-n<0: | ||
can_buy=False | ||
break | ||
else: | ||
left_needs_copy[j]-=n | ||
if can_buy: | ||
if tuple(left_needs_copy) in self.dp: | ||
a=self.dp[tuple(left_needs_copy)] | ||
else: | ||
a = back_track(left_needs_copy,current_price+items[-1]) | ||
result = min(result, a) | ||
b=current_price | ||
for j in range(len(left_needs)): | ||
b+=left_needs[j]*price[j] | ||
result=min(result,b) | ||
key = tuple(left_needs) | ||
self.dp[key]=result | ||
return result | ||
return back_track(needs,0) | ||
s = Solution() | ||
print(s.shoppingOffers([2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def findLongestChain(self, pairs: List[List[int]]) -> int: | ||
if len(pairs)==0: | ||
return 0 | ||
pairs=sorted(pairs,key=lambda x:x[0]) | ||
dp=[1]*len(pairs) | ||
for i in range(1,len(pairs)): | ||
l,r=pairs[i] | ||
for j in range(i-1,-1,-1): | ||
pre_l,pre_r=pairs[j] | ||
if pre_r<l: | ||
dp[i]=max(dp[i],dp[j]+1) | ||
return dp[-1] | ||
s = Solution() | ||
print(s.findLongestChain([[3,4],[2,3],[1,2]])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def countSubstrings(self, s: str) -> int: | ||
count=0 | ||
for i in range(len(s)): | ||
j=1 | ||
count+=1 | ||
while i-j>=0 and i+j<len(s) and s[i-j]==s[i+j]: | ||
count+=1 | ||
j+=1 | ||
for i in range(1,len(s)): | ||
if s[i]==s[i-1]: | ||
count+=1 | ||
j=1 | ||
while i - j -1 >= 0 and i + j < len(s) and s[i - j -1] == s[i + j]: | ||
count += 1 | ||
j+=1 | ||
return count | ||
s = Solution() | ||
print(s.countSubstrings('aaabbb')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution: | ||
def minSteps(self, n: int) -> int: | ||
if n<=1: | ||
return 0 | ||
dp=[i for i in range(n+1)] | ||
dp[0]=0 | ||
dp[1]=1 | ||
for i in range(2,n+1): | ||
for j in range(2,i): | ||
if i%j==0: | ||
dp[i]=min(dp[i],dp[j]+(i//j)) | ||
return dp[-1] | ||
s = Solution() | ||
print(s.minSteps(1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
from typing import List | ||
|
||
from leetcode.Q101_Symmetric_Tree import TreeNode | ||
|
||
|
||
class Solution: | ||
def generateTrees(self, n: int) -> List[TreeNode]: | ||
dp=[[[] for _ in range(n+1)] for _ in range(n+1)] | ||
for l in range(1,n+1): | ||
for i in range(1,n+1): | ||
j=l+i | ||
if j>=n+1: | ||
break | ||
current_result =[] | ||
for k in range(i,j+1): | ||
node = TreeNode(k) | ||
current_node_list =[node] | ||
if k>i: | ||
for m in range(len(dp[i][k])): | ||
left_child = dp[i][k][m] | ||
node.left=left_child | ||
if k<j: | ||
for m in range(len(dp[k][j])): | ||
pass | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
from typing import List | ||
|
||
from leetcode.Q101_Symmetric_Tree import TreeNode | ||
|
||
import copy | ||
class Solution: | ||
def generateTrees(self, n: int) -> List[TreeNode]: | ||
if n==0: | ||
return [] | ||
def dfs(l,r): | ||
if l>r: | ||
return [None] | ||
else: | ||
node_list=[] | ||
for i in range(l,r+1): | ||
left_Nodes = dfs(l,i-1) | ||
right_Nodes = dfs(i+1,r) | ||
for l_c in left_Nodes: | ||
for r_c in right_Nodes: | ||
node = TreeNode(i) | ||
node.left=l_c | ||
node.right=r_c | ||
node_list.append(node) | ||
return node_list | ||
return dfs(1,n) | ||
s = Solution() | ||
print(s.generateTrees(3)) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Solution: | ||
def numTrees(self, n: int) -> int: | ||
if n<1: | ||
return 0 | ||
dp=[[0]*(n+1) for _ in range(n+1)] | ||
for i in range(1,n+1): | ||
dp[i][i]=1 | ||
for l in range(1,n+1): | ||
for i in range(1,n+1): | ||
j=i+l | ||
if j>=n+1: | ||
break | ||
if l==1: | ||
dp[i][j] = dp[i][j-1] + dp[i+1][j] | ||
else: | ||
for k in range(i,j+1): | ||
if k==i: | ||
dp[i][j]=dp[i][j]+dp[k+1][j] | ||
elif k==j: | ||
dp[i][j]=dp[i][j]+dp[i][k-1] | ||
else: | ||
dp[i][j]=dp[i][j]+dp[i][k-1]*dp[k+1][j] | ||
return dp[1][-1] | ||
s = Solution() | ||
print(s.numTrees(4)) |