一种区间交问题的奇怪姿势

Update[220504]: 原来这种数据结构叫珂朵莉树啊,真神奇。。。

我们要解决什么问题

区间交问题,是我们在做题中经常遇到的问题。

例如,Insert Interval一题,就是比较直白的区间交问题:

给定一系列的整数区间,再插入一个新的区间,问合并后的整数区间是什么

类似的还有Merge Intervals

给定一系列可能有重叠的整数区间,求合并后的整数区间

另一种区间交问题的描述是时间区间相关的问题,如Time Intersection:

给定用户A和用户B的在线时间区间,问两人同时在线的时间区间

又如经典的会议室安排问题Meeting RoomsMeeting Rooms II:

给定N个会议的时间区间,问一个人能否参加所有的会议

以及

给定N个会议的时间区间,问最少需要多少个会议室

还有系统设计与API设计包装后的算法题Range Module。归根结底,都是整数区间问题的变形或者包装。

传统解法

对于这类区间问题,传统的解法是将区间使用顺序容器(如vector …

more ...

Why do I quit Leetcode contest?

Foreword

The title is quite "Yin-ish" as it seems, but I'm not a "Yin-ist". You know what I mean.

Why I quit?

I did a quite a good job in Leetcode Weekly Contest, at least in my point of view. I won a 4th place and a 6th place in …

more ...

Single Number Problem

Introduction

There are a lot of interview problem based on the 1D-array, which is the one of the easiest "data structure".

But the problem about that simple data structure might not be that simple. Here is the summary of the problem about 1D-array.

Of course, most of them come from …

more ...

How to "Rotate Image"?

啥?

原题戳我

Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?

示意图如下:

rotate-image

当然,矩阵中的数字不一定是规律的。

为什么要提出这个问题

自觉是一个不聪明的人(双低。。。<(=@_@;=)?>)。

别人一下子就想明白的事,在我这里要计算好几个来回。

所以努力想找到一个思维方法去弥补这个不足。

就以这题为例,如何使用简单、直接的方法,迅速正确的找出变化的映射规律。

初步思路 …

more ...