leetcode || 121、Best Time to Buy and Sell Stock

problem:

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.

Hide Tags

Array Dynamic
Programming

题意:给定一支股票的价格表,决定哪天买哪天卖受益最大,卖的时间在买的后面

thinking:

(1)其实就是求一个数组元素两个元素之间的最大差值,使用后面的数减去前面的数

(2)典型的 DP思路,开一个N大小的数组,记录该位置的减去前面最小元素的差值,

最后找出差值数组的最大值即可,时间复杂度O(N)

code:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        if(n==0)
            return 0;
        vector<int> f(n,0);
        int minprice=prices[0];
        for(int i=0;i<n;i++)
        {
            minprice=min(minprice,prices[i]);
            f[i]=prices[i]-minprice;
        }
        int profit=f[0];
        for(int j=0;j<n;j++)
            profit=max(profit,f[j]);
        return profit;
    }
};
时间: 2024-08-02 22:09:55

leetcode || 121、Best Time to Buy and Sell Stock的相关文章

leetcode || 123、Best Time to Buy and Sell Stock III

problem: 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. Note: You may not engage in multiple transactions at the same

leetcode || 122、Best Time to Buy and Sell Stock II

problem: 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple t

leetcode第一刷_Best Time to Buy and Sell Stock II

sched.c sched.h 代码分析笔记 首先上header file sched.h #ifndef _SCHED_H #define _SCHED_H #define HZ 100 #define NR_TASKS 64 #define TASK_SIZE 0x04000000 #define LIBRARY_SIZE 0x00400000 #if (TASK_SIZE & 0x3fffff) #error "TASK_SIZE must be multiple of 4M&qu

【leetcode】122.Best Time to Buy and Sell Stock II

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) /************************ * @description: dynamic programming. * 相邻元素做

LeetCode 笔记23 Best Time to Buy and Sell Stock III

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. 现在A股涨这么好,要是股票都提前知道价格就好了(@[email pro

&amp;lt;LeetCode OJ&amp;gt; 121. /122. Best Time to Buy and Sell Stock(I / II)

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. 分析:

LeetCode OJ: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 transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 简单的动

leetcode笔记:Best Time to Buy and Sell Stock III

一. 题目描述 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 at most two transactions. Note: You may not engage in multiple transactions at the same

LeetCode OJ 122. 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 profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho