Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03ebc74

Browse files
ybian19azl397985856
authored andcommitted
feat: 121.best-time-to-buy-and-sell-stock update Python3 implementation (azl397985856#81)
1 parent 442151d commit 03ebc74

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

‎problems/121.best-time-to-buy-and-sell-stock.md‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
4040

4141
## 代码
4242

43-
语言支持:JS,Python,C++
43+
语言支持:JS,Python,C++
4444

4545
JS Code:
4646

@@ -60,31 +60,31 @@ JS Code:
6060
*
6161
* Say you have an array for which the i^th element is the price of a given
6262
* stock on day i.
63-
*
63+
*
6464
* If you were only permitted to complete at most one transaction (i.e., buy
6565
* one and sell one share of the stock), design an algorithm to find the
6666
* maximum profit.
67-
*
67+
*
6868
* Note that you cannot sell a stock before you buy one.
69-
*
69+
*
7070
* Example 1:
71-
*
72-
*
71+
*
72+
*
7373
* Input: [7,1,5,3,6,4]
7474
* Output: 5
7575
* Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit
7676
* = 6-1 = 5.
7777
* Not 7-1 = 6, as selling price needs to be larger than buying price.
78-
*
79-
*
78+
*
79+
*
8080
* Example 2:
81-
*
82-
*
81+
*
82+
*
8383
* Input: [7,6,4,3,1]
8484
* Output: 0
8585
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
86-
*
87-
*
86+
*
87+
*
8888
*/
8989
/**
9090
* @param {number[]} prices
@@ -111,28 +111,19 @@ var maxProfit = function(prices) {
111111
Python Code:
112112

113113
```python
114-
# 应用Kadane's algorithms
115114
class Solution:
116115
def maxProfit(self, prices: 'List[int]') -> int:
117-
"""
118-
step by step
119-
"""
120-
# error case
121-
if len(prices) < 1:
122-
return 0
123-
124-
# caluate the daily gains, break into a subarray problem
125-
gains = [prices[i]-prices[i-1] for i in range(1, len(prices))]
126-
127-
loc_max = global_max = 0 #not gains[0] in case of negative
128-
for i in range(len(gains)):
129-
loc_max = max(loc_max + gains[i], gains[i])
130-
if loc_max > global_max:
131-
global_max = loc_max
132-
"""
133-
Runtime: 48 ms, faster than 34.50% of Python3 online submissions for Best Time to Buy and Sell Stock.
134-
Memory Usage: 14.1 MB, less than 10.26% of Python3 online submissions for Best Time to Buy and Sell Stock.
135-
"""
116+
if not prices: return 0
117+
118+
min_price = float('inf')
119+
max_profit = 0
120+
121+
for price in prices:
122+
if price < min_price:
123+
min_price = price
124+
elif max_profit < price - min_price:
125+
max_profit = price - min_price
126+
return max_profit
136127
```
137128

138129
C++ Code:
@@ -164,4 +155,3 @@ public:
164155
165156
- [122.best-time-to-buy-and-sell-stock-ii](./122.best-time-to-buy-and-sell-stock-ii.md)
166157
- [309.best-time-to-buy-and-sell-stock-with-cooldown](./309.best-time-to-buy-and-sell-stock-with-cooldown.md)
167-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /