A more clear explanation on why sum of subarray works.:

Suppose we have original array:
[a0, a1, a2, a3, a4, a5, a6]

what we are given here(or we calculate ourselves) is:
[b0, b1, b2, b3, b4, b5, b6]

where,
b[i] = 0, when i == 0
b[i] = a[i] - a[i - 1], when i != 0

suppose if a2 and a6 are the points that give us the max difference (a2 < a6)
then in our given array, we need to find the sum of sub array from b3 to b6.

b3 = a3 - a2
b4 = a4 - a3
b5 = a5 - a4
b6 = a6 - a5

adding all these, all the middle terms will cancel out except two
i.e.

b3 + b4 + b5 + b6 = a6 - a2

a6 - a2 is the required solution.

so we need to find the largest sub array sum to get the most profit

  • 当数据库上有多个事务同时执行的时候,就可能出现藏独、不可重复度,幻读的问题,为了解决这些问题,就有了隔离级别的概念,在谈隔离级别之前,你首先要知道,你隔离的越严实,效率就会越低。
  • 说明:创建新表
    create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],…)
  • MySQL 最基本的SQL语法/语句
  • 回滚段包括很多回滚操作
    关于121.买卖股票

相关文章: