{"id":7102,"date":"2020-07-16T00:58:25","date_gmt":"2020-07-16T07:58:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7102"},"modified":"2020-07-16T01:05:12","modified_gmt":"2020-07-16T08:05:12","slug":"leetcode-1512-number-of-good-pairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1512-number-of-good-pairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1512. Number of Good Pairs"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>A pair&nbsp;<code>(i,j)<\/code>&nbsp;is called&nbsp;<em>good<\/em>&nbsp;if&nbsp;<code>nums[i]<\/code>&nbsp;==&nbsp;<code>nums[j]<\/code>&nbsp;and&nbsp;<code>i<\/code>&nbsp;&lt;&nbsp;<code>j<\/code>.<\/p>\n\n\n\n<p>Return the number of&nbsp;<em>good<\/em>&nbsp;pairs.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,1,1,3]\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,1,1,1]\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>Each pair in the array are <em>good<\/em>.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Enumerate all the pairs.<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int numIdenticalPairs(vector<int>& nums) {\n    int ans = 0;\n    for (int i = 0; i < nums.size(); ++i)\n      for (int j = i + 1; j < nums.size(); ++j)\n        ans += nums[i] == nums[j];\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\nclass Solution {\n  public int numIdenticalPairs(int[] nums) {\n    int ans = 0;\n    for (int i = 0; i < nums.length; ++i)\n      for (int j = i + 1; j < nums.length; ++j)\n        if (nums[i] == nums[j]) ++ans;\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def numIdenticalPairs(self, nums: List[int]) -> int:\n    n = len(nums)\n    ans = 0\n    for i in range(n):\n      for j in range(i + 1, n):\n        if nums[i] == nums[j]: ans += 1\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Hashtable<\/strong><\/h2>\n\n\n\n<p>Store the frequency of each number so far, when we have a number x at pos j, and it appears k times before. Then we can form additional k pairs.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(range)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int numIdenticalPairs(vector<int>& nums) {\n    int ans = 0;\n    vector<int> f(101);\n    for (int i = 0; i < nums.size(); ++i)\n      ans += f[nums[i]]++;\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  public int numIdenticalPairs(int[] nums) {\n    int ans = 0;\n    int[] f = new int[101];\n    for (int i = 0; i < nums.length; ++i)      \n      ans += f[nums[i]]++;\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def numIdenticalPairs(self, nums: List[int]) -> int:\n    f = defaultdict(int)\n    ans = 0\n    for x in nums:\n      ans += f[x]\n      f[x] += 1\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums. A pair&nbsp;(i,j)&nbsp;is called&nbsp;good&nbsp;if&nbsp;nums[i]&nbsp;==&nbsp;nums[j]&nbsp;and&nbsp;i&nbsp;&lt;&nbsp;j. Return the number of&nbsp;good&nbsp;pairs. Example 1: Input: nums = [1,2,3,1,1,3] Output: 4 Explanation: There are 4 good&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-7102","post","type-post","status-publish","format-standard","hentry","category-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7102"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7102\/revisions"}],"predecessor-version":[{"id":7106,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7102\/revisions\/7106"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}