Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Java to find the maximum Sum of subarrays

Shulou Source: shulou.com Published: 2022-05-31 17:55:38 09月30日 Update

This article mainly explains how to use Java to find the maximum sum of subarrays. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn how to use Java to find the maximum sum of subarrays.

Enter an integer array with both positive and negative numbers. One or more consecutive integers in an array form a subarray, each with a sum. Find the maximum value of the sum of all subarrays. The time complexity is O (n). Because the time complexity is O (n), it means that we can only have for loops, not nested for loops; we can only analyze the flaws in this topic semantically. Static void maxSubArraySum3 (int [] a) {/ / omit parameter check boolean allNegative=true; int len=a.length; int [] p=new int [len]; for (int item0 / i0) {allNegative=false;}} if (item0) {p [0] = a [0] } else {p [I] = p [I-1] + a [I];}} if (allNegative) {System.out.println ("maxSubArraySum=0");} else {int max=p [0]; int min=p [0]; for (int iDeposit IMAX) {max=p [I] } if (p [I]

Tags: Array maximum Quadrant Learning complexity size content complexity time Loop parameters multiple that is ideas situations integers articles more maximum positive numbers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Huawei Microsoft OPPO Reno