Created
May 11, 2017 17:06
-
-
Save jianminchen/0c1044b44f8096788ad29edb53e1da33 to your computer and use it in GitHub Desktop.
Rookie 3 - max score - int array - time out test case 6 - 10 - bitmask using bit array, bit manipulation is fast enough to avoid timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace MaxScore_usingBitArray | |
{ | |
class MaxScore_usingBitArray | |
{ | |
/// <summary> | |
/// source code reference is here: | |
/// https://www.hackerrank.com/contests/rookierank-3/challenges/max-score/forum/comments/299005 | |
/// The best way to define a subset of the array is with a bitmask, one bit for each element | |
/// in the array. This means you can put the memos in an array, with up to 2^20 elements. | |
/// However, since the recursive version always calls itself on a mask with one bit removed, | |
/// we can actually just iterate through the memo array in order, filling it in as we go. | |
/// The memos will always exist in the array and we can just look them up instead of doing a | |
/// recursive function call. | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) | |
{ | |
ProcessInput(); | |
//RunTestcase(); | |
} | |
public static void RunTestcase() | |
{ | |
int n = 3; | |
long[] a = new long[] { 4, 8, 5 }; | |
long maxScore = getMaxScore(a); | |
Console.WriteLine(maxScore); | |
} | |
public static void ProcessInput() | |
{ | |
int n = Convert.ToInt32(Console.ReadLine()); | |
string[] data = Console.ReadLine().Split(' '); | |
long[] numbers = Array.ConvertAll(data, Int64.Parse); | |
long maxScore = getMaxScore(numbers); | |
Console.WriteLine(maxScore); | |
} | |
public static long getMaxScore(long[] a) | |
{ | |
// Complete this function | |
const int Size = 20; | |
var bitmask = new int[Size]; | |
return getMaxScore(a, bitmask, a.Sum()); | |
} | |
static Dictionary<string, long> memo = new Dictionary<string, long>(); | |
/// <summary> | |
/// bitwise NOT - flip the bits of the number, bit symbol ~ | |
/// | |
/// </summary> | |
/// <param name="numbers"></param> | |
/// <param name="bitmask"></param> | |
/// <param name="sum"></param> | |
/// <returns></returns> | |
private static long getMaxScore(long[] numbers, int[] bitmask, long sum) | |
{ | |
var key = encode(bitmask); | |
if (memo.ContainsKey(key)) | |
{ | |
return memo[key]; | |
} | |
var max = 0L; | |
for (var i = 0; i < numbers.Length; i++) | |
{ | |
if ((bitmask[i]) != 0) | |
{ | |
continue; | |
} | |
bitmask[i] = 1; | |
var num = numbers[i]; | |
var score = ((sum - num) % num) + getMaxScore(numbers, bitmask, sum - num); | |
bitmask[i] = 0 ; // backtracking, unmask the bit - ith bit | |
max = Math.Max(score, max); | |
} | |
memo[encode(bitmask)] = max; | |
return max; | |
} | |
private static string encode(int[] bitmask) | |
{ | |
return string.Join(",", bitmask); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment