leetcode — best-time-to-buy-and-sell-stock-ii
/**
* Source : https://oj.leetcode.com/problems/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 BestTimeToBuyAndSellStock2 {
/**
* 找出最大可能的收益
* 可以交易多次,但是同一时间手中只能有一只股票
* 只要第二天的价格高于第一天,那么就可以在第一天买入,第二天卖出,这样就能保证每次赚钱,而且能赚到整个时间段内所有可能的利润
*
* @param prices
* @return
*/
public int maxProfit (int[] prices) {
int result = 0;
for (int i = 1; i < prices.length; i++) {
result += prices[i] > prices[i-1] ? prices[i] - prices[i-1] : 0;
}
return result;
}
public static void main(String[] args) {
BestTimeToBuyAndSellStock2 bestTimeToBuyAndSellStock2 = new BestTimeToBuyAndSellStock2();
System.out.println(bestTimeToBuyAndSellStock2.maxProfit(new int[]{1,2,3,4,5}));
System.out.println(bestTimeToBuyAndSellStock2.maxProfit(new int[]{-2,1,3,5,-9,1,2}));
}
}
leetcode — best-time-to-buy-and-sell-stock-ii的更多相关文章
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- [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 al ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- [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 al ...
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- LeetCode——Best Time to Buy and Sell Stock II
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
随机推荐
- 使用handler倒计时
package com.example.jikangwang.myapplication; import android.content.Intent; import android.os.Handl ...
- Hashlib加密,内置函数,安装操作数据库
hashlib模块的md5加密: md5同样的内容加密后是一样的 md5加密后是不可逆的.即能加密,没办法解密. 撞库: 只是针对简单的,因为同样的内容加密后是一样的,难一点就不行了. 登录密码:注册 ...
- web项目部署到服务器中浏览器中显示乱码
项目部署之后浏览器打开查看时页面乱码 这里可能需要修改一下tomcat配置文件,首先找到Tomcat的安装路径下的conf/server.xml文件,找到之后可以CTRL+F搜索如下的内容: < ...
- leetcode-求众数
题目:求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3 ...
- 最新鲜最详细的Android SDK下载安装及配置教程
//来源: http://www.cnblogs.com/summary-2017/p/8073225.html 最近Neo突发神经,想要将学过的一些计算机视觉.机器学习中的算法都放到移动设备上去跑跑 ...
- ubuntu amd64 的锐捷连接解决办法---武汉大学
昨日博主闲来弄了个ubuntu玩玩,于是上网成了个问题,博主武大信息学部,锐捷上校园网.装的是13.04的amd64. 凑巧在珞珈山水bbs上看到我在解决上网出现问题出现的相同情况,但是没有人回答,于 ...
- Ubuntu 16.04下sublime text3安装
安装方法 在确保Ubuntu更新了国内镜像源的前提下,使用ppa安装: sudo add-apt-repository ppa:webupd8team/sublime-text-3 sudo apt- ...
- 基于 TensorFlow 在手机端实现文档检测
作者:冯牮 前言 本文不是神经网络或机器学习的入门教学,而是通过一个真实的产品案例,展示了在手机客户端上运行一个神经网络的关键技术点 在卷积神经网络适用的领域里,已经出现了一些很经典的图像分类网络,比 ...
- 【转载】看StackOverflow如何用25台服务器撑起5.6亿的月PV
问答社区网络 StackExchange 由 100 多个网站构成,其中包括了 Alexa 排名第 54 的 StackOverflow.StackExchang 有 400 万用户,每月 5.6 亿 ...
- Win10 安装 VMWare中 MAC OS X的安装,VMWare tools的配置与iOS的Helloworld
iOS的开发必须在MAC OS X系统下进行,这很蛋疼,现在MACBOOK动不动就上千上万大洋,这足够买台配置怪兽了,好吗?然而,我们是可以通过在VMWare中安装MAC OS X进行iOS开发的.对 ...