You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Divide an Array Into Subarrays With Minimum Cost I
4
4
sidebar_label: 3010 -Divide an Array Into Subarrays With Minimum Cost I
5
5
tags:
@@ -15,9 +15,7 @@ You are given an array of integers nums of length n.
15
15
16
16
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.
17
17
18
-
You need to divide nums into 3 disjoint contiguous
19
-
subarrays
20
-
.
18
+
You need to divide nums into 3 disjoint contiguous subarrays.
21
19
22
20
Return the minimum possible sum of the cost of these subarrays.
23
21
@@ -64,7 +62,7 @@ It can be shown that 12 is the minimum cost achievable.
64
62
65
63
### Intuition
66
64
67
-
cost = nums[0] + smallest ele + 2nd smallest ele
65
+
cost = nums[0] + smallest + 2nd smallest
68
66
69
67
Explanation
70
68
You have to divide array in 3 parts think of making 2 slice/cuts in array. No matter how you cut the array, first part of array will always come, cost of 1st part will be nums[0] and it will always be added. So nums[0]is fixed.
@@ -148,14 +146,6 @@ public:
148
146
149
147
#### Complexity Analysis
150
148
151
-
- Time complexity:
152
-
$O(n)$
153
-
We only traverse through the array once
154
-
155
-
- Space complexity:
156
-
$O(1)$
157
-
Since we just have 2 pointers
158
-
159
-
-**LeetCode Problem**: [ Count Elements With Maximum Frequency](https://leetcode.com/problems/find-beautiful-indices-in-the-given-array-i/description/)
149
+
- Time complexity: $O(n)$ We only traverse through the array once
0 commit comments