[LeetCode] Best Time to Buy and Sell

 

1. ๋ฌธ์ œ : Link

๊ฐ€์žฅ ์‹ผ ๊ฐ’์— ์‚ฌ์„œ ๊ฐ€์žฅ ๋น„์‹ผ ๊ฐ’์— ํŒŒ๋Š” ๋ฒ•์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

 

2. ํ’€์ด

๋ฆฌ์ŠคํŠธ ์ˆœํšŒํ•˜๋ฉฐ min๊ฐ’๊ณผ max๊ฐ’์˜ ์ฐจ์ด return

 

3. ์ฝ”๋“œ

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        num = 10001
        maxx = 0
        result = 0
        for p in prices:
            if num > p:
                num = p
            else:
                result = p - num
                if maxx < result:
                    maxx = result
        return maxx

'๐Ÿ’ป Coding Problems Solving > Array | String | Loop' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] Product of Array Expect Self  (0) 2022.07.09
[LeetCode] Contains Duplicate  (0) 2022.07.09
[LeetCode] Two Sum  (0) 2022.06.30
[BOJ 1789] ์ˆ˜๋“ค์˜ ํ•ฉ  (0) 2022.06.26
[BOJ 1978] ์†Œ์ˆ˜์ฐพ๊ธฐ  (0) 2022.06.22
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ