归档 2015年2月5日

[LeetCode题解]从两个有序数组的并集中寻找第k小元素

Given two sorted arrays A, B of size m and n respectively. Find the k-th smallest element in the union of A and B. You can assume that there are no duplicate elements.

不得不承认这道题目解决起来非常的巧妙。像大多数难题一样,需要经过非常巧妙的观察才可以用简洁的方式求解。

朴素解法, O(m+n):

将两个数组进行合并,然后寻找第k小的元素可能非常直观。合并操作需要花费额外的O(m + n)的空间。线性运行时间已经很好了,但我们还能再做一些优化吗?

朴素解法的优化, ...

继续阅读

昨天

明天

归档