InterviewStreet / Challenges / Stock Trading

本文介绍了一个股票交易模拟问题,目标是通过准确预测未来连续N天的股票价格来制定最优买卖策略,实现最大利润。输入包括若干测试案例,每例包含预测价格序列;输出则是对应的最大可得利润。

Stock Trading(40 points)

You are magically able to predict with complete accuracy the price of a stock for the next N consecutive days. Each day, you can either buy one unit of stock, sell any number of stock units you have already bought, or do nothing. What is the maximum profit you can obtain by planning your trading strategy optimally?

Input:
The first line contains the number of test cases T. T test cases follow. The first line contains N. The next line contains N space seperated integers, denoting the estimated price of the stock on the N days.

Output:
Output T lines, containing the maximum profit which can be obtained for the corresponding test case.

Constraints:
1 <= T <= 10
1 <= N <= 50000
All stock prices are between 1 and 100000

Sample Input:
3
3
5 3 2
3
1 2 100
4
1 3 1 2

Sample Output:
0
197
3

Explanation:
For the first case, you cannot obtain any profit because the stock price never rises.
For the second case, you can buy one unit of stock on the first two days, and sell them both on the third day.

 

C#代码实现,代码提交InterviewStreet后测试只通过了5/11,尚未找到原因

namespace Stock
{
    class Solution
    {
        static void Main(string[] args)
        {
            int numTestcase = int.Parse(Console.ReadLine());
            int[][] testcases = new int[numTestcase][];
            for (int i = 0; i < numTestcase; i++)
            {
                int numStockPrices = int.Parse(Console.ReadLine());
                testcases[i] = new int[numStockPrices];
                string[] strPrices = Console.ReadLine().Split();
                for (int j = 0; j < numStockPrices; j++)
                {
                    testcases[i][j] = int.Parse(strPrices[j]);
                }
            }

            foreach (int[] testcase in testcases)
            {
                Console.WriteLine(CalculateMaxProfit(testcase, 0));
            }

            Console.ReadLine();
        }

        private static int CalculateMaxProfit(int[] testcase, int start)
        {
            int indexMaxPrice = FindMaxPrice(testcase, start);
            int leftProfit = 0;
            for (int i = start; i < indexMaxPrice; i++)
            {
                leftProfit += testcase[indexMaxPrice] - testcase[i];
            }
            int rightProfit = 0;
            if (indexMaxPrice + 1 < testcase.Length - 1)
            {
                rightProfit = CalculateMaxProfit(testcase, indexMaxPrice + 1);
            }

            return leftProfit + rightProfit;
        }

        private static int FindMaxPrice(int[] testcase, int start)
        {
            int indexMaxPrice = start;
            int maxPrice = testcase[start];
            for (int i = start + 1; i < testcase.Length; i++)
            {
                if (maxPrice < testcase[i])
                {
                    maxPrice = testcase[i];
                    indexMaxPrice = i;
                }
            }
            return indexMaxPrice;
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值