From d6ddac48e80abc4865eb57582bbd1ed0c19bb6e6 Mon Sep 17 00:00:00 2001 From: Chee Hwa Tang Date: Tue, 12 Sep 2023 20:55:14 +0800 Subject: [PATCH 1/2] 2006. Count Number of Pairs With Absolute Difference K (Hash Table) --- ...airsWithAbsoluteDifferenceK_HashTable.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 solutions/2006. Count Number of Pairs With Absolute Difference K/CountNumberOfPairsWithAbsoluteDifferenceK_HashTable.java diff --git a/solutions/2006. Count Number of Pairs With Absolute Difference K/CountNumberOfPairsWithAbsoluteDifferenceK_HashTable.java b/solutions/2006. Count Number of Pairs With Absolute Difference K/CountNumberOfPairsWithAbsoluteDifferenceK_HashTable.java new file mode 100644 index 0000000..5e86fae --- /dev/null +++ b/solutions/2006. Count Number of Pairs With Absolute Difference K/CountNumberOfPairsWithAbsoluteDifferenceK_HashTable.java @@ -0,0 +1,46 @@ +package com.cheehwatang.leetcode; + +import java.util.HashMap; +import java.util.Map; + +// Time Complexity : O(n), +// where 'n' is the length of 'nums'. +// We traverse 'nums' to count the numbers in 'nums', and we traverse the HashMap with a maximum size of 'n'. +// +// Space Complexity : O(n), +// where 'n' is the length of 'nums'. +// The HashMap has a maximum size of 'n' if the numbers in 'nums' are all unique. + +public class CountNumberOfPairsWithAbsoluteDifferenceK_HashTable { + + // Approach: + // Using a HashMap to record the frequency of occurrence for all the numbers in 'nums'. + // This only add the number to the HashMap if it is in 'nums'. + + public int countKDifference(int[] nums, int k) { + + // Record the frequency for all the numbers into the HashMap. + Map map = new HashMap<>(); + for (int number : nums) { + map.put(number, map.getOrDefault(number, 0) + 1); + } + + // The number of combinations is the multiples of both frequency. + // e.g. [1,1,2,2,2] can have 2 * 3 = 6 combinations for k = 1. + // Check for the frequency for both key - k and key + k, as the k difference can be in both directions. + // Once the count is added, set the value of the number in the HashMap to 0, so that it will not be double counted. + // Do note that we cannot remove the key-value pair, + // as it causes Exceptions in the map.keySet() which uses an iterator. + int count = 0; + for (Integer key : map.keySet()) { + if (map.containsKey(key - k)) { + count += map.get(key) * map.get(key - k); + } + if (map.containsKey(key + k)) { + count += map.get(key) * map.get(key + k); + } + map.put(key, 0); + } + return count; + } +} From 8a7a614cf7f9c013dc4e71b3328fc4e15f72dea4 Mon Sep 17 00:00:00 2001 From: Chee Hwa Tang Date: Tue, 12 Sep 2023 20:59:08 +0800 Subject: [PATCH 2/2] Added 2006. Count Number of Pairs With Absolute Difference K (Hash Table) --- README.md | 54 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 36e5f15..d39c9b4 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,18 @@ Solution Topics + + September 12th + 2006. Count Number of Pairs With Absolute Difference K + $\text{\color{TealBlue}Easy}$ + + Hash Table + + + Array, + Hash Table + + September 11th 2006. Count Number of Pairs With Absolute Difference K @@ -74,17 +86,6 @@ Dynamic Programming - - September 7th - 1155. Number of Dice Rolls With Target Sum - $\text{\color{Dandelion}Medium}$ - - Dynamic Programming - Tabulation - - - Dynamic Programming - -

@@ -1565,13 +1566,15 @@ 2006 Count Number of Pairs With Absolute Difference K Java with - Brute Force or - Counting + Brute Force, + Counting or + Hash Table $\text{\color{TealBlue}Easy}$ Array, - Counting + Counting, + Hash Table @@ -3383,10 +3386,12 @@ $\text{\color{TealBlue}Easy}$ Array, - Counting + Counting, + Hash Table Solution Using - Brute Force Approach + Brute Force Approach or + Hash Table @@ -5554,6 +5559,23 @@ + + 2006 + Count Number of Pairs With Absolute Difference K + + Java + + $\text{\color{TealBlue}Easy}$ + + Array, + Counting, + Hash Table + + Solution Using + Brute Force, + Counting + + 2007 Find Original Array From Doubled Array