Showing posts with label Leetcode 4. Show all posts
Showing posts with label Leetcode 4. Show all posts

Thursday, January 5, 2017

The kth largest element from two sorted arrays

January 5, 2017

Introduction
Spent over 2 hours to study Leetcode 4 and Leetcode 215: the medium of two sorted array, and then Leetcode 215: the kth largest element from the array, and then, Julia spent over one hour to work on the code for the algorithm: the kth largest element from two sorted array.

Workout

Here is the C# practice: binary search, time complexity: not O(lg n + lg m), smaller one: O(lg(n+m)), n, m are the length of two sorted arrays.

Julia likes to work on the test case a little more time, 20 - 30 minutes, and then post the algorithm on stackexchange.com for a code review.

Read the article again, learn more about the analysis.

January 9, 2017
The most important feedback about the code - code review:
Just so you know, your solution appears to be O(logk(n+m)). The reason is that ArraySplice() makes a copy of the array, which takes either O(n) or O(m) time. If you would just avoid doing the copy and instead pass a starting index for each array to your function, you would be down to O(logk) time. – JS1

Actionable Items:

1. Read a few articles about the algorithm, list here:

2. Write a new version of algorithm to solve all issues, post it on code review. Learn by doing.

3. C# practice with the correction of time complexity issue - array splice. Code is here.

Sunday, May 15, 2016

Leetcode 4: Median of two sorted array - a warmup practice

May 15, 2015

Review the leetcode 4: Median of two sorted array, warm up the algorithm.

Here is the last practice:

https://github.com/jianminchen/Leetcode_C-/blob/master/4MedianOfTwoSortedArrays.cs

Step 1: 
Read the last practice 10 - 20 minutes on May 15, 2016
-- write down the ideas: 
1. Brute force solution: O(nlogn)
2. Linear solution: O(n) 

3. Transform to the kth element problem first, and then: 
Try to get rid of k/2 elements once, so the algorithm will go to logk, k = (m+n)/2 

Some analysis and reasoning: 
Assuming that A and B both arrays are with length > k/2, and then, compare A[k/2-1] and B[k/2-1]. 

Read the blog again:
http://blog.csdn.net/yutianzuijin/article/details/11499917

http://blog.csdn.net/zxzxy1988/article/details/8587244

Step 2: 
Spend 30 minutes to read and then write some code. 

Related to Leetcode 215: Find kth largest element in the array. 


Question and Answer:

1. Write down what you learn through this practice.
Answer: The algorithm is very well defined, is a special case of find kth element in two sorted arrays.

2. Algorithms learning and important thing to learn:

Answer:
1. Always know how to solve the problem using naive solution, brute force one first.

O(nlogn) -> O(n) -> O(log(m+n)/2)

2. And then, discuss the improvement.



Tuesday, June 9, 2015

Leetcode 4: Median of two sorted arrays

Problem statement: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)).
May 17, 2015
这道题我想了好几个小时, 读了题解, 没有读明白.直到我读了Leetcode题解, 总算读懂了题目的分析.让我感叹计算机科学的奇妙.有大学学数学分析的感觉
下面是题解分析:

这是一道非常经典的题。这题更通用的形式是,给定两个已经排序好的数组,找到两者所有
素中第k 大的元素。

1. O(m + n) 的解法比较直观,直接merge 两个数组,然后求第k 大的元素。

2. O(k)时间,O(1) 但是,当k 很接近m + n 时候,这个方法还是O(m + n) 的。

过我们仅仅需要第k 大的元素,是不需要排序这么复杂的操作的。可以用一个计数器
记录当前已经找到第m 大的元素了。同时我们使用两个指针pA pB,分别指向A B 组的
一个元素,使用类似于merge sort 的原理,如果数A 当前元素小,那么pA++,同m++;如果
B 当前元素小,那么pB++,同m++终当m 等于k 时候,就得到了我们的答案,O(k)
时间,O(1) 间。但是,当k 很接近m + n 时候,这个方法还是O(m + n) 的。

3. 更好的方案

有没有更好的方案呢?我们可以考虑从k 入手。如果我们每次都能够删除一个一定在第k 大元
素之前的元素,那么我们需要进行k 次。但是如果每次我们都删除一半呢?由于A B 都是有序
的,我们应该充分利用这里面的信息,类似于二分查找,也是充分利用了有序
A B 的元素个数都大于k/2,我们将A 的第k/2 个元素(即A[k/2-1])和B 的第k/2
个元素(即B[k/2-1]进行比较,有以下三种情况(为了简化这里先假设k 为偶数,所得到的结
论对于k 是奇数也是成立的):
• A[k/2-1] == B[k/2-1]
• A[k/2-1] > B[k/2-1]
• A[k/2-1] < B[k/2-1]
如果A[k/2-1] < B[k/2-1],意味着A[0] A[k/2-1] 的肯定在A [B] top k 元素的范 内,换句话说,A[k/2-1 不可能大于A [ B 的第k 大元素。给读者证明。 因此,我们可以放心的删除A 组的这k/2 个元素。同理,当A[k/2-1] > B[k/2-1] 时,可以删除B 组的k/2 个元素。

A[k/2-1] == B[k/2-1] 时,说明找到了第k 大的元素,直接返回A[k/2-1] B[k/2-1] 即可。
因此,我们可以写一个递归函数。那么函数什么时候应该终止呢
A B 是空时,直接返回B[k-1] A[k-1]
k=1 是,返回min(A[0], B[0])
A[k/2-1] == B[k/2-1] 时,返回A[k/2-1] B[k/2-1]
--
Read some blogs:

Practice code sharing: C# code: 

2017 January 5
Rewrite C# program for each time complexity:
O(m+n)
O(k)
O(log(m + n)

Post 3 code review on stackexchange.com code review.