【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指定了 ...
随机推荐
- vue 全局 js 方法
1.新增 getCurrentDataType.js 文件 import cookieUtils from '@/config/cookieUtils' function getCurrentData ...
- supermarket
题目链接 题意:给你n个商品,商品的利润和商品的过期时间,商品必须在过期时间内卖才能算利润,每天只能卖一件商品,问利润最大值. 思路:用并查集+贪心的思路,先给商品从大到小排序,然后选择过期时间的根节 ...
- 判断PC或者是APP
function isPC() { if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { return fa ...
- 用js实现摇一摇功能
function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 window.addEventListener('devicemot ...
- js判断一件商品加还是减
<div class="right"> <div onclick="javascript: ClickJian(this);" data-i= ...
- 如何取消bootstrap浮动时的padding值
在bootstrap框架中使用浮动时,可以对元素添加pull-left 或 pull-right 类来达到目的.但是框架会默认给该元素添加一个15px的左右padding. <div class ...
- jquery 查找元素,id,class
查找元素下的class 带有.pageactive的a标签 $('a.pageactive') 标签a和..pageactive不要有空格,有空格找不到 ======================= ...
- [Fw]中断的初始化
要使用中断肯定得初始化,这些初始化在系统启动时已经为你做好了,但是我们还是来看看怎样初始化的,这样就能更好的理解中断机制了.先看下面函数: 355 void __init init_ISA_irqs ...
- SpringBoot-技术专区-异步编程
最近在实现一个聚合搜索的需求时,由于需要从五个索引中查询数据,然后再将搜索结果组合返回给前端app展现,显然这个地方不能再用同步的方式来操作了,如果有一个索引查询出现耗时较长,那么其余的请求都会排同步 ...
- Linux下安装git本地库与服务器端远程库
1. git是一个分布式版本管理系统,关于该工具的详细介绍,我认为廖雪峰老师介绍的非常全面:https://www.liaoxuefeng.com/wiki/896043488029600. 不 ...