leetCode 系列:best time to buy and sell stock
来源:互联网 发布:网络骚扰电话怎么举报 编辑:程序博客网 时间:2024/05/19 03:43
由于最近在准备找工作,会刷写LeetCode的经典题目。因为最近编码能力实在不强,有些题目需要借鉴其他博主或者大神的解题思路,如有冒犯,请多原谅。如有错误,请指正,定会虚心接受并改正,不胜感激!
题目描述:
Say you have an array for which the i th element is the price of a given stock on dayi.
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
大体意思就是:有一只股票的某些天的某些价格波动,如例子所示:第一天价格7,第二天的价格是1,问哪一天买入,哪一天卖出获得的利润最大。从例子看,第二天买入,第五天卖出获利最大,即利润为6-1=5,第一天是7,第二天是1,但是不会第二天买第一天卖,所以只能用买入那天的后面几天做卖出操作才可以获利。
package Array;/** * Created by wuxiaosi on 2017/9/24. */public class MaxProfit { public static void main(String[] args) { int[] num = {5,1,2,3,6,14}; int p=0; for(int i=0;i<num.length;i++){ for(int j=i;j<num.length;j++){ if(num[j]-num[i]>p){ p=num[j]-num[i]; } } } System.out.print(p); }}
阅读全文
0 0
- 【Leetcode长征系列】Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock 系列
- LeetCode-Best Time to Buy and Sell Stock系列
- LeetCode Best Time to Buy and Sell Stock 系列
- LeetCode Week12: Best Time to Buy and Sell Stock系列
- leetCode 系列:best time to buy and sell stock
- LeetCode系列:best time to buy and sell stock
- leetcode Best Time to Buy and Sell Stock 系列
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- (java)Best Time to Buy and Sell Stock系列
- Best Time to Buy and Sell Stock 系列问题
- Best Time to Buy and Sell Stock系列问题题解
- Best Time to Buy and Sell Stock系列问题
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- 【玩转数据系列十五】机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码)
- React 组件间通信
- 深入理解泛型(二) 协变性和逆变性
- PHP email
- reverse digits of an integer
- leetCode 系列:best time to buy and sell stock
- 实现一个栈Stack,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值的操作)的时间复杂度为O(1)
- sendfile:Linux中的"零拷贝"
- PHP内核FastCGI探索之路
- 两个阿里云linux服务器之间传文件
- table表格网页布局的特点和影响
- 关于eclipse和myeclipse用同一个Tomcat时出现的问题
- 01_求给定字符串最长重复子串
- OpenGL实现相机视频NV21格式转RGB格式