首页 > TAG信息列表 > Subarray

LeetCode 1567. Maximum Length of Subarray With Positive Product

原题链接在这里:https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/ 题目: Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecuti

[LeetCode] 2104. Sum of Subarray Ranges

You are given an integer array nums. The range of a subarray of nums is the difference between the largest and smallest element in the subarray. Return the sum of all subarray ranges of nums. A subarray is a contiguous non-empty sequence of element

[LeetCode] 907. Sum of Subarray Minimums

Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7. Example 1: Input: arr = [3,1,2,4] Output: 17 Explanation: Subarrays are [3

Count Subarray by Element

2262. Total Appeal of A String Hard The appeal of a string is the number of distinct characters found in the string. For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c

LeetCode 907. Sum of Subarray Minimums

原题链接在这里:https://leetcode.com/problems/sum-of-subarray-minimums/ 题目: Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

LeetCode 2104. Sum of Subarray Ranges

原题链接在这里:https://leetcode.com/problems/sum-of-subarray-ranges/ 题目: You are given an integer array nums. The range of a subarray of nums is the difference between the largest and smallest element in the subarray. Return the sum of all subarray ra

LeetCode 2090. K Radius Subarray Averages

原题链接在这里:https://leetcode.com/problems/k-radius-subarray-averages/ 题目: You are given a 0-indexed array nums of n integers, and an integer k. The k-radius average for a subarray of nums centered at some index i with the radius k is the a

LeetCode 0152 Maximum Product Subarray

原题传送门 1. 题目描述 2. Solution 1 1、思路分析 1》 参考LC53 Maximum Subarray的转态转移方程,得到本问题的状态转移方程: f[i] = max{f(i-1) * nums[i], nums[i]} 以nums = [5, 6, -3, 4, -3]为例,得f = [5, 30, -3, 4, -3],得到结果为30。而实际上结果应该是nums所有元素的乘

C - Increase Subarray Sums

题目链接 题意 给一个数组 a[n] 与整数 x ,定义 k (0<=k&&k<=n) ,每次可以操作 k 个数使其加上 x ,f(k) 为当前序列a 中最大的连续子段和,求出每一个f(k) 思路 定义数组 d[i] 为区间为 i 时最大的连续子段和,然后遍历 k ,更新 d[i] 的值,d[ i ] = min( i , k ) * x + d[ i ] , 最后遍历

LeetCode 0053 Maximum Subarray

原题传送门 1. 题目描述 2. Solution 1、思路分析 dynamic programming a> 状态定义,f(i) 以nums[i]结尾的最大连续子序列和 b> 状态转移方程,f(i) = max{f(i - 1) + nums[i] , nums[i]} c> 边界,f(0) = nums[0] 用变量 curSum 替代 f(i - 1),res 替代 f(i) 以降低空间复杂度 2、代码

AT5749 Subarray Sum

前言 一道比较简单的题。( 完了完了完了要开学了要开学了。。。 题目大意 给定三个整数 \(N,K,S\)。 请你找到一个 \(N\) 个元素的整数序列,其中每一个元素在区间 \([1,10^9]\) 内。这个序列满足 \(K\) 个子序列的和为 \(S\)。 分析 要求有 \(K\) 个子序列的和是 \(S\),那么我们直接

[LeetCode] 1186. Maximum Subarray Sum with One Deletion 删除一次得到子数组最大和

Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least on

CF1197D Yet Another Subarray Problem(思维+前缀和)

思路: 先考虑暴力的做法,\(O(n^2)\)枚举所有的区间,计算后取最大值。 考虑怎么优化一下,可以发现,如果当前的左端点为\(l\),那么将\(a_l,a_{l+m},a_{l+2m}\)的值全部减去\(k\),那么当求和时选到这些数的时候\(\frac{r-l+1}{m}\)就会加一,也就满足了所求式子的后半段。由于\(m\)很小,我们可

2021-8-3 Shortest Unsorted Continuous Subarray

难度 中等 题目 Leetcode: Shortest Unsorted Continuous Subarray Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order. Return the sho

【LeetCode】53. Maximum Subarray(简单难度)

解法一 动态规划思路一 用一个二维数组 dp[ i ][ len ] 表示从下标i开始,长度为 len 的子数组的元素和。 这样长度是 len+1 的子数组就可以通过长度是 len 的子数组去求,也就是下边的递推式, dp [ i ] [ len + 1 ] = dp[ i ] [ len ] + nums [ i + len - 1]。 考虑到求 i+1的

795. Number of Subarrays with Bounded Maximum

We are given an array nums of positive integers, and two positive integers left and right (left <= right). Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least left and at

Subarray sort

refer to : https://www.algoexpert.io/questions/Subarray%20Sort Problem Statement    Analysis     Code def subarraySort(array): minOutOfOrder = float("inf") maxOutOfOrder = float("-inf") for i in range(len(array)):

1695. Maximum Erasure Value

You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. Return the maximum score you can get by erasing exactly one su

LeetCode53——Maximum Subarray——3 different methods

这道题目让我们求出一个线性数组中最大的连续子数组的和,题目要求如下: 这道题的题目很简单,解法也有多种,在这里总结3种解法去解决这个问题,在比较中加深对不同思路算法的理解,这三种算法是动态规划、模拟法、滑动窗口。 首先是动态规划法,正如我们之前提过的,递推形式的动态规划基

1574. Shortest Subarray to be Removed to Make Array Sorted

Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing. A subarray is a contiguous subsequence of the array. Return the length of the shortest subarray to remove.   Example 1:

最大子数组和

题目:最大子数组和 1.题目描述 所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组。比如,数组 A = [-2, -3, 4, -1, -2, 1, 5, -3], 最大子数组应为[4, -1, -2, 1, 5],其和为7。 2.解题思路   2.1 动态规划   2.2 分治法 3.代码  3.1 动态规划 #include <i

[LeetCode] 978. Longest Turbulent Subarray

Given an integer array arr, return the length of a maximum size turbulent subarray of arr. A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. More formally, a subarray [arr[i], arr[i + 1], ..., a

978. Longest Turbulent Subarray

仅供自己学习   思路: 1.很明显,又是滑动窗口的题,只要 if判断能满足 ><,或<>就让右指针右移一个元素,并且记录长度 right-left+1。如果不满足则 left = right,再重复上述步骤   代码: 1 class Solution { 2 public: 3 int maxTurbulenceSize(vector<int>& arr) { 4 i

[LeetCode] 643. Maximum Average Subarray I

Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. Example 1: Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Explanation: Maximum ave

[LeetCode 1712] Ways to Split Array Into Three Subarrays

A split of an integer array is good if: The array is split into three non-empty contiguous subarrays - named left, mid, right respectively from left to right. The sum of the elements in left is less than or equal to the sum of the elements in mid