您的位置:首页 > 文旅 > 旅游 > 页面设计的简短概念_内容营销的方法_东莞网络推广策略_手机百度提交入口

页面设计的简短概念_内容营销的方法_东莞网络推广策略_手机百度提交入口

2024/12/28 14:18:19 来源:https://blog.csdn.net/weixin_64593595/article/details/142623711  浏览:    关键词:页面设计的简短概念_内容营销的方法_东莞网络推广策略_手机百度提交入口
页面设计的简短概念_内容营销的方法_东莞网络推广策略_手机百度提交入口

//Best Time to Buy and Sell Stockl
//Say you have an array for which the ith element is the price of a given stock on day i.
//Desian an algorithm to find the maximum profit, You mav complete as many transactions as you like lle.. 
//buy one and sell one share othe stock multiple times)
//Note: You may not engage in multiple transactions at the same time (i.., you must sell the stock before you buy again
//Example 1:
//Input:[7,1,5,3,6,4]
//Output:7
//Explanation: Buyon day2(price=1)and sell on day 3(price = 5),profit = 5-1 = 4.
//Then buy on day4(price=3)and sell on day5(price =6),profit =6-3 = 3.
//Example 2:
//Input:[1,2,3,4,5]
//0utput:4
//Explanation: Buyon day1(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 ase,no transaction is done, i.e. max profit = 0.

#include <stdio.h>
//贪心算法
int maxProfit(int* prices, int pricesSize) {int maxProfit = 0;for (int i = 1; i < pricesSize; ++i){// 只要今天的价格高于昨天的价格,就可以获利if (prices[i] > prices[i - 1]) {maxProfit += prices[i] - prices[i - 1];}}return maxProfit;
}int main() {// 示例输入int prices1[] = {7, 1, 5, 3, 6, 4};int prices2[] = {1, 2, 3, 4, 5};int prices3[] = {7, 6, 4, 3, 1};printf("Example 1: Max Profit = %d\n", maxProfit(prices1, 6)); // 输出: 7printf("Example 2: Max Profit = %d\n", maxProfit(prices2, 5)); // 输出: 4printf("Example 3: Max Profit = %d\n", maxProfit(prices3, 5)); // 输出: 0return 0;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com