【发布时间】:2020-08-23 07:17:41
【问题描述】:
给定 (1, 2, 3,..N) 的两个排列,
Consider for n = 5
5 4 3 2 1
3 2 4 1 5
在两个排列中找到满足 index(a)
(4,1) index(4)<index(1) in both permutations.
(3,1)
(2,1)
(3,2)
我们可以在 O(n2) 中轻松做到这一点,但我觉得我们可以在 O(nlogn) 中做到这一点。你能帮忙吗?
另一个例子...
for n = 5
3 4 1 5 2
1 3 2 5 4
这里的答案是 5
(3,4) index(3)<index(4) in both perms.
(3,5) index(3)<index(5) in both perms.
(3,2)
(1,5)
(1,2)
【问题讨论】:
-
什么是a和b?
-
a 和 b 是从 1 到 n 的整数
-
所以 4 总是大于 1,不是吗?没有任何排列。
-
对不起,我的错,我们需要比较索引。我道歉
-
@fjardon 没有链接,但我在 Uber 软件工程实习生的测试中遇到了这个问题。