【leetcode】915. Partition Array into Disjoint Intervals
题目如下:

解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值。那么我们可以先把数组从最左边开始到数组最右边所有子序列的最大值都求出来存入leftMax数组,同时也把从数组最右边开始到最左边的所有子序列的最小值求出来存入rightMin数组,最后找出最小的i使得leftMax[i] <= rightMin[i+1]即可。
代码如下:
class Solution(object):
def partitionDisjoint(self, A):
"""
:type A: List[int]
:rtype: int
"""
leftMax = [0] * len(A)
rightMin = [0] * len(A)
for i in range(len(A)):
if i == 0:
leftMax[i] = A[i]
else:
leftMax[i] = max(leftMax[i-1],A[i]) for i in range(-1,-len(A)-1,-1):
if i == -1:
rightMin[i] = A[i]
else:
rightMin[i] = min(rightMin[i + 1], A[i]) #print leftMax,rightMin
res = 0
for i in range(len(leftMax)-1):
if leftMax[i] <= rightMin[i+1]:
res = i
break return res + 1
【leetcode】915. Partition Array into Disjoint Intervals的更多相关文章
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
- [LeetCode] 915. Partition Array into Disjoint Intervals 分割数组为不相交的区间
Given an array A, partition it into two (contiguous) subarrays left and right so that: Every element ...
- 【leetcode】352. Data Stream as Disjoint Intervals
问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- 【leetcode】1020. Partition Array Into Three Parts With Equal Sum
题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
随机推荐
- C#-弄懂泛型和协变、逆变
脑图概览 泛型声明和使用 协变和逆变 <C#权威指南>上在委托篇中这样定义: 协变:委托方法的返回值类型直接或者间接地继承自委托前面的返回值类型; 逆变:委托签名中的参数类型继承自委托方法 ...
- 51nod 1384:全排列(STL)
题目链接 记住next_permutation函数的用法,另外string在这里比char[]慢好多啊.. //#include<bits/stdc++.h> //using namesp ...
- Oracle RAC常用命令
Oracle Clusterware的命令集可以分为以下4种,其中用的最多的是crsctl和srvctl:节点层:osnodes olsnodes -n -i -s olsnodes -l -p 网络 ...
- 9.27-uname,useradd命令
打印系统信息 [root@wen ~]# uname Linux [root@wen ~]# uname -r #内核版本 2.6.32-573.el6.x86_64 [root@wen ~]# un ...
- html 和 body标签的 css 设置
个人猜测浏览器的机制:H5页面底板上有一张画布,画布高度可以被撑高.html.body等元素是固定在画布上的.浏览器中页面的滚动是跟着画布滚动的.(fixed定位是脱离这种机制的,相对浏览器窗口定位的 ...
- vue绑定属性、绑定class及绑定style
1.绑定属性 v-bind 或者 : 例如:<img :src="pic_src" /> <template> <div id="app& ...
- Scrapy模拟登陆豆瓣抓取数据
scrapy startproject douban 其中douban是我们的项目名称 2创建爬虫文件 进入到douban 然后创建爬虫文件 scrapy genspider dou douban. ...
- PHP的Session机制解析 2
在鸟哥的博客看到对php session的过期时间的一篇文章,在此记录. 原文地址:http://www.laruence.com/2012/01/10/2469.html 以下是鸟哥博客原文: 今天 ...
- 信息安全-攻击-XSS:XSS/CSS 攻击
ylbtech-信息安全-攻击-XSS:XSS/CSS 攻击 XSS攻击通常指的是通过利用网页开发时留下的漏洞,通过巧妙的方法注入恶意指令代码到网页,使用户加载并执行攻击者恶意制造的网页程序.这些恶意 ...
- 107、TensorFlow变量(三)
创建秩为1的张量 # create a rank1 tensor object import tensorflow as tf mystr = tf.Variable(["Hello&quo ...