본문 바로가기

알고리즘

[Algorithm] min-cost-climbing-stairs 문제

 

https://leetcode.com/problems/min-cost-climbing-stairs/

 

Min Cost Climbing Stairs - 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


처음에 풀었을 때

- 매개변수로 주어지는 cost 배열을 이용 안 하고 dp 테이블을 따로 만듬 

- 시간복잡도 올라감 

class Solution(object):
    def minCostClimbingStairs(self, cost):
        cost.append(0)
        n = len(cost)
        d = cost # 이 부분에서 급 올라간듯
        for i in range(len(cost) - 3, -1, -1):
            d[i] = cost[i] + min(d[i + 1], d[i + 2])
        print(min(d[0],d[1]))
        return min(d[0], d[1])

 


시간복잡도 줄인 방향 풀이 

- dp 테이블 따로 선언 안 하고 cost 배열 그대로 이용함

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

'알고리즘' 카테고리의 다른 글

[Algorithm] 217-Contains-Duplicate  (1) 2022.06.07
[Algorithm] 213-house-robber2  (0) 2022.05.27
[Algorithm] 편집 거리  (0) 2022.05.12
[Algorithm] 금광 문제  (0) 2022.04.27
[Algorithm] 1로 만들기  (0) 2022.04.25