关闭

leetcode 121. Best Time to Buy and Sell Stock

标签: leetcode算法
165人阅读 评论(0) 收藏 举报
分类:

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0

In this case, no transaction is done, i.e. max profit = 0.

class Solution {
public:
static bool sortbyprice(pair<int,int>&obj1,pair<int,int>&obj2){
if(obj1.first>obj2.first){
return false;
}
return true;
}
int maxProfit(vector<int>& prices) 
    { 
int days = prices.size();  
if (days <= 1)return 0;     
int start = 0, end = 1;      
int profit = 0;      
while (end<days)

if (prices[end]>prices[start])
profit = max(profit, prices[end] - prices[start]);
else                 
start = end;                 
end++; 
}         
return profit; 
    }
    /*
int maxProfit(vector<int>& prices) {
int max_profite = 0;
int i,j;
int n = prices.size();
bool flag=false;
vector<pair<int,int>>pricesort;
for(i=0;i<n;i++){
pricesort.push_back(make_pair(prices.at(i),i));
}
sort(pricesort.begin(),pricesort.end(),sortbyprice);
for (j=n-1;j>=1;j--){
   if((pricesort.at(j).second>pricesort.at(j-1).second)&&(pricesort.at(j).first>pricesort.at(j-1).first)){
   flag = true;
}
}
if(flag==false){
   return 0;
}
for(i=0;i<n-1;i++){
for(j=n-1;j!=i;j--){
if(pricesort.at(j).second>pricesort.at(i).second){
return pricesort.at(j).first-pricesort.at(i).first;
}
}
}
return 0;
}*/
};
/*
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int max_profite = 0;
        int i,j;
        int n = prices.size();
        for(i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
                max_profite = max_profite>prices.at(j)-prices.at(i)?max_profite:prices.at(j)-prices.at(i);
            }
        }
        return max_profite;
    }
};*/

0
0
查看评论

LeetCode(121)Best Time to Buy and Sell Stock

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one trans...
  • feliciafay
  • feliciafay
  • 2014-01-21 02:39
  • 2528

leetcode【121+122+123 best time to buy and sell stock】【python】

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最优值,而动态规划是要记录下来达到当前最优解的所有途径,由局部一步步得到全局最优。这几道题都是给你一个...
  • u014251967
  • u014251967
  • 2016-09-12 21:04
  • 789

【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】

【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Say you have an array for which the ith element is the price of ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-14 06:16
  • 6923

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

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 prof...
  • u011613729
  • u011613729
  • 2014-07-24 10:11
  • 3434

121. Best Time to Buy and Sell Stock [easy] (Python)

题目链接https://leetcode.com/problems/best-time-to-buy-and-sell-stock/题目原文 Say you have an array for which the ithi^{th} element is the price of a given...
  • coder_orz
  • coder_orz
  • 2016-05-28 01:02
  • 980

关于LeetCode中Best Time to Buy and Sell Stock一题的理解

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one tran...
  • zsy112371
  • zsy112371
  • 2016-09-09 13:55
  • 831

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益。分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天...
  • ljiabin
  • ljiabin
  • 2015-04-06 11:10
  • 21729

[leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)

Question: 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 t...
  • Xiaohei00000
  • Xiaohei00000
  • 2016-03-10 23:02
  • 1747

[LeetCode] Best Time to Buy and Sell Stock IV

Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design an algorithm to find the maximum profit. You may ...
  • u011331383
  • u011331383
  • 2015-02-23 00:08
  • 8758

<LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)

121. Best Time to Buy and Sell Stock My Submissions Question Total Accepted: 81473 Total Submissions: 234230 Difficulty: ...
  • EbowTang
  • EbowTang
  • 2016-01-15 16:12
  • 1758
    个人资料
    • 访问:133542次
    • 积分:1802
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:184篇
    • 译文:0篇
    • 评论:15条
    最新评论