Link: 3010. Divide an Array Into Subarrays With Minimum Cost I - Easy
Track: Amazon Tag

Question

Restate

Given an integer array nums, we must choose exactly 3 elements to minimize the total cost, with the constraint that nums[0] must be included.
Return the minimum possible sum.

Edge Case

  • if len(nums) == 3, return sum
  • Negative number allowed
  • Duplicates allowed

Method 1 - Sort time is O()
Method 2 - Scan

Method 2 - Scan

Approach

We only need the smallest and second smallest values in nums[1:].

Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Code

from typing import List
 
class Solution:
    def minimumCost(self, nums: List[int]) -> int:
        min_a = min_b = float('inf')
 
        for num in nums[1:]:
            if num < min_a:
                min_b = min_a
                min_a = num
            elif num < min_b:
                min_b = num
 
        return nums[0] + min_a + min_b
 

History

  • Feb-18-2026 Peeked