주식을 사고팔기 가장 좋은 시점
https://leetcode.com/problems/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 stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
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. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
- 1 <= prices.length <= 105
- 0 <= prices[i] <= 104
풀이 1. 브루트 포스로 계산
def maxProfit(self prices: List[int]) -> int:
max_price = 0
for i, price in enumerate(prices):
for j in range(i, len(prices)):
max_price = max(pricees[j] - price, max_price)
return max_price
'Python > 코딩테스트' 카테고리의 다른 글
[Leetcode] 344.Reverse String (0) | 2021.06.24 |
---|---|
[Leetcode] 238. Product of Array Except Self (0) | 2021.06.23 |
[Leetcode] 125.Valid Palindrome (0) | 2021.06.22 |
[Leetcode] 42.Trappling Rainbow Water (0) | 2021.06.17 |
[Leetcode] 1. Tow Sum (0) | 2021.06.16 |