2897. Apply Operations on Array to Maximize Sum of Squares
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 43% Topics: Array, Hash Table, Greedy, Bit Manipulation
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogr), r = max(nums)
# Space: O(logr)
# bit manipulation, greedy, freq table
class Solution(object):
def maxSum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
MOD = 10**9+7
l = max(nums).bit_length()
cnt = [0]*l
for i in xrange(l):
for x in nums:
if x&(1<<i):
cnt[i] += 1
return reduce(lambda x, y: (x+y)%MOD, (sum(1<<i for i in xrange(l) if cnt[i] >= j)**2 for j in xrange(1, k+1)))
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions