상위 150개 인터뷰쉬움

주식을 사고 팔기에 가장 좋은 시기

'주식을 사고 팔기에 가장 좋은 시기' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

prices 배열이 제공됩니다. 여기서 prices[i]i번째 날의 특정 주식 가격입니다.

특정 주식을 구매할 하루를 선택하고 해당 주식을 판매할 미래의 다른 날을 선택하여 수익을 극대화하려고 합니다.

이 거래에서 얻을 수 있는 최대 이익을 반환합니다. 이익을 얻을 수 없으면 0을(를) 반환하세요.

maxProfit(prices: List[int]) -> int 함수를 작성하세요.

제약
  • 1 <= len(prices) <= 10^5
  • 0 <= prices[i] <= 10^4

Example 1
Input
prices = [7, 1, 5, 3, 6, 4]
Output
5
Explanation

Buy on day 2 (price = 1) and sell on day 5 (price = 6). Profit = 6 - 1 = 5.

Example 2
Input
prices = [7, 6, 4, 3, 1]
Output
0
Explanation

No profitable transaction is possible since prices only decrease.

Need a Hint?
세트나 힙과 같은 슬라이딩 윈도우 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.