Created
November 17, 2016 07:03
-
-
Save jianminchen/f8a6b90e021f3a01c7c52e800130d509 to your computer and use it in GitHub Desktop.
kindergarten adventure - hackerRank - study code C# - No. 5 score 30 of 30
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.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