Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created November 23, 2016 19:00
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/85980e8f52c701027fbf25a8f2293e96 to your computer and use it in GitHub Desktop.
Save jianminchen/85980e8f52c701027fbf25a8f2293e96 to your computer and use it in GitHub Desktop.
stone division - Hackerrank woman codesprint #2 - study code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stoneDivisionStudyCode
{
class Program
{
/*
* Nov. 21, 2016
* study code:
* https://www.hackerrank.com/contests/womens-codesprint-2/challenges/stone-division-2/editorial
*
*/
public static Dictionary<long, long> dp = new Dictionary<long,long>();
public static Dictionary<long, bool> done = new Dictionary<long,bool>();
public static long[] arr = new long[1003];
public static long n;
public static int m;
static void Main(string[] args)
{
solution();
//testCase1();
}
/*
* 1
* 12 3
* 2 3 4
*/
private static void testCase1()
{
done.Clear();
n = 12;
m = 3;
arr = new long[3]{2, 3, 4};
long ans = rec(n);
Console.WriteLine(ans);
}
/*
* Nov. 22, 2016
* recurisve function
*
* how to mkae it easy to construct the recursive function?
* The formula -
* If there are 12 nodes in a pile, then, if choose 3, divide into 3 piles,
* how many in each pile? pile/ runner
* So, the task to calculate how many moves there is:
* 1. divide once, 1
* 2. plus 3 piles, for each pile to handle the same task
*
* Oh, do not get lost! Julia
*
* one more thing, using memorization.
* declare an arary d[] = new int[1003], save the result
* extra dictionary to look up if it is calculated.
*/
private static long rec(long pile)
{
if(done.ContainsKey(pile))
{
return dp[pile];
}
long ans = 0;
for(int i = 0; i < m; i++)
{
long runner = arr[i];
if( pile% runner == 0 &&
(pile/runner)> 1 )
{
ans = Math.Max(ans, 1 + ( (pile/runner) * rec(runner) ) );
}
}
done[pile] = true;
return dp[pile] = ans;
}
/*
* Nov. 22, 2016
*/
private static void solution() {
int q = int.Parse(Console.ReadLine());
for(int k=0; k<q; k++){
done.Clear();
string[] nAndm = Console.ReadLine().Split(' ');
n = long.Parse(nAndm[0]);
m = int.Parse(nAndm[1]);
arr = Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
long ans = rec(n);
Console.WriteLine(ans);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment