Problem solution · Python

Maximize Subarray Gcd Score

Maximize Subarray Gcd Score: a Python solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Binary search
Source
Kamyu LeetCode Solutions
Length
80 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Maximize Subarray Gcd Score, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 80 lines of Python from the credited upstream file maximize-subarray-gcd-score.py.
  • The implementation visibly relies on sequence storage.
  • 2 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximize Subarray Gcd Score · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(nlogn * logr), r = max(nums)# Space: O(n + logr) # number theory, suffix-gcd states, dp, binary searchclass Solution(object):    def maxGCDScore(self, nums, k):        """        :type nums: List[int]        :type k: int        :rtype: int        """        def gcd(a, b):            while b:                a, b = b, a%b            return a            lookup = [0]*len(nums)        for i in xrange(len(nums)):            while nums[i]&1 == 0:                nums[i] >>= 1                lookup[i] += 1        lookup2 = [[] for _ in xrange(max(lookup)+1)]        for i, e in enumerate(lookup):            lookup2[e].append(i)        result = 0        dp = {}        for i, x in enumerate(nums):            new_dp = {}            new_dp[x, lookup[i]] = [i]*2            for (g, e), v in dp.iteritems():  # |g * e| = O(logr)                ng = gcd(g, x)                ne = min(e, lookup[i])                if (ng, ne) not in new_dp:                    new_dp[ng, ne] = [float("inf")]*2                new_dp[ng, ne][0] = min(new_dp[ng, ne][0], v[0])                left = bisect_left(lookup2[ne], v[0])  # Time: O(logn)                right = bisect_right(lookup2[ne], i)-1  # Time: O(logn)                new_dp[ng, ne][1] = min(new_dp[ng, ne][1], v[0] if right-left+1 <= k else lookup2[ne][right-k]+1)            dp = new_dp            for (g, e), v in dp.iteritems():  # |g * e| = O(logr)                result = max(result, g*(i-v[0]+1)<<e, g*(i-v[1]+1)<<(e+1))        return result  # Time:  O(n^2 + n * logr), r = max(nums)# Space: O(1)# number theory, brute forceclass Solution2(object):    def maxGCDScore(self, nums, k):        """        :type nums: List[int]        :type k: int        :rtype: int        """        def gcd(a, b):            while b:                a, b = b, a%b            return a         def lower_bit(x):            return x&-x         result = 0        for i in xrange(len(nums)):            mn = float("inf")            g = cnt = 0            for j in xrange(i, len(nums)):                g = gcd(g, nums[j])                bit = lower_bit(nums[j])                if bit < mn:                    mn = bit                    cnt = 0                if bit == mn:                    cnt += 1                result = max(result, g*(j-i+1)*(2 if cnt <= k else 1))                if g*(len(nums)-i)*2 <= result:                    break        return result  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗