Created
March 25, 2017 00:08
-
-
Save jianminchen/2c9298e47eb888c4bdc3a222735940dc to your computer and use it in GitHub Desktop.
Hour Rank 7 - Nikita and the game - study code review - http://codereview.stackexchange.com/a/146412/123986
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 NikitaAndtheGram | |
{ | |
/// <summary> | |
/// https://www.hackerrank.com/contests/hourrank-7/challenges/array-splitting | |
/// code review: | |
/// http://codereview.stackexchange.com/a/146412/123986 | |
/// </summary> | |
class NikitaAndtheGame | |
{ | |
static void Main(string[] args) | |
{ | |
var queries = Convert.ToInt32(Console.ReadLine()); | |
for(int i = 0; i < queries; i++) | |
{ | |
var length = Convert.ToInt32(Console.ReadLine()); | |
var array = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); | |
Console.WriteLine(GetMaxScore(array, 0, array.Length)); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="array"></param> | |
/// <param name="start"></param> | |
/// <param name="end"></param> | |
/// <returns></returns> | |
// start inclusive, end exclusive | |
public static int GetMaxScore(int[] array, int start, int end) | |
{ | |
for (int i = 1; i < end - start; i++) | |
{ | |
int split = start + i; | |
int leftHand = SumOfArray(array, start, split); | |
int rightHand = SumOfArray(array, split, end); | |
if (leftHand == rightHand) | |
{ | |
int leftScore = GetMaxScore(array, start, split); | |
int rightScore = GetMaxScore(array, split, end); | |
return 1 + Math.Max(leftScore, rightScore); | |
} | |
} | |
return 0; | |
} | |
// start inclusive, end exclusive | |
public static int SumOfArray(int[] array, int start, int end) | |
{ | |
int sum = 0; | |
for (int i = start; i < end; i++) | |
{ | |
sum += array[i]; | |
} | |
return sum; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment