Skip to content

Instantly share code, notes, and snippets.

@vdonchev
Created October 11, 2015 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdonchev/e206655ef66b27f592eb to your computer and use it in GitHub Desktop.
Save vdonchev/e206655ef66b27f592eb to your computer and use it in GitHub Desktop.
namespace Needles
{
using System;
using System.Collections.Generic;
using System.Linq;
class Needles
{
static List<int> results = new List<int>();
static void Main()
{
string input = Console.ReadLine();
List<int> hayStack = Console.ReadLine()
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
List<int> needles = Console.ReadLine()
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse)
.ToList();
foreach (var needle in needles)
{
bool match = false;
for (int i = 0; i < hayStack.Count; i++)
{
if (hayStack[i] >= needle)
{
match = true;
ReturnIndex(hayStack, i - 1);
break;
}
}
if (!match)
{
ReturnIndex(hayStack, hayStack.Count - 1);
}
}
Console.WriteLine(string.Join(" ", results));
}
private static void ReturnIndex(List<int> nums, int i)
{
while (i >= 0)
{
if (nums[i] != 0)
{
results.Add(i + 1);
return;
}
i--;
}
results.Add(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment