php求正负数数组中连续元素最大值示例_php实例
php实现正负数数组最大子序列,要求给出数组,该数组由正负数字组成,找出该数组中连续元素组成的子数组的最大值。
这其实得算是个背包变种吧。
$list = array(1,-3,-5,-7,8,9,-11,5);
$cur = 0;
$term = 0;
$res = 0;
$begin = 0;
foreach($list as $k => $v){
$cur += $v;
if($cur $cur = 0;
$begin = $k + 1;
}
if($cur > $res){
$res = $cur;
$term = $k;
}
}
$max_seq = array_slice($list, $begin, ($term - $begin) + 1);
echo $res . ',';
print_r($max_seq);
//17,Array ( [0] => 8 [1] => 9 )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use the math.Max function to obtain the maximum value in a set of numbers. In mathematics and programming, it is often necessary to find the maximum value in a set of numbers. In Go language, we can use the Max function in the math package to achieve this function. This article will introduce how to use the math.Max function to obtain the maximum value in a set of numbers, and provide corresponding code examples. First, we need to import the math package. In the Go language, you can use the import keyword to import a package, as shown below: import"mat

Use Python's max() function to get the maximum value in a sequence or set. In Python programming, we often need to find the largest element from a sequence or set. Python provides a built-in function max(), which can implement this function very conveniently. The max() function can accept any iterable object as a parameter, including lists, tuples, sets, etc. It returns the largest element in the passed object. The following is the basic syntax of the max() function: max(iterable[,def

How to get the maximum value in a PHP array When writing PHP code, you often need to perform various operations on the array, including getting the maximum value in the array. In this article, we will introduce how to use PHP's built-in and custom functions to get the maximum value in an array, and provide corresponding code examples. Using the PHP built-in function max() PHP provides a built-in function max() that can easily get the maximum value from an array. Here is a code example using this function: <?php$numbers

Discuss a problem given a binary number. We have to remove a little bit from it so that the remaining number should be the maximum among all other options like Input:N=1011Output:111Explanation:Weneedtoremoveonebitsoremoving0bitwillgiveamaximumnumberthanremovingany1’sbit.111>101,011.Input:111Output:11Explanation:Sinceallthebitsare1sowecanremovean

In this article, we will use C++ to solve the problem of finding the number of subarrays whose maximum and minimum values are the same. The following is an example of the problem −Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2 },{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3, 1,5,

TreeSet is a class in JavaCollectionFramework that implements the SortedSet interface. It stores elements in ascending order and does not allow duplicate values, so access and retrieval times become faster. Because of this excellent feature, TreeSets are often used to store large amounts of information that need to be searched quickly. We will use the Comparable interface to sort a given TreeSet and then, using the built-in methods, try to get the highest and lowest value elements from that TreeSet. Java Program to Get the Highest and Lowest Value Elements from a TreeSet Before entering the program, let us first familiarize ourselves with some conceptually similar interfaces when we want to press the natural order of custom objects

How to use the MAX function in MySQL to find the maximum value of a field In MySQL, we can use the MAX function to find the maximum value of a field. The MAX function is an aggregate function used to find the maximum value of a specified field. The syntax for using the MAX function is as follows: SELECTMAX(column_name)FROMtable_name; where column_name is the name of the field to find the maximum value, and table_name is the name of the table to be queried. Down

Suppose we have three numbers N, M and K. There are N horizontal rows and M vertical rows. We will write integers between 1 and K in each cell and define the sequences A and B such that − for every i in the range 1 to N, A[i] is all The minimum value of an element. For every j in the range 1 to M, B[j] is the maximum value of all elements in column j. We need to find the number of (A,B). If the answer is too large, the result modulo 998244353 is returned. So if the input is N=2; M=2; K=2, the output will be 7 because (A[1], A[2], B[1], B[2]) can be (1, 1,1,1), (1,1,1,2), (1,1,
