题解 | #买卖股票的最好时机(二)#
买卖股票的最好时机(二)
http://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int pre = sc.nextInt(), max = 0;
//不用计算买入卖出时机, 只需要把所有的上涨区间加起来即可
//空间复杂度: O(1), 计算复杂度: O(n)
for ( int i=1; i<n; i++){
int current = sc.nextInt();
max += (pre < current ? current - pre : 0);
pre = current;
}
System.out.println(max);
}
}