Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 17, 2016 07:02
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/d393f05b2fc8a5e97856ebe58f0f2d8d to your computer and use it in GitHub Desktop.
Save jianminchen/d393f05b2fc8a5e97856ebe58f0f2d8d to your computer and use it in GitHub Desktop.
Kindergarten adventure - study code D - score 30 (maximum score 30)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void D(Dictionary<int,int> d) {
Console.WriteLine("---");
foreach (var k in d.Keys) {
Console.WriteLine("\t" + k + ":\t" + d[k]);
}
Console.WriteLine("---");
}
static void Main(String[] args) {
int n = int.Parse(Console.ReadLine());
var arr = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
var d = new Dictionary<int, int>();
int willMakeIt = 0;
for (int i = 0; i < arr.Length; i++) {
int timeLeft = i - arr[i];
if (!d.ContainsKey(timeLeft)) d[timeLeft] = 0;
d[timeLeft]++;
if (timeLeft >= 0) willMakeIt++;
}
//D(d);
//Console.WriteLine(string.Format("{0}: {1}", 0, willMakeIt));
int best = willMakeIt;
int bestIndex = 0;
for (int i = 1; i < arr.Length; i++) {
int originalTimeLeftForPrevious = i - 1 - arr[i-1];
d[originalTimeLeftForPrevious]--;
if (d.ContainsKey(i-1)) {
willMakeIt -= d[i-1];
}
int newTimeLeftForPrevious = n - 1 - arr[i-1] + i;
if (!d.ContainsKey(newTimeLeftForPrevious)) d[newTimeLeftForPrevious] = 0;
d[newTimeLeftForPrevious]++;
if (newTimeLeftForPrevious - i >= 0 && originalTimeLeftForPrevious < i-1) willMakeIt++;
//D(d);
//Console.WriteLine(string.Format("{0}: {1}", i, willMakeIt));
if (willMakeIt > best) { best = willMakeIt; bestIndex = i; }
}
Console.WriteLine(bestIndex + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment