返回数组的最大总和

发布于 2025-02-11 05:00:58 字数 265 浏览 1 评论 0原文

我很难为以下问题找到解决方案。 “给定一个整数数组A,您将数组(连续的)子阵列划分为长度为k。分区后,每个子阵列都会更改其值,以成为子阵列的最大值。

这些子阵列将用于创建一个新的数组在分区时,新数组的总和应具有最大的

示例 。 输入:a = [1、15、7、9、2、5、10],k = 3

输出:newarray = [15、15、15、15、9、10、10、10]

一种可能的解决方案是尝试所有可能的解决方案分区并找到最高总和。但是我正在寻找更好的解决方案。

I have trouble to find a solution for the following question.
"Given an integer array A, you partition the array in (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of the subarray.

These subarrays will be used to create a new array in the order when they are partitioned. The sum of the new array should have the maximum value.

Example:
Input: A = [1, 15, 7, 9, 2, 5, 10], k = 3

Output: newArray = [15, 15, 15, 9, 10, 10, 10]

One possible solution is to try all possible partitions and find the max sum. But I am looking for a better solution.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2025-02-18 05:00:58

一个可选的实现是创建一个词典,该字典存储在分区中的第一个值,如果下一个值大于存储的值,请删除字典中的一个值,直到分区结束为止。并重复所有分区。

A posible implementation is to create a dictionary that stores the first value in the partition and if the next value is greater than the one stored, get rid of the one in the dictionary until the end of the partition. And repeat this for all partitions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文