Skip to content

Commit abeb834

Browse files
solve #3010: Divide an Array Into Subarrays With Minimum Cost I in java
1 parent 05a620a commit abeb834

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@
882882
| 2996 | [Smallest Missing Integer Greater Than Sequential Prefix Sum](https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum) | [![Java](assets/java.png)](src/SmallestMissingIntegerGreaterThanSequentialPrefixSum.java) | |
883883
| 3000 | [Maximum Area of Longest Diagonal Rectangle](https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle) | [![Java](assets/java.png)](src/MaximumAreaOfLongestDiagonalRectangle.java) | |
884884
| 3005 | [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency) | [![Java](assets/java.png)](src/CountElementsWithMaximumFrequency.java) | |
885-
| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i) | | |
885+
| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i) | [![Java](assets/java.png)](src/DivideAnArrayIntoSubarraysWithMinimumCostI.java) | |
886886
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i) | | |
887887
| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys) | | |
888888
| 3024 | [Type of Triangle](https://leetcode.com/problems/type-of-triangle) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class DivideAnArrayIntoSubarraysWithMinimumCostI {
6+
public int minimumCost(int[] array) {
7+
int smallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE;
8+
for (int i = 1 ; i < array.length ; i++) {
9+
if (array[i] < smallest) {
10+
secondSmallest = smallest;
11+
smallest = array[i];
12+
} else if (array[i] < secondSmallest) {
13+
secondSmallest = array[i];
14+
}
15+
}
16+
return array[0] + smallest + secondSmallest;
17+
}
18+
}

0 commit comments

Comments
 (0)