Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 13, 2016 07:30
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/1411dc08a2a2c059454f788add19bceb to your computer and use it in GitHub Desktop.
Save jianminchen/1411dc08a2a2c059454f788add19bceb to your computer and use it in GitHub Desktop.
kindergarten adventure - score 0 - pass test case 0 and 1, wrong answer and timeout
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
class Solution
{
static void Main(String[] args)
{
SegmentTreeProcessing();
//TestSampleTestcase();
//Test1000Nodes();
}
/*
* Sample test case:
* n = 1
* 1, 0, 0
*
* segment tree:
*
* 0
* / \
* 0 2
* / \ /
* 2 1 1
*
*/
private static void TestSampleTestcase()
{
int n = 3;
int[] extraMinutes = new int[] { 1, 0, 0 };
var tree = CollectDrawingsClockWise(n, extraMinutes);
var id = tree.GetMaximumBinarySearch();
Debug.Assert(id == 2);
}
/*
* Code change:
*
*/
public static void SegmentTreeProcessing()
{
int n = int.Parse(Console.ReadLine());
var extraMinutes = Console.ReadLine().Split().Select(int.Parse).ToArray();
var tree = CollectDrawingsClockWise(n, extraMinutes);
Console.WriteLine(tree.GetMaximumBinarySearch());
}
/*
* Make this circle work first - train myself to think carefully
*
* 1. collect drawing clockwise
* 2. 0,1, 2, ..., n-1 total n people, start from i, then, i+1, ..., n, 0, 1, ..., i-1
*
*/
private static SegmentTree CollectDrawingsClockWise(int n, int[] extraMinutes)
{
var tree = new SegmentTree(n);
for (int startPosition = 0; startPosition < n; startPosition ++)
{
for (int visiting = startPosition; visiting < n; visiting++)
{
int extraMinutesNow = extraMinutes[visiting];
int minutedPassed = visiting - startPosition;
if (extraMinutesNow <= minutedPassed)
tree.ModifyNode(visiting, 1);
}
int minutesUsed = n -1 - startPosition;
for (int visiting = 0; visiting < startPosition; visiting++)
{
int extraMinutesNow = extraMinutes[visiting];
int minutePassed = minutesUsed + visiting;
if (extraMinutesNow <= minutePassed)
tree.ModifyNode(visiting, 1);
}
}
return tree;
}
public class SegmentTree
{
private readonly int[] tree;
public SegmentTree(int size)
{
tree = new int[size * 2];
}
/*
* code review: Dec. 10, 2016
* from leaf node,
* left % 2 == 1 => ?
*
*/
public void Modify_old(int start, int count, int value)
{
int size = tree.Length / 2;
int left = start + size;
int right = start + count + size; // open border
for (; left < right; left >>= 1, right >>= 1)
{
if (left % 2 == 1) tree[left++] += value;
if (right % 2 == 1) tree[--right] += value;
}
}
/*
* Segment Tree code review:
*
*/
public void Modify(int start, int count, int value)
{
int size = tree.Length / 2;
int left = start + size;
int right = start + count + size; // open border
for (int i = left; i < right; i++)
{
tree[i] += value;
}
}
public void ModifyNode(int start, int value)
{
int size = tree.Length / 2;
int left = start + size;
int right = start + 1 + size; // open border
for (int i = left; i > 0 ; i = i/2)
{
tree[i] += value;
}
}
/*
* code review: Dec. 10, 2016
* Find the leaf node, leftmost one
*
*/
public int Query(int index)
{
int i = index + tree.Length / 2;
return tree[i];
}
/*
* Binary search to maintain the time complexity to O(n)
* n - 100000
* Tree total nodes: maximum is 200000
*/
public int GetMaximumBinarySearch()
{
int totalNodes = tree.Length;
StringBuilder IDInBinary = new StringBuilder();
int index = 0;
while(index < totalNodes)
{
if ((2 * index + 1) < totalNodes && (2 * index + 2) < totalNodes)
{
if (tree[2 * index + 1] >= tree[2 * index + 2])
{
IDInBinary.Append('0');
index = 2 * index + 1;
}
else
{
IDInBinary.Append('1');
index = 2 * index + 2;
}
}
else if ((2 * index + 1) < totalNodes)
{
IDInBinary.Append('0');
break;
}
else
break;
}
return Convert.ToInt32(IDInBinary.ToString(), 2) + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment