Re: [閒聊] 每日leetcode
1726. Tuple with Same Product
## 思路
先計算a*b counter
如果有N組pair的相乘一樣, 取兩個pair產生{a,b,c,d} -- C(N, 2)
又每個組合可以有8種排序, 所以加總後再乘8
e.g. (2,6,3,4)
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
## CODE
```cpp
class Solution {
public:
int tupleSameProduct(vector<int>& nums) {
int n = nums.size();
unordered_map<int, int> counter;
for (int i=0; i<n; ++i) {
for (int j=i+1; j<n; ++j) {
counter[nums[i] * nums[j]]++;
}
}
int res = 0;
for (auto [value, count]: counter) {
res += count * (count-1) / 2;
}
return res * 8;
}
};
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 212.102.50.112 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1738841432.A.2E6.html
討論串 (同標題文章)
完整討論串 (本文為第 1325 之 1552 篇):