2018-09-27 23:33:49

问题描述:

问题求解:

方法一、DP(MLE)

动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划,即使不MLE,也是肯定会在大规模的数据量上TLE的。

    public int sumSubarrayMins(int[] A) {
int res = 0;
int mod = (int)Math.pow(10, 9) + 7;
int[][] dp = new int[A.length][A.length];
for (int i = 0; i < A.length; i++) dp[i][i] = A[i];
for (int len = 2; len <= A.length; len++) {
for (int i = 0; i <= A.length - len; i++) {
int j = i + len - 1;
dp[i][j] = Math.min(dp[i + 1][j], dp[i][j - 1]);
}
}
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
res = (res + dp[i][j]) % mod;
}
}
return res;
}

方法二、

数据量已经基本表明时间复杂度在O(nlogn)左右比较好,那么直接使用dp肯定是不会通过所有的例子的。

本题还有另外一种思路:

res = sum(A[i] * f(i))
where f(i) is the number of subarrays,
in which A[i] is the minimum.

难点就在于求f(i),为了求f(i)需要求left[i]和right[i]。

left[i]:A[i]左边严格大于A[i]的个数

right[i]:A[i]右边大于等于A[i]的个数

f(i) = (left[i] + 1) * (right[i] + 1),其实就是一个排列组合,左边取一个可能,那么可以从右边取right[i] + 1种来进行组合。

这里需要特别注意的一点是:首先本问题是允许重复的子数组的,这里的重复是指数字上相等,但是是不允许完全一致的区间,因此左边必须是严格大于,否则会出现重复的情况。计算left,right数组可以使用Stack在O(n)时间复杂度完成求解,最后的res计算也是线性时间,因此总的时间复杂度为O(n)。

    public int sumSubarrayMins(int[] A) {
int res = 0;
int mod = (int)1e9 + 7;
int[] left = new int[A.length];
int[] right = new int[A.length];
Stack<int[]> stack = new Stack<>();
stack.push(new int[]{Integer.MIN_VALUE, -1});
for (int i = 0; i < A.length; i++) {
while (stack.peek()[0] > A[i])
stack.pop();
left[i] = i - stack.peek()[1];
stack.push(new int[]{A[i], i});
}
stack.clear();
stack.push(new int[]{Integer.MIN_VALUE, A.length});
for (int i = A.length - 1; i >= 0; i--) {
while (stack.peek()[0] >= A[i])
stack.pop();
right[i] = stack.peek()[1] - i;
stack.push(new int[]{A[i], i});
}
for (int i = 0; i < A.length; i++) {
res = (res + A[i] * left[i] * right[i]) % mod;
}
return res;
}

子数组最小值的总和 Sum of Subarray Minimums的更多相关文章

  1. [LeetCode] 907. Sum of Subarray Minimums 子数组最小值之和

    Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...

  2. [Swift]LeetCode907. 子数组的最小值之和 | Sum of Subarray Minimums

    Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...

  3. LeetCode 643. 子数组最大平均数 I(Maximum Average Subarray I)

    643. 子数组最大平均数 I 643. Maximum Average Subarray I 题目描述 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. LeetCo ...

  4. 【LeetCode】643. 子数组最大平均数 I Maximum Average Subarray I (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 目录 题目描述 题目大意 解题方法 方法一:preSum 方法二:滑动窗口 刷题心得 日期 题目地址:https://leetc ...

  5. [Swift]LeetCode643. 子数组最大平均数 I | Maximum Average Subarray I

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  6. [Swift]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

    In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...

  7. 907. Sum of Subarray Minimums

    Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...

  8. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  9. leetcode907 Sum of Subarray Minimums

    思路: 对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果.时间复杂度O(n). 实现: class Solution { public: int sumSubarr ...

随机推荐

  1. 为什么不应该使用ZooKeeper做服务发现

    [编者的话]本文作者通过ZooKeeper与Eureka作为Service发现服务(注:WebServices体系中的UDDI就是个发现服务)的优劣对比,分享了Knewton在云计算平台部署服务的经验 ...

  2. openstack cloud init set password

    设置代理和password #!/bin/bash cd /home/ubuntu wget otcloud-gateway.bj.intel.com/script.tar.gz ]; then cu ...

  3. MongoDB入门一

    一.环境配置 1.下载MongoDB,找到Bin目录下所有的.exe文件,拷贝到G盘MongoDB(新建)下,在MongoDB下建一个data文件,用于存放数据,创建一个logs文件夹,文件夹下创建一 ...

  4. darknet中的若干问题

    2018-04-20: https://github.com/pjreddie/darknet/issues/717 改了一下午,然后/usr/include/c++/4.8/bits/stl_rel ...

  5. How to install Maven on Windows

    To install Apache Maven on Windows, you just need to download the Maven’s zip file, and Unzip it to ...

  6. topcoder srm 450 div1

    problem1 link 用$f[i][0],f[i][1]$表示从$i$位置开始Alice是先手是否可以胜利,是后手是否可以胜利. problem2link 每次钱数够$price$时可以选择使得 ...

  7. Python3 tkinter基础 Canvas create_line 画实线与虚线

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. HDU 4632 Palindrome subsequence & FJUT3681 回文子序列种类数(回文子序列个数/回文子序列种数 容斥 + 区间DP)题解

    题意1:问你一个串有几个不连续子序列(相同字母不同位置视为两个) 题意2:问你一个串有几种不连续子序列(相同字母不同位置视为一个,空串视为一个子序列) 思路1:由容斥可知当两个边界字母相同时 dp[i ...

  9. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  10. python 之 运算符

    Python 运算符   Python 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4和5被称为操作数,"+"号为运算 ...