Problem solution · Python

Number of Alternating Xor Partitions

Number of Alternating Xor Partitions: a Python solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
61 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

Dynamic programming

For Number of Alternating Xor Partitions, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 61 lines of Python from the credited upstream file number-of-alternating-xor-partitions.py.
  • The implementation visibly relies on sequence storage, hash lookup, cached states.
  • No explicit loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

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 codeNumber of Alternating Xor Partitions · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(n)# Space: O(1) # dpclass Solution(object):    def alternatingXOR(self, nums, target1, target2):        """        :type nums: List[int]        :type target1: int        :type target2: int        :rtype: int        """        MOD = 10**9+7        vals = [0, target1, target1^target2, target2]        dp = [0]*len(vals)        dp[0] = 1        prefix = 0        for i in xrange(len(nums)-1):            new_dp = dp[:]            prefix ^= nums[i]                        for j in xrange(len(vals)):                if vals[j] != prefix:                    continue                new_dp[j] = (new_dp[j]+dp[(j-1)%len(dp)])%MOD            dp = new_dp        prefix ^= nums[-1]        result = 0        for i in xrange(len(vals)):            if vals[i] != prefix:                continue            result = (result+dp[(i-1)%len(dp)])%MOD        return result  # Time:  O(n)# Space: O(n)import collections  # freq tableclass Solution2(object):    def alternatingXOR(self, nums, target1, target2):        """        :type nums: List[int]        :type target1: int        :type target2: int        :rtype: int        """        MOD = 10**9+7        cnt1 = collections.defaultdict(int)        cnt2 = collections.defaultdict(int)        cnt2[0] = 1        result = prefix = 0        for x in nums:            prefix ^= x            c1 = cnt2[prefix^target1]            c2 = cnt1[prefix^target2]            cnt1[prefix] = (cnt1[prefix]+c1)%MOD            cnt2[prefix] = (cnt2[prefix]+c2)%MOD        return (c1+c2)%MOD 

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 ↗