https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Best Time to Buy and Sell Stock - LeetCode
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 choosing a different day in the future to sell that
leetcode.com
1
2
3
4
5
6
7
8
9
10
|
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit, min_price = 0, sys.maxsize
for price in prices:
min_price = min(min_price, price)
profit = max(profit, price-min_price)
return profit
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 1523. Count Odd Numbers in an Interval Range (0) | 2023.02.13 |
---|---|
leetcode 234. Palindrome Linked List (0) | 2023.02.11 |
leetcode 2149. Rearrange Array Elements by Sign (0) | 2023.02.10 |
leetcode 238. Product of Array Except Self (0) | 2023.02.10 |
leetcode 15. 3Sum (0) | 2023.02.09 |
댓글