买卖股票的最佳时机
来源:互联网 发布:人工智能四大平台 垄断 编辑:程序博客网 时间:2024/04/28 08:27
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。
样例
样例
给出一个数组样例 [3,2,3,1,2], 返回 1
import java.util.Scanner;/** * 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。样例给出一个数组样例 [3,2,3,1,2], 返回 1 * @author Dell * */public class Test149 { public static int maxProfit(int[] prices) { int max=0; for(int i=0;i<prices.length;i++) { int sum=0; for(int j=i+1;j<prices.length;j++) { if(prices[j]-prices[i]>sum) { sum=prices[j]-prices[i]; } } if(max<sum) max=sum; } return max; }public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();int[] a=new int[n];for(int i=0;i<a.length;i++){a[i]=sc.nextInt();}System.out.println(maxProfit(a));}}
阅读全文
0 0
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- 买卖股票的最佳时机
- LintCode-买卖股票的最佳时机
- 题目:买卖股票的最佳时机
- LintCode--买卖股票的最佳时机
- LintCode_149_买卖股票的最佳时机
- Lintcode:买卖股票的最佳时机
- 买卖股票的最佳时机 II
- 买卖股票的最佳时机 III
- 买卖股票的最佳时机|_1
- 买卖股票的最佳时机||_1
- 买卖股票的最佳时机,lintcode
- 【LintCode】买卖股票的最佳时机
- lintcode--买卖股票的最佳时机
- Lintcode 买卖股票的最佳时机
- 买卖股票的最佳时机-LintCode
- LintCode_149_买卖股票的最佳时机
- [leetcode]508. Most Frequent Subtree Sum
- JAVA学习笔记——List集合
- jquery/css实现步骤条
- 大端和小端
- SSH理论学习总结
- 买卖股票的最佳时机
- SSM框架
- ubuntu中开启、关闭防火墙
- Computer Vision for Autonomous Vehicles: Problems, Datasets and State-of-the-Art
- 23种设计模式(1):单例模式(Singleton Pattern)
- notifyDataSetChanged()无效原因
- 洛谷 P1051 谁拿了最多奖学金
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- Tornado应用笔记02-RequestHandler与Application