C++实现LeetCode(122.买股票的最佳时间之二)
[LeetCode] 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).
这道跟之前那道Best Time to Buy and Sell Stock 买卖股票的最佳时间很类似,但都比较容易解答。这道题由于可以无限次买入和卖出。我们都知道炒股想挣钱当然是低价买入高价抛出,那么这里我们只需要从第二天开始,如果当前价格比之前价格高,则把差值加入利润中,因为我们可以昨天买入,今日卖出,若明日价更高的话,还可以今日买入,明日再抛出。以此类推,遍历完整个数组后即可求得最大利润。代码如下:
C++ 解法:
class Solution { public: int maxProfit(vector<int>& prices) { int res = 0, n = prices.size(); for (int i = 0; i < n - 1; ++i) { if (prices[i] < prices[i + 1]) { res += prices[i + 1] - prices[i]; } } return res; } };
Java 解法:
public class Solution { public int maxProfit(int[] prices) { int res = 0; for (int i = 0; i < prices.length - 1; ++i) { if (prices[i] < prices[i + 1]) { res += prices[i + 1] - prices[i]; } } return res; } }
类似题目:
Best Time to Buy and Sell Stock with Cooldown
Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock
到此这篇关于C++实现LeetCode(122.买股票的最佳时间之二)的文章就介绍到这了,更多相关C++实现买股票的最佳时间之二内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
相关推荐
-
C++实现LeetCode(118.杨辉三角)
[LeetCode] 118.Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1], [1,1], [1
-
C++实现LeetCode(120.三角形)
[LeetCode] 120.Triangle 三角形 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum
-
C++实现LeetCode(121.买卖股票的最佳时间)
[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 only permitted to complete at most one transaction (ie, buy one and sell one share of the sto
-
C++实现LeetCode(115.不同的子序列)
[LeetCode] 115. Distinct Subsequences 不同的子序列 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be n
-
C++实现LeetCode(123.买股票的最佳时间之三)
[LeetCode] 123.Best Time to Buy and Sell Stock III 买股票的最佳时间之三 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 at most two transactions. Note: You
-
C++实现LeetCode(119.杨辉三角之二)
[LeetCode] 119. Pascal's Triangle II 杨辉三角之二 Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. In Pascal's triangle, each number is the sum of the two numbers directly
-
C++实现LeetCode(117.每个节点的右向指针之二)
[LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针之二 Given a binary tree struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, t
-
C++实现LeetCode(116.每个节点的右向指针)
[LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针 You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val;
-
C++实现LeetCode(122.买股票的最佳时间之二)
[LeetCode] 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
-
C++实现LeetCode(309.买股票的最佳时间含冷冻期)
[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 algorithm to find the maximum profit. You may complete as many transactions as
-
C++实现LeetCode(188.买卖股票的最佳时间之四)
[LeetCode] 188.Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四 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 at most k transactions. Note: You m
-
C++实现LeetCode(132.拆分回文串之二)
[LeetCode] 132.Palindrome Partitioning II 拆分回文串之二 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example: Input: "aab" Output: 1 Explan
-
java获取当前日期和时间的二种方法分享
有两种方法:方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码: 复制代码 代码如下: import java.util.*;import java.text.*;//以下默认时间日期显示方式都是汉语语言方式//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDa
-
详解CentOS5.5 下搭建 PHP 环境(最佳的LAMP环境)
本文详细阐述在 Linux 系统中搭建 PHP 环境,由于 PHP 就是由 C 语言编写的,最初也是运行在 Linux 系统中,所以Linux 是 PHP 的最佳环境. 关于本文中使用到的软件,请点击此链接下载. CentOS5.5现在官方已经不再提供,推荐大家使用centos6以上版本: centos6.8下载地址:http://www.jb51.net/softs/499124.html centos7.2下载地址:http://www.jb51.net/softs/499109.html
-
详解Nginx服务器中配置超时时间的方法
一.啥时候用到 用来设置请求资源和服务器返回的时间,保证一个请求占用固定时间,超出后报504超时!这样可以保证一个请求占用过长时间. 二.主要参数 使用nginx服务器如果遇到timeou情况时可以如下设置参数,使用fastcgi: fastcgi_connect_timeout 75; 链接 fastcgi_read_timeout 600; 读取 fastcgi_send_timeout 600; 发请求 这两个选项. fastcgi_read_timeout是指
-
C++中Boost.Chrono时间库的使用方法
前言 大家应该都有所体会,时钟这个东西在程序中扮演者重要的角色,在系统编程的时候睡眠.带超时的等待.带超时的条件变量.带超时的锁都会用到,但是往往对特定系统依赖性很大,感觉即使不考虑系统的跨平台性,如果能使用一个稳定的接口,同时如果能够方便的对时刻.时段等进行相关的操作和运算,将是再好不过的了. 在boost库中和时间相关的库有Boost.DateTime和Boost.Chrono,前者专注于时间时刻以及本地化相关的内容,而后者主要是时刻.时长和时间的计算等内容.当然,C++11标准已经支持st
随机推荐
- SWT(JFace) FTP客户端实现
- 超级简单实现JavaScript MVC 样式框架
- Java中异常处理之try和catch代码块的使用
- python基于右递归解决八皇后问题的方法
- 基于Python实现的扫雷游戏实例代码
- 使用PHP的日期与时间函数技巧
- js特殊字符过滤的示例代码
- C# 动态加载程序集信息
- Node.js开发教程之基于OnceIO框架实现文件上传和验证功能
- 项目管理利器-Maven(Windows安装)图文教程
- CentOS 5.5使用yum来安装LAMP(php运行环境)
- 从csdn弄下来的页面预先载入效果
- JS双击变input框批量修改内容
- Spring入门实战之Profile详解
- 深入解析php中的foreach函数
- Android app启动时黑屏或者白屏的原因及解决办法
- Android 多媒体播放API简单实例
- MySQL中的SUM函数使用教程
- python的numpy模块安装不成功简单解决方法总结
- 浅谈Python编程中3个常用的数据结构和算法