微信会被封?!包子 Leetcode 1512 solution Number of Good Pairs
包子君讲解的leetcode题目是越来越简单,在标题党的路上确实越走越远,对不起包子粉了?
微信在美国被封这个现在还没有定论,估计北美的同学们没慌,但是父母亲戚们可能更着急。其实大可不必,首先被封的概率很小。其次怎么封?不管怎么封都有解决的办法,不管是VPN翻墙或者直接去用其他的app,老毛子无踪可寻的telegram,年青一代一直在玩的QQ,或者B端的钉钉,lark(没用过,正好试试)。在目前的阶段,大伙儿只要想联系,总是会有办法的。也许不看朋友圈视频号还能省点时间做点有意义的事情呢 ?
Leetcode solution 1512. Number of Good Pairs
Blogger: https://blog.baozitraining.org/2020/07/leetcode-solution-1512-number-of-good.html
Youtube: https://youtu.be/dvnjvOLh88k
博客园: https://www.cnblogs.com/baozitraining/p/13338525.html
B站: https://www.bilibili.com/video/BV1hK4y1x7Py/
Problem Statement
Given an array of integers nums
.
A pair (i,j)
is called good if nums[i]
== nums[j]
and i
< j
.
Return the number of good pairs.
Example 1:
Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3]
Output: 0
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Problem link
Video Tutorial
You can find the detailed video tutorial here
- Youtube
- B站
Thought Process
Array is the most basic data structures and common solutions inclue
- Nested for loops O(N^2)
- Sort O(NlgN)
- Use extra space O(N)
This applies to this problem perfectly. The O(N^2) brute force solution is naive. We can also use a Map data structure (key is the number, value is the occurrence count) thus O(N). We can also sort the array and use this simple formula (also leetcode's hint) to calculate the good number pair.
Good pairs = N * (N-1) / 2 where N is how many duplicate numbers, this is from combination C(n^2), from n elements pick two
Solutions
Use Map
1 public int numIdenticalPairs(int[] nums) {
2 if (nums == null || nums.length == 0) {
3 return 0;
4 }
5
6 // key: int value; value: number of occurrence
7 Map<Integer, Integer> lookup = new HashMap<>();
8 int goodPairsCount = 0;
9 for (int i : nums) {
10 if (lookup.containsKey(i)) {
11 goodPairsCount += lookup.get(i);
12 lookup.put(i, lookup.get(i) + 1);
13 } else {
14 lookup.put(i, 1);
15 }
16 }
17
18 return goodPairsCount;
19 }
Time Complexity: O(N), N is the array size Space Complexity: O(N) since we use extra Map
Sort
1 public int numIdenticalPairsWithSort(int[] nums) {
2 if (nums == null || nums.length == 0) {
3 return 0;
4 }
5
6 Arrays.sort(nums);
7
8 int goodPairsCount = 0;
9 int start = 0;
10 int end = 0;
11
12 while (end < nums.length) {
13 if (nums[end] == nums[start]) {
14 end++;
15 continue;
16 }
17 // if you have n occurrences of a number, the good pair is n*(n-1)/2, Cn^2
18 int delta = end - start;
19 if (delta > 1) {
20 goodPairsCount += delta * (delta - 1) / 2;
21 }
22 start = end;
23 }
24 // handle the case 1, 2, 3, 3, 3
25 if (start != nums.length - 1) {
26 goodPairsCount += (end - start) * (end - start - 1) / 2;
27 }
28 return goodPairsCount;
29 }
Time Complexity: O(NlgN), N is the array size since we sort Space Complexity: O(1) no extra space is used
References- None
- 学习Altas 笔记[JS简单调用服务端方法]
- 使用WinSCP软件在windows和Linux中进行文件传输
- 线程安全的Generic Dictionary
- Python 项目实践三(Web应用程序)第五篇
- CentOS 6.3下 安装 Mono 3.2 和Jexus 5.4
- Python 项目实践三(Web应用程序)第四篇
- 负载均衡环境下缓存处理
- WordPress Ajax 异步加载 自定义评论表情
- “分期网”域名fenqi.wang以16888元成交
- Python 项目实践三(Web应用程序) 第三篇
- Python 项目实践三(Web应用程序)第二篇
- 第一个IronPython程序(之一)
- mono-3.4.0 源码安装时出现的问题 [do-install] Error 2 [install-pcl-targets] Error 1 解决方法
- 安装 IronPython
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 精讲RestTemplate第10篇-使用代理作为跳板发送请求
- 精讲响应式WebClient第5篇-请求超时设置与异常处理
- 精讲响应式WebClient第4篇-文件上传与下载
- 【大家的项目】Rust Base62 库学习和分析
- ES2020 中 Javascript 10 个你应该知道的新功能
- Webpack5 跨应用代码共享 - Module Federation
- 【Rust日报】2020-09-16 - Rust 2021 规划
- 精讲响应式WebClient第3篇-POST、DELETE、PUT方法使用
- C++的黑魔法: 用四种方式实现add!
- 精讲响应式WebClient第2篇-GET请求阻塞与非阻塞调用方法详解
- 算法篇:位运算异或的使用(一)
- 精讲响应式webclient第1篇-响应式非阻塞IO与基础用法
- k8s 二进制集群平滑升级 1.15.2升级至1.16.4
- 使用Java API进行tar.gz文件及文件夹压缩解压缩
- 动画 | 一文掌握 Flex 布局