【LeetCode】493. Reverse Pairs 翻轉對(Hard)(JAVA)
【LeetCode】493. Reverse Pairs 翻轉對(Hard)(JAVA)
題目地址: https://leetcode.com/problems/reverse-pairs/
題目描述:
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2 * nums[j].
You need to return the number of important reverse pairs in the given array.
Example1:
Input: [1,3,2,3,1]
Output: 2
Example2:
Input: [2,4,3,5,1]
Output: 3
Note:
- The length of the given array will not exceed 50,000.
- All the numbers in the input array are in the range of 32-bit integer.
題目大意
給定一個陣列 nums ,如果 i < j 且 nums[i] > 2 * nums[j] 我們就將 (i, j) 稱作一個重要翻轉對。
你需要返回給定陣列中的重要翻轉對的數量。
解題方法
- 這個題其實就是找逆序對的個數,只要採用穩定排序的方式即可
- 排序穩定且 O(nlogn) 的演算法只有歸併排序了
插入排序
- 採用插入排序的方式
- 在插入的時候先用二分法找出比 2 * num 大的個數
- 演算法複雜度 O(n^2) ; 雖然插入的位置查詢是 O(logn) ,但是插入元素的時候需要把插入位置後面的元素統一往後移,時間複雜度是 O(n)
class Solution {
public int reversePairs(int[] nums) {
int res = 0;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
res += insert(list, nums[i]);
}
return res;
}
public int insert(List<Integer> list, int num) {
int res = 0;
if (list.size() == 0) {
list.add(num);
return res;
}
int start = 0;
int end = list.size() - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (list.get(mid) > num * 2L) {
end = mid - 1;
} else {
start = mid + 1;
}
}
res = list.size() - start;
start = 0;
end = list.size() - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (list.get(mid) == num) {
start = mid;
break;
}
if (list.get(mid) > num) {
end = mid - 1;
} else {
start = mid + 1;
}
}
list.add(start, num);
return res;
}
}
執行耗時:466 ms,擊敗了5.15% 的Java使用者
記憶體消耗:46.6 MB,擊敗了98.68% 的Java使用者
歸併排序
- 歸併排序的時間複雜度是 O(nlogn) ,並且是空間穩定的(也就是交換不會改變相對位置)
- 假設 n1 = [start, mid], n2 = [mid + 1, end], n1 和 n2 都已經排好序了,也都找除了逆序對個數,現在就需要找出 n1 和 n2 之間的逆序對個數;只要 n1 固定,然後在 n2 中找即可(可以優化用二分查詢)
- 找逆序對的時候不要忘記了把 n1 和 n2 兩個排序好的陣列合並了
class Solution {
public int reversePairs(int[] nums) {
if (nums.length == 0) {
return 0;
}
return reversePairsRecursive(nums, 0, nums.length - 1);
}
public int reversePairsRecursive(int[] nums, int left, int right) {
if (left == right) {
return 0;
} else {
int mid = (left + right) / 2;
int n1 = reversePairsRecursive(nums, left, mid);
int n2 = reversePairsRecursive(nums, mid + 1, right);
int ret = n1 + n2;
// 首先統計下標對的數量
int i = left;
int j = mid + 1;
while (i <= mid) {
while (j <= right && (long) nums[i] > 2 * (long) nums[j]) {
j++;
}
ret += j - mid - 1;
i++;
}
// 隨後合併兩個排序陣列
int[] sorted = new int[right - left + 1];
int p1 = left, p2 = mid + 1;
int p = 0;
while (p1 <= mid || p2 <= right) {
if (p1 > mid) {
sorted[p++] = nums[p2++];
} else if (p2 > right) {
sorted[p++] = nums[p1++];
} else {
if (nums[p1] < nums[p2]) {
sorted[p++] = nums[p1++];
} else {
sorted[p++] = nums[p2++];
}
}
}
for (int k = 0; k < sorted.length; k++) {
nums[left + k] = sorted[k];
}
return ret;
}
}
}
執行耗時:73 ms,擊敗了44.01% 的Java使用者
記憶體消耗:48.1 MB,擊敗了52.64% 的Java使用者
相關文章
- LeetCode 493. 翻轉對(歸併排序 || 離散化+樹狀陣列)LeetCode排序陣列
- [LeetCode] Reverse Integer 翻轉整數LeetCode
- Reverse Card (Hard Version)
- Swap Nodes in Pairs leetcode javaAILeetCodeJava
- Reverse Integer leetcode javaLeetCodeJava
- D2. Reverse Card (Hard Version)
- [CareerCup] 1.2 Reverse String 翻轉字串字串
- Reverse Words in a String leetcode javaLeetCodeJava
- Evaluate Reverse Polish Notation leetcode javaLeetCodeJava
- [LeetCode] Swap Nodes in Pairs 成對交換節點LeetCodeAI
- [LeetCode] Reverse Nodes in k-Group 每k個一組翻轉連結串列LeetCode
- Reverse Linked List II leetcode javaLeetCodeJava
- Reverse Nodes in k-Group leetcode javaLeetCodeJava
- Leetcode Swap Nodes in PairsLeetCodeAI
- leetcode Reverse IntegerLeetCode
- 【刷題日記】leetcode-493 翻轉對LeetCode
- Leetcode 24 Swap Nodes in PairsLeetCodeAI
- LeetCode-Palindrome PairsLeetCodeAI
- LeetCode 24 Swap Nodes in PairsLeetCodeAI
- Leetcode-Swap Nodes in PairsLeetCodeAI
- CF1967B2 Reverse Card (Hard Version) 題解
- Leetcode 7 Reverse IntegerLeetCode
- LeetCode-Reverse BitsLeetCode
- 【LeetCode】Palindrome Pairs(336)LeetCodeAI
- Leetcode-Problem:Reverse IntegerLeetCode
- Leetcode Reverse Words in a StringLeetCode
- Leetcode Evaluate Reverse Polish NotationLeetCode
- leetcode Reverse Words in a StringLeetCode
- leetcode刷題--Reverse BitsLeetCode
- [LeetCode] 336. Palindrome PairsLeetCodeAI
- LeetCode677. Map Sum PairsLeetCodeAI
- LeetCode-Find K Pairs with Smallest SumsLeetCodeAI
- Leetcode 線性表 Swap Nodes in PairsLeetCodeAI
- leetcode:動態規劃( hard )LeetCode動態規劃
- leetcode 344. Reverse StringLeetCode
- LeetCode-Reverse Vowels of a StringLeetCode
- LeetCode 第 7 題(Reverse Integer)LeetCode
- LeetCode-Reverse Words in a StringLeetCode