Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cf7a7ca

Browse files
committed
Add solution #2006
1 parent 5865203 commit cf7a7ca

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,682 LeetCode solutions in JavaScript
1+
# 1,683 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1538,6 +1538,7 @@
15381538
2001|[Number of Pairs of Interchangeable Rectangles](./solutions/2001-number-of-pairs-of-interchangeable-rectangles.js)|Medium|
15391539
2002|[Maximum Product of the Length of Two Palindromic Subsequences](./solutions/2002-maximum-product-of-the-length-of-two-palindromic-subsequences.js)|Medium|
15401540
2003|[Smallest Missing Genetic Value in Each Subtree](./solutions/2003-smallest-missing-genetic-value-in-each-subtree.js)|Hard|
1541+
2006|[Count Number of Pairs With Absolute Difference K](./solutions/2006-count-number-of-pairs-with-absolute-difference-k.js)|Easy|
15411542
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15421543
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15431544
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 2006. Count Number of Pairs With Absolute Difference K
3+
* https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums and an integer k, return the number of pairs (i, j) where
7+
* i < j such that |nums[i] - nums[j]| == k.
8+
*
9+
* The value of |x| is defined as:
10+
* - x if x >= 0.
11+
* - -x if x < 0.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var countKDifference = function(nums, k) {
20+
const map = new Map();
21+
let result = 0;
22+
23+
for (const num of nums) {
24+
result += (map.get(num - k) || 0) + (map.get(num + k) || 0);
25+
map.set(num, (map.get(num) || 0) + 1);
26+
}
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)