题目如下:

解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素。例如[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. postman-Runner

    postman Runner配置 preview查看参数

  2. 使用UncaughtExceptionHandler重启线程

    先复习Java中的异常 java.lang.Throwable 顶层父类 |– Error错误:JVM内部的严重问题,如OOM,程序员无法在代码中无法处理. |–Exception异常:普通的问题.通 ...

  3. Jmeter下载文件和保存文件

    Jmeter下载文件: 任意在网上搜索一张图片,地址为https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&a ...

  4. 深度学习中的batch、epoch、iteration的含义

    深度学习的优化算法,说白了就是梯度下降.每次的参数更新有两种方式. 第一种,遍历全部数据集算一次损失函数,然后算函数对各个参数的梯度,更新梯度.这种方法每更新一次参数都要把数据集里的所有样本都看一遍, ...

  5. oracle中日期转换

    oracle中,日期转换函数有很多,常用命令如下: to_char()命令将时间戳转换为用户规定的日期格式,如: SELECT TO_CHAR(sysdate,'YYYY-MM-DD hh24:mi: ...

  6. 【BZOJ2639】矩形计算(二维普通莫队)

    题意:输入一个n*m的矩阵,矩阵的每一个元素都是一个整数,然后有q个询问,每次询问一个子矩阵的权值. 矩阵的权值是这样定义的,对于一个整数x,如果它在该矩阵中出现了p次,那么它给该矩阵的权值就贡献p^ ...

  7. 设置element表格透明样式

    1.element table 表格 修改背景为透明并去除边框 .el-table{ /* 表格字体颜色 */ color:white; /* 表格边框颜色 */ /* border: 0.5px s ...

  8. Minimal Power of Prime

    题目链接 题意:输入n,求所有质因子幂的最小值.n奇大无比. 思路:先对n所有n开五次方根的质因子约完,然后如果没有除尽的话,因子最多也就4个了,所以幂数大于1的情况有p1^4,p1^3, p1^2  ...

  9. Activator.CreateInstance with parameters

    https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8#Sy ...

  10. T1218:取石子游戏

    [题目描述] 有两堆石子,两个人轮流去取.每次取的时候,只能从较多的那堆石子里取,并且取的数目必须是较少的那堆石子数目的整数倍,最后谁能够把一堆石子取空谁就算赢. 比如初始的时候两堆石子的数目是25和 ...