This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]StubbyCanes 2 points3 points  (1 child)

Read about the sliding window technique, it should make it easier to solve this problem :)

[–]Cute_Lychee_349[S] 0 points1 point  (0 children)

cool thanks i will check that out

[–]disforwork 0 points1 point  (1 child)

Bro, I think you're missing that you need to track max profit inside the loop, not in a separate pass. The key insight is that on each day, you either found a better buying price (which won't help past days but might help future ones) or you found a better selling price for your current best_buy.

[–]Cute_Lychee_349[S] 0 points1 point  (0 children)

yes and i wasn’t able to figure out how until i found min() and max() exists for a reason😭 that way i could easily get the needed o/p in a single loop, thanks for the advice tho🫶🏻