https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Best Time to Buy and Sell Stock - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
풀이 과정
각 날짜별로 주식의 가격이 주어지는데, 싸게 사서 비싸게 팔때의 최대 이익을 구하는 문제이다.
각 날짜에서 현재 가격 - 가장 싸게 살수 있던 가격을 계산하면서 계산한 값중 가장 큰 값을 반환하면 된다.
소스 코드
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = 10001
profit = 0
for price in prices:
min_price = min(price, min_price)
profit = max(profit, price - min_price)
return profit
'알고리즘 문제 풀이 > 리트코드' 카테고리의 다른 글
LeetCode - 206. Reverse Linked List [Python] (0) | 2022.07.27 |
---|---|
LeetCode - 234. Palindrome Linked List [Python] (0) | 2022.07.23 |
LeetCode - 238. Product of Array Except Self [Python] (0) | 2022.07.22 |
LeetCode - 561. Array Partition [Python] (0) | 2022.07.22 |
LeetCode - 15. 3Sum [Python] (0) | 2022.07.22 |