@Hadev
하댑의 개발 기록
@Hadev
전체 방문자
오늘
어제
  • All categories (65)
    • 📒 Tech Note (56)
      • Flutter (0)
      • Unity C# (1)
      • 웹 프로그래밍 (12)
      • CS 기본기 뚝딱 (3)
      • 알고리즘 & 자료구조 (10)
      • DB (6)
      • Cloud (10)
      • DevOps (14)
    • 🔖 Story (9)
      • 💻 개발 언저리 공부 (4)
      • ⛵️ 취미 & 팁 (5)
      • 💸 재테크 (0)

인기 글

티스토리

hELLO · Designed By 정상우.
@Hadev
🚀 Hadev Tech Blog
ABOUT
TAG
GUESTBOOK
[LeetCode] 121. Best Time to Buy and Sell Stock
📒 Tech Note/알고리즘 & 자료구조

[LeetCode] 121. Best Time to Buy and Sell Stock

2022. 8. 20. 00:58

*알고리즘 스터디에 참여하면서 Blind 75 LeetCode Questions 목록에 있는 문제를 풀이합니다. 

문제: 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:
        if not prices:
            return 0

        maxProfit = 0
        minPurchase = prices[0]
        for i in range(1, len(prices)):		
            maxProfit = max(maxProfit, prices[i] - minPurchase)
            minPurchase = min(minPurchase, prices[i])
        return maxProfit

주식의 최저값을 첫번째 인자를 넣어주고, 배열을 하나씩 조회하면서
(현재 가격 - 최저값)과 수익값 중, 최대값을 수익값으로 보고 바꿔준다. 
현재 가격과 수익값 중 최저값을 수익값으로 보고 바꿔준다.

저작자표시 비영리 변경금지 (새창열림)

'📒 Tech Note > 알고리즘 & 자료구조' 카테고리의 다른 글

[LeetCode] 53. Maximum Subarray  (0) 2022.09.03
[LeetCode] 238. Product of Array Except Self  (0) 2022.08.24
[LeetCode] 217. Contains Duplicate  (0) 2022.08.24
[LeetCode] 1. Two Sum  (0) 2022.08.20
[선형 자료구조] 1. Array  (0) 2022.08.14
    '📒 Tech Note/알고리즘 & 자료구조' 카테고리의 다른 글
    • [LeetCode] 238. Product of Array Except Self
    • [LeetCode] 217. Contains Duplicate
    • [LeetCode] 1. Two Sum
    • [선형 자료구조] 1. Array
    @Hadev
    @Hadev

    티스토리툴바