[NeetCode/Python] Buy and Sell Crypto

2024. 9. 19. 01:48·Algorithm/NeetCode

문제 링크: https://neetcode.io/problems/buy-and-sell-crypto

 

NeetCode

 

neetcode.io

 

투 포인터/슬라이딩 윈도우

 

[내 풀이]

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        maxProfit = 0
        for i in range(len(prices)-1):
            for j in range(i, len(prices)):
                profit = prices[j] - prices[i]
                if profit > maxProfit:
                    maxProfit = profit

        return maxProfit

 

비효율적인 코드.. 그냥 범위가 작아서 쓸수 있는 코드...

 

 

[솔루션]

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        
        lowest = prices[0]
        for price in prices:
            if price < lowest:
                lowest = price
            res = max(res, price - lowest)
        return res

 

 

시간 복잡도

n이 prices의 길이일 때,

내 코드: O(n^2)

솔루션: O(n)

'Algorithm > NeetCode' 카테고리의 다른 글

[NeetCode/Python] Duplicate Integer  (0) 2024.10.28
[NeetCode/Python] Meeting Schedule  (0) 2024.10.02
[NeetCode/Python] Min Cost Climbing Stairs  (0) 2024.09.19
[NeetCode/Python] Kth Largest Integer in a Stream  (0) 2024.09.03
[NeetCode/Python] Two Integer Sum  (0) 2024.08.31
'Algorithm/NeetCode' 카테고리의 다른 글
  • [NeetCode/Python] Duplicate Integer
  • [NeetCode/Python] Meeting Schedule
  • [NeetCode/Python] Min Cost Climbing Stairs
  • [NeetCode/Python] Kth Largest Integer in a Stream
빵빵0
빵빵0
(아직은) 공부하고 정리하는 블로그입니다.
  • 빵빵0
    Hack Your World
    빵빵0
  • 전체
    오늘
    어제
    • 분류 전체보기 (87)
      • Error Handling (7)
      • Project (5)
        • MEV (2)
      • Architecture (0)
        • API (0)
        • Cache (0)
        • 사소한 고민거리 (0)
      • Computer Science (4)
        • Data Structure (2)
        • Database (1)
        • Cloud (0)
        • OS (0)
        • Infra, Network (1)
        • AI (0)
      • Language (7)
        • Go (7)
        • Rust (0)
        • Python (0)
        • Java (0)
      • Algorithm (40)
        • BaekJoon (18)
        • Programmers (7)
        • LeetCode (6)
        • NeetCode (9)
      • SW Books (9)
        • gRPC Up & Running (1)
        • System Design Interview (2)
        • 스프링 입문을 위한 자바 객체지향의 원리와 이해 (6)
        • 블록체인 해설서 (0)
        • 후니의 쉽게 쓴 CISCO 네트워킹 (0)
      • BlockChain (4)
        • Issues (0)
        • Research (4)
        • Tech (0)
      • Own (5)
        • TIR(Today I Read) (0)
        • Personal (2)
        • Novel (0)
        • Memo (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    BFS
    NeetCode
    BaekJoon
    큐
    ethereum
    프로그래머스
    스택
    chart
    블록체인
    MongoDB
    BEAKJOON
    blockchain
    KBW
    golang
    context
    LeetCode
    Palindrome
    Programmers
    Hash Table
    MEV
    candlechart
    백준
    two pointer
    2024
    DP
    Greedy
    goroutine
    go
    EVM
    Python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
빵빵0
[NeetCode/Python] Buy and Sell Crypto
상단으로

티스토리툴바