Created
May 8, 2017 22:55
-
-
Save jianminchen/4e300344b11bca3d3c99fa5e37795217 to your computer and use it in GitHub Desktop.
Rookie 3 - max score - bit mask study code - pass all test cases
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 | |
return getMaxScore(a, 0, a.Sum()); | |
} | |
static Dictionary<int, long> memo = new Dictionary<int, 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) | |
{ | |
if (memo.ContainsKey(bitmask)) | |
{ | |
return memo[bitmask]; | |
} | |
var max = 0L; | |
for (var i = 0; i < numbers.Length; i++) | |
{ | |
var bitToCheck = 1 << i; | |
if ((bitmask & bitToCheck) != 0) | |
{ | |
continue; | |
} | |
bitmask |= bitToCheck; | |
var num = numbers[i]; | |
var score = ((sum - num) % num) + getMaxScore(numbers, bitmask, sum - num); | |
bitmask &= ~ bitToCheck; // backtracking, unmask the bit - ith bit | |
max = Math.Max(score, max); | |
} | |
memo[bitmask] = max; | |
return max; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment