Given an array with n integers, you need to find if there is triplets (I, J, K) which satisfies following conditions:
- 0 < I, I + 1 < J, J + 1 < K < n-1
- Sum of Subarrays (0, i-1), (i + 1, j-1), (j + 1, k-1) and (k + 1, n-1) should be equal.
Where we define that Subarray (L, R) represents a slice of the original array starting from the element indexed L to the E Lement indexed R.
Example:
Input: [1,2,1,2,1,2,1]output:trueexplanation:i = 1, j = 3, k = 5. SUM (0, i-1) = SUM (0, 0) = 1sum (i + 1, j-1) = SUM (2, 2) = 1sum (j + 1, k-1) = SUM (4, 4) = 1sum (k + 1, n-1) = SUM (6, 6) = 1
Note:
- 1 <= N <= 2000.
- Elements in the given array would be in range [-1,000,000, 1,000,000].
Title tag: Array
The topic gives us a nums array, which we use three split lines I, J, K to divide the array into 4 parts, and the sum of each part is the same. I j K all have their own boundaries.
Time complexity O (n*n) method is more ingenious, change the order of the search, and combined with the use of HashSet can be n^3 will be n^2 time.
Start by creating an accumulated sum array to record each num in the Nums (from 0 to num), making it easy to use when searching later.
Traverse the middle of the split Line J Range (left to right):
When the dividing line J is determined, the dividing lines I and k are traversed respectively within the appropriate range;
Traverse the range of the split line I, from left to J: Find all cases where sum1 (I left) and sum2 (right) are equal, join set. (SUM1 and sum2 are equal, they may be the correct answer.)
Traverse the range of the dividing line K, from J to the right: Find Sum3 (k left) and Sum4 (k right) equal to all cases, each encounter once sum3 = = sum4 situation, go to set to find the value of sum3 whether or not appear, some words sum1 = sum2 = SUM3 = SUM4, find the answer and return directly.
Java Solution:
Runtime beats 74.91%
Completion Date: 09/26/2017
Keywords: Array, HashSet
Key points: The search order is to determine the median j, and then find I and K combined HashSet to determine sum1 = sum2 = SUM3 = Sum4
1 classSolution2 {3 Public BooleanSplitarray (int[] nums)4 {5 if(Nums.length < 7)//At least need 7 numbers6 return false;7 8 int[] sum =New int[nums.length];9Sum[0] = nums[0];Ten //Create sum Array:each sum have sum from 1 to i One for(intI=1; i<nums.length; i++) ASum[i] = sum[i-1] +Nums[i]; - - //For J-middle cut the for(intj=3; j<nums.length-3; J + +) - { -Hashset<integer> set =NewHashset<>(); - //For I-left cut + for(intI=1; i<j-1; i++) - { + intSUM1 = sum[i-1]; A intsum2 = sum[j-1]-Sum[i]; at if(Sum1 = =sum2) -Set.add (SUM1);//Add potential answers into set - } - //For K-right cut - for(intk=j+2; k<nums.length-1; k++) - { in intSUM3 = sum[k-1]-Sum[j]; - intSUM4 = sum[nums.length-1]-Sum[k]; to if(sum3 = = Sum4 && set.contains (SUM3))// + return true; - } the * } $ Panax Notoginseng return false; - } the}
Resources:
Https://discuss.leetcode.com/topic/85026/simple-java-solution-o-n-2
Leetcode List of topics- leetcode Questions List
Leetcode 548. Split array with Equal Sum (split arrays make sub-arrays the same as all) $