12.4—贪心法—Best Time to Buy and Sell Sto II

描述
Say you have an array for which the i-th 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).

#include<iostream>
using namespace std;
int SellStockII(int a[], int n)
{
	if (a == NULL || n <= 1)
		return 0;
	int sum = 0;
	for (int i = 0; i < n - 1; i++)
		if (a[i + 1]>a[i])
			sum += a[i + 1] - a[i];
	return sum;
}
int main()
{
	const int n = 6;
	int a[n] = { 2, 4, 1, 5, 3, 4 };
	int maxsum = SellStockII(a, n);
	cout << maxsum << endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Xcode 12.4是一款由Apple开发的集成开发环境(IDE),用于在Mac OS X操作系统上进行软件开发。它包含了iOS 14.4、iPadOS 14.4、tvOS 14.4、watchOS 7.2以及macOS Big Sur 11.1的SDK。Xcode 12.4支持iOS 9及以上版本、tvOS 9及以上版本、watchOS 2及以上版本,并需要在安装有macOS 10.15.4及以上版本的电脑上使用。 如果您的电脑是运行在Catalina 10.15.7上的,而App Store显示由于版本过低无法安装Xcode,您可以尝试从第三方网站下载Xcode 12.4的应用程序。一个相对安全的下载地址是https://cloud.mfpud.com/Application/Xcode/,您可以下载Xcode_12.4.xip文件并解压缩后安装。然后,您还需要下载Xcode 12.4的真机配置包,将其拖到已解压的Xcode 12.4应用程序中的指定位置。 请注意,从非官方和未经验证的第三方网站下载Xcode应用程序可能存在安全风险,因此建议您谨慎操作。 希望这些信息对您有所帮助。如果您还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [2021-07-09](https://blog.csdn.net/bjnbt/article/details/118610239)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Xcode12.4发布说明(翻译)](https://blog.csdn.net/guoyongming925/article/details/113865330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值