493. Reverse Pairs
package LeetCode_493 import java.util.* /** * 493. Reverse Pairs * 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: 1. The length of the given array will not exceed 50,000. 2. All the numbers in the input array are in the range of 32-bit integer. * */ class Solution { /* * solution 1: like merge sort, split array into two sub-array, and check left side if meet the condition, * Time complexity:O(nlogn), Space complexity:O(n) * */ var result = 0 fun reversePairs(nums: IntArray): Int { if (nums.size < 2) { return 0 } mergeSort(nums, 0, nums.size - 1) return result } private fun mergeSort(nums: IntArray, left: Int, right: Int) { if (left < right) { val mid = left + (right - left) / 2 mergeSort(nums, left, mid) mergeSort(nums, mid + 1, right) merge(nums, left, mid, right) } } private fun merge(nums: IntArray, left: Int, mid: Int, right: Int) { var i = left var j = mid + 1 while (i <= mid && j <= right) { if (nums[i].toLong() <= 2 * nums[j].toLong()) { i++ } else { //because array is sorted, so the index in left was meet the condition, //so other index in right of current index are meet the condition as well /* for example: left: 1,3,5,7 | right: 2,4,6,8 (3,2),(5,2),(7,2) are Reverse Pairs */ result += mid - i + 1 j++ } } //sort current range Arrays.sort(nums, left, right + 1) } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)