Skip to content

Instantly share code, notes, and snippets.

@craigminihan
Created July 24, 2017 22:44
Show Gist options
  • Save craigminihan/3fe48dfa1bce61d765d674a3dfc80a31 to your computer and use it in GitHub Desktop.
Save craigminihan/3fe48dfa1bce61d765d674a3dfc80a31 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
namespace TestCodility1
{
class Program
{
public static int move(int[] positions, int[] knownCosts, int maxHop, int start)
{
var minCost = int.MaxValue;
for (var i = 1; minCost != 0 && i <= maxHop; ++i)
{
var pos = start + i;
if (pos >= positions.Length)
{
minCost = 0;
}
else if (knownCosts[pos] >= 0)
{
minCost = Math.Min(minCost, knownCosts[pos]);
}
else if (positions[pos] >= 0)
{
var cost = move(positions, knownCosts, maxHop, pos);
if (cost >= 0)
{
cost = Math.Max(positions[pos], cost);
minCost = Math.Min(minCost, cost);
knownCosts[pos] = minCost;
}
}
}
return minCost == int.MaxValue ? -1 : minCost;
}
public static int solution(int[] A, int D)
{
var knownCosts = Enumerable.Range(0, A.Length).Select(v => -1).ToArray();
return move(A, knownCosts, D, -1);
}
static void CheckSolution(int[] A, int D, int expected)
{
var result = solution(A, D);
if (result != expected)
{
var msg = $"{expected} != {result}";
Console.WriteLine(msg);
throw new Exception(msg);
}
}
static void Main(string[] args)
{
CheckSolution(new[] { 0 }, 1, 0);
CheckSolution(new[] { 3, 2, 1 }, 1, 3);
CheckSolution(new[] { 2 }, 1, 2);
CheckSolution(new[] { 1, -1, 0, 2, 3, 5 }, 3, 2);
CheckSolution(new[] { 1, -1, -1, 3, 2, 5 }, 3, 3);
CheckSolution(new[] { -1, -1, -1, 3, 2, 5 }, 3, -1);
CheckSolution(new[] { -1, -1, -1, 3, 2, 5 }, 4, 3);
CheckSolution(new[] { -1, -1, -1, 3, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, 4, 17);
CheckSolution(new[] { -1, -1, -1, 3, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }, 5, 26);
}
}
}
using System;
namespace TestCodility2
{
class Program
{
public static int solutionBetter(int[] A, int X)
{
int N = A.Length;
if (N == 0)
{
return (-1);
}
int l = 0;
int r = N - 1;
while (l < r)
{
int m = (l + r) / 2;
var delta = A[m] - X;
if (delta == 0)
{
return m;
}
else if (delta > 0)
{
r = m - 1;
}
else
{
l = m + 1;
}
}
return -1;
}
public static int solution(int[] A, int X)
{
int N = A.Length;
if (N == 0)
{
return (-1);
}
int l = 0;
int r = N - 1;
while (l < r)
{
int m = (l + r) / 2;
if (A[m] > X)
{
r = m - 1;
}
else if (l < m)
{
l = m;
}
else if (A[l] == X)
{
r = l;
}
else
{
l = r;
}
}
if (A[l] == X)
{
return l;
}
return -1;
}
static void CheckSolution(int[] A, int X, int expected)
{
var result = solution(A, X);
if (result != expected)
{
var msg = $"{expected} != {result}";
Console.WriteLine(msg);
throw new Exception(msg);
}
}
static void CheckSolution(int[] A, int X, int expectedMin, int expectedMax)
{
var result = solution(A, X);
//var result = solutionBetter(A, X);
if (result < expectedMin || result > expectedMax)
{
var msg = $"{result} != [{expectedMin}...{expectedMax}]";
Console.WriteLine(msg);
throw new Exception(msg);
}
}
static void Main(string[] args)
{
CheckSolution(new[] { 1, 2, 5, 9, 9 }, 5, 2);
CheckSolution(new[] { 1, 2, 5, 9, 9 }, 10, -1);
CheckSolution(new[] { -9, -3, -1 }, -1, 2);
CheckSolution(new[] { -9, -3, -1 }, -9, 0);
CheckSolution(new[] { -9, -3, -1 }, -3, 1);
CheckSolution(new[] { -9, -3, -3, -3, -3, -1 }, -3, 1, 4);
CheckSolution(new[] { -9, -9, -9, -9, -3, -3, -3, -3, -1 }, -3, 4, 7);
CheckSolution(new[] { -9, -3, -3, -3, -3, -2, -1 }, -2, 5);
CheckSolution(new[] { -9, -3, 0, 2, 5 }, -500, -1);
CheckSolution(new[] { 0 }, -500, -1);
CheckSolution(new[] { 0 }, 0, 0);
CheckSolution(new int[0], 0, -1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment