Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間10月前 (2025/02/06 19:30), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1325/1552 (看更多)
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
文章代碼(AID): #1df9rOBc (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1df9rOBc (Marginalman)