题解 | #买卖股票的最好时机(二)#

买卖股票的最好时机(二)

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);
    }
}
全部评论

相关推荐

Twilight_m...:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务