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.

用一个数组A[n-1]来存储当天与前一天股票的差价,从而将该题转化为最大子序列和的问题。仅仅只是没有负值。最小为0罢了。

A[i]表示第i+1天股票价格与第i天的股票价格的差值。

public class Solution {public int maxsum(int [] num){int sum=0;int max=Integer.MIN_VALUE;for(int i=0;i<num.length;i++){sum+=num[i];if(max<sum) max=sum;if(sum<0) sum=0;}return max>0?max:0;}public int maxProfit(int[] prices) {if(prices.length==0||prices.length==1) return 0;int [] sub=new int [prices.length-1];for(int i=0;i<sub.length;i++){sub[i]=prices[i+1]-prices[i];}return maxsum(sub);}
}

转载于:https://www.cnblogs.com/brucemengbm/p/6901105.html

LeetCode 121 Best Time to Buy and Sell Stock相关推荐

  1. 【贪心 和 DP + 卖股票】LeetCode 121. Best Time to Buy and Sell Stock

    LeetCode 121. Best Time to Buy and Sell Stock Solution1:我的答案 动态规划和贪心不要区分的那么明显嘛~~~ class Solution { p ...

  2. Leetcode——121. Best Time to Buy and Sell Stock

    题目原址 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目描述 Say you have an ...

  3. leetcode 121.Best Time to Buy and Sell Stock 股票买卖最佳时间

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

  4. LeetCode 121. Best Time to Buy and Sell Stock

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

  5. LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)

    翻译 话说你有一个数组,其中第i个元素表示在第i天的股票价格.如果你被只被允许最多一次交易(例如,买入然后卖出一个股票),设计一个算法并找出最大利润. 原文 Say you have an array ...

  6. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  7. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  8. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  9. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

最新文章

  1. 逆元+费马小定理+扩展欧几里得
  2. python面向对象(part3)--继承
  3. jQuery、jQury UI、jQuery Mobile----读书笔记
  4. 菜鸟教程 之 JavaScript 教程
  5. vue页面渲染(请求返回的后端)data,文本换行与显示bug 的分析与解决提示
  6. python编程是干嘛的-编程小白必看!Python到底能做什么?
  7. MatConvNet安装问题
  8. 电脑检测不到硬盘原因 电脑检测不到硬盘怎么办
  9. 美化桌面软件测试面试题,软件测试面试题的经典逻辑题
  10. Java调用百度/高德地图API实现根据经纬度查地名
  11. Application loader:ERROR ITMS-90168: The binary you uploaded was invalid.
  12. Unity3D 制作游戏简单“跑马灯”功能
  13. 网络直播电视之寻找直播地址(下)
  14. Photoshop照片一键转换手绘效果图动作
  15. 国二c语言考试要点,全国计算机二级考试c语言考试要点
  16. 如何在Sqlserver 中创建非dbo 的表
  17. TIVA 123GXL的边沿计数模式测量低频PWM
  18. TCP FIN扫描探测原理
  19. 离线数仓 (十三) --------- DWD 层搭建
  20. 2020华为春招--N阶方阵旋转(python)

热门文章

  1. Mysql学习总结(14)——Mysql主从复制配置
  2. 【Ajax技术】使用XHR对象发送和接受数据
  3. 9点到17点半 cron_SpringQuartz定时任务的cron表达式书写
  4. 802.11b协议的一些介绍和说明
  5. Ubuntu下如何用Empathy上QQ
  6. python进行ocr识别图片上的中文
  7. 数据集神经网络共同进步
  8. matplotlib全图
  9. LeetCode39.组合总和 JavaScript
  10. 2018年『web』开发者不得不知的技术趋势