博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Problem Best Time to Buy and Sell Stock I
阅读量:4322 次
发布时间:2019-06-06

本文共 656 字,大约阅读时间需要 2 分钟。

Problem Description:

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Solution:

1     public int maxProfit(int[] prices) { 2         if (prices.length <= 1) return 0; 3         int max = 0; 4         int low = prices[0]; 5         for (int i = 0; i < prices.length; i++){ 6             low = Math.min(low, prices[i]); 7             max = Math.max(max, prices[i] - low); 8         } 9         return max;10     }

 

转载于:https://www.cnblogs.com/liew/p/3815127.html

你可能感兴趣的文章
帝国cms修改栏目后文章列表的url错误怎么解决
查看>>
Linux下查看用户列表
查看>>
字符串左旋转操作
查看>>
PyQt5发布技巧:指定插件(plugins)路径
查看>>
centos 6.6编译安装nginx
查看>>
http接口调用
查看>>
进入OS前的两步之PendSV(任务切换)
查看>>
using-ef-code-first-with-an-existing-database
查看>>
关于h5页面内嵌到andriod时的webview在设置缩放问题
查看>>
echarts中间有字饼图Demo1
查看>>
java泛型的理解
查看>>
钩子教程 - 原理(十八) : LowLevelMouseProc
查看>>
C语言关键字 - 铁布衫:const 转载
查看>>
IE浏览器清浮动
查看>>
Ubuntu docker 使用命令 系列二
查看>>
在SharePoint 2010中使用托管元数据导航和关键词筛选器查找列表或库的内容
查看>>
学习MongoDB(二) Replica Set集群配置
查看>>
在只有一个网线的前提下,实现两个电脑之间的局域网通信(伽卡他卡电子教室通信)...
查看>>
maximun-depth-of-binary-tree
查看>>
jump game
查看>>