InterviewStreet / Challenges / Stock Trading

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;
        }
    }
}


 

你可能感兴趣的:(C#)