LeetCode 373. Find K Pairs with Smallest Sums | all4win78



LeetCode 373. Find K Pairs with Smallest Sums | all4win78

You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.

Define a pair (u,v) which consists of one element from the first array and one element from the second array.

Find the k pairs (u1,v1),(u2,v2) …(uk,vk) with the smallest sums.

Example 1:

Given nums1 = [1,7,11], nums2 = [2,4,6],  k = 3  Return: [1,2],[1,4],[1,6]  The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6] 

Example 2:

Given nums1 = [1,1,2], nums2 = [1,2,3],  k = 2  Return: [1,1],[1,1]  The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3] 

Example 3:

Given nums1 = [1,2], nums2 = [3],  k = 3   Return: [1,3],[2,3]  All possible pairs are returned from the sequence: [1,3],[2,3] 

Analysis:

首先对题目进行分析,为了方便,我用Ai来表示nums1[i],用Bi来表示nums2[i]。我们可以知道[Ai, Bj]永远比[Ai+m, Bj+n]小(m+n>0)。换句话说,如果[Ai, Bj]还没有被排进list,那么[Ai+m, Bj+n]永远不可能被考虑加入list。再换句话说,在任意一个时刻,对于每一个Ai(Bi),最多只需要考虑一种组合。根据这个分析的结果,我的想法是,控制一个集合S,里面包含所有当前会被考虑排进list的组合,每次在其中选出和最小的,并更新这个集合。

为了不那么抽象,我决定用图片辅助说明,不妨有A={A1, A2, A3, A4}以及B={B1, B2, B3, B4}。假设在某一时刻,list={[A1, B1], [A2, B1], [A3, B1], [A1, B2]}。下图中黑色线条代表已经加入list的组合,红色代表当前需要被考虑的组合,也就是S={[A1,B3], [A2, B2], [A1, B3]}。

373_1

那么这个组合怎么得到呢?其实规律很简单,对于每一个Ai,如果在list中和Ai组合的最大数字为Bj,那么只需要考虑[Ai, Bj+1];如果[Ai, B1]进了list,而[Ai + 1, B1]还没有进list,那么需要考虑[Ai + 1, B1]。

根绝这个思路,那么可以设计算法,既然每次我们都需要找S中最小的组合来加入list,很容易就想到用一个PriorityQueue来储存和更新S。到了这一步,基本也就解决了这道题,只需要再考虑edge cases就可以了。

不过依我对于LC test cases的尿性的了解,以及我多年来谨慎小心的作风,我觉得LC很可能在一些小的edge cases来阴我。对于这道题,容易想到,如果我们对于两个int求和(或者做差),可能会有overflow(underflow)的问题,所以我当即为我的机智点了赞,然后哼哧哼哧地把很多地方的int换成了long。然后我就一遍AC,心情大好,心想毕竟是心思缜密的我啊!但是我是一个强迫症,想看看不改成int会有什么结果,然而依旧AC!(╯‵□′)╯︵┻━┻  LC你太让我失望了,test cases写得这么马虎,怎么对得起我付的钱?!还钱!


Read full article from LeetCode 373. Find K Pairs with Smallest Sums | all4win78


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts