股票市场问题(The Stock Market Problem)

Question: Let us suppose we have an array whose ith element gives the price of a share on the day i.
If you were only permitted to buy one share of the stock and sell one share of the stock, design an algorithm to find the best times to buy and sell.

要求最佳的买进和卖出时间,也就是利润最大化的点。当买进时的股价小于卖出时的股价就能产生利润。

第一眼看上去,我们也许会想到找到数组的最小点和最大点,但是买必须是在卖之前。

问题等价于:

Find i and j that maximizes Aj – Ai, where i < j.

简单的方法是计算所有的可能性,在比较最大值,需要的时间是O(n2),也许我们能在O(n)的时间内解决。

要解决这个问题,需要追踪最小值

遍历的同时,更新最小值的索引

比较目前的值与最小值的差值

遍历时更新最大差值

算法实现。

#include<stdio.h>

void getBestTime(int stocks[], int size)
{
    int buy = 0, sell = 0;

    int min = 0;
    int i;
    int maxDiff = 0;
    buy = sell = 0;

    for (i = 0; i < size; i++)
    {
        if (stocks[i] < stocks[min])
            min = i;
        int diff = stocks[i] - stocks[min];

        if (diff > maxDiff)
        {
            buy = min;
            sell = i;
            maxDiff = diff;
        }
    }

    printf("\nThe day to buy is- %d at price %d",buy, stocks[buy]);
    printf("\nThe day to sell is- %d at price %d",sell, stocks[sell]);
}

int main(void)
{
    int stocks[] = {3, 6, 10, 2, 66, 43, 1, 23};

    int buy = 0;
    int sell = 0;

    getBestTime(stocks, 8);

    return 0;
}
时间: 2024-11-19 04:34:02

股票市场问题(The Stock Market Problem)的相关文章

1578: [Usaco2009 Feb]Stock Market 股票市场

1578: [Usaco2009 Feb]Stock Market 股票市场 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 414  Solved: 199[Submit][Status][Discuss] Description 尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中受到打击,现在她们开始着手于股市. Bessie很有先见之明,她不仅知道今天S (2 <= S <= 50)只股票的价格,还知道接下来一共D(2 <= D <

(zhuan) Using the latest advancements in AI to predict stock market movements

Using the latest advancements in AI to predict stock market movements 2019-01-13 21:31:18 This blog is copied from: https://github.com/borisbanushev/stockpredictionai In this notebook I will create a complete process for predicting stock price moveme

Stock market clustering

2019/10/3 homework_3 - Jupyter Notebooklocalhost:8891/notebooks/Desktop/hw03/homework_3.ipynb 1/12Stock market clusteringData Structures and Algorithms Using Python, September 2019Imperial College Business SchoolThis assignment is divided into three

word Stock Market Indices

Stock Market Indices USA Africa Asia and Pacific Canada Europe Middle East South America International BBC Global 30 - world stock market index of 30 of the largest companies by stock market value in Europe, Asia and the Americas. iShares MSCI EAFE I

[BZOJ1578] [Usaco2009 Feb]Stock Market 股票市场(DP)

传送门 可以看出 第一天买,第三天卖 == 第一天买,第二天卖完再买,第三天卖 所以我们只考虑前一天买,后一天卖即可 那么有按天数来划分 f[i][j]表示前i天,共有j元,最大的盈利 第一维可以省去 那么有两种选择,不买 或者 前一天买,后一天卖 #include <cstdio> #include <cstring> #include <iostream> #define N 500001 #define max(x, y) ((x) > (y) ? (x)

【BZOJ】1578: [Usaco2009 Feb]Stock Market 股票市场

[题意]给定s个股票和d天,给出价格矩阵s*d,每天可以买入或卖出整数倍股票,初始资金m,求最大利益.m<=200000,s<=50,d<=10. [算法]完全背包 [题解]关键在于转化:第一天买入-第三天卖出,相当于,第一天买入-第二天卖出-第二天买入-第三天卖出.那么买卖股票就变成相邻两天的事情了. 对于每一天,就是完全背包,总重量为资金,重量为当天价格,价值为第二天价格-当天价格. f[i][j]表示前i个股票使用资金j能获得的最大收益. f[i][j]=f[i-1][j-A[i]

[Usaco2009 Feb]Stock Market 股票市场 完全背包

Code: #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<stack> #include<string> using namespace std; void setIO(string a){ freopen((a+".in").c_str(),"r",stdin),freope

Latent Semantic Analysis(LSA)

背景:什么是LSA? Latent Semantic Analysis(LSA)中文翻译为潜语义分析,也被叫做Latent Semantic Indexing ( LSI ).意思是指通过分析一堆(不止一个)文档去发现这些文档中潜在的意思和概念,什么叫潜在的意思?我第一次看到这个解释,直接懵逼.其实就是发现文档的中心主题吧?假设每个词仅表示一个概念,并且每个概念仅仅被一个词所描述,LSA将非常简单(从词到概念存在一个简单的映射关系). 根据常识我们知道两个很常见的语言现象:1. 存在不同的词表示

IJCAI_论文-深度学习-Deep Learning for Event-Driven Stock Prediction

Deep Learning for Event-Driven Stock Prediction Reading time:2019/3/30-2019/4/12  Theme:Deep learning; CNN; NLP Abstract: We propose a deep learning method for eventdriven stock market prediction. First, events are extracted from news text, and repre