일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 쓰릴오브파이트
- 30분
- 스탭퍼
- Problem Solving
- 괜찮음
- 파비최
- 사이드
- 만화도
- 월간
- 10분
- 화상영어
- English
- 뭐든
- 3줄정리
- realclass
- 읽기
- 프로젝트
- 개발자
- leetcode
- 미드시청
- Writing
- 운동
- 영어공부
- Daily Challenge
- 링피트
- 잡생각
- 리얼 클래스
- FIT XR
- 영어원서읽기
- 매일
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.02.25 Today's Challenge 본문
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Best Time to Buy and Sell Stock - LeetCode
Can you solve this real interview question? Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosin
leetcode.com
n이 10^5라 O(n^2)은 TL 난다 =_=
알고 있었지만 시도 해봄. 역시나 TL
min_price와 max_profit 변수를 따로 둬서 풀면 되네..
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price, max_profit = float('inf'), 0
for price in prices:
if price < min_price:
min_price = price
else:
max_profit = max(max_profit, price - min_price)
return max_profit
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.02.27 Today's Challenge (0) | 2023.02.27 |
---|---|
2023.02.26 Today's Challenge (0) | 2023.02.26 |
2023.02.24 Today's Challenge (0) | 2023.02.24 |
2023.02.23 Today's Challenge (0) | 2023.02.23 |
2023.02.22 Today's Challenge (0) | 2023.02.22 |
Comments