- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/descriptionopen in new window
Sayyou 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 (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
可以多次进行买卖操作,但是每天只能要么买要么卖,求最大收益。
这个题不是说可以任意的挑选,而是要按照每天的顺序挑选。这样就很简单了,只要后面的一天比这天的价格高就买入卖出就可。
Java解法如下:
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int ans = 0;
for(int i = 0; i < len - 1; i++){
if(prices[i + 1] > prices[i]){
ans += prices[i + 1] - prices[i];
}
}
return ans;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 二刷,python
二刷的时候对这个题有更深的看法了,这个题一定是要有序的输入,我们在某个位置不能同时买卖,但是可以看做先全卖掉再全买下,那么就相当于没有买卖。比如一个区间划分成了两部分,区间的总长度=两个子区间的长度和。
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
ans = 0
for i in range(len(prices) - 1):
if prices[i + 1] > prices[i]:
ans += prices[i + 1] - prices[i]
return ans
1 2 3 4 5 6 7 8 9 10 11 12
三刷,C++。和上面的代码思路完全一致。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int N = prices.size();
int res = 0;
for (int i = 1; i < N; i ++) {
if (prices[i] > prices[i - 1]) {
res += prices[i] - prices[i - 1];
}
}
return res;
}
};
1 2 3 4 5 6 7 8 9 10 11 12 13
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
有一个经典的面试问题,利润最大化,一次交易买股票,允许n次交易和k次交易。 有人问我类似的问题,但有一个扭曲约束:您可以多次购买股票(任何一天不超过一个单位),但您不能在卖出股票后购买。 这有一个你只
我看到其他帖子人们无法加载“我的销售工具”,而我恰好在同一条船上。 支持还没有回应,我希望在星期一做一个 Windows 8/Paypal API 集成的演示,来自 http://paypal.git
默认情况下,Woocommerce Force Sells 仅列出产品标题。我还需要他们在每个标题旁边的括号中显示产品价格。可以看看here就像这个截图: 但应该是: Birth Pool Hire
我无法在 metatrader5 中获取 MQL5 的交易利润值,因为它始终返回 0.0: MqlTradeCheckResult result; trade.CheckResult(result
我在面试中遇到了一个算法问题,但我似乎无法弄明白。我了解它应该如何工作,但无法通过算法对其进行排序。 因此假设一家公司交易石油桶并且一次只能保留一个石油桶。假设公司知道一年中每一天的每桶价格。所以它作
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/open in new window Total Accepte
明天,我会见一个不从事技术工作的客户,但他可能会问 RubyOnRails 是否是他网站的正确选择。他可能认为没有足够的 RoR 程序员,他将成为该语言的“人质”。 我有充分的理由使用 RoR,客户也
我的团队主要使用基于 Flex 的应用程序工作。话虽如此,几乎没有任何约定(甚至让它们重构本身就是一个奇迹)等等。 来自 .NET + CruiseControl.NET 背景,我一直渴望让每个人都使
iOS InApp 购买:为什么您必须在销售产品之前获取产品列表? 遵循此列表:http://troybrant.net/blog/2010/01/in-app-purchases-a-full-wa
本文整理了Java中com.github.robozonky.common.remote.Zonky.sell()方法的一些代码示例,展示了Zonky.sell()的具体用法。这些代码示例主要来源于G
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/ 题目描述
根据亚马逊销售合作伙伴 API 文档,我能够获得 LWA 访问 token 。但是,我无法向 REST API 发出请求。 https://github.com/amzn/selling-partne
我在 Android 开发者页面找到了两个使用 Android 计费的教程:Selling In-App products和 Play Billing Library 为什么 Google 提供了两种
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ 题目描述 Sayyou ha
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/#/descriptionopen in new windo
题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是否可以过滤,我为特定产品设置的追加销售产品/将如何根据产品名称显示在我的表格中?我的想法是只显示以某个词开头的产品,例如“Decor”,其余产品不显示在表格中。这是我的代码 upsell.phtm
我正在尝试计算 csv 中第 [7] 行的所有值的售价,了解折扣百分比存储在列表 Y_pred 中的所有客户的售价。对于第一个客户,它计算正确。对于下一个客户,我将包含最终销售价格的列表(名为 sel
我找不到任何关于发布到/阅读 Facebook 购买、销售、交易组的文档,特别是组特定的帖子属性,例如价格、标题、已售/未售等。 Graph API 中是否存在这种能力,如果存在,需要哪些字段或边?
我是一名优秀的程序员,十分优秀!