★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10348715.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

[Swift]LeetCode493. 翻转对 | Reverse Pairs相关推荐

  1. [LintCode] Reverse Pairs 翻转对

    For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair. return t ...

  2. Reverse Pairs

    For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair. return t ...

  3. leetcode练习 Reverse Pairs

    几次练手之后,决定进军hard难度 最近的课程主要讲的是分治,就选择了相关的题目. Given an array nums, we call (i, j) an important reverse p ...

  4. c++内置函数实现字符串翻转(reverse,strrev,string 构造函数)

    c++内置函数实现字符串翻转(reverse,strrev,string 构造函数) 在写程序的时候,我们经常需要将字符串进行翻转.c++中内置的函数有不少个可以实现该功能. 1.strrev函数.( ...

  5. [Swift]LeetCode832. 翻转图像 | Flipping an Image

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  6. 翻转函数 reverse

    数字翻转 给定一个整数,请将该数各个位上数字反转得到一个新数. 新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零. 输入格式 输入共 1 行,1 个整数 N. ...

  7. [Swift]LeetCode206. 反转链表 | Reverse Linked List

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. Mysql翻转字符串reverse

    颠倒字符串 SELECT reverse('chinese'); # esenihc SELECT reverse('pep'); # pep SELECT reverse(1234567); # 7 ...

  9. [Swift]LeetCode293. 翻转游戏 $ Flip Game

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. MySQL 5.7 vs 8.0,哪个性能更牛?
  2. java读取本地文件下载_java 读取本地的json文件
  3. python 类可以调用实例变量_python中的实例方法、静态方法、类方法、类变量和实例变量浅析...
  4. EL 表达式 JSTL 标签库
  5. 【软考-软件设计师】计算机系统基础知识
  6. ECCV 2020 Spotlight | 图像定位上的细粒化区域相似性自监督
  7. 如何通俗理解拉格朗日对偶问题(part2)
  8. 超声换能器的原理及设计_超声波发生器、变幅杆、焊头的匹配介绍
  9. dotNET Core实现分布式环境下的流水号唯一
  10. linux硬盘为啥分区,Linux下为什么要进行磁盘的分区
  11. 导入要素类到要素数据集当中(C++)(ArcObject)史上最快
  12. linux刷新profile文件,Linux下修改.bash_profile 文件改变PATH变量的值
  13. 联想笔记本大写提示软件_联想威6 2021款怎么样?值得买吗?下面几点或许可以帮到您...
  14. angularjs学习:通信
  15. ADS2020 Crack使用教程
  16. Oracle数据库之日期查询
  17. 计算机网络本地连接,电脑本地连接受限制或无连接怎么办
  18. 关闭win10任务视图功能
  19. java消息平台_Java微信公众平台之消息管理
  20. WiFi大师专业版SAAS小程序+强强联合2.0模式正式开启

热门文章

  1. python怎么让电脑说话_懒人专用的奇淫技巧,用Python实现炫酷的语音操作电脑
  2. 怎样使用nat和桥接方式解决虚拟机联网问题
  3. Java如何创建项目
  4. POJ 3470 Walls 已翻译
  5. 报表开发知识大全(1) 什么是报表
  6. ncr管理系统_NCR餐饮系统操作指南
  7. NCR3网络技术速成笔记(1)
  8. P2600 [ZJOI2008]瞭望塔(半平面交)
  9. 科技云报道:巨头云集的云电脑市场,为何小酷云一枝独秀?
  10. Python获取手机4K壁纸,一个入门练手的案例