[NeetCode/Python] Min Cost Climbing Stairs

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

문제 링크: https://neetcode.io/problems/min-cost-climbing-stairs

 

NeetCode

 

neetcode.io

 

예전에 알고리즘 감을 살릴겸, 순차적으로 구하는 방법, 역순으로 구하는 방법 두 방법 모두를 사용해서 풀어보았다.

점점 이제 이지 난이도에서 벗어나야할텐데... 준비중이다!

 

 

[내 코드 1: 순차적 풀이]

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        n = len(cost)
        result = [0 for i in range(len(cost))]

        # cost.length >= 2
        result[0], result[1] = cost[0], cost[1]
        for i in range(2, n):
            result[i] = min(result[i-1], result[i-2]) + cost[i]
        
        return min(result[n-1], result[n-2])

 

 

[내 코드 2: 역순 풀이]

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        for i in range(len(cost)-3, -1, -1):
            cost[i] += min(cost[i + 1], cost[i + 2])
        
        return min(cost[0], cost[1])

 

시간 복잡도

두 풀이 모두 n이 cost 리스트의 길이일 때,

=> O(n)

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

[NeetCode/Python] Meeting Schedule  (0) 2024.10.02
[NeetCode/Python] Buy and Sell Crypto  (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
[NeetCode/Python] Plus One  (0) 2024.08.30
'Algorithm/NeetCode' 카테고리의 다른 글
  • [NeetCode/Python] Meeting Schedule
  • [NeetCode/Python] Buy and Sell Crypto
  • [NeetCode/Python] Kth Largest Integer in a Stream
  • [NeetCode/Python] Two Integer Sum
빵빵0
빵빵0
(아직은) 공부하고 정리하는 블로그입니다.
  • 빵빵0
    Hack Your World
    빵빵0
  • 전체
    오늘
    어제
    • 분류 전체보기 (92)
      • 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 (8)
        • Go (8)
        • 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 (8)
        • TIR(Today I Read) (3)
        • Personal (2)
        • Novel (0)
        • Memo (3)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
빵빵0
[NeetCode/Python] Min Cost Climbing Stairs
상단으로

티스토리툴바