파비의 매일매일 공부기록

2023.12.28 Today's Challenge 본문

Problem Solving/LeetCode

2023.12.28 Today's Challenge

fabichoi 2023. 12. 28. 23:45

https://leetcode.com/problems/minimum-time-to-make-rope-colorful/

 

Minimum Time to Make Rope Colorful - LeetCode

Can you solve this real interview question? Minimum Time to Make Rope Colorful - Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon. Alice wants the rope to be colorful. She does

leetcode.com

완탐으로 푸는 문제

class Solution:
    def minCost(self, colors: str, neededTime: List[int]) -> int:
        total, i, j = 0, 0, 0

        while i < len(neededTime) and j < len(neededTime):
            cur_total, cur_max = 0, 0

            while j < len(neededTime) and colors[i] == colors[j]:
                cur_total += neededTime[j]
                cur_max = max(cur_max, neededTime[j])
                j += 1
            
            total += cur_total - cur_max
            i = j
        
        return total
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.12.30 Today's Challenge  (0) 2023.12.30
2023.12.29 Today's Challenge  (0) 2023.12.29
2023.12.27 Today's Challenge  (0) 2023.12.27
2023.12.26 Today's Challenge  (0) 2023.12.26
2023.12.25 Today's Challenge  (1) 2023.12.25
Comments