Re: [閒聊] 每日leetcode

作者: JerryChungYC (JerryChung)   2024-11-08 04:39:33
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個版本
剩我不會位元與了 漬
後來研究了一下有大概看懂了 但還是漬
作者: JerryChungYC (JerryChung)   2024-11-08 04:41:00
第一個也可直接count('1') 但提交bit_count()快了些然後後來才知道有.bit_length() 還自己len()-2 哈
作者: sustainer123 (caster)   2024-11-08 07:29:00
大師
作者: DJYOMIYAHINA (通通打死)   2024-11-08 08:44:00
大師 剩我AC就不管了

Links booklink

Contact Us: admin [ a t ] ucptt.com