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 9c58516

Browse files
Update 12-00121-best-time-to-buy-and-sell-stock.py
1 parent c127f72 commit 9c58516

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

‎12-00121-best-time-to-buy-and-sell-stock.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121

2222
# SLIDING WINDOW:
2323
# Two pointers move in the same direction
24-
# one variable will be selling price and one will be buying
24+
# one variable will be selling price(r) and one will be buying(l)
2525
# only move selling price iteratively
26-
# moving buying price only when buying > selling -> jump it to current selling price
26+
# moving buying price only when minimum is found
2727
# Start at 0 and 1 index
2828
# maintain a profit variable to calculate max profit, initiating at 0
29-
# compare l and r -> if l > r, not point in calculating profit, so l will become r, and r will move 1 place further
30-
# if l < r, calculate profit and update the profit variable to get max profit, move r one place further
29+
# compare l and r -> if l < r, calculate profit and update the profit variable to get max profit, move r one place further
30+
# if l > r, profits are negative, so move l. Now, l can move 1 step, but there's no point in doing that
31+
# currently, r is at the minimum point and we want l to be at minimum
32+
# so we move l to r's place
33+
# increment r in any case
34+
# continue loop until r is out of bounds
35+
3136

3237
class Solution:
3338
def maxProfit(self, prices: List[int]) -> int:

0 commit comments

Comments
(0)

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