/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2025年9月10日 星期三

C++示範bitset取代hashset解Leetcode 1733 Minimum Number of People to Teach(含Py3 code)


C++示範bitset取代hashset解Leetcode 1733  Minimum Number of People to Teach
C++ STL的unordered_set與bitset都能解這題,但執行效率差很多,有誰寫C++其執行效能要比類似的Python code還要慢上好久倍?用hashset就如此
[Python code請進 ]
-----
C++ STL's unordered_set and bitset can both solve this problem, but their execution efficiency is very different. Has anyone written C++ that performs many times slower than similar Python code? This is the case with hashsets.
class Solution:
    def minimumTeachings(self, n: int, languages: List[List[int]], friendships: List[List[int]]) -> int:
        know=[]
        for L in languages:
            know.append(set(L))
        
        need=set()
        for a1, b1 in friendships:
            a, b=a1-1, b1-1
            if know[a] & know[b]: continue
            need.add(a)
            need.add(b)
        
        if not need: return 0

        ans=float('inf')
        for lang in range(1, n+1):
            cnt=0
            for i in need:
                if lang not in know[i]: cnt+=1
            ans=min(ans, cnt)
        return ans
        
        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章