我的数据如下:
Date Value
2011-01-01 1
2011-01-02 5
2011-01-03 30
.....
2012-01-01 4
我想计算数据中具有最大值总和的30天时段。
不确定我的问题是否有意义,因为我没有一个好的数学头,因此很难解释。
由于 史蒂夫
答案 0 :(得分:2)
这应该为你做。它假定基于零(0)的阵列系统。
sum <- 0 for i = 0 to 29 sum <- sum + value(i) max <- sum start <- 0 for i = 30 to value.lengh-1 sum <- sum - value(i-30) + value(i) if sum > max then max <- sum start <- i-29
此处max
包含30个连续值的最大总和,start
包含最长30天的起点。
答案 1 :(得分:1)
伪代码:
MaxStartDate = FirstDate
MaxTotal = -1 (something that is definitely below your possible total)
for n=0 to lastDate-30
{
tempMax = 0
for m=n to n+29
tempMax = tempMax + date(m)
if tempMax > MaxTotal
{
MaxTotal = tempMax
MaxStartDate = date(n)
}
}
当for循环结束时,MaxTotal将是您最高的30天总数,MaxStartDate将是30天内的第一天。
答案 2 :(得分:0)
for first 30 days in your data, compute the sum of all values
let this be called as max_sum
let start_date be the first date in the input data
Loop 'i' from 1 to (total dates - 30)
temp_sum = (temp_sum + value of date (30+i) - value of date (i))
if max_sum < temp_sum
max_sum = temp_sum
start_date= ith date
max_sum
的最终值是以start_date
为起点的任何30天内的最大值总和。