[Lincode] #149 买卖股票的最佳时机
来源:互联网 发布:淘宝的昵称怎么改手机 编辑:程序博客网 时间:2024/05/17 22:05
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。
样例
给出一个数组样例 [3,2,3,1,2], 返回 1
public class Solution { /* * @param prices: Given an integer array * @return: Maximum profit */ public int maxProfit(int[] prices) { // write your code here int re = 0;int start = Integer.MAX_VALUE;int end = Integer.MAX_VALUE;for (int n : prices) {if (n < start) {re = Math.max(re, end - start);start = n;end = n;} else if (n > end)end = n;}return Math.max(re, end - start); }}
阅读全文
0 0
- [Lincode] #149 买卖股票的最佳时机
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- lintcode-买卖股票的最佳时机-149
- LintCode-买卖股票的最佳时机
- 题目:买卖股票的最佳时机
- LintCode--买卖股票的最佳时机
- LintCode_149_买卖股票的最佳时机
- Lintcode:买卖股票的最佳时机
- 买卖股票的最佳时机 II
- 买卖股票的最佳时机 III
- 买卖股票的最佳时机|_1
- 买卖股票的最佳时机||_1
- 买卖股票的最佳时机,lintcode
- 【LintCode】买卖股票的最佳时机
- lintcode--买卖股票的最佳时机
- Lintcode 买卖股票的最佳时机
- android 华为手机导航栏适配
- SVN之svnadmin初始化配置-yellowcong
- 第一次安装centos7
- 关于学习javascript的笔记
- Linux(Ubuntu)下编译PJSIP2.7
- [Lincode] #149 买卖股票的最佳时机
- IDEA下创建Springboot,freemarker,mockserver,Gradle项目
- sshkey管理Linux系统的联网登录
- ITIL的服务目录管理流程的关键知识
- 顺序栈的压栈和出栈
- 百度语音REST API -- 简单Demo
- spark 集群启动后,worker 节点worker 进程一段时间后自动结束
- 编程题
- Java设计模式之责任链模式