| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 29 |
| 30 |
Tags
- 잡생각
- 매일
- 화상영어
- 쓰릴오브파이트
- leetcode
- 운동
- 개발자
- 리얼 클래스
- 괜찮음
- 3줄정리
- 10분
- 스탭퍼
- 뭐든
- 30분
- Writing
- 링피트
- 파비최
- 만화도
- 읽기
- 프로젝트
- FIT XR
- Problem Solving
- 미드시청
- realclass
- English
- 영어원서읽기
- 월간
- Daily Challenge
- 영어공부
- 사이드
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