Home Backend Development PHP Tutorial php求正负数数组中连续元素最大值示例_php实例

php求正负数数组中连续元素最大值示例_php实例

Jun 07, 2016 pm 05:20 PM
maximum value

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 )

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use math.Max ​​function to get the maximum value in a set of numbers Use math.Max ​​function to get the maximum value in a set of numbers Jul 24, 2023 pm 01:24 PM

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

Get the maximum value in a sequence or set using Python's max() function Get the maximum value in a sequence or set using Python's max() function Aug 22, 2023 pm 02:10 PM

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 maximum value in PHP array How to get maximum value in PHP array Jul 07, 2023 am 11:24 AM

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

In C++, remove one bit of a binary number to obtain the maximum value In C++, remove one bit of a binary number to obtain the maximum value Sep 17, 2023 pm 03:53 PM

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

Write a code using C++ to find the number of subarrays with the same minimum and maximum values Write a code using C++ to find the number of subarrays with the same minimum and maximum values Aug 25, 2023 pm 11:33 PM

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,

Use the sorting logic of TreeSet in Java to get the maximum and minimum elements in the set Use the sorting logic of TreeSet in Java to get the maximum and minimum elements in the set Sep 02, 2023 pm 12:33 PM

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 How to use the MAX function in MySQL to find the maximum value of a field Jul 12, 2023 pm 08:28 PM

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

C++ program to find pairs of sequences holding minimum and maximum elements in a sequence C++ program to find pairs of sequences holding minimum and maximum elements in a sequence Sep 05, 2023 pm 11:29 PM

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,

See all articles