返回介绍

solution / 3000-3099 / 3010.Divide an Array Into Subarrays With Minimum Cost I / README_EN

发布于 2024-06-17 01:02:58 字数 4293 浏览 0 评论 0 收藏 0

3010. Divide an Array Into Subarrays With Minimum Cost I

中文文档

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_.

 

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

Solutions

Solution 1: Traverse to Find the Smallest and Second Smallest Values

We set the first element of the array $nums$ as $a$, the smallest element among the remaining elements as $b$, and the second smallest element as $c$. The answer is $a+b+c$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

class Solution:
  def minimumCost(self, nums: List[int]) -> int:
    a, b, c = nums[0], inf, inf
    for x in nums[1:]:
      if x < b:
        c, b = b, x
      elif x < c:
        c = x
    return a + b + c
class Solution {
  public int minimumCost(int[] nums) {
    int a = nums[0], b = 100, c = 100;
    for (int i = 1; i < nums.length; ++i) {
      if (nums[i] < b) {
        c = b;
        b = nums[i];
      } else if (nums[i] < c) {
        c = nums[i];
      }
    }
    return a + b + c;
  }
}
class Solution {
public:
  int minimumCost(vector<int>& nums) {
    int a = nums[0], b = 100, c = 100;
    for (int i = 1; i < nums.size(); ++i) {
      if (nums[i] < b) {
        c = b;
        b = nums[i];
      } else if (nums[i] < c) {
        c = nums[i];
      }
    }
    return a + b + c;
  }
};
func minimumCost(nums []int) int {
  a, b, c := nums[0], 100, 100
  for _, x := range nums[1:] {
    if x < b {
      b, c = x, b
    } else if x < c {
      c = x
    }
  }
  return a + b + c
}
function minimumCost(nums: number[]): number {
  let [a, b, c] = [nums[0], 100, 100];
  for (const x of nums.slice(1)) {
    if (x < b) {
      [b, c] = [x, b];
    } else if (x < c) {
      c = x;
    }
  }
  return a + b + c;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文