Sunday, November 20, 2016

Woman's CodeSprint 2 - Minimum Loss - in the contest (series 1 of 10)

Nov. 20, 2016

Julia worked on the algorithm in the contest, and then solved the algorithm with full score - 35.

Here is the problem statement:
https://www.hackerrank.com/contests/womens-codesprint-2/challenges/minimum-loss

Julia's C# solution:

https://gist.github.com/jianminchen/af474846d81b96f4e70fc1a5866dca15

The ideas used in the algorithm:

Sort the array with house price.

Use bucket sort similar idea to go through each bucket, compare to previous if the current is less than minimum loss or not. Each bucket keeps the two value - max/ min value.

After the contest study:

There is a solution written in C#, much simpler and smarter than Julia's C# code:

https://gist.github.com/jianminchen/c382bc1b40e47c2740a25abcaeffb846

Based on the assumption that housing price is different for each year, but I did not find the word in the problem statement.

Discussion of Time Complexity:

1. Brute force solution - O(n^2), choose any two year to compare the price. Will time-out!

2. Using Binary search - therefore, it is easy to find the minimum price, O(nlogn)
Maintain a binary search tree!

Study this C# solution using Binary search tree:

https://gist.github.com/jianminchen/c910f5d1f37309c70b489e6c75b0678c

Very classical solution using binary search tree, and very clever solution.

Memorize the solution. Warm up the solution sometimes in the future.

Compared to Java submission using TreeSet class:
https://gist.github.com/jianminchen/6fa69ef28c849d63ed0dc4b419cd0dee


3.


No comments:

Post a Comment