-
Notifications
You must be signed in to change notification settings - Fork 2
/
1726.同积元组.js
29 lines (27 loc) · 965 Bytes
/
1726.同积元组.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* @lc app=leetcode.cn id=1726 lang=javascript
*
* [1726] 同积元组
*/
// @lc code=start
/**
* @param {number[]} nums
* @return {number}
*/
var tupleSameProduct = function(nums) {
let tupleCount = 0;
let products = {}; // we'll keep track of how many times we've seen a given product before
for (let a = 0; a < nums.length; a++) {
for (let b = a + 1; b < nums.length; b++) {
let product = nums[a] * nums[b];
if (products[product]) { // we've seen at least one other pair of numbers with the same product already
tupleCount += 8 * products[product]; // multiply by 8 because for any 4 numbers there are 8 permutations
products[product] += 1; // increment the count, if we see this product again there are even more possible tuple combinations
} else {
products[product] = 1; // mark as seen once
}
}
}
return tupleCount;
};
// @lc code=end