Re: [閒聊] 每日leetcode

看板Marginalman作者 (JerryChung)時間1年前 (2024/11/08 04:39), 編輯推噓2(202)
留言4則, 3人參與, 1年前最新討論串1091/1548 (看更多)
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater- than-zero ※ 引述《dont (dont)》之銘言: : 2275. Largest Combination With Bitwise AND Greater Than Zero 剩我不會位元與了 好想漬 今天不想打題目 直接貼 Code Runtime 213ms 100.00% Memory 40.74MB 5.47% 試了一下 速度比位元與快 但空間直接墊底 修了一下也沒什麼差別 算了 Python Code: class Solution: def largestCombination(self, candidates: List[int]) -> int: max_len = len(bin(max(candidates)))-2 return max(int(''.join(j), 2).bit_count() for j in zip(*[bin(i)[2:]. rjust(max_len, "0") for i in candidates])) Runtime 616ms 28.23% Memory 26.70MB 81.46% 這個則是空間最好的版本了 一樣不會位元與 只好自己手做 然後看了一下空間最小的 是用修改user.out 算作弊吧 扣掉之後大概算前排 Python Code: class Solution: def largestCombination(self, candidates: List[int]) -> int: t = defaultdict(int) for i in candidates: i_b = bin(i) for n in range(-1, -i.bit_length()-1, -1): if i_b[n] == '1': t[n] += 1 return max(t.values()) JavaScript 也丟了 用空間版改的 就不附Code了 898ms跟41ms都是100% 哈 總共提交了快30個版本 剩我不會位元與了 漬 後來研究了一下有大概看懂了 但還是漬 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.20.205 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1731011977.A.AF3.html

11/08 04:41, 1年前 , 1F
第一個也可直接count('1') 但提交bit_count()快了些
11/08 04:41, 1F

11/08 04:42, 1年前 , 2F
然後後來才知道有.bit_length() 還自己len()-2 哈
11/08 04:42, 2F

11/08 07:29, 1年前 , 3F
大師
11/08 07:29, 3F

11/08 08:44, 1年前 , 4F
大師 剩我AC就不管了
11/08 08:44, 4F
文章代碼(AID): #1dBIM9hp (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dBIM9hp (Marginalman)