[LeetCode] Divide an Array Into Subarrays With Minimum Cost I

3010. Divide an Array Into Subarrays With Minimum Cost I

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int minimumCost(vector<int>& nums) {
int res = 1e9, n = nums.size();
for(int i = 1; i < n; i++) {
for(int j = i + 1; j < n; j++) {
res = min(res, nums[0] + nums[i] + nums[j]);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/01/21/PS/LeetCode/divide-an-array-into-subarrays-with-minimum-cost-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.