Created
October 11, 2015 18:26
-
-
Save vdonchev/e206655ef66b27f592eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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