当前位置: 首页 > news >正文

LeetCode -- Best Time to Buy and Sell Stock II

题目描述:


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


Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).


就是给出一些天的股票价格,算出最大利润。要求:在卖掉当前手中股票之前不能买下一次的股票。


由于题目的这个要求,本题的难度就降低了。只需要在一次遍历中,判断下一次的价格是否比当前手里的价格高,高了就卖,算出差值就是利润,累加这个差值就可以了。


实现代码:






public class Solution {
    public int MaxProfit(int[] prices) {
        if(prices == null || prices.Length == 0){
		return 0;
	}
	
	var sum = 0;
	for(var i = 0; i < prices.Length - 1; i ++){
		sum += prices[i] < prices[i+1] ? prices[i+1]-prices[i] : 0;
	}
	
	return sum;
    }
}


相关文章:

  • 海闊天空 信樂團
  • Contains Duplicate III
  • LeetCode -- Combination Sum
  • MySQL添加用户
  • LeetCode -- Candy
  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • Azure 证书配置错误: The service configuration file does not provide the certificate identification
  • Linux精彩一句话最新版
  • LeetCode -- Count Complete Tree Node
  • LeetCode -- Isomorphic Strings
  • 【mysql】环境安装、服务启动、密码设置
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • conda常用的命令
  • CSS居中完全指南——构建CSS居中决策树
  • es6要点
  • iOS 颜色设置看我就够了
  • JavaScript的使用你知道几种?(上)
  • JSONP原理
  • leetcode388. Longest Absolute File Path
  • magento2项目上线注意事项
  • Making An Indicator With Pure CSS
  • MySQL几个简单SQL的优化
  • ng6--错误信息小结(持续更新)
  • python_bomb----数据类型总结
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 工作中总结前端开发流程--vue项目
  • 将 Measurements 和 Units 应用到物理学
  • 利用DataURL技术在网页上显示图片
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 新手搭建网站的主要流程
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 一天一个设计模式之JS实现——适配器模式
  • 转载:[译] 内容加速黑科技趣谈
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • gunicorn工作原理
  • kubernetes资源对象--ingress
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • $L^p$ 调和函数恒为零
  • (1)(1.9) MSP (version 4.2)
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (Java数据结构)ArrayList
  • (理论篇)httpmoudle和httphandler一览
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (强烈推荐)移动端音视频从零到上手(上)