贪心策略---买卖股票的最大收益 II
买卖股票的最大收益 II
122. Best Time to Buy and Sell Stock II (Easy)
题目描述:
可以进行多次交易,多次交易之间不能交叉进行,可以进行多次交易。
思路分析:
当访问到一个prices[i]且prices[i]-prices[i-1]>0,那么就把prices[i]-prices[i-1]添加到收益中。
代码:
public int maxProfit(int []prices){
    int profit=0;
    for(int i=1;i<prices.length;i++){
        if(prices[i]>prices[i-1]){
            profit=profit+(prices[i]-prices[i-1]);
        }
    }
    return profit;
}
贪心策略---买卖股票的最大收益 II的更多相关文章
- 买卖股票的最佳时机I II III IV
		I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ... 
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
		Say you have an array for which the ith element is the price of a given stock on day i. Design an al ... 
- 贪心——122.买卖股票的最佳时机II
		给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次 ... 
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
		Say you have an array for which the ith element is the price of a given stock on day i. Design an al ... 
- [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
		Say you have an array for which the ith element is the price of a given stock on day i. Design an al ... 
- lintcode:买卖股票的最佳时机 IV
		买卖股票的最佳时机 IV 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格. 设计一个算法来找到最大的利润.你最多可以完成 k 笔交易. 注意事项 你不可以同时参与多笔交易(你必须在再次 ... 
- [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 were ... 
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
		Say you have an array for which the ith element is the price of a given stock on day i. Design an al ... 
- 【LeetCode】122、买卖股票的最佳时机 II
		Best Time to Buy and Sell Stock II 题目等级:Easy 题目描述: Say you have an array for which the ith element i ... 
随机推荐
- ES基本原理
			Elasticsearch是基于一款高性能的.可扩展的信息检索工具库Lucene构建的强大的搜索引擎,在很多情况,它也被作为NoSql数据库并取得了很好的效果,下面介绍下ES的基本概念,映射到数据库的 ... 
- python使用etcd
			import sys import etcd client = etcd.Client( host='127.0.0.1', port=2379, allow_reconnect=True) clie ... 
- 手工实现hashset
			package cn.study.lu.four; import java.util.*; /** * 手工实现hashmap,加深理解底层原理 * @author Administrator * * ... 
- No module named flask 导包失败,Python3重新安装Flask模块
			在部署环境过程中,通过pip install -r requirements.txt安装包,结果启动项目时总是报错,显示没有flask模块,通过pip install flask还是不行,于是下载fl ... 
- spring security权限架架mvn坐标
			<!-- spring security start --> <dependency> <groupId>org.springframework.security& ... 
- linux nginx+php源码安装
			PHP安装 1)下载 wget http://cn2.php.net/distributions/php-5.6.30.tar.gz 2)解压 tar –xf php-5.6.30 3)进入目录 cd ... 
- Nginx 禁止IP访问 只允许域名访问
			今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案: Nginx的默认虚拟主机在用户 ... 
- DHCP服务器怎么设置怎么启动
			DHCP:动态主机配置协议,服务器用于为网络中的客户端自动分配IP地址.这种方法避免了由于手动配置IP地址导致的IP地址冲突问题,同时也减少了网络管理员的工作量. 工具/原料 在配置DHCP服务器时, ... 
- 牛客提高D3t2 点与面
			分析 对于每一个点只要维护它前面/后面的一小一大组合的数量 对于这个可以维护两个树状数组 然后从前往后/从后往前分别扫一遍相乘即可 代码 #include<iostream> #inclu ... 
- centos7 安装gdal2.3.1
			在直接源码安装gdal2.3时报错,大概意思是说没有安装SFCGAL. 1.centos更新cmake到3.5版本: wget https://cmake.org/files/v3.5/cmake-3 ... 
