《leetCode》:Best Time to Buy and Sell Stock III

2024-05-14 04:38
文章标签 leetcode iii time best buy sell stock

本文主要是介绍《leetCode》: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.

思路一:报超时错误

思路:分而治之,将prices分成两个部分,分别求这两部分的最大值,每个部分只允许存在一次交易 ;时间复杂度为O(n^2);

实现代码如下:


int getMaxProfitOneTransactions(int* prices, int pricesSize){if(prices==NULL||pricesSize<1){return 0;}int max=0;int buyPrice=prices[0];for(int i=0;i<pricesSize;i++){if(prices[i]<buyPrice){buyPrice=prices[i];}else{int profit=prices[i]-buyPrice;max=(max>profit)?max:profit;}}return max; 
}
int maxProfit(int* prices, int pricesSize) {if(prices==NULL||pricesSize<1){return 0;}int max=0;for(int i=0;i<pricesSize;i++){int maxPartOne_Two=getMaxProfitOneTransactions(prices,i)+getMaxProfitOneTransactions(prices+i,pricesSize-i);if(maxPartOne_Two>max){max=maxPartOne_Two;}}return max;
}

思路二

思路还是上面的思路,只是就空间来换取时间

/*
思路:分而治之,将prices分成两个部分,分别求这两部分的最大值 
解释:
首先,因为能买2次(第一次的卖可以和第二次的买在同一时间),但第二次的买不能在第一次的卖左边。
所以维护2个表,f1和f2,size都和prices一样大。
意义:
f1[i]表示 -- 截止到i下标为止,左边所做交易能够达到最大profit;
f2[i]表示 -- 从i下标开始,右边所做交易能够达到最大profit;
那么,对于f1 + f2,寻求最大即可。
*/int max(int a,int b){return (a>b)?a:b;
}int maxProfit(int* prices, int pricesSize) {if(prices==NULL||pricesSize<1){return 0;}int *f1=(int *)malloc(pricesSize*sizeof(int));int *f2=(int *)malloc(pricesSize*sizeof(int)); if(f1==NULL||f2==NULL){exit(EXIT_FAILURE);}//先看第一个交易f1[0]=0;int buyPriceMin=prices[0];for(int i=1;i<pricesSize;i++){if(prices[i]<buyPriceMin){buyPriceMin=prices[i];f1[i]=f1[i-1];//注意}else{int profit=prices[i]-buyPriceMin;f1[i]=max(f1[i-1],profit);}}//再看第二个交易,从右到左,寻找交易利润最大 f2[pricesSize-1]=0;int SellPriceMax=prices[pricesSize-1];for(int i=pricesSize-2;i>=0;i--){if(SellPriceMax<prices[i]){SellPriceMax=prices[i];f2[i]=f2[i+1];//注意}else{int profit=SellPriceMax-prices[i];f2[i]=max(f2[i+1],profit);}}//在f1+f2中寻找的最大值就是最大利润值int maxProfit=0;for(int i=0;i<pricesSize;i++){maxProfit=max(maxProfit,f1[i]+f2[i]);} return maxProfit;
}

上面的精简版本

int max(int a,int b){return (a>b)?a:b;
}
int min(int a,int b){return (a<b)?a:b;
}
int maxProfit(int* prices, int pricesSize) {if(prices==NULL||pricesSize<1){return 0;}int *f1=(int *)malloc(pricesSize*sizeof(int));int *f2=(int *)malloc(pricesSize*sizeof(int)); if(f1==NULL||f2==NULL){exit(EXIT_FAILURE);}//先看第一个交易f1[0]=0;int buyPriceMin=prices[0];for(int i=1;i<pricesSize;i++){buyPriceMin=min(buyPriceMin,prices[i]);f1[i]=max(f1[i-1],prices[i]-buyPriceMin);//用上面两行替换到下面被注释的部分
//      if(prices[i]<buyPriceMin){
//          buyPriceMin=prices[i];
//      }
//      else{
//          int profit=prices[i]-buyPriceMin;
//          f1[i]=max(f1[i-1],profit);
//      }}//再看第二个交易,从右到左,寻找交易利润最大 f2[pricesSize-1]=0;int SellPriceMax=prices[pricesSize-1];for(int i=pricesSize-2;i>=0;i--){SellPriceMax=max(SellPriceMax,prices[i]);f2[i]=max(f2[i+1],SellPriceMax-prices[i]);//用上面两行替换到下面被注释的部分
//      if(SellPriceMax<prices[i]){
//          SellPriceMax=prices[i];
//      }
//      else{
//          int profit=SellPriceMax-prices[i];
//          f2[i]=max(f2[i+1],profit);
//      }}//在f1+f2中寻找的最大值就是最大利润值int maxProfit=0;for(int i=0;i<pricesSize;i++){maxProfit=max(maxProfit,f1[i]+f2[i]);} return maxProfit;
}

这篇关于《leetCode》:Best Time to Buy and Sell Stock III的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/987766

相关文章

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python 标准库time时间的访问和转换问题小结

《Python标准库time时间的访问和转换问题小结》time模块为Python提供了处理时间和日期的多种功能,适用于多种与时间相关的场景,包括获取当前时间、格式化时间、暂停程序执行、计算程序运行时... 目录模块介绍使用场景主要类主要函数 - time()- sleep()- localtime()- g

如何使用 Bash 脚本中的time命令来统计命令执行时间(中英双语)

《如何使用Bash脚本中的time命令来统计命令执行时间(中英双语)》本文介绍了如何在Bash脚本中使用`time`命令来测量命令执行时间,包括`real`、`user`和`sys`三个时间指标,... 使用 Bash 脚本中的 time 命令来统计命令执行时间在日常的开发和运维过程中,性能监控和优化是不

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param