[Swift]LeetCode493. 翻转对 | 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.


给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对

你需要返回给定数组中的重要翻转对的数量。

示例 1:

输入: [1,3,2,3,1]
输出: 2

示例 2:

输入: [2,4,3,5,1]
输出: 3

注意:

  1. 给定数组的长度不会超过50000
  2. 输入数组中的所有数字都在32位整数的表示范围内。

Runtime: 1632 ms

Memory Usage: 20.3 MB

 1 class Solution {
 2     func reversePairs(_ nums: [Int]) -> Int {
 3         var res:Int = 0
 4         var copy:[Int] = nums.sorted(by:<)
 5         var bit:[Int] = [Int](repeating:0,count:copy.count + 1)
 6         for ele in nums
 7         {
 8             res += search(&bit, index(copy, 2 * ele + 1))
 9             insert(&bit, index(copy, ele))
10         }
11         return res
12     }
13
14     func index(_ arr:[Int],_ val:Int) -> Int
15     {
16         var l:Int = 0
17         var r:Int = arr.count - 1
18         var m:Int = 0
19         while(l <= r)
20         {
21             m = l + ((r - l) >> 1)
22             if arr[m] >= val
23             {
24                 r = m - 1
25             }
26             else
27             {
28                 l = m + 1
29             }
30         }
31         return l + 1
32     }
33
34     func search(_ bit:inout [Int],_ i:Int) -> Int
35     {
36         var i = i
37         var sum:Int = 0
38         while(i < bit.count)
39         {
40             sum += bit[i]
41             i += i & -i
42         }
43         return sum
44     }
45
46     func insert(_ bit:inout [Int],_ i:Int)
47     {
48         var i = i
49         while(i > 0)
50         {
51             bit[i] += 1
52             i -= i & -i
53         }
54     }
55 }

原文地址:https://www.cnblogs.com/strengthen/p/10348715.html

时间: 2024-08-14 22:44:47

[Swift]LeetCode493. 翻转对 | Reverse Pairs的相关文章

[LeetCode] 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:

LeetCode -Reverse Pairs

my solution: class Solution { public: int reversePairs(vector<int>& nums) { int length=nums.size(); int count=0; for (int i=0;i<length;i++) { for(int j=i+1;j<length;j++) { if(nums[i]>2*nums[j]) count++; } } return count; } }; wrong answ

Reverse Pairs

For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return total of reverse pairs in A. Example Given A = [2, 4, 1, 3, 5] , (2, 1), (4, 1), (4, 3) are reverse pairs. return 3 Solution 1: the same as the leetcode ques

翻转整数 Reverse digits of a number

两种方法翻转一个整数,顺序翻转和递归翻转 这里没考虑overflow的情况 递归的作用是使得反向处理,即从递归栈的最低端开始处理,通过画图可得. 如果是rec(num/10): 12345 1234 123 12 1         <-- 递归使得这个最先处理 package recursion; public class Reverse_digits_of_a_number { public static void main(String[] args) { int num = 123; S

493. Reverse Pairs(BST, BIT, MergeSort)

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:

LeetCode 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

493. 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:

493 Reverse Pairs 翻转对

给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对.你需要返回给定数组中的重要翻转对的数量.示例 1:输入: [1,3,2,3,1]输出: 2示例 2:输入: [2,4,3,5,1]输出: 3注意:    给定数组的长度不会超过50000.    输入数组中的所有数字都在32位整数的表示范围内.详见:https://leetcode.com/problems/reverse-pairs/description/ C

K组翻转链表 &#183; Reverse Nodes in

[抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1->..->nk->next.. // to head->nk->..->n1->next.. // return n1 每k个转一次,再递归 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [