Skip to content

Commit 2afe779

Browse files
Merge pull request algorithm001#722 from ellie88/master
第四周
2 parents 9a19962 + 0419748 commit 2afe779

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Week_04/id_94/LeetCode_241_094.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
4+
class Solution(object):
5+
def diffWaysToCompute(self, input: str) -> List[int]:
6+
7+
list = []
8+
for i in range(len(input)):
9+
c = input[i]
10+
if c == "+" or c =="-" or c =="*" or c=="/":
11+
12+
left = self.diffWaysToCompute(input[:i])
13+
right = self.diffWaysToCompute(input[i+1:])
14+
15+
for le in left:
16+
for ri in right:
17+
if c == "+":
18+
list.append(int(le)+int(ri))
19+
elif c == "-":
20+
list.append(int(le)-int(ri))
21+
elif c == "*" :
22+
list.append(int(le)*int(ri))
23+
elif c == "/":
24+
list.append(int(le) / int(ri))
25+
26+
if len(list)==0:
27+
list.append(int(input))
28+
return list
29+
30+
31+
32+
if __name__=="__main__":
33+
result = Solution().diffWaysToCompute("11/2")
34+
print(result)

Week_04/id_94/LeetCode_455_094.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def findContentChildren( g: List[int], s: List[int]) -> int:
6+
7+
g = sorted(g)
8+
s = sorted(s)
9+
num = 0
10+
for v in g:
11+
for k in s:
12+
if v <= k:
13+
num+=1
14+
s.remove(k)
15+
break
16+
17+
return num
18+
19+
if __name__=="__main__":
20+
21+
22+
g= [262, 437, 433, 102, 438, 346, 131, 160, 281, 34, 219, 373, 466, 275, 51, 118, 209, 32, 108, 57, 385, 514, 439, 73,
23+
271, 442, 366, 515, 284, 425, 491, 466, 322, 34, 484, 231, 450, 355, 106, 279, 496, 312, 96, 461, 446, 422, 143,
24+
98, 444, 461, 142, 234, 416, 45, 271, 344, 446, 364, 216, 16, 431, 370, 120, 463, 377, 106, 113, 406, 406, 481,
25+
304, 41, 2, 174, 81, 220, 158, 104, 119, 95, 479, 323, 145, 205, 218, 482, 345, 324, 253, 368, 214, 379, 343, 375,
26+
134, 145, 268, 56, 206]
27+
s = [149, 79, 388, 251, 417, 82, 233, 377, 95, 309, 418, 400, 501, 349, 348, 400, 461, 495, 104, 330, 155, 483, 334,
28+
436, 512, 232, 511, 40, 343, 334, 307, 56, 164, 276, 399, 337, 59, 440, 3, 458, 417, 291, 354, 419, 516, 4, 370,
29+
106, 469, 254, 274, 163, 345, 513, 130, 292, 330, 198, 142, 95, 18, 295, 126, 131, 339, 171, 347, 199, 244, 428,
30+
383, 43, 315, 353, 91, 289, 466, 178, 425, 112, 420, 85, 159, 360, 241, 300, 295, 285, 310, 76, 69, 297, 155, 416,
31+
333, 416, 226, 262, 63, 445, 77, 151, 368, 406, 171, 13, 198, 30, 446, 142, 329, 245, 505, 238, 352, 113, 485, 296,
32+
337, 507, 91, 437, 366, 511, 414, 46, 78, 399, 283, 106, 202, 494, 380, 479, 522, 479, 438, 21, 130, 293, 422, 440,
33+
71, 321, 446, 358, 39, 447, 427, 6, 33, 429, 324, 76, 396, 444, 519, 159, 45, 403, 243, 251, 373, 251, 23, 140, 7,
34+
356, 194, 499, 276, 251, 311, 10, 147, 30, 276, 430, 151, 519, 36, 354, 162, 451, 524, 312, 447, 77, 170, 428, 23,
35+
283, 249, 466, 39, 58, 424, 68, 481, 2, 173, 179, 382, 334, 430, 84, 151, 293, 95, 522, 358, 505, 63, 524, 143,
36+
119, 325, 401, 6, 361, 284, 418, 169, 256, 221, 330, 23, 72, 185, 376, 515, 84, 319, 27, 66, 497]
37+
print(Solution.findContentChildren(g,s))

Week_04/id_94/LeetCode_714_094.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxProfit( prices: List[int], fee: int) -> int:
6+
n = len(prices)
7+
if n<2:
8+
return 0
9+
dp1 = [0 for _ in range(n)] #第i天手上有股票时的最大收益
10+
dp2 = [0 for _ in range(n)] #第i天手上无股票时的最大收益
11+
dp1[0] = -prices[0]
12+
for i in range(1,n):
13+
dp1[i] = max(dp1[i-1],dp2[i-1]-prices[i]) #昨天手上有股票与今天手上有股票的收益对比
14+
dp2[i] = max(dp2[i-1],dp1[i-1]+prices[i]-fee) #昨天手上 无股票的收益和今天手上无股票的收益对比
15+
return dp2[n-1]
16+
17+
18+
if __name__=="__main__":
19+
prices = [1, 3, 2, 8, 4, 9]
20+
fee = 2
21+
print(Solution.maxProfit(prices,fee))

0 commit comments

Comments
 (0)