前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信会被封?!包子 Leetcode 1512 solution Number of Good Pairs

微信会被封?!包子 Leetcode 1512 solution Number of Good Pairs

作者头像
包子面试培训
发布2020-07-23 14:21:58
5400
发布2020-07-23 14:21:58
举报
文章被收录于专栏:包子铺里聊IT

包子君讲解的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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array are good.

Example 3:

代码语言:javascript
复制
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
代码语言:javascript
复制
 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
代码语言:javascript
复制
 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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-07-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 包子铺里聊IT 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Statement
  • Video Tutorial
  • Thought Process
  • Solutions
    • Use Map
      • Sort
      • References
      相关产品与服务
      VPN 连接
      VPN 连接(VPN Connections)是一种基于网络隧道技术,实现本地数据中心与腾讯云上资源连通的传输服务,它能帮您在 Internet 上快速构建一条安全、可靠的加密通道。VPN 连接具有配置简单,云端配置实时生效、可靠性高等特点,其网关可用性达到 99.95%,保证稳定、持续的业务连接,帮您轻松实现异地容灾、混合云部署等复杂业务场景。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档