【leetcode】907. Sum of Subarray Minimums
题目如下:

解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素。例如[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的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- [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 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- 907. Sum of Subarray Minimums
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...
随机推荐
- http 换成 https
UPDATE SYS_MENU M SET M.href = ( SELECT CASE WHEN substr(N.href, 0, 5) = 'http:' THEN 'https:'||subs ...
- k-近邻算法(kNN)完整代码
from numpy import *#科学计算包 from numpy import tile from numpy import zeros import operator #运算符模块 impo ...
- [CSP-S模拟测试]:water(BFS)
题目描述 有一块矩形土地被划分成$n\times m$个正方形小块.这些小块高低不平,每一小块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但是不能直接流入对角相连的小块中.一场大雨 ...
- python中的list和generator
# -*- coding:utf-8 -*- # author : Keekuun # 列表生成式,list l1 = [i for i in range(10)] # 或者 l2 = list(ra ...
- Caused by: java.lang.ClassNotFoundException: com.mchange.v2.cfg.MConfig
出错原因:c3p0 为0.9.5.2版本 而使用了 mchange-commons-java 的版本为0.2.3.4,mchange-commons-java 的版本太高了, 将mchange-com ...
- APPium连接真机输入框中输入的内容与代码中不一致
今天解决了上一个问题,又碰到了一个新的问题. 问题:连接真机输入框中输入的内容与代码中不一致. 描述: 想实现登录页面输入用户名和密码自动登录,可是在输入用户名和密码的框中输入的内容总是与代码中的不一 ...
- 【SpringBoot】 理解Spirng中的IOC原理
前言 前文已经介绍了Spring Bean的生命周期,在这个周期内有一个重要的概念就是: IOC容器 大家也知道IOC是Sping 的重要核心之一,那么如何理解它呢,它又是产生什么作用呢?本文就IOC ...
- JarvisOJ 逆向Writeup
1. 爬楼梯 先运行apk,查看具体的功能 爬一层楼是可以点击的,爬到了,看FLAG是不可以点击的.我们可以大致的了解到到了具体的楼层才可以看到flag,多次打开软件,楼层数目是随机的. 用APKID ...
- Bentley二次开发中的,沿曲线构造拉伸实体问题
引用文件:Bentley.Interop.MicroStationDGN 本人开发过程中遇到问题: 创建多个线段及弧线,通过自动创建复杂链获得,沿曲线构造拉伸实体的Path参数,拉伸曲线路径首尾特别近 ...
- Ajax 中Post 与 Get的 实例
Ajax中 Post 与 Get 实例 Ajax的POST实例,index.html <html> <head> <script type="text/ja ...