Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 17, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/f8a6b90e021f3a01c7c52e800130d509 to your computer and use it in GitHub Desktop.
Save jianminchen/f8a6b90e021f3a01c7c52e800130d509 to your computer and use it in GitHub Desktop.
kindergarten adventure - hackerRank - study code C# - No. 5 score 30 of 30
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
/// <summary>
/// https://www.hackerrank.com/contests/university-codesprint/challenges/kindergarten-adventures
/// </summary>
class Solution3
{
static void Main(String[] args)
{
TextReader tIn = Console.In;
TextWriter tOut = Console.Out;
int N = int.Parse(tIn.ReadLine());
int[] T = tIn.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
int[] B = new int[N];
int[] E = new int[N];
for (int i = 0; i < N; i++)
{
if (T[i] >= N) continue;
if (T[i] <= i)
{
B[0]++;
E[i - T[i]]++;
if (i < N - 1)
{
B[i + 1]++;
E[N - 1]++;
}
}
if (T[i] > i)
{
if (i < N - 1)
{
B[i + 1]++;
E[(N + i - T[i]) % N]++;
}
}
}
int ixm = 0;
int max = 0;
int crr = 0;
for (int i = 0; i < N; i++)
{
crr += B[i];
if (crr > max)
{
max = crr;
ixm = i;
}
crr -= E[i];
}
tOut.WriteLine(ixm + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment