发现很难理解两种 shot 和 k-shot 策略算法。这里又是一个问题:
Q1) arr 是一个长度为 n 的数组。计算 A[j0]-A[i0] + A[j1]-A[i1] 的最大值,条件是 i0 < j0 < i1 < j1
Ans)我们可以在 O(n) 中进行单次(即最大利润买卖股票)。我们可以应用相同的技术从 0..j 中找到最大值,从 j..n 中找到最大值。这将是 O(n2) 解决方案。
Elements of Programming interviews book suggests a way of doing this in O(n) time by:
doing a forward iteration and storing solution for A[0:j] such that 1<=j<=n-1 and then a backward iteration for A[j:n-1] such that 0<=j<=n-2 and then combining the two results. Does anyone have any idea how this can be done?
Q2)你会怎么做k-shot?
谢谢!!