|
21 | 21 |
|
22 | 22 | # SLIDING WINDOW: |
23 | 23 | # 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) |
25 | 25 | # 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 |
27 | 27 | # Start at 0 and 1 index |
28 | 28 | # 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 | + |
31 | 36 |
|
32 | 37 | class Solution: |
33 | 38 | def maxProfit(self, prices: List[int]) -> int: |
|
0 commit comments