Created
December 2, 2016 00:56
-
-
Save jianminchen/d6c675533578d50049c636e566695830 to your computer and use it in GitHub Desktop.
HackerRank woman codesprint #2 - minimum cost - using List<Int64>, 3 test cases time out, score 24/ 35. Need to continue. Great workout using C# List.BinarySearch, Insert API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace minimumLoss | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Process(); | |
//testcase3(); | |
} | |
private static void Process() | |
{ | |
int n = int.Parse(Console.ReadLine()); | |
Int64[] prices = new Int64[n]; | |
string[] arr = Console.ReadLine().Split(' '); | |
prices = Array.ConvertAll(arr, Int64.Parse); | |
Console.WriteLine(MinimumLossCal(n, prices)); | |
} | |
private static void Testcase2() | |
{ | |
Int64[] prices = new Int64[5] { 20, 7, 8, 2, 5 }; | |
Debug.Assert(MinimumLossCal(5, prices) == 2); | |
} | |
private static void testcase3() | |
{ | |
Int64[] prices = new Int64[4] { 2, 3, 4, 1 }; | |
Debug.Assert(MinimumLossCal(4, prices) == 1 ); | |
} | |
/* | |
* minimum loss | |
* finished time: 4:02pm | |
* | |
* read Java TreeSet floor method: | |
* https://www.tutorialspoint.com/java/util/treeset_floor.htm | |
* | |
* use C# List<T> BinarySearch, Insert API | |
* https://msdn.microsoft.com/en-us/library/w4e7fxsh(v=vs.110).aspx | |
* | |
* Advised by: | |
* | |
* | |
* | |
* The idea is to go over each house in the reverse order, add one by one into the List object, but | |
* the List is also a binary search tree. How to do it? To maintain the binary search tree, | |
* using BinarySearch to find the position first, similar to Java TreeSet class floor method, | |
* meanwhile, update minimum loss value if the insertion position is not the smallest one in the tree. | |
* | |
* The time complexity is O(n), each time, binary search will take O(logn) time. Therefore, the final | |
* time complexity should be O(nlogn). | |
*/ | |
private static Int64 MinimumLossCal(int n, Int64[] prices) | |
{ | |
List<Int64> data = new List<Int64>(); | |
Int64 minLoss = Int64.MaxValue; | |
for (int i = n - 1; i >= 0; i--) | |
{ | |
Int64 curPrice = prices[i]; | |
var index = data.BinarySearch(curPrice); | |
if (index < 0) | |
{ | |
int pos = ~index; | |
if (pos > 0) | |
{ | |
Int64 newDiff = curPrice - data[pos-1]; | |
minLoss = (newDiff < minLoss) ? newDiff : minLoss; | |
} | |
data.Insert(pos, curPrice); | |
} | |
// if there is one in the binary search tree, then no need to insert the duplicate one in the tree. | |
} | |
return minLoss; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment