Leetcode_122_Best Time to Buy and Sell Stock II

时间:2021-12-24 20:19:02

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43155725

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).

思路:

(1)题意为给定一个数组,数组中第i个元素的值对应着第i天的股票,你可以完成多次交易,但是每次交易只能买入一次并卖出,求进行多次交易所能得到的最大利润。该题为Best Time to Buy and Sell Stock的加强版。

(2)与Best Time to Buy and Sell Stock类似,该题同样考查的是最大差值。只不过该题考查数组中所有相邻且递增元素的数值之差的总和。只要第i+1天的值大于第i天的值,则可买入,求得利润(差值),遍历整个数组,得到所用差值之和即为总的利润。

(3)该题还是比较简单。希望对你有所帮助。

算法代码实现如下:

	/**
	 *
	 * @author liqq
	 */
	public int maxProfit(int[] x) {
		if (x == null || x.length <= 1)
			return 0;

		int min = x[0];
		int profit = 0;

		for (int i = 0; i < x.length; i++) {
			if (min > x[i]) {
				min = x[i];
			} else {
				profit += x[i] - min;
				min = x[i];
			}
		}
		return profit;
	}

Leetcode_122_Best Time to Buy and Sell Stock II的更多相关文章

  1. &lbrack;LintCode&rsqb; 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 ...

  2. LEETCODE —— Best Time to Buy and Sell Stock II &lbrack;贪心算法&rsqb;

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  3. 27&period; Best Time to Buy and Sell Stock &amp&semi;&amp&semi; Best Time to Buy and Sell Stock II &amp&semi;&amp&semi; Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  4. Leetcode-122 Best Time to Buy and Sell Stock II

    #122  Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...

  5. 【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 ...

  6. 31&period; leetcode 122&period; Best Time to Buy and Sell Stock II

    122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...

  7. LeetCode&colon; 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 ...

  8. 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 ...

  9. leetcode 121&period; Best Time to Buy and Sell Stock 、122&period;Best Time to Buy and Sell Stock II 、309&period; Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

随机推荐

  1. WPF系列&colon;画图

    Line 在两个坐标点之间画一条直线,通过四个属性设置它的起始和结束 <Line Stroke="Blue" StrokeThickness="3" X1 ...

  2. winform自定义按钮菜单

    //填写其他报表按钮        private void btnWriteRep_Click(object sender, EventArgs e)        {            try ...

  3. c&plus;&plus;父类指针强制转为子类指针后的测试(帮助理解指针访问成员的本质)&lpar;反多态&rpar;

    看下面例子: #include "stdafx.h" #include <iostream> class A {  //父类 public: void  f()   / ...

  4. 链路层三种类型的MAC地址

    若需要转载,请注明出处. 我们知道,链路层都是以MAC地址来进行通信双方的地址标识的,如下图:在应用中根据接收方的多寡来进行划分,可分为以下三种: 单播(Unicast) 多播(Multicast) ...

  5. UEFI安装Win10

    启动方式: Legency UEFI 分区表类型: MBR 主引导记录 最多4个主分区 最大支持2.1T硬盘 GPT 基于可扩展固件接口(EFI) 分区数不限 NTFS格式最大支持256T硬盘 安装模 ...

  6. 基于ITextSharp插件在ASP&period;NET MVC中将图表导出为PDF

    样本: 在这个示例中,我们使用的是微软给我们提供的数据库,也就是家喻户晓的Northwind数据库.要下载Microsoft的免费样本Northwind数据库,您需要访问以下URL.下载Northwi ...

  7. HighCharts使用更多图表HighChartsMore

    添加highcharts-moreimport HighCharts from 'highcharts'import highchartsMore from 'highcharts/highchart ...

  8. IE 8 浏览器 F12 调试功能无法使用

      “按下F12之后,开发人员工具在桌面上看不到,但是任务栏里有显示.将鼠标放在任务栏的开发人员工具上,出现一片透明的区域,选中之后却出不来.将鼠标移动到开发人员工具的缩略图上,右键-最大化,工具就全 ...

  9. AdmBaseController 判断是否登录

    代码 using Service.IService; using System; using System.Collections.Generic; using System.Linq; using ...

  10. 六,mysql优化——小知识点

    1,选择适当的字段类型,特别是主键 选择字段的一般原则是保小不保大,能占用字节小的字段就不用大字段.比如主键,建议使用自增类型,这样节省空间,空间就是效率!按4个字节和按32个字节定位一条记录,谁快谁 ...