2020년/코테

[코테연습] Minimum Cost For Tickets #Python

위지원 2020. 8. 28. 16:47

문제 

 

https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3436/

 

Explore - LeetCode

LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.

leetcode.com

 

푸는데 걸린 시간 : 1시간 10분

문제 이해 잘못해서 30분을 날려먹었다 ㅇ. < 나는,, 저 날짜를 모두 포함해서 여행할 수 있도록 하게 해야하는 줄 알았다... 이렇게 다방면으로 멍청하기도 힘들것인데 

class Solution:
    def mincostTickets(self, days, costs) :
        ticketCosts = [0 for i in range(max(days)+1)]

        def existIdx(paramDay): #-idx 에러 방지용
            try:
                return ticketCosts[paramDay]
            except IndexError:
                return 0
        # 오늘 사고 + 3 case
        # 1. 1일전에 티켓을 샀다.+
        # 2. 7일전에 티켓을 샀다.
        # 3. 30일전에 티켓을 샀다.

        for day in range(1, len(ticketCosts)):
            if day not in days:
                ticketCosts[day] = ticketCosts[day-1]
                continue

            print("현재 날짜:",day,
                  "\n1일전에 살경우:",existIdx(day-1)+costs[0],
                  "\n7일전에 살경우:",existIdx(day-7)+costs[1],
                  "\n30일전에 살경우:",existIdx(day-30)+costs[2])

            ticketCosts[day] = min(existIdx(day-1)+costs[0],
                                   existIdx(day-7)+costs[1],
                                   existIdx(day-30)+costs[2])

        return ticketCosts[-1]

https://weejw.tistory.com/391?category=892552

 

[코테 연습] 라면 공장 Python

문제 설명 라면 공장에서는 하루에 밀가루를 1톤씩 사용합니다. 원래 밀가루를 공급받던 공장의 고장으로 앞으로 k일 이후에야 밀가루를 공급받을 수 있기 때문에 해외 공장에서 밀가루를 수입�

weejw.tistory.com

저 문제 풀때가 생각났다. 

그래서 저때 방법으로 또 과거를 되뇌이면서 그때 뭐가 좋았더라..하고 minimum 푸는 식으로 dp 사용 풀이를 했다.