题目
题目链接:
https://www.nowcoder.com/practice/11662ff51a714bbd8de809a89c481e21
思路
前缀和数组+动态规划
Java代码
java
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
public int[] FindGreatestSumOfSubArray (int[] array) {
//前缀和+动态规划
int n = array.length;
if (n <= 1) return array;
int[] presum = new int[n + 1]; //前缀和
for (int i = 0; i < n; i++) {
presum[i + 1] = presum[i] + array[i];
}
int[] dp = new int[n];
dp[0] = array[0];
int pre = array[0];
int maxSum = array[0];
int maxSumEnd = 0;
//存放最的子数组和的子数组下标列表
List<Integer> maxsumll = new ArrayList<>();
for (int i = 1; i < n ; i++) {
int p1 = array[i];
int p2 = array[i] + pre;
int cur = Math.max(p1, p2);
//maxSum=Math.max(maxSum,cur);
if (maxSum <= cur) {
if (maxSum < cur) {
maxsumll.clear();
}
maxsumll.add(i);
maxSum = cur;
maxSumEnd = i;
}
pre = cur;
}
//System.out.println(maxSum+", "+maxSumEnd);
//System.out.println(sumIdx);
int maxSize = 0;
int maxSizeIdx = 0;
for (Integer idx : maxsumll) {
//for (int i = idx; i <=idx ; i++) {
for (int j = 0; j <= idx ; j++) {
if (presum[idx + 1] - presum[j] == maxSum) {
//size=Math.max(size,idx+1-j);
if (maxSize < idx + 1 - j) {
maxSize = idx + 1 - j;
maxSizeIdx = idx;
}
break;
}
// }
}
}
// System.out.println("size:"+ maxSize+" maxSizeidx:"+ maxSizeIdx);
int[] ans = new int[maxSize];
for (int i = 0; i < maxSize ; i++) {
ans[i] = array[maxSizeIdx - maxSize + 1 + i];
}
return ans;
}
}
Go代码
go
package main
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
func FindGreatestSumOfSubArray(array []int) []int {
//前缀和+动态规划
n := len(array)
if n <= 1 {
return array
}
presum := make([]int, n+1)
for i := 0; i < n; i++ {
presum[i+1] = presum[i] + array[i]
}
pre := array[0]
maxSum := array[0]
maxSumll := []int{} //存放最的子数组和的子数组下标列表
for i := 1; i < n; i++ {
p1 := array[i]
p2 := array[i] + pre
cur := p1
if cur < p2 {
cur = p2
}
if maxSum <= cur {
if maxSum < cur {
maxSumll = []int{}
}
maxSum = cur
maxSumll = append(maxSumll, i)
}
pre = cur
}
maxSize := 0
maxSizeIdx := 0
for _, idx := range maxSumll {
for j := 0; j <= idx; j++ {
if presum[idx+1]-presum[j] == maxSum {
if maxSize < idx+1-j {
maxSize = idx + 1 - j
maxSizeIdx = idx
}
break
}
}
}
ans := make([]int, maxSize)
for i := 0; i < maxSize; i++ {
ans[i] = array[maxSizeIdx+1-maxSize+i]
}
return ans
}
PHP代码
php
<?php
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
function FindGreatestSumOfSubArray( $array )
{
//前缀和+动态规划
$n= count($array);
if($n <=1){
return $array;
}
$presum = [0=>0];
for($i=0;$i<$n;$i++){
$presum[$i+1] = $presum[$i]+$array[$i];
}
$pre = $array[0];
$max = $array[0];
$list = array(); //存放$max对应的子数组的结束下标
for($i=1;$i<$n;$i++){
$cur = $array[$i];
$p2 = $array[$i]+$pre;
if($cur < $p2){
$cur = $p2;
}
if($max<=$cur){
if($max<$cur) {
$list = array();
}
$list[count($list)] = $i;
$max = $cur;
}
$pre=$cur;
}
$maxLen =0;
$maxLenIdx =0;
foreach ($list as $idx){
for($j=0;$j<=$idx;$j++){
if($presum[$idx+1] -$presum[$j] ==$max){
if($maxLen < $idx+1-$j){
$maxLen = $idx+1-$j;
$maxLenIdx = $idx;
}
break;
}
}
}
$ans=[];
for($i=0;$i<$maxLen;$i++){
$ans[$i] = $array[$maxLenIdx-$maxLen+1+$i];
}
return $ans;
}
C++代码
cpp
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型vector
* @return int整型vector
*/
vector<int> FindGreatestSumOfSubArray(vector<int>& array) {
//前缀和+动态规划
int n = array.size();
if (n <= 1)
return array;
//前缀和
vector<int> presum(n + 1, 0);
for (int i = 0; i < n; i++) {
presum[i + 1] = presum[i] + array[i];
}
int pre = array[0];
int max = array[0]; //最大的子数组和
vector<int> list; //存放max对应的子数组的下标列表
for (int i = 1; i < n; i++) {
int p1 = array[i];
int p2 = array[i] + pre;
int cur = p1;
if (cur < p2) {
cur = p2;
}
if (max <= cur) {
if (max < cur) {
list.clear();
}
max = cur;
list.push_back(i);
}
pre = cur;
}
int maxLen = 0;
int maxLenIdx = 0;
for (int i = 0; i < list.size(); i++) {
int idx = list[i];
for (int j = 0; j <= idx; j++) {
if (presum[idx + 1] - presum[j] == max) {
if (maxLen < idx + 1 - j) {
maxLen = idx + 1 - j;
maxLenIdx = idx;
}
break;
}
}
}
vector<int> ans(maxLen);
for (int i = 0; i < maxLen; i++) {
ans[i] = array[maxLenIdx - maxLen + 1 + i];
}
return ans;
}
};