-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode3010.java
More file actions
41 lines (36 loc) · 1.37 KB
/
LeetCode3010.java
File metadata and controls
41 lines (36 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class LeetCode3010 {
public static void main(String[] args) {
// 输入:nums = [1,2,3,12]
// 输出:6
System.out.println(new Solution3010().minimumCost(new int[] { 1, 2, 3, 12 }));
// 输入:nums = [5,4,3]
// 输出:12
System.out.println(new Solution3010().minimumCost(new int[] { 5, 4, 3 }));
// 输入:nums = [10,3,1,1]
// 输出:12
System.out.println(new Solution3010().minimumCost(new int[] { 10, 3, 1, 1 }));
// 输入:nums = [1,2,1]
// 输出:4
System.out.println(new Solution3010().minimumCost(new int[] { 1, 2, 1 }));
// 输入:nums = [1,6,1,5]
// 输出:7
System.out.println(new Solution3010().minimumCost(new int[] { 1, 6, 1, 5 }));
}
}
class Solution3010 {
public int minimumCost(int[] nums) {
int firstMinIndex = nums[1] > nums[2] ? 2 : 1;
int secondMinIndex = nums[1] > nums[2] ? 1 : 2;
for (int i = 3; i < nums.length; i++) {
if (nums[i] < nums[secondMinIndex]) {
if (nums[i] <= nums[firstMinIndex]) {
secondMinIndex = firstMinIndex;
firstMinIndex = i;
} else {
secondMinIndex = i;
}
}
}
return nums[0] + nums[firstMinIndex] + nums[secondMinIndex];
}
}