1. 程式人生 > >LeetCode-Algorithms #004 Median Of Two Sorted Arrays, Database #178 Rank Scores

LeetCode-Algorithms #004 Median Of Two Sorted Arrays, Database #178 Rank Scores

 LeetCode-Algorithms #004 Median Of Two Sorted Arrays

給定兩個已經完成排序的整數陣列, 陣列長度分別為m和n. 找到這兩個陣列的中位數, 演算法的時間複雜度不超過O(log(m+n)).

這兩個陣列不會都是空的.

 1 class Solution {
 2     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
 3         //建立一個結果陣列
 4         int[] result;
 5         //處理nums1或nums2為空的特殊情況
6 if(nums1==null){ 7 result=nums2; 8 }else if(nums2==null){ 9 result=nums1; 10 } 11 //都非空時把兩個陣列拼接起來並重新排序 12 else{ 13 result = new int[nums1.length+nums2.length]; 14 System.arraycopy(nums1, 0, result, 0, nums1.length);
15 System.arraycopy(nums2, 0, result, nums1.length, nums2.length); 16 Arrays.sort(result); 17 } 18 //求出並返回中位數 19 int l = result.length; 20 if(l%2==0){ 21 double ans = (double) (result[l/2] + result[l/2 - 1])/2; 22 return ans; 23
} else { 24 double ans = result[l/2]; 25 return ans; 26 } 27 } 28 }

我的想法就是把兩個陣列拼到一起再重新排序, 但這顯然是個笨辦法, 但這麼做好像時間複雜度上是符合要求的, 因為也只用了一次拼接和兩次arraycopy和一次sort

最後雖然順利通過了, 但顯然不是題目設定上想讓你採取的方法

回頭看LeetCode提供的一種解答思路, 想法上是把兩個陣列都分割成兩部分:

如果我們能夠滿足上面這兩個條件, 即左右兩邊元素個數相同, 及左側最大值小於右側最小值

由於兩個原陣列本身就是排序完成的, 那麼中位數自然就找到了

藉由這種思路, 我們可以知道需要四個變數, i, j表示劃分原陣列時選擇的位置, m, n表示原陣列的長度,

以及這四個變數間的關係, 最後用二分查詢法找到合適的i,j 找到中位數.

 1 class Solution {
 2     public double findMedianSortedArrays(int[] A, int[] B) {
 3         int m = A.length;
 4         int n = B.length;
 5         if (m > n) { // to ensure m<=n
 6             int[] temp = A; A = B; B = temp;
 7             int tmp = m; m = n; n = tmp;
 8         }
 9         int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
10         while (iMin <= iMax) {
11             int i = (iMin + iMax) / 2;
12             int j = halfLen - i;
13             if (i < iMax && B[j-1] > A[i]){
14                 iMin = i + 1; // i is too small
15             }
16             else if (i > iMin && A[i-1] > B[j]) {
17                 iMax = i - 1; // i is too big
18             }
19             else { // i is perfect
20                 int maxLeft = 0;
21                 if (i == 0) { maxLeft = B[j-1]; }
22                 else if (j == 0) { maxLeft = A[i-1]; }
23                 else { maxLeft = Math.max(A[i-1], B[j-1]); }
24                 if ( (m + n) % 2 == 1 ) { return maxLeft; }
25 
26                 int minRight = 0;
27                 if (i == m) { minRight = B[j]; }
28                 else if (j == n) { minRight = A[i]; }
29                 else { minRight = Math.min(B[j], A[i]); }
30 
31                 return (maxLeft + minRight) / 2.0;
32             }
33         }
34         return 0.0;
35     }
36 }

LeetCode-Database #178 Rank Scores

 

為分數排名, 同分的顯示相同的名次, 具體可以看圖中的例子

嗯... 

簡單來說就是

我不會...

看兩個答案吧:

SELECT t1.Score,
-- 這個思路不錯, 用一個自連接獲取排名
(
SELECT COUNT(DISTINCT t2.Score)+1
FROM Scores t2
WHERE t1.Score < t2.Score
) AS Rank
FROM Scores t1
ORDER BY t1.Score DESC
-- 思路是如果本行分數和上一行不同, 排名就加一, 明顯比上面快一些
SELECT
Score, @rank := @rank + (@prev <> (@prev := Score)) Rank FROM Scores, (Select @rank := 0, @prev := -1) tmp ORDER BY Score desc

點了不少, 不過主要就是這兩類寫法, 下面那個我確實不太熟悉, 包括@變數 := 值這種寫法都忘得差不多了...

SQL這邊稍微複雜一點的應用就沒什麼思路, 借這個機會多練習吧.

相關推薦

LeetCode-Algorithms #004 Median Of Two Sorted Arrays, Database #178 Rank Scores

 LeetCode-Algorithms #004 Median Of Two Sorted Arrays 給定兩個已經完成排序的整數陣列, 陣列長度分別為m和n. 找到這兩個陣列的中位數, 演算法的時間複雜度不超過O(log(m+n)). 這兩個陣列不會都是空的. 1 class Solution {

leetcode -- Algorithms -- 4_ Median of Two Sorted Arrays

right == err perfect -s time ase class and 00 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o

#leetcode-algorithms-4 Median of Two Sorted Arrays

() ould max over -o 同時 swap 解法 resp leetcode-algorithms-4 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size

LeetCode 004 Median of Two Sorted Arrays - Java

min -type port 尋找 style 得到 over size cti There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th

Leetcode Array 4 Median of Two Sorted Arrays

部分 存在 滿足 find cti itl title 要去 double 做leetcode題目的第二天,我是按照分類來做的,做的第一類是Array類,碰見的第二道題目,也就是今天做的這個,題目難度為hard。題目不難理解,但是要求到了時間復雜度,就需要好

LeetCode】4.Median of Two Sorted Arrays 兩個排序陣列的中位數

示例 1: nums1 = [1, 3] nums2 = [2] 中位數是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 中位數是 (2 + 3)/2 = 2.5 解題思路: 糟糕- -沒理解題意,首先需要知道“中位數”

LeetCode 4】Median of Two Sorted Arrays(Python)

Problem: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median

LeetCode:4. Median of Two Sorted Arrays(找出兩個有序陣列的中間數)

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall

LeetCode-4:Median of Two Sorted Arrays (兩個排序陣列的中位數)

題目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overa

leetcode】4. Median of Two Sorted Arrays

4. Median of Two Sorted Arrays Problem Solution 1、python (108ms) 2、C (36ms) Problem There are two sorte

LeetCode[Algorithms] Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The ove

[LeetCode]Median of Two Sorted Arrays 二分查找兩個有序數組的第k數(中位數)

大於 data div ble 關系 操作 spa 兩個 -1 二分。情況討論 因為數組有序,所以能夠考慮用二分。通過二分剔除掉肯定不是第k位數的區間。如果數組A和B當前處理的下標各自是mid1和mid2。則 1、假設A[mid1]<B[mid2], ①

LeetCodemedian of two sorted arrays

個數 i++ target 們的 data- 關於 arrays 推斷 get 題目:median of two sorted arrays 知識點:二分查找,中位數定義 public class Solution { /* * 關於:leetco

LeetCode: Median of Two Sorted Arrays

-o code htm problem median art cnblogs edi sort https://leetcode.com/problems/median-of-two-sorted-arrays/solution/ http://www.cnblogs.c

LeetCode解題筆記 - 4. Median of Two Sorted Arrays

res pre cnblogs 返回 熱門 median 輸入 cat find There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the

[LeetCode] 4. Median of Two Sorted Arrays 兩個有序數組的中位數

數據 pub art cti AI nts highlight sta binary There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t

leetcode--js--Median of Two Sorted Arrays

復雜 lex 真的 return complex spa cat time 一個數 問題描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the media

LeetCode 4: Median of Two Sorted Arrays

over 排列 com assume 時間 pre des cti 中間 Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi

leetcode 4. Median of Two Sorted Arrays

pub double div median ray ffffff sorted https min findKthNumber是在當前範圍內第k小的數。 class Solution { public: double findMedianSortedArray

Leetcode|【4】Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complex