Created
          February 3, 2020 06:02 
        
      - 
      
- 
        Save anhnguyen1618/02ec3a9201a3a07b6003a0aa5b44761f to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or 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
    
  
  
    
  | import java.io.*; | |
| import java.math.*; | |
| import java.security.*; | |
| import java.text.*; | |
| import java.util.*; | |
| import java.util.concurrent.*; | |
| import java.util.function.*; | |
| import java.util.regex.*; | |
| import java.util.stream.*; | |
| import static java.util.stream.Collectors.joining; | |
| import static java.util.stream.Collectors.toList; | |
| class Result { | |
| public static long computeSum(List<Integer> integerArray) { | |
| int sum = 0; | |
| for (int i: integerArray) sum += i; | |
| return sum; | |
| } | |
| /* | |
| * Complete the 'getMaximumScore' function below. | |
| * | |
| * The function is expected to return a LONG_INTEGER. | |
| * The function accepts INTEGER_ARRAY integerArray as parameter. | |
| */ | |
| public static long getMaximumScore(List<Integer> integerArray) { | |
| System.out.println(integerArray); | |
| int left = 0; | |
| int right = integerArray.size()-1; | |
| int operationCounter = 1; | |
| long sum = computeSum(integerArray); | |
| long score = 0; | |
| while (left <= right) { | |
| System.out.println(sum); | |
| if (operationCounter % 2 == 0) { | |
| score -= sum; | |
| int removedIndex = left; | |
| // | |
| long firstGap = 0; | |
| long firstnextSum = sum - integerArray.get(left); | |
| firstGap += firstnextSum; | |
| long firstnextnextSum = firstnextSum - Math.max(integerArray.get(left +1), integerArray.get(right)); | |
| firstGap -= firstnextnextSum; | |
| long secGap = 0; | |
| long secnextSum = sum - integerArray.get(right); | |
| secGap += secnextSum; | |
| long secnextnextSum = secnextSum - Math.max(integerArray.get(left), integerArray.get(right -1)); | |
| secGap -= secnextnextSum; | |
| System.out.println("gap " + firstGap + " " + secGap); | |
| if (firstGap < secGap) removedIndex = right; | |
| // | |
| // if (integerArray.get(left) > integerArray.get(right)) { | |
| // removedIndex = right; | |
| // long gap = 0; | |
| // long nextSum = sum - integerArray.get(removedIndex); | |
| // gap += nextSum; | |
| // long nextnextSum = nextSum - Math.max(integerArray.get(left), integerArray.get(right -1)); | |
| // gap -= nextnextSum; | |
| // if (gap < 0) removedIndex = left; | |
| // } // | |
| sum -= integerArray.get(removedIndex); | |
| if (removedIndex == left) { | |
| left++; | |
| } else { | |
| right--; | |
| } | |
| } else { | |
| score += sum; | |
| int removedIndex = left; | |
| // | |
| long firstGap = 0; | |
| long firstnextSum = sum - integerArray.get(left); | |
| firstGap -= firstnextSum; | |
| //System.out.println(firstnextSum); | |
| long firstnextnextSum = firstnextSum - Math.min(integerArray.get(left +1), integerArray.get(right)); | |
| firstGap += firstnextnextSum; | |
| //System.out.println(firstnextnextSum); | |
| long secGap = 0; | |
| long secnextSum = sum - integerArray.get(right); | |
| secGap -= secnextSum; | |
| long secnextnextSum = secnextSum - Math.min(integerArray.get(left), integerArray.get(right -1)); | |
| secGap += secnextnextSum; | |
| System.out.println("gap " + firstGap + " " + secGap); | |
| if (firstGap < secGap) removedIndex = right; | |
| // | |
| // if (integerArray.get(left) < integerArray.get(right)) { | |
| // removedIndex = right; | |
| // long gap = 0; | |
| // long nextSum = sum - integerArray.get(removedIndex); | |
| // gap -= nextSum; | |
| // long nextnextSum = nextSum - Math.min(integerArray.get(left), integerArray.get(right -1)); | |
| // gap += nextnextSum; | |
| // System.out.println("gap " + gap + " " + nextSum + " " + nextnextSum); | |
| // if (gap < 0) removedIndex = left; | |
| // } | |
| sum -= integerArray.get(removedIndex); | |
| if (removedIndex == left) { | |
| left++; | |
| } else { | |
| right--; | |
| } | |
| } | |
| operationCounter++; | |
| } | |
| return score; | |
| // Write your code here | |
| } | |
| } | |
| public class Solution { | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment