File tree Expand file tree Collapse file tree 4 files changed +50
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 4 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxProfit (self , prices : List [int ]) -> int :
3+ """
4+ ์ง๊ธ๊น์ง ๋ณธ ๊ฐ๊ฒฉ ์ค ๊ฐ์ฅ ์์ ๊ฐ์ ๊ณ์ ์ ์ฅํ๊ณ
5+ ํ์ฌ ๊ฐ๊ฒฉ์์ ๊ทธ ์ต์๊ฐ์ ๋บ ๊ฐ์ผ๋ก ์ต๋ ์ด์ต์ ๊ฐฑ์ ํฉ๋๋ค.
6+ ํ ๋ฒ ์ํํ๋ฉด์ ์ต๋ profit์ ์ฐพ๋ ๋ฐฉ์์
๋๋ค
7+ """
8+ min_price = float ('inf' )
9+ max_profit = 0
10+
11+ for price in prices :
12+ min_price = min (min_price , price )
13+ max_profit = max (max_profit , price - min_price )
14+
15+ return max_profit
16+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def containsDuplicate (self , nums : List [int ]) -> bool :
3+ """
4+ nums๋ฅผ set์ผ๋ก ๋ฐ๊ฟจ์ ๋ ๊ธธ์ด๊ฐ ์ค์ด๋ค๋ฉด ๋ฐฐ์ด ์์ ์ค๋ณต ๊ฐ์ด ์๋ค๋ ์๋ฏธ์
๋๋ค.
5+ """
6+ if len (nums ) != len (set (nums )):
7+ return True
8+ else :
9+ return False
10+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def maxSubArray (self , nums : List [int ]) -> int :
3+ """
4+ ํ์ฌ๊น์ง ์ด์ด์ ๋ง๋ ๋ถ๋ถํฉ์ด ์์๋ฉด ๋ฒ๋ฆฌ๊ณ ๋ค์ ์์ํ์ฌ ๊ฐฑ์ ํ๋ ๊ฒ์
๋๋ค.
5+ """
6+ now_sum = nums [0 ]
7+ max_sum = nums [0 ]
8+
9+ for num in nums [1 :]:
10+ now_sum = max (num , now_sum + num )
11+ max_sum = max (max_sum , now_sum )
12+ return max_sum
13+
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
3+ """
4+ nums๋ฅผ ์์์๋ถํฐ ํ๋์ฉ ๋ณด๋ฉด์ ํ์ฌ ๊ฐ(nums[i])๊ณผ ๋ํด์ target์ด ๋๋ ๊ฐ์ด ๋ค์ชฝ ๋ฐฐ์ด์ ์๋์ง ํ์ธํฉ๋๋ค.
5+ ์์ผ๋ฉด ๊ทธ ๊ฐ์ index๋ฅผ ์ฐพ์์ ๊ฐ์ด ๋ฐํํฉ๋๋ค.
6+ """
7+ dp = []
8+ for i in range (len (nums )):
9+ if (target - nums [i ]) in nums [i + 1 :]:
10+ return [i , nums [i + 1 :].index (target - nums [i ]) + i + 1 ]
11+
You canโt perform that action at this time.
0 commit comments