122. Best Time to Buy and Sell Stock(二) leetcode解题笔记

时间:2022-08-27 14:45:02

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

这一题算是第一题的基础上稍微增加一点难度吧 数组的含义与我上文提到的一致 只是说他不限制你只能买一次了 你可以买任意次  只是在你再次买之前你必须先卖掉你之前买的东西  最终目的还是求最大利润。

这里其实理解了题目的话 解题感觉不难

可以画一个折线图 这道题的意思就是把所有上坡的地方加起来 粗糙的画个图

122. Best Time to Buy and Sell Stock(二)  leetcode解题笔记

每一个红色圈住的地方代表一个小上坡 都已加进去

有了思路 码代码

public class Solution {
public int maxProfit(int[] prices) {
int profit=0;
for(int i=0;i<prices.length-1;i++){
int tmp=prices[i+1]-prices[i];
if(tmp>0){
profit+=tmp;
}
}
return profit;
}
}

  一次过 哦也

空间复杂度o(1)级别  遍历一次时间复杂度0(n)  直接就是最优解了 不用看discuss了 继续看下一题

122. Best Time to Buy and Sell Stock(二) leetcode解题笔记的更多相关文章

  1. 188&period; Best Time to Buy and Sell Stock IV leetcode解题笔记

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

  2. 122&period; Best Time to Buy and Sell Stock II ——LeetCode

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

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

  4. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  5. 【一天一道LeetCode】&num;122&period; Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  6. 122&period; Best Time to Buy and Sell Stock II&commat;python

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

  7. leetcode:122&period; Best Time to Buy and Sell Stock II(java)解答

    转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...

  8. &lbrack;LeetCode&rsqb; 122&period; 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 ...

  9. 【刷题-LeetCode】122 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 ...

随机推荐

  1. Sql Server系列:分区表操作

    1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...

  2. cer pfx格式数字证书区别

    作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...

  3. jQuery诞生记-原理与机制

    一.看似偶然的东西实际是必然会发生的 我大学时候在图书馆翻过一本很破旧的书,讲生物理论的,主要内容就是探讨生命的产生是偶然还是必然.里面很多亚里士多德都看不懂的公式计算什么的,还有模拟原始地球环境出现 ...

  4. nth-of-type和nth-child的区别

    看CSS3时发现了一个nth-of-type选择器,发现平时基本没见过用,就研究了一下,w3c是这样说明的: :nth-of-type(n) 选择器匹配属于父元素的特定类型的第 N 个子元素的每个元素 ...

  5. Linux内核是如何创建一个新进程的?

    进程描述 进程描述符(task_struct) 用来描述进程的数据结构,可以理解为进程的属性.比如进程的状态.进程的标识(PID)等,都被封装在了进程描述符这个数据结构中,该数据结构被定义为task_ ...

  6. FindPkgConfig----CMake的pkg-config模块

    FindPkgConfig A pkg-config module for CMake. CMake的pkg-config模块. Finds the pkg-config executable and ...

  7. 18&period;22 sprintf函数功能

    函数功能:把格式化的数据写入某个字符串 函数原型:int sprintf( char *buffer, const char *format [, argument] … ); 返回值:字符串长度(s ...

  8. CCF 201509-3 模版生成系统

    试题编号: 201509-3 试题名称: 模板生成系统 时间限制: 1.0s 内存限制: 256.0MB 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的 ...

  9. 取n的某些位

    实例十一:取n的某些位 方法:result=(n>>4)&(~(~0<<4)) 取出某数n的低4位. 数值0  0000 0000 ~0   1111 1111 ~0& ...

  10. &lbrack;洛谷P2057&rsqb;&lbrack;bzoj1934&rsqb;善意的投票(最大流)

    题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来 ...