Solution 1

  • TimeO(n)
  • SpaceO(1)

where, n is the length of nums

Python · 2026-02-01
class Solution:
    '''
    Time Complexity: O(n)
    Space Complexity: O(1)
    where, n is the length of nums
    '''
    def minimumCost(self, nums: List[int]) -> int:
        a = 1 
        for i in range(2, len(nums)):
            if nums[i] < nums[a]:
                a = i
        b = 2 if a == 1 else 1
        for i in range(2, len(nums)):
            if nums[i] < nums[b] and i != a:
                b = i
        return nums[0] + nums[a] + nums[b]
Leet Code/python.py · L4923–4939