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 ...
随机推荐
- jaxb生成cdata块
1.需要添加一个适配器: import javax.xml.bind.annotation.adapters.XmlAdapter; public class AdapterCDATA extends ...
- python修改linux主机ip
修改虚拟机的主机ip 和hostname import os, sys def conf_ip(ip): iplist = [] f = open("/etc/sysconfig/netwo ...
- cursor: hand和cursor:pointer的区别
cursor:hand 与 cursor:pointer 的效果是一样的,都像光标指向链接一样,光标变成手行. cursor:hand :IE完全支持.但是在firefox是不支持的,没有效果. cu ...
- bzoj4400
/* * 此题同bzoj2725 * 增加了枚举边的操作 */ #include <bits/stdc++.h> ;// oo = 999999999; #define LL long l ...
- DRF-视图类组件
补充 GET books-------->查看数据--------------------> 返回所有数据列表 :[{},{},{}] POST books-------->添加数 ...
- 2019CCPC-江西省赛C题 HDU6569 GCD预处理+二分
Trap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- 图的基本存储的基本方式一(SDUT 3116)
Problem Description 解决图论问题,首先就要思考用什么样的方式存储图.但是小鑫却怎么也弄不明白如何存图才能有利于解决问题.你能帮他解决这个问题么? Input 多组输入,到文件结尾. ...
- http接口测试工具 REST Client
以下几个工具为常用rest测试工具 1.Postman, 可以下载客户端或者安装浏览器插件 2.Insomnia,window客户端 3.Apizza,在线极客专属的api协作管理工具 ...
- setjmp
#include <setjmp.h> #include <stdio.h> jmp_buf j; void raise_exception(void) { printf(&q ...
- js和jQuery实现的Ajax
1. JS实现Ajax <!doctype html> <html lang="en"> <head> <meta charset=&qu ...