你打算构建一些障碍赛跑路线。给你一个 下标从 0 开始 的整数数组 obstacles
,数组长度为 n
,其中 obstacles[i]
表示第 i
个障碍的高度。
对于每个介于 0
和 n - 1
之间(包含 0
和 n - 1
)的下标 i
,在满足下述条件的前提下,请你找出 obstacles
能构成的最长障碍路线的长度:
- 你可以选择下标介于
0
到i
之间(包含0
和i
)的任意个障碍。 - 在这条路线中,必须包含第
i
个障碍。 - 你必须按障碍在
obstacles
中的 出现顺序 布置这些障碍。 - 除第一个障碍外,路线中每个障碍的高度都必须和前一个障碍 相同 或者 更高 。
返回长度为 n
的答案数组 ans
,其中 ans[i]
是上面所述的下标 i
对应的最长障碍赛跑路线的长度。
示例 1:
输入:obstacles = [1,2,3,2] 输出:[1,2,3,3] 解释:每个位置的最长有效障碍路线是: - i = 0: [1], [1] 长度为 1 - i = 1: [1,2], [1,2] 长度为 2 - i = 2: [1,2,3], [1,2,3] 长度为 3 - i = 3: [1,2,3,2], [1,2,2] 长度为 3
示例 2:
输入:obstacles = [2,2,1] 输出:[1,2,1] 解释:每个位置的最长有效障碍路线是: - i = 0: [2], [2] 长度为 1 - i = 1: [2,2], [2,2] 长度为 2 - i = 2: [2,2,1], [1] 长度为 1
示例 3:
输入:obstacles = [3,1,5,6,4,2] 输出:[1,1,2,3,2,2] 解释:每个位置的最长有效障碍路线是: - i = 0: [3], [3] 长度为 1 - i = 1: [3,1], [1] 长度为 1 - i = 2: [3,1,5], [3,5] 长度为 2, [1,5] 也是有效的障碍赛跑路线 - i = 3: [3,1,5,6], [3,5,6] 长度为 3, [1,5,6] 也是有效的障碍赛跑路线 - i = 4: [3,1,5,6,4], [3,4] 长度为 2, [1,4] 也是有效的障碍赛跑路线 - i = 5: [3,1,5,6,4,2], [1,2] 长度为 2
提示:
n == obstacles.length
1 <= n <= 105
1 <= obstacles[i] <= 107
方法一:树状数组
树状数组,也称作“二叉索引树”(Binary Indexed Tree)或 Fenwick 树。 它可以高效地实现如下两个操作:
- 单点更新
update(x, delta)
: 把序列 x 位置的数加上一个值 delta; - 前缀和查询
query(x)
:查询序列[1,...x]
区间的区间和,即位置 x 的前缀和。
这两个操作的时间复杂度均为
本题我们使用树状数组 tree[x]
来维护以 x 结尾的最长上升子序列的长度。
def update(x, val):
while x <= n:
c[x] = max(c[x], val)
x += lowbit(x)
def query(x):
s = 0
while x > 0:
s = max(s, c[x])
x -= lowbit(x)
return s
class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
@staticmethod
def lowbit(x):
return x & -x
def update(self, x, val):
while x <= self.n:
self.c[x] = max(self.c[x], val)
x += BinaryIndexedTree.lowbit(x)
def query(self, x):
s = 0
while x > 0:
s = max(s, self.c[x])
x -= BinaryIndexedTree.lowbit(x)
return s
class Solution:
def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
s = sorted(set(obstacles))
m = {v: i for i, v in enumerate(s, 1)}
tree = BinaryIndexedTree(len(m))
ans = []
for v in obstacles:
x = m[v]
ans.append(1 + tree.query(x))
tree.update(x, ans[-1])
return ans
class Solution {
public int[] longestObstacleCourseAtEachPosition(int[] obstacles) {
TreeSet<Integer> ts = new TreeSet();
for (int v : obstacles) {
ts.add(v);
}
int idx = 1;
Map<Integer, Integer> m = new HashMap<>();
for (int v : ts) {
m.put(v, idx++);
}
BinaryIndexedTree tree = new BinaryIndexedTree(m.size());
int n = obstacles.length;
int[] ans = new int[n];
for (int i = 0; i < n; ++i) {
int v = obstacles[i];
int x = m.get(v);
ans[i] = tree.query(x) + 1;
tree.update(x, ans[i]);
}
return ans;
}
}
class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void update(int x, int val) {
while (x <= n) {
c[x] = Math.max(c[x], val);
x += lowbit(x);
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s = Math.max(s, c[x]);
x -= lowbit(x);
}
return s;
}
public static int lowbit(int x) {
return x & -x;
}
}
class BinaryIndexedTree {
public:
int n;
vector<int> c;
BinaryIndexedTree(int _n)
: n(_n)
, c(_n + 1) {}
void update(int x, int val) {
while (x <= n) {
c[x] = max(c[x], val);
x += lowbit(x);
}
}
int query(int x) {
int s = 0;
while (x > 0) {
s = max(s, c[x]);
x -= lowbit(x);
}
return s;
}
int lowbit(int x) {
return x & -x;
}
};
class Solution {
public:
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
set<int> s(obstacles.begin(), obstacles.end());
int idx = 1;
unordered_map<int, int> m;
for (int v : s) m[v] = idx++;
BinaryIndexedTree* tree = new BinaryIndexedTree(m.size());
int n = obstacles.size();
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
int v = obstacles[i];
int x = m[v];
ans[i] = 1 + tree->query(x);
tree->update(x, ans[i]);
}
return ans;
}
};
type BinaryIndexedTree struct {
n int
c []int
}
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
c := make([]int, n+1)
return &BinaryIndexedTree{n, c}
}
func (this *BinaryIndexedTree) lowbit(x int) int {
return x & -x
}
func (this *BinaryIndexedTree) update(x, val int) {
for x <= this.n {
if this.c[x] < val {
this.c[x] = val
}
x += this.lowbit(x)
}
}
func (this *BinaryIndexedTree) query(x int) int {
s := 0
for x > 0 {
if s < this.c[x] {
s = this.c[x]
}
x -= this.lowbit(x)
}
return s
}
func longestObstacleCourseAtEachPosition(obstacles []int) []int {
s := make(map[int]bool)
for _, v := range obstacles {
s[v] = true
}
var t []int
for v, _ := range s {
t = append(t, v)
}
sort.Ints(t)
m := make(map[int]int)
for i, v := range t {
m[v] = i + 1
}
n := len(obstacles)
ans := make([]int, n)
tree := newBinaryIndexedTree(len(m))
for i, v := range obstacles {
x := m[v]
ans[i] = 1 + tree.query(x)
tree.update(x, ans[i])
}
return ans
}