Leetcode 刷题: Best Time to buy and sell stock
来源:互联网 发布:mac地址怎么查 编辑:程序博客网 时间:2024/05/12 12:58
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.
Subscribe to see which companies asked this question
Show Similar Problems
public class Solution {
public int maxProfit(int[] prices) {
//make decision for extreme conditions
if(prices.length==0 || prices == null)
return 0;
int profit=0, min=Integer.MAX_VALUE;
for(int i:prices){
min=(i < min)?i:min;
profit= (i-min)>profit ?(i-min):profit;
}
return profit;
}
}
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刷题【Array】 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
- 【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
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- jquery中使用detach 移除元素的使用场合
- LintCode算法题:Fibonacci Sequence
- POJ - 1961 Period
- 一道笔试题:字符串变形
- mongodb 主从配置、高可用配置
- Leetcode 刷题: Best Time to buy and sell stock
- Can you find it? (HDU_2141) 二分查找
- 正则 数字与小数的匹配
- OBJ-C类和对象学习笔记
- 找规律类博弈(3)
- KMP算法
- 最新版FusionCharts2D面积图
- mac osx 下 安装atom github官方出品的开发工具
- <Android HAL 之路> HAL 简介