Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2016 05:50
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/8b7822488dbf607ef187da2cb46e146c to your computer and use it in GitHub Desktop.
Save jianminchen/8b7822488dbf607ef187da2cb46e146c to your computer and use it in GitHub Desktop.
HackerLand Radio transmitter - University codesprint - contest performance - score 6 of 15 with one bug - on line 64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HackerlandRadioTransmitters
{
/*
* start from 8:29pm
* https://www.hackerrank.com/contests/university-codesprint/challenges/hackerland-radio-transmitters
*
*/
class Program
{
static void Main(string[] args)
{
process();
//testCase1();
}
private static void process()
{
string[] arr = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(arr[0]);
int k = Convert.ToInt32(arr[1]);
int[] house = ToInt(Console.ReadLine().Split(' '));
Console.WriteLine(calculateTransmitters(n, k, house));
}
private static void testCase1()
{
int[] house = new int[8]{7,2,4,6,5,9,12, 11};
int test = calculateTransmitters(8,2,house);
}
/*
* start: 8:33pm
* exit: 9:33pm
*/
private static int calculateTransmitters(int n, int k, int[] house)
{
int count = 0;
Array.Sort(house);
int transmiterPos = 0;
int start = -1;
foreach(int item in house)
{
if (start < 0)
{
start = item;
count++;
transmiterPos = item;
}
int range = item - start;
transmiterPos = (range <= k) ? item : transmiterPos;
if(range > 2 * k)
{
start = item;
transmiterPos = item;
count++;
}
}
return count;
}
private static int[] ToInt(string[] arr)
{
int len = arr.Length;
int[] res = new int[len];
for (int i = 0; i < len; i++)
{
res[i] = Convert.ToInt32(arr[i]);
}
return res;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment