-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple_same_product.cpp
More file actions
56 lines (51 loc) · 1.38 KB
/
tuple_same_product.cpp
File metadata and controls
56 lines (51 loc) · 1.38 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* =====================================================================================
*
* Filename: tuple_same_product.cpp
*
* Description: 1726. Tuple with Same Product
* https://leetcode.com/problems/tuple-with-same-product/
*
* Version: 1.0
* Created: 02/07/2025 22:46:49
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <unordered_map>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::pair;
using std::unordered_map;
using std::vector;
class Solution {
public:
int tupleSameProduct(vector<int>& nums) {
unordered_map<int, int> product_counts;
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
product_counts[nums[i] * nums[j]] += 1;
}
}
int sum = 0;
for (const auto& [_, val] : product_counts) {
sum += 8 * val * (val - 1) / 2;
}
return sum;
}
};
TEST(Solution, tupleSameProduct) {
vector<pair<vector<int>, int>> cases = {
std::make_pair(vector<int>{2, 3, 4, 6}, 8),
std::make_pair(vector<int>{1, 2, 4, 5, 10}, 16),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().tupleSameProduct(c.first), c.second);
}
}