当前位置:网站首页>Divide the array into three equal parts [problem analysis]
Divide the array into three equal parts [problem analysis]
2022-06-11 23:51:00 【REN_ Linsen】
Divide the array into three parts equal to and
Preface
Solve more than one problem , Open your eyes , See following , Achieve mastery through a comprehensive .
One 、 Divide the array into three parts equal to and

Two 、 Multiple solutions to problems
1、 branch 3 Group
// Divide the array into three parts equal to and .
public class CanThreePartsEqualSum {
/* target: Divided into three ordered subarrays , Require three subarrays and equal . The sum of three ordered subarrays is equal , That means every array and = sum / 3. When sum % 3 != 0 when , It is impossible to divide into three equal ordered subarrays , After all, numbers can't be taken apart . Then iterate through the array , Find the sum of successive subarrays as sum / 3 Of , After traversing , If you can find three , Group successfully . */
public boolean canThreePartsEqualSum(int[] arr) {
// Get array and .
// This tone API The way is too slow .
// int total = Arrays.stream(arr).sum();
int total = 0;
for (int i : arr) total += i;
// Decide whether it is right 3 Remainder .
if (total % 3 != 0) return false;
// Get the sum of each group .
int target = total / 3;
// Traverse arr, Find out how many groups can be divided into target
int n = 0, cur = 0;
for (int i : arr) {
if ((cur += i) == target && (++n) == n) cur = 0;
// according to bug1/2 modify .
if (n >= 3) return true;
}
// Divide into three groups and you will succeed , Otherwise failure .
// bug1: It may all be 0.
// return n == 3;
// bug2: There are many pairs and for 0 Of .
// return n == 3 || n == arr.length;
// return n >= 3;
return false;
}
}
2、 branch 2 Group
// Complete the traversal from left to right , There will be details ---- There is 0 To whom ? So using n >= 3 To determine .
// How to calculate from both ends at the same time , And only calculate 2 Group , Then the remaining group in the middle must be target, Can solve 0 Who belongs to -- All belong to the middle .
class CanThreePartsEqualSum2 {
/* target: Divided into three ordered subarrays , Require three subarrays and equal . The sum of three ordered subarrays is equal , That means every array and = sum / 3. When sum % 3 != 0 when , It is impossible to divide into three equal ordered subarrays , After all, numbers can't be taken apart . Then double pointer traverses the array , Find the sum of successive subarrays before and after sum / 3 Of , Find two elements in between , The success is divided into three groups . */
public boolean canThreePartsEqualSum(int[] arr) {
// Get array and .
int total = 0;
for (int i : arr) total += i;
// Decide whether it is right 3 Remainder .
if (total % 3 != 0) return false;
// Get the sum of each group .
int target = total / 3;
// Double pointer traverses the array back and forth , Find three groups of left, middle and right target
int left = 1, right = arr.length - 2;
int leftSum = arr[0], rightSum = arr[arr.length - 1];
while (left < right) {
if (leftSum != target) leftSum += arr[left++];
if (rightSum != target) rightSum += arr[right--];
if (leftSum == rightSum && rightSum == target && left <= right) return true;
}
return false;
}
}
summary
1) Solve more than one problem , Open your eyes , See following .
reference
边栏推荐
- My creation anniversary
- (linear DP) acwing 898 Number triangle
- Dblink operation in Oracle
- 2022 618笔记本选购指北
- 2022 operation of simulation examination platform for safety officer C certificate
- Lake Shore—SuperTran 连续流低温恒温器系统
- (linear DP | monotone queue) acwing 895 Longest ascending subsequence
- (dp) acwing 902. Minimum editing distance
- MySQL some simple commands
- 2022 safety officer-b certificate theoretical question bank and simulation test
猜你喜欢

2022 high place installation, maintenance and removal of simulated examination platform for operation certificate examination question bank

Wechat applet Bluetooth development

SAP SD create / modify price list

Teach you to play with SSM framework

Introduction to Solr Basics

Lake Shore - supertran continuous flow cryogenic thermostat system

Graph and graph traversal

【JUC系列】Executor框架之概览

CVPR 2022 | meta learning performance in image regression task

思科私有动态路由协议:EIGRP
随机推荐
C collection of questions for project review
Queue (C language)
Read 5g RF terminal industry
Dom Knowledge point Summary
05 classification learning notes lihongyi's in-depth study 2021
(dp) acwing 899. Edit distance
Read the logstash principle
明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级
Mmdetection custom fetch detection result script and image_ demo. Py parsing
Meet o & M (I) common questions for O & M interview
Antigen products enter the family, and Chinese medical device enterprises usher in a new blue ocean
MySQL some simple commands
I2C read / write process
sonarqube介紹和安裝步驟
Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade
JS to add an attribute to an array object
PHP mkdir(): permission denied uploading a file will change the folder permission to 411 permission
解决IDEA下载插件慢的问题
New Year Countdown JS case
Lake Shore—SuperVariTemp 低温恒温器