Programming: Java

Creative Lab: buy low, buy lower

After a few days of writing this algorithm. Finally it is fast (can be faster). Again, still find that this particular lab is difficult. Maybe that is why it is ranked as “Challenger” and this lab is not graded. Oh well. Shouldn’t be selfish and insist that this lab should be marked.

And it is able to run 20,000 data set in 3 secs. That is on the benchmarking system, sunfire.


The questions itself

Each time you buy a stock, the price must be lower than the previous time you bought it. A list of daily prices of a stock is given in a textfile stock. For example:

12
68
69
54
70
68
64
70
62
67
78
98
87

The value n on the first line indicates the number of days, and the following n lines contain the daily prices of the stock.

Write a program Stock.java to read the values from the textfile stock, and computes the longest sequence of decreasing prices and the number of sequences that have this length. For the above example, the answers are:

Longest length: 4 (Sequences are: 70 – 68 – 64 – 62 and 69 – 68 – 64 – 62)
Number of sequences with this length: 2

Your program should output two integers on a single line, separated by a space. For the above example, the output should be:

4 2

You may assume that the prices are integers in the range [0, 20000].

This problem is a challenger because an inefficient algorithm would take too long. To have an indication of how fast your algorithm runs, you should time your program. A fast algorithm should be able to run on a data file with 20000 prices in sunfire within 15 seconds (already a conservative limit).