题目如下:

解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素。例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4,5]。那么对于元素2来说,和左边区间合并组成[2,3]以及和右边区间合并组成[2,4,5],这两段区间包括2在内的所有subarray的最小值都是2,其分别可以组成的subarry的个数是 len([3])和len([4,5]),左右区间合并在一起可以组成的subarray的个数是len([3])*len([4,5]),再加上2本身自己组成一个独立的subarray [2],其总数就是 len([3]) + len([4,5]) + len([3])*len([4,5]) + 1,转成通项表达式就是 len(left) + len(right) + len(left)*len(right) + 1。怎么快速找到其左右两边最近的小于自己的元素?可以参考【leetcode】84. Largest Rectangle in Histogram 。

代码如下:

class Solution(object):
def sumSubarrayMins(self, A):
"""
:type A: List[int]
:rtype: int
"""
dic = {}
for i in range(len(A)):
if A[i] not in dic:
dic[A[i]] = 0
else:
dic[A[i]] += 1
A[i] = float(str(A[i]) + '.' + ''*dic[A[i]])
dp_left = [0] * len(A)
dp_left[0] = -1
for i in xrange(1, len(dp_left)):
j = i - 1
while A[i] <= A[j] and j != -1:
j = dp_left[j]
dp_left[i] = j
# print dp dp_right = [0] * len(A)
dp_right[-1] = len(A)
for i in xrange(len(dp_right) - 2, -1, -1):
j = i + 1
while j != len(A) and A[i] <= A[j]:
j = dp_right[j]
dp_right[i] = j res = 0
for i in range(len(A)):
left = i - dp_left[i] - 1
right = dp_right[i] - i - 1
A[i] = int(A[i])
res += (left*A[i] + right*A[i] + (left*right)*A[i] + A[i])
#print A[i],res
#print dp_left
#print dp_right
return res % (pow(10,9) + 7)

【leetcode】907. Sum of Subarray Minimums的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. [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 ...

  3. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  4. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  5. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  6. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  7. 【Leetcode】209. Minimum Size Subarray Sum

    Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  8. 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...

  9. 907. Sum of Subarray Minimums

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

随机推荐

  1. iPython清屏命令

    !clear for Unix-like systems !CLS for Windows

  2. 25-Node.js学习笔记-express-app.locals对象

    app.locals对象 将变量设置到app.locals对象下面,这个数据在所有的模板中都可以获取到 app.locals.users=[{ name:'柠檬不酸', age:20 },{ name ...

  3. 2,Spring MVC 学习总结(二)- 方法(Action)参数映射

    一,Controller层方法(Action)参数映射 1,自动参数映射 1.1,基本数据类型参数映射 方法的参数可以是任意基本数据类型,如果方法参数名与http中请求的参数名称相同时会进行自动映射. ...

  4. yii2.0增删改查实例讲解

    yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...

  5. SQL Server 2005 的动态管理视图DMV和函数DMF

    优化 的动态管理视图DMV和函数DMF SQL Server 05提供了动态管理视图Dynamic Management Views和函数 Functions,方便了我们对系统运行情况的监控,故障诊断 ...

  6. 【LeetCode 57】插入区间

    题目链接 [题解] 这题要分四种情况. 第一种.区间在所有区间的前面. 第二种.区间在所有区间的后面. 第三种.区间在某两个区间之间但是没有交集. 第四种.区间和某个区间产生了相交. 对于第四种枚举第 ...

  7. 【SPOJ8222】Substrings (后缀自动机)

    题意: 给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值. 求F(1)..F(Length(S)) Length(S) <= 250000 思路:板子中st[x]定义为r ...

  8. python练习题之全选框全不选反选

    功能 实现分为两大部分: 第一body 部分,1,通过<li>无序列表标签实现选项的基本样式,通过input的checkbox标签实现里面的复选框功能.用到了标签的嵌套.然后选项的js方法 ...

  9. STM32几个IO的工作模式

    浮空,顾名思义就是浮在空中,上面用绳子一拉就上去了,下面用绳子一拉就沉下去了.  开漏,就等于输出口接了个NPN三极管,并且只接了e,b. c极 是开路的,你可以接一个电阻到3.3V,也可以接一个电阻 ...

  10. apache基础,apache环境搭建,apache的3种使用方式(IP、端口、域名)

    一台服务器上多个网站同时运行,基于域名访问,IP访问,端口访问. http服务使用的端口是80 HTTPS使用的是443 协议名称://机器地址:端口号/路径名/文件名 协议名称—— 所使用的访问协议 ...