Skip to content

Commit 8abe861

Browse files
committed
2020-03-08
1 parent 51055d5 commit 8abe861

File tree

6 files changed

+57
-21
lines changed

6 files changed

+57
-21
lines changed

0121.买卖股票的最佳时机/0121-买卖股票的最佳时机.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,9 @@ def maxProfit(self, prices):
44
:type prices: List[int]
55
:rtype: int
66
"""
7-
# dp[i][k][0] = max(dp[i ¨C 1][k][0], dp[I ¨C 1][k][1] + prices[i])
8-
# dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k][0] - prices[i])
9-
# µÚÒ»Ìâ k = 1
10-
11-
# dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i])
12-
# dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i])
13-
14-
# µ±i = 0ʱ£¬
15-
# dp[0][0] = 0
16-
# dp[0][1] = -prices[0]
17-
dp = [[0 for _ in range(2)] for _ in range(len(prices))]
18-
for i, price in enumerate(prices):
19-
if i == 0:
20-
dp[0][0] = 0
21-
dp[0][1] = -price
22-
else:
23-
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i])
24-
dp[i][1] = max(dp[i - 1][1], - prices[i])
25-
26-
return dp[len(prices) - 1][0] if prices else 0
27-
7+
res = 0
8+
pre_min = prices[0] if prices else 0
9+
for price in prices:
10+
res = max(res, price - pre_min)
11+
pre_min = min(pre_min, price)
12+
return res
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def reverseLeftWords(self, s, n):
3+
"""
4+
:type s: str
5+
:type n: int
6+
:rtype: str
7+
"""
8+
return s[n:] + s[:n]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
res = 0
8+
pre_min = prices[0] if prices else 0
9+
for price in prices:
10+
res = max(res, price - pre_min)
11+
pre_min = min(pre_min, price)
12+
return res
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def sumNums(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
return sum(range(1, n + 1))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def add(self, a, b):
3+
"""
4+
:type a: int
5+
:type b: int
6+
:rtype: int
7+
"""
8+
return sum([a, b])
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def constructArr(self, a):
3+
"""
4+
:type a: List[int]
5+
:rtype: List[int]
6+
"""
7+
left = [1 for _ in a]
8+
right = [1 for _ in a]
9+
for i in range(1, len(a)):
10+
left[i] = left[i - 1] * a[i - 1]
11+
12+
for i in range(len(a) - 2, -1, -1):
13+
right[i] = right[i + 1] * a[i + 1]
14+
15+
return [left[i] * right[i] for i in range(len(a))]
16+

0 commit comments

Comments
 (0)