Skip to content

Commit 38de252

Browse files
committed
2 parents 1916e50 + b56d4c0 commit 38de252

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: 3010. Divide an Array Into Subarrays With Minimum Cost I
3+
description: 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.
4+
keywords:
5+
[
6+
LeetCode,
7+
3010. Divide an Array Into Subarrays With Minimum Cost I,
8+
Easy,
9+
Array,
10+
Sorting,
11+
Enumeration,
12+
]
13+
tags: [LeetCode, Easy, Array, Sorting, Enumeration]
14+
---
15+
16+
# [{frontMatter.title}](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i/)
17+
18+
import Tabs from "@theme/Tabs";
19+
import TabItem from "@theme/TabItem";
20+
import CodeBlock from "@theme/CodeBlock";
21+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
22+
import Solution from "!!raw-loader!./solution.js";
23+
import Description from "./_Description.md";
24+
import Examples from "./_Examples.md";
25+
26+
<DifficultyBadge difficulty="Easy" />
27+
28+
<Tabs>
29+
<TabItem value="description" label="題目描述" default>
30+
<details open>
31+
<summary>Description</summary>
32+
<Description />
33+
<Examples />
34+
</details>
35+
</TabItem>
36+
37+
<TabItem value="solution" label="解答">
38+
## Solution
39+
<CodeBlock language="js">{Solution}</CodeBlock>
40+
</TabItem>
41+
</Tabs>
42+
43+
## 解題思路
44+
45+
因為 nums 的第一個項目一定會包含在最終答案之中,所以第一步就是先加上 `nums[0]`
46+
接著移除 `nums[0]` 後,再對剩下來的進行排序
47+
排序後把最小和第二小再加進 `result` 就是答案了!
48+
49+
## 心得
50+
51+
好像有點鑽漏洞的解法
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
You are given an array of integers `nums` of length `n`.
2+
3+
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`.
4+
5+
You need to divide `nums` into `3` disjoint contiguous subarrays.
6+
7+
Return the minimum possible sum of the cost of these subarrays.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#### Example 1:
2+
3+
> **Input:** [1,2,3,12]
4+
> **Output:** 6
5+
> **Explanation:**
6+
> The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6.
7+
> The other possible ways to form 3 subarrays are:
8+
>
9+
> - [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
10+
> - [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.
11+
12+
#### Example 2:
13+
14+
> **Input:** [5,4,3]
15+
> **Output:** 12
16+
> **Explanation:** The best possible way to form 3 subarrays is: [5], [4],
17+
> and [3] at a total cost of 5 + 4 + 3 = 12.
18+
> It can be shown that 12 is the minimum cost achievable.
19+
20+
> #### Example 3:
21+
22+
> **Input:** [10,3,1,1]
23+
> **Output:** 12
24+
> **Explanation:** The best possible way to form 3 subarrays is: [10,3],
25+
> [1], and [1] at a total cost of 10 + 1 + 1 = 12.
26+
> It can be shown that 12 is the minimum cost achievable.
27+
28+
## Constraints:
29+
30+
- `1 <= dimensions.length <= 100`
31+
- `dimensions[i].length == 2`
32+
- `1 <= dimensions[i][0], dimensions[i][1] <= 100`
33+
34+
Input: nums =
35+
Output: 12
36+
Explanation:
37+
Example 3:
38+
39+
Input: nums = [10,3,1,1]
40+
Output: 12
41+
Explanation: The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.
42+
It can be shown that 12 is the minimum cost achievable.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var minimumCost = function (nums) {
6+
let result = 0;
7+
result += nums[0];
8+
nums.splice(0, 1);
9+
10+
nums.sort((a, b) => a - b);
11+
12+
result += nums[0] + nums[1];
13+
14+
return result;
15+
};

0 commit comments

Comments
 (0)