Skip to main content
Back to problems
#3010
Easy Algorithms

Divide an array into subarrays with minimum cost i

Array Sorting Enumeration
80.5% acceptance
Feb 25, 2026
523
26
You are given an array of integers nums of length n. The cost of an array is the value of its first element. For example, the cost of [1,2,3] is 1 while the cost of [3,4,1] is 3. You need to divide nums into 3 disjoint contiguous subarrays. Return the minimum possible sum of the cost of these subarrays.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_cost(nums: Vec<i32>) -> i32 {
    let mut rest = nums[1..].to_vec();
    rest.sort();
    nums[0] + rest[0] + rest[1]
  }
}