前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode 1964. Find the Longest Valid Obstacle Course at Each Position

Leetcode 1964. Find the Longest Valid Obstacle Course at Each Position

作者头像
Tyan
发布2021-08-20 14:48:59
3400
发布2021-08-20 14:48:59
举报
文章被收录于专栏:SnailTyan

1. Description

Find the Longest Valid Obstacle Course at Each Position
Find the Longest Valid Obstacle Course at Each Position

2. Solution

**解析:**Version 1,这道题跟Leetcode 300很像,可以构造一个最长非递减子序列,使用order作为有序序列保持最长非递减子序列长度,当新元素大于或等于有序序列的最后一个元素时,此时增加新元素到有序序列中,否则,则将新元素插入到当前序列中,替换比其大的元素,保证左侧元素都比它小,此时长度不变,order中相同序列位置上始终保留较小的元素,这样利于插入新元素。插入新元素时,结果就是序列长度,更新元素时,长度为索引位值加1

  • Version 1
代码语言:javascript
复制
class Solution:
    def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
        n = len(obstacles)
        order = []
        result = []
        for i, obstacle in enumerate(obstacles):
            if len(order) == 0 or order[-1] <= obstacle:
                order.append(obstacle)
                result.append(len(order))
            else:
                index = bisect.bisect_right(order, obstacle)
                order[index] = obstacle
                result.append(index+1)
        return result

Reference

  1. https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Description
  • 2. Solution
  • Reference
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档