-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (80 loc) · 2.67 KB
/
main.cpp
File metadata and controls
86 lines (80 loc) · 2.67 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Source: https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i
// Title: Divide an Array Into Subarrays With Minimum Cost I
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 **<button type="button" aria-haspopup="dialog" aria-expanded="false" aria-controls="radix-:r1k:" data-state="closed" class="">subarrays</button>.
//
// Return the **minimum** possible **sum** of the cost of these subarrays.
//
// **Example 1:**
//
// ```
// Input: nums = [1,2,3,12]
// Output: 6
// Explanation: The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
// The other possible ways to form 3 subarrays are:
// - [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
// - [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
// ```
//
// **Example 2:**
//
// ```
// Input: nums = [5,4,3]
// Output: 12
// Explanation: The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.
// It can be shown that 12 is the minimum cost achievable.
// ```
//
// **Example 3:**
//
// ```
// Input: nums = [10,3,1,1]
// Output: 12
// Explanation: The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
// It can be shown that 12 is the minimum cost achievable.
// ```
//
// **Constraints:**
//
// - `3 <= n <= 50`
// - `1 <= nums[i] <= 50`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <vector>
using namespace std;
// The first element must be chosen.
// The rest two must be the smallest numbers.
class Solution {
public:
int minimumCost(vector<int>& nums) {
nth_element(nums.begin() + 1, nums.begin() + 3, nums.end());
return nums[0] + nums[1] + nums[2];
}
};
// The first element must be chosen.
// The rest two must be the smallest numbers.
class Solution2 {
public:
int minimumCost(vector<int>& nums) {
int n = nums.size();
auto min1 = nums[1], min2 = nums[2];
if (min1 > min2) swap(min1, min2);
for (auto i = 3; i < n; ++i) {
auto num = nums[i];
if (num < min1) {
min2 = min1;
min1 = num;
} else if (num < min2) {
min2 = num;
}
}
return nums[0] + min1 + min2;
}
};