Leetcode 2939. Maximum Xor Product

  • [Leetcode 2939. Maximum Xor Product](#Leetcode 2939. Maximum Xor Product)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)
    • [3. 代码优化:](#3. 代码优化:)

1. 解题思路

这一题思路上来说我们就是逐位进行考虑。

对于xor操作,显然我们只有以下两种情况:

  1. 00或者11:此时我们总可以令两者都变成11,此时获得的乘积一定最大;
  2. 01或者10:此时我们变换的结果也是01或者10,但两者的乘积变化就会有所区别。

因此,这里我们主要需要考虑的就是第二种情况,也就是对于这些位上的01分配问题,而这个不难证明,假设其他位上有结论 a > b a>b a>b,那么总是将1分配到b上面可以使得结果更大。

故而,我们只需要基于这个思路进行代码实现就行了。

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def maximumXorProduct(self, a: int, b: int, n: int) -> int:
        MOD = 10**9 + 7
        
        def num2digits(num):
            digits = [0 for i in range(50)]
            idx = 0
            while num != 0:
                digits[idx] = num % 2
                num = num // 2
                idx += 1
            return digits
        
        def digits2num(digits):
            flag = 1
            ans = 0
            for d in digits:
                ans += flag * d
                flag = flag * 2 % MOD
            return ans % MOD
        
        def convert(digits_a, digits_b):
            status = 0
            for idx in range(49, -1, -1):
                if idx >= n:
                    if digits_a[idx] > digits_b[idx]:
                        if status == 0:
                            status = 1
                    elif digits_a[idx] < digits_b[idx]:
                        if status == 0:
                            status = 2
                    continue
                if digits_a[idx] == 0 and digits_b[idx] == 0:
                    digits_a[idx] = 1
                    digits_b[idx] = 1
                elif digits_a[idx] == 1 and digits_b[idx] == 1:
                    digits_a[idx] = 1
                    digits_b[idx] = 1
                elif digits_a[idx] == 0 and digits_b[idx] == 1:
                    if status == 0:
                        status = 2
                    elif status == 2:
                        digits_a[idx] = 1
                        digits_b[idx] = 0
                else:
                    if status == 0:
                        status = 1
                    elif status == 1:
                        digits_a[idx] = 0
                        digits_b[idx] = 1
            return
        
        digits_a = num2digits(a)
        digits_b = num2digits(b)
        convert(digits_a, digits_b)
        na = digits2num(digits_a)
        nb = digits2num(digits_b)
        return na * nb % MOD          

提交代码评测得到:耗时69ms,占用内存16.3MB。

3. 代码优化:

看了一下大佬们的提交结果,思路也大差不差,不过实现上就比我优雅很多了,给一个大佬的实现如下,膜拜一下。

python 复制代码
class Solution:
    def maximumXorProduct(self, a: int, b: int, n: int) -> int: 
        for i in range(n):
            if (a & (1 << i)) == 0 and (b & (1 << i)) == 0:
                a += 1 << i
                b += 1 << i
        for i in range(n):
            if a > b and (a & (1 << i)) != 0 and (b & (1 << i)) == 0 and (a - (1 << i)) * (b + (1 << i)) > a * b:
                a -= 1 << i
                b += 1 << i
            elif a < b and (a & (1 << i)) == 0 and (b & (1 << i)) != 0 and (a + (1 << i)) * (b - (1 << i)) > a * b:
                a += 1 << i
                b -= 1 << i
        return a * b % int(1e9 + 7)
相关推荐
Espresso Macchiato8 天前
Leetcode 3388. Count Beautiful Splits in an Array
leetcode·z算法·leetcode medium·leetcode周赛428·leetcode 3388
Espresso Macchiato2 个月前
Leetcode 3316. Find Maximum Removals From Source String
leetcode·动态规划·leetcode medium·leetcode 3316·leetcode双周赛141
Espresso Macchiato3 个月前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
Espresso Macchiato3 个月前
Leetcode 3301. Maximize the Total Height of Unique Towers
leetcode·leetcode medium·leetcode双周赛140·leetcode 3301
Espresso Macchiato3 个月前
Leetcode 3302. Find the Lexicographically Smallest Valid Sequence
leetcode medium·lcs·leetcode 3302·leetcode双周赛140·最大公共子序列
Espresso Macchiato4 个月前
Leetcode 3255. Find the Power of K-Size Subarrays II
leetcode·leetcode medium·leetcode 3255·leetcode 3254·leetcode周赛137
Espresso Macchiato5 个月前
Leetcode 3240. Minimum Number of Flips to Make Binary Grid Palindromic II
leetcode·leetcode medium·回文·leetcode 3240·leetcode双周赛136
Espresso Macchiato5 个月前
Leetcode 3234. Count the Number of Substrings With Dominant Ones
排列组合·leetcode medium·容斥原理·leetcode 3234·leetcode周赛408
Espresso Macchiato6 个月前
Leetcode 3201. Find the Maximum Length of Valid Subsequence I
leetcode medium·leetcode题解·leetcode 3201·leetcode周赛404
依旧风轻6 个月前
136. 只出现一次的数字
数据结构·算法·swift·xor