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. Redhat普通用户如何使用管理员权限

    作为一个普通用户, 很多地方收到权限的控制, 下面展示sudoers大法, 主要就是sudo的问题了. 下面的是我的普通用户lee 还没有授权之前是这样的 开始授权 切换到root用户, 修改sudo ...

  2. python简说(二十五)面向对象

    面向对象编程: 类 一个种类.一个模型 实例.实例化.对象 实例.对象: 根据模型制作出来的东西. 实例化: 就是做东西的这个过程. class My: my=My() 私有 方法 类里面的函数 属性 ...

  3. 快速阅读《QT5.9 c++开发指南》1

    简介:<QT5.9 c++开发指南>的作者是和i三位主要从事地球物理探测仪器设计.数据处理方法研究和软件开发等工作的博士们,这本书以QT Widget为主要内容,比较全面地教授了QT开发桌 ...

  4. Job for php-fpm.service failed because the control process exited with error code. See "systemctl status php-fpm.service" and "journalctl -xe" for details.

    [root@web01 ~]#  systemctl start php-fpm Job for php-fpm.service failed because the control process ...

  5. AndroidO Treble架构分析【转】

    本文转载自:https://blog.csdn.net/yangwen123/article/details/79835965 从AndroidO开始,google引入了Treble架构,目的是为了方 ...

  6. 2018年第九届蓝桥杯B组题C++汇总解析-fishers

    2018年第九届蓝桥杯B组题C++解析-fishers 题型 第一题:第几天 第二题:明码 第三题:乘积尾零 第四题:测试次数 第五题:快速排序 第六题:递增三元组 第七题:螺旋折线 第八题:日志统计 ...

  7. (转)Spring & SpringMVC学习

    https://shimo.im/docs/CzXTpHe7DlYbknEn/   掌握过程:   业务逻辑(漏洞.合理性处理).设计-->技术流程.原理.搭建.整体架设-->源码分析.断 ...

  8. [java变量] - 字符串数组转long型数组

    //定义字符串 String str = "1,3,6,9,4,2,1,6"; //截取字符串 String[] strArr = str.split(",") ...

  9. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  10. 原生js仿jquery一些常用方法

    原生js仿jquery一些常用方法 下面小编就为大家带来一篇原生js仿jquery一些常用方法(必看篇).小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧   最近迷上了原 ...