gpt4 book ai didi

algorithm - 修改 LIST 使得一个元素大于之前所有元素的总和

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:05:38 26 4
gpt4 key购买 nike

Suppose an array = {2,5,7,8,10}. You need to find the length of Longest Increasing Sub-sequence such that a element is not less than the sum of all elements before it.

在这种情况下,答案可以是 {2,5,7}、{2,5,8} 或 {2,8,10}。所以长度 = 3
这在 O(n^2) 中很容易解决。由于 LIS 长度可以在 O(n log n) 中找到。由于问题只问长度,所以,我认为这个问题也可以在 O(n log n) 中解决。但是我该怎么做呢?

最佳答案

其实你根本不需要DP Solution。
先把数字按非降序排列。并从左到右循环。跟踪当前总和。
如果下一个数字不小于总和,则将其添加到 LIS。否则继续下一个数字。
可以证明贪心解是最优解。自己证明 ;)

关于algorithm - 修改 LIST 使得一个元素大于之前所有元素的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41314235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com