본문 바로가기
python-algorithm

leetcode 3010. Divide an Array Into Subarrays With Minimum Cost I

by 무적김두칠 2024. 1. 23.

https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

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

 

1
2
3
4
5
6
7
8
9
from itertools import combinations
class Solution:
    def minimumCost(self, nums: List[int]) -> int:
        answer = 99999
        com = combinations(nums[1:], 2)
        for case in com:
            answer = min(sum(case), answer)
        answer +=  nums[0]
        return answer
cs

0번째 인덱스 값은 무조건 고정이므로 나머지 위치에서 2개씩 뽑아서 그 인덱스 들이 subarray의 각각의 시작점이라고 

유도해서 구함

반응형

댓글