Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 14, 2016 18:34
Show Gist options
  • Save jianminchen/c17ce782080a99a2480ff1c6eb390628 to your computer and use it in GitHub Desktop.
Save jianminchen/c17ce782080a99a2480ff1c6eb390628 to your computer and use it in GitHub Desktop.
Array construction - HackerRank university codesprint - score 0, pass only one test case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayConstruction_P1
{
class ArrayConstruction
{
/*
* Array construction:
*
* start time: 11:16pm
* end time: 11:32pm
* finish of writing first version
* Try to score 1<=q <= 10
* 10% of 80 - 8 points
*
* submit second time:
* 12:33pm
* first time, run-time error on sample test case
*/
static void Main(string[] args)
{
process();
//testCase1();
}
private static void testCase1()
{
string[] arr = "3 3 4".Split(' ');
int n = Convert.ToInt32(arr[0]);
int s = Convert.ToInt32(arr[1]);
int k = Convert.ToInt32(arr[2]);
Console.WriteLine(lexicoSmallest(n, s, k));
}
private static void process()
{
int q = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < q; i++)
{
string[] arr = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(arr[0]);
int s = Convert.ToInt32(arr[1]);
int k = Convert.ToInt32(arr[2]);
Console.WriteLine(lexicoSmallest(n, s, k));
}
}
/*
* Nov. 11, 2016
* start time: 11:21pm
*
* brute force solution, try to get
* 1<= n <=5
* 0<= s <=10
* 0<= k <=20
*/
private static string lexicoSmallest(int n, int s, int k)
{
int[] arr = new int[n];
IList<string> list = new List<string>();
StringBuilder sb = new StringBuilder();
buildResult(list, n, s, k, 0, sb);
if (list.Count == 0)
return "-1";
return list[0].ToString();
}
/*
* use this code as an example
* https://github.com/jianminchen/Leetcode_C-/blob/master/17LetterCombinationOfAPhoneNUmber_DFS.cs
*
* Nov. 12, 2016
* DFS
*
* make the array increasing order
*
* Time complexity:
*
*/
private static void buildResult(
IList<string> list,
int length,
int sum,
int sumDiff,
int index,
StringBuilder current
)
{
if (checkLength(current, length))
{
string s = current.ToString();
if (!sumExactly(current, sum) ||
!sumDiffExactly(current, sumDiff))
return;
keepSmallerOne(list, current);
return;
}
int sumArr = calSum(current);
int min = getLastOne(current);
for(int i = min; i<= sum; i++)
{
bool isReady = sumChecking(sum, sumArr, i) &&
sumDiffChecking(sumDiff, current, i);
if (!isReady)
break;
string add = (current.ToString().Length==0 )?i.ToString() : (" " + i.ToString());
current.Append(add);
buildResult(list, length, sum, sumDiff, index +1, current);
int len = add.Length;
current.Remove(current.Length - len, len); // remove last char
}
}
/*
* start: 12:40pm
*
*/
private static bool sumExactly(StringBuilder sb, int sum)
{
int sumArr = calSum(sb);
return (sumArr == sum);
}
/*
* start: 12:44pm
* end time: 12:50pm
*/
private static bool sumDiffExactly(StringBuilder sb, int sumDiff)
{
string s = sb.ToString();
if (s == null || s.Length == 0)
return false;
int[] arr = toIntArr(s);
int count = 0;
for(int i=0; i< arr.Length; i++)
for(int j=i+1; j< arr.Length; j++)
{
count += Math.Abs(arr[i] - arr[j]);
}
return count == sumDiff;
}
/*
* Fix the bug - check length of array
* start: 12:32pm
*/
private static bool checkLength(StringBuilder sb, int length)
{
string s = sb.ToString();
if (s == null || s.Length == 0)
return false;
return s.Split(' ').Length >= length;
}
/*
* start: 10:00am
*/
private static int calSum(StringBuilder current)
{
string s = current.ToString();
if (s == null || s.Length == 0)
return 0;
//int[] arr = Array.ConvertAll(s.Split(' '), int.Parse);
int[] arr = toIntArr(s);
return arr.Sum();
}
private static int[] toIntArr(string s)
{
if (s == null || s.Length == 0)
return new int[] { };
string[] arr = s.Split(' ');
int[] res = new int[arr.Length];
for(int i = 0; i < arr.Length; i++)
{
res[i] = Convert.ToInt32(arr[i]);
}
return res;
}
/*
* Nov. 12, 2016
* start time: 10:46am
* http://stackoverflow.com/questions/4387901/how-to-convert-a-string-to-int-in-c-sharp-and-net-2-0
*/
private static bool sumCheckingRetired(
int sum,
StringBuilder current,
int newItem)
{
// int[] arr = Array.ConvertAll(current.ToString().Split(' '), int.Parse);
int[] arr = toIntArr(current.ToString());
if (newItem + arr.Sum() < sum)
return true;
else
return false;
}
private static bool sumChecking(
int sum,
int sumArray,
int newItem)
{
if (newItem + sumArray <= sum)
return true;
else
return false;
}
/*
* start: 11:05am
*/
private static int getLastOne(StringBuilder sb)
{
string s = sb.ToString();
if (s == null || s.Length == 0)
return 0;
string[] arr = sb.ToString().Split(' ');
return (Convert.ToInt32(arr[arr.Length - 1]));
}
/*
* start: 11:07am
*/
private static bool sumDiffChecking(
int sum,
StringBuilder current,
int newItem
)
{
string s = current.ToString();
if (s == null || s.Length == 0)
return true;
//int[] arr = Array.ConvertAll(current.ToString().Split(' '), int.Parse);
int[] arr = toIntArr(current.ToString());
int len = arr.Length;
int total = 0;
for(int i=0; i<= len; i++)
for(int j=i+1; j<=len; j++)
{
int first = (i == len) ? newItem : arr[i];
int second = (j == len) ? newItem : arr[j];
total += Math.Abs(first - second);
}
if (total <= sum)
return true;
else
return false;
}
/*
* start: 11:21am
*/
private static void keepSmallerOne(
IList<string> list,
StringBuilder current)
{
if (list.Count == 0)
{
list.Add(current.ToString());
return;
}
string prev = list[0].ToString();
// int[] arr1 = Array.ConvertAll(prev.Split(' '), int.Parse);
// int[] arr2 = Array.ConvertAll(current.ToString().Split(' '), int.Parse);
int[] arr1 = toIntArr(prev);
int[] arr2 = toIntArr(current.ToString());
bool firstOneSmaller = true;
for(int i = 0; i < arr1.Length; i++)
{
if(arr1[i] > arr2[i])
{
firstOneSmaller = false;
break;
}
}
if(!firstOneSmaller)
{
list.Clear();
list.Add(current.ToString());
}
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment