国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

121. Best Time to Buy and Sell Stock

2019-11-06 06:04:54
字體:
來源:轉載
供稿:網友

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.

Example 1:

Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]Output: 0In this case, no transaction is done, i.e. max profit = 0.

Subscribe to see which companies asked this question.

public class Solution {    public int maxProfit(int[] prices) {        if(prices.length==0)            return 0;        int best = 0;		int mins = prices[0];		for (int i = 1; i < prices.length; ++i) {			if (prices[i] < mins)				mins = prices[i];			else				best = best > (prices[i] - mins) ? best : (prices[i] - mins);		}		return best;            }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永宁县| 迁西县| 通江县| 红原县| 大邑县| 财经| 灵丘县| 湖口县| 那坡县| 屏南县| 读书| 临清市| 彭阳县| 新化县| 昭觉县| 周至县| 独山县| 鲁甸县| 邢台市| 大关县| 通化县| 峨眉山市| 龙南县| 昂仁县| 张家口市| 河源市| 阳高县| 依兰县| 漯河市| 凯里市| 玉龙| 长治县| 清涧县| 山西省| 天峻县| 自贡市| 呼伦贝尔市| 南投县| 那坡县| 始兴县| 蓝田县|