The trick here is to name the state correctly, then let the implementation follow. For 1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- Two pointer
- note: if without sorting and using two pointer technique, the O(N^3) algorithm gives TLE
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numTriplets.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Two pointer
02//note: if without sorting and using two pointer technique, the O(N^3) algorithm gives TLE
03//Runtime: 56 ms, faster than 100.00% of C++ online submissions for Number of Ways Where Square of Number Is Equal to Product of Two Numbers.
04//Memory Usage: 11.6 MB, less than 100.00% of C++ online submissions for Number of Ways Where Square of Number Is Equal to Product of Two Numbers.
05class Solution {
06public:
07 int numTriplets(vector<int>& nums1, vector<int>& nums2) {
08 int n1 = nums1.size(), n2 = nums2.size();
09 int ans = 0;
10
11 map<int, int> counter1, counter2;
12
13 for(int i = 0; i < n1; ++i){
14 ++counter1[nums1[i]];
15 }
16
17 for(int j = 0; j < n2; ++j){
18 ++counter2[nums2[j]];
19 }
20
21 for(auto it1 = counter1.begin(); it1 != counter1.end(); ++it1){
22 long long a = it1->first;
23 long long acount = it1->second;
24 auto it2 = counter2.begin();
25 auto it3 = counter2.rbegin();
26 for(; it2 != counter2.end() && it3 != counter2.rend(); ){
27 long long b = it2->first;
28 long long bcount = it2->second;
29 long long c = it3->first;
30 long long ccount = it3->second;
31 if(c < b) break;
32
33 // cout << a << ", " << b << ", " << c << endl;
34
35 if(a*a > b*c){
36 ++it2;
37 }else if(a*a < b*c){
38 ++it3;
39 }else if(a*a == b*c){
40 if(b == c){
41 ans += acount * (bcount * (bcount-1) >> 1);
42 }else{
43 ans += acount * bcount * ccount;
44 }
45 // cout << "ans: " << ans << endl;
46 // break;
47 ++it2;
48 ++it3;
49 }
50 }
51 }
52
53 swap(counter1, counter2);
54 for(auto it1 = counter1.begin(); it1 != counter1.end(); ++it1){
55 long long a = it1->first;
56 long long acount = it1->second;
57 auto it2 = counter2.begin();
58 auto it3 = counter2.rbegin();
59 for(; it2 != counter2.end() && it3 != counter2.rend(); ){
60 long long b = it2->first;
61 long long bcount = it2->second;
62 long long c = it3->first;
63 long long ccount = it3->second;
64 if(c < b) break;
65
66 // cout << a << ", " << b << ", " << c << endl;
67
68 if(a*a > b*c){
69 ++it2;
70 }else if(a*a < b*c){
71 ++it3;
72 }else if(a*a == b*c){
73 if(b == c){
74 ans += acount * (bcount * (bcount-1) >> 1);
75 }else{
76 ans += acount * bcount * ccount;
77 }
78 // cout << "ans: " << ans << endl;
79 ++it2;
80 ++it3;
81 }
82 }
83 }
84
85 return ans;
86 }
87};
Cost