Best Time to Buy and Sell Stock_Runtime error 1
来源:互联网 发布:域名不以封疆 编辑:程序博客网 时间:2024/06/07 08:52
#include<iostream>#include<vector>#define INF -32760 using namespace std;class Solution {public: int maxProfit(vector<int> &prices) {int *P;int result;P=new int[prices.size()];for(int i=0;i<prices.size();i++)P[i]=prices[i];result=max_sub_arry(P,0,prices.size()-1);delete []P;return result;}int MAX_cross_sum(int A[],int low,int mid,int high);int max_three(int a,int b,int c);int max_two(int a,int b);int max_sub_arry(int A[],int low,int high);};int Solution::MAX_cross_sum(int A[],int low,int mid,int high) { int max_cross; int left_sum=INF; int max_left=0; int i=0; int sum=0; int right_sum=INF;//右边的最大值 int max_right=0;//右边的最大值时的编号. //左边的最大 for(i=mid;i>=low;i--) { sum=sum+A[i]; if(sum>=left_sum) { left_sum=sum; max_left=i; } } //右边的最大 sum=0; for(i=mid+1;i<=high;i++) { sum=sum+A[i]; if(sum>=right_sum) { right_sum=sum; max_right=i; } } max_cross=left_sum+right_sum; return max_cross; }int Solution::max_three(int a,int b,int c) { if(a>=b&&a>=c) return a; else if(b>=a&&b>=c) return b; else return c; } int Solution::max_two(int a,int b) { if(a>=b) return a; else return b; } int Solution::max_sub_arry(int A[],int low,int high) { if(low==high) return A[low]; else if((high-low)==1) return max_two(A[low],A[high]); else return max_three(MAX_cross_sum(A,low,(low+high)/2,high), max_sub_arry(A,low,(low+high)/2-1), max_sub_arry(A,((low+high)/2)+1,high)); } int main(){int arry[10]={-1,2,3,9,-2,3,4,-2,6,-1};vector<int> prices;for(int i=0;i<10;i++)prices.push_back(arry[i]);for(int i=0;i<10;i++)cout<<prices[i]<<endl;cout<<prices.size()<<endl;Solution A;cout<<A.maxProfit(prices)<<endl;system("pause"); return 1; }
0 0
- Best Time to Buy and Sell Stock_Runtime error 1
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- <数据结构> 实验三 栈和队列——选做题
- HDU-4772-Zhuge Liang's Password
- Android指令中英文解
- 匿名内部类JAVA
- 数据结构——实验一 VC编程工具的灵活使用
- Best Time to Buy and Sell Stock_Runtime error 1
- OpenTextFile
- iOS Developer:真机测试
- android ContentResolver ContentProvider 详解
- 近期非技术类书籍读书计划
- com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value
- Android中的style和theme的用法
- UINavigationController 导航控制器
- system函数的总结