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

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

http://www.nowcoder.com/practice/351b87e53d0d44928f4de9b6217d36bb

import java.util.; import java.io.; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = null; while((str = in.readLine()) != null){ int n = Integer.parseInt(str); String[] arr = in.readLine().split(" "); int[] res = new int[n]; for(int i = 0;i < n;i++){ res[i] = Integer.parseInt(arr[i]); } if(n ==1){ System.out.println(0); continue; } if(n == 2){ if(res[1] > res[0]){ System.out.println(res[1] - res[0]); continue; }else{ System.out.println(0); continue; } } int[] dest = new int[n]; dest[0] = 0; dest[1] = res[1] - res[0]; int max = dest[1]; for(int i = 2;i < n;i++){ dest[i] = Math.max(res[i] - res[i-1],res[i] - res[i-1]+dest[i-1]); if(dest[i] > max){ max = dest[i]; } } if(max > 0){ System.out.println(max); }else{ System.out.println(0); } } } }

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

04-17 18:32
门头沟学院 Java
野猪不是猪🐗:他跟你一个学校,你要是进来之后待遇比他好,他受得了?
点赞 评论 收藏
分享
03-25 19:00
东北大学 Java
程序员牛肉:太好了,是聊天记录。不得不信了。 当个乐子看就好,不要散播焦虑
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务