- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
大小为 N A [0...N-1] 的数组包含一些正整数。我需要递减某个元素的最少次数是多少,这样就没有两个元素(A[i] 和 A[j],i != j,A[i]>0,A[j]>0)差异 > M ?
到目前为止我的方法:
for(int i = N-1;i>=0;i--)
{
for(int j = 0;j<=i-1;j++)
{
while(A[i]-A[j]>M)
{
A[i]--;
ans++;
}
}
}
但是,这不是正确的解决方案。例如,
A = {3 2 1} 和 M = 0
最佳解决方案是将 A[2] 递减一次,将 A[0] 递减一次。
这使得数组 A = {2 2 0}
因为,A[2] = 0 我们可以忽略它,因为我们只担心非零元素。
但是,这段代码产生 ans = 3。
有什么解决方案吗?
最佳答案
这可以在 O(N log N) 时间内完成,如果数组已预排序,则为 O(N) 时间。
在伪代码中:
Given: A : array of ints, M = max difference
sort(A); //O(N log N) time
int start = end = 0; //this is a subsequence that we will move though the array
int sum_before = 0; //sum of all elements before our subsequence
int sum_after = sum_all(A); // sum of all elements after our subsequence -- O(N) time
int best_answer = sum_after; //we could always decrement everything to zero
for (start=0; start < A.length; ++start )
{
int maxval = A[start]+M; //A is sorted, so this never gets smaller
//extend end to find the longest subsequence starting
//at A[start] that we don't have to change
while( end < A.length && A[end]<=maxval)
{
//we can increment end at most A.length times, so
//this loop is O(N) in total for all iterations
sum_after-=A[end];
++end;
}
//if we leave everything between start and end unchanged
//the we'll need to decrement everything before to zero and
//everything after to maxval
int current_answer = sum_before + sum_after - (A.length - end)*maxval;
best_answer = min(best_answer, current_answer);
//next subsequence excludes A[start] -- it goes into the "before" sum
sum_before+=A[start];
}
关于arrays - 对于数组 A [0...N-1],在没有两个非零元素的差 > M 之前,我必须将元素递减多少次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491905/
所以我得到了这个形状为 (31641600,2) 的 numpy 数组,其中有一些零值,如果不是很多的话。 我们称这个数组为 X。 正在做: print len(X) >>> 31641600 然后做
TL;DR:我有一个 IBOutlet ( UILabel ) 在 Storyboard 中正确连接。访问(展开)它在 viewDidLoad() 中工作正常...但几秒钟后它的值为零。一个观察点说,
我想用numpy数组进行非零cumsum。只需跳过数组中的零并应用 cumsum 即可。假设我有一个 np.数组 a = np.array([1,2,1,2,5,0,9,6,0,2,3,0]) 我的结
该代码运行,甚至产生预期的输出。但是,在构建日志中,我总是得到进程终止,状态为 2(0 分钟,5 秒) 或该日志的某些变体。 代码- #include #include void main() { p
我读了一些关于 TCP window scaling 的东西和 BDP (不太清楚),我无法弄清楚到底是什么原因导致发送方的 TCP 实现设置非零 WS,用户模式客户端程序是否会以某种方式影响它?我认
我有一个字典 { 'a': 'a', 'b': 0, 'c': {} } 我需要省略所有具有空值的键(0 是不被认为是空的)。所以,它的输出应该是 { 'a': 'a', 'b': 0 }。 对于 {
我想在 python 中的 pandas 数据帧上应用 cumsum,但没有零。只是我想保留零并在数据帧上执行 cumsum 。假设我有这样的数据框: import pandas as pd df =
我使用 getimagesize 函数获取图像的宽度和高度,如下所示: list($width,$height) = getimagesize($source_pic); 如何使用 IF 条件来检查
在 PHP 中(使用内置函数)我想用小数转换/格式化数字,以便只显示非零小数。但是,我的另一个要求是,如果它是一个没有十进制值的数字,我仍然希望显示为零。例子: 9.000 -> 9.0 9.100
我可以使用 df['TOTAL'] = df.sum(axis=1) 向此 DF 添加一个 TOTAL 列,它会像这样添加行元素: col1 col2 TOTAL 0 1.0 5.0
我正在玩 python 列表,我想在不使用列表时从内存中删除它。(我有大数据列表,可能有数千或数百万个元素..数据类型-> 浮点型) 我试过这段代码,看看删除在 python 中是如何工作的(示例)
我正在研究 Linux 设备驱动程序代码。我无法透露这段代码的具体用途。我会尽力解释我的情况。当我们收到 USB 中断,表明有来自 USB 的数据时,下面的代码将在中断上下文中执行。数据将以 URB
能力 manpage比较长,有些东西我没有完全理解。 例如,决定我们是否有权访问 CAP_NET_RAW 的函数看起来如何? 输入: a = 有效 uid 为 0 b = 有一些真实的/保存的/任何为
我正在创建一个测试用例,用于测试 TVIroom 类的委托(delegate)函数 didDisconnectwithError 是否可以被调用。为此,我需要向委托(delegate)函数传递一个空白
我想知道 numpy.nonzero/numpy.flatnonzero 返回的索引顺序。 我在文档中找不到任何关于它的内容。它只是说: A[nonzero(flag)] == A[flag] 虽然在
假设我有这样的数据框 id p1 p2 p3 p4 1 0 9 0 4 2 0 0 0 4 3 1 3 10 7 4 1 5 3
这个问题在这里已经有了答案: Division of integers in Java [duplicate] (7 个答案) 关闭 9 年前。 我有一个音量控制 slider 来控制 Androi
我们正在运行一个名为 Axe 的工具检查 HTML 页面的有效性和 508 合规性/可访问性。 此错误作为违规出现: Elements should not have tabindex greater
所以我有两个这样的模型 class ModelParent include Mongoid::Document field :name, :type => String has_one :
我是一名优秀的程序员,十分优秀!