R 为什么这个最大的后续和代码可以工作?

R 为什么这个最大的后续和代码可以工作?,r,sum,subsequence,R,Sum,Subsequence,Rosetta代码具有以下任务,如下所述: 给定一个整数序列,找到一个使其元素之和最大化的连续子序列,也就是说,没有其他单个子序列的元素加起来的值大于这个值。空子序列的和被认为是0;因此,如果所有元素都为负,则结果必须是空序列 这不是一个非常复杂的问题,可以在十行或更少的时间内轻松解决。但是,我很困惑。我在下面复制了它: max.subseq <- function(x) { cumulative <- cumsum(x) min.cumulative.so.far <

Rosetta代码具有以下任务,如下所述:

给定一个整数序列,找到一个使其元素之和最大化的连续子序列,也就是说,没有其他单个子序列的元素加起来的值大于这个值。空子序列的和被认为是0;因此,如果所有元素都为负,则结果必须是空序列

这不是一个非常复杂的问题,可以在十行或更少的时间内轻松解决。但是,我很困惑。我在下面复制了它:

max.subseq <- function(x) {
  cumulative <- cumsum(x)
  min.cumulative.so.far <- Reduce(min, cumulative, accumulate=TRUE)
  end <- which.max(cumulative-min.cumulative.so.far)
  begin <- which.min(c(0, cumulative[1:end]))
  if (end >= begin) x[begin:end] else x[c()]
}

max.subseq让我们从
累积的结果开始分解它

  • min.cumulative.so.far
    将为您提供当前索引之前所有索引的最小
    cumulative
  • 在下一步中,我们计算序列中每个元素的最佳子序列的
    累积
    ,从
    min.cumulative.to.far
    到每个索引的值<代码>结束
  • 将是最佳子序列的结束,该子序列可以是1
  • 这就是有趣的地方:如果所有值都是负值,
    end
    将是1,
    cumulative[1:end]
    的所有值都是负值,因此
    which.min()
    的结果将是2,因为值小于0(索引1处的值)。这将导致
    begin
    大于
    end
    ,函数将返回一个空向量。否则,
    begin
    将被设置为索引
    end
    之前的最低值之后的第一个索引,正如我们已经确定的那样,该索引是基于
    累积
    在索引
    begin
    处的最大子序列的最后一个元素。这就是将
    0
    添加到向量的原因。如果相关的
    min.so.far
    为负值,则从向量中的下一个值开始。如果所有
    累积值均为正值,只需从开头开始(添加的
    0
    将是最低值)。这一步中发生的唯一一件事是实际查找索引,该索引的
    累计
    最小值为索引
    结束
  • 因此,回报微不足道。如上所述,如果所有值均为负值,则为空,否则从具有最大和的子序列的开始到结束
    x
    边缘大小写:所有值均为负值

    > x <-  -(1:10)
    > cumulative <- cumsum(x)
    > cumulative
     [1]  -1  -3  -6 -10 -15 -21 -28 -36 -45 -55
    > min.cumulative.so.far <- Reduce(min, cumulative, accumulate=TRUE)
    > min.cumulative.so.far
     [1]  -1  -3  -6 -10 -15 -21 -28 -36 -45 -55
    > end <- which.max(cumulative-min.cumulative.so.far)
    > end
    [1] 1
    > begin <- which.min(c(0, cumulative[1:end]))
    # c(0, cumulative[1:end]) is c(0, -1) in this case as 1:end = 1:1
    > begin
    [1] 2
    > if (end >= begin) x[begin:end] else x[c()]
    integer(0)
    
    > x <- c(1, 5, -9, 3, 7, 1, 2, 4, 5, -6)
    > cumulative <- cumsum(x)
    > cumulative
     [1]  1  6 -3  0  7  8 10 14 19 13
    > min.cumulative.so.far <- Reduce(min, cumulative, accumulate=TRUE)
    > min.cumulative.so.far
     [1]  1  1 -3 -3 -3 -3 -3 -3 -3 -3
    > end <- which.max(cumulative-min.cumulative.so.far)
    > end
    [1] 9
    > begin <- which.min(c(0, cumulative[1:end]))
    > begin
    [1] 4
    > if (end >= begin) x[begin:end] else x[c()]
    [1] 3 7 1 2 4 5
    
    >x累计
    [1]  -1  -3  -6 -10 -15 -21 -28 -36 -45 -55
    >最小累积到目前为止最小累积到目前为止
    [1]  -1  -3  -6 -10 -15 -21 -28 -36 -45 -55
    >结束
    [1] 1
    >开始
    [1] 2
    >if(end>=begin)x[begin:end]else x[c()]
    整数(0)
    
    标准案例

    > x <-  -(1:10)
    > cumulative <- cumsum(x)
    > cumulative
     [1]  -1  -3  -6 -10 -15 -21 -28 -36 -45 -55
    > min.cumulative.so.far <- Reduce(min, cumulative, accumulate=TRUE)
    > min.cumulative.so.far
     [1]  -1  -3  -6 -10 -15 -21 -28 -36 -45 -55
    > end <- which.max(cumulative-min.cumulative.so.far)
    > end
    [1] 1
    > begin <- which.min(c(0, cumulative[1:end]))
    # c(0, cumulative[1:end]) is c(0, -1) in this case as 1:end = 1:1
    > begin
    [1] 2
    > if (end >= begin) x[begin:end] else x[c()]
    integer(0)
    
    > x <- c(1, 5, -9, 3, 7, 1, 2, 4, 5, -6)
    > cumulative <- cumsum(x)
    > cumulative
     [1]  1  6 -3  0  7  8 10 14 19 13
    > min.cumulative.so.far <- Reduce(min, cumulative, accumulate=TRUE)
    > min.cumulative.so.far
     [1]  1  1 -3 -3 -3 -3 -3 -3 -3 -3
    > end <- which.max(cumulative-min.cumulative.so.far)
    > end
    [1] 9
    > begin <- which.min(c(0, cumulative[1:end]))
    > begin
    [1] 4
    > if (end >= begin) x[begin:end] else x[c()]
    [1] 3 7 1 2 4 5
    
    >x累计
    [1]  1  6 -3  0  7  8 10 14 19 13
    >最小累积到目前为止最小累积到目前为止
    [1]  1  1 -3 -3 -3 -3 -3 -3 -3 -3
    >结束
    [1] 9
    >开始
    [1] 4
    >if(end>=begin)x[begin:end]else x[c()]
    [1] 3 7 1 2 4 5
    
    是您的第二步,行
    结束修改了邮政s.t。第二步包括
    结束的计算。最重要的部分是步骤3中的变量
    begin
    。谢谢你的评论。