leetcode907 Sum of Subarray Minimums
思路:
对于每个数字A[i],使用单调栈找到A[i]作为最小值的所有区间数量,相乘并累加结果。时间复杂度O(n)。
实现:
class Solution
{
public:
int sumSubarrayMins(vector<int>& A)
{
int res = ;
stack<int> st;
int n = A.size();
const int MOD = 1e9 + ;
for (int i = ; i < n; i++)
{
while (!st.empty() && A[i] < A[st.top()])
{
int tmp = st.top(); st.pop();
int last = st.empty() ? - : st.top();
res = (res + (i - tmp) * (tmp - last) % MOD * A[tmp] % MOD) % MOD;
}
st.push(i);
}
while (!st.empty())
{
int tmp = st.top(); st.pop();
int last = st.empty() ? - : st.top();
res = (res + (n - tmp) * (tmp - last) % MOD * A[tmp] % MOD) % MOD;
}
return res;
}
}
leetcode907 Sum of Subarray Minimums的更多相关文章
- [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 ...
- 907. Sum of Subarray Minimums
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
- [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 ...
- 子数组最小值的总和 Sum of Subarray Minimums
2018-09-27 23:33:49 问题描述: 问题求解: 方法一.DP(MLE) 动态规划的想法应该是比较容易想到的解法了,因为非常的直观,但是本题的数据规模还是比较大的,如果直接使用动态规划, ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- LC 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- 动态规划-Maximum Subarray-Maximum Sum Circular Subarray
2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...
- [Swift]LeetCode918. 环形子数组的最大和 | Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- Maximum Sum Circular Subarray LT918
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
随机推荐
- c语言:当指针成为参数后
指针就是一种指向内存地址的变量,利用它的一些特性我们可以完成很多工作 两个数字从小到大排序(引申的业务场景,对企业大佬的银行存款金额进行排序,这当然通过交换变量对应的数值来实现,否则盖茨大爷的钱可能全 ...
- C# Parallel 使用
ParallelLoopResult result = Parallel.For(, , i => { Console.WriteLine("{0}, task: {1}, threa ...
- 浅谈JavaScript严格模式(use strict)
"use strict" 的目的是指定代码在严格条件下执行. 严格模式下你不能使用未声明的变量. 严格模式声明 严格模式通过在脚本或函数的头部添加 "use strict ...
- jQuery.proxy(function,context)
jQuery.proxy(function,context) 概述 jQuery 1.4 新增.返回一个新函数,并且这个函数始终保持了特定的作用域.大理石平台检定规程 当有事件处理函数要附加到元素上, ...
- 004——转载C#禁止改变窗体大小
原文链接:http://www.cnblogs.com/shaozhuyong/p/5545005.html 1.先把MaximizeBox和MinimumBox设置为false,这时你发现最大最小化 ...
- Java集合总结(一):列表和队列
java中的具体容器类都不是从头构建的,他们都继承了一些抽象容器类.这些抽象容器类,提供了容器接口的部分实现,方便具体容器类在抽象类的基础上做具体实现.容器类和接口的关系架构图如下: 虚线框表示接口, ...
- 虚拟机Linux无法查看本地ip地址 解决办法
解决方案: 1.虚拟机与本机采用的连接方式为:Host-only模式,其中几种连接模式的区别我不做介绍,自己百度.如果之前连接方式不为Host-only,更改之后需要重新启动虚拟机. 2.将本机的两块 ...
- 1617:转圈游戏 ybt
1617:转圈游戏 时间限制: 1000 ms 内存限制: 524288 KB提交数: 540 通过数: 326 [题目描述] nn 个小伙伴(编号从 00 到 n−1n−1 ...
- 文件对比工具 Beyond Compare 4.2.9中文破解版for mac
链接:https://pan.baidu.com/s/1AsESVIYsn9Lv6qz2TfROrQ 密码:6o63链接:https://pan.baidu.com/s/1eiGgRHfPTEERlH ...
- Send Boxes to Alice
E. Send Boxes to Alice 首先求出每一个位置的前缀和. 对答案进行复杂度为\(\sqrt{a[n]}\)的遍历,因为最后的答案不可能大于\(\sqrt{a[n]}\) for(ll ...