leetcode 5756. 兩個數組最小的異或值之和(狀態壓縮dp)

image.png

題目

給你兩個整數數組?nums1 和?nums2?,它們長度都為?n?。

兩個數組的 異或值之和?為?(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + … + (nums1[n - 1] XOR nums2[n - 1])?(下標從 0 開始)。

比方說,[1,2,3] 和?[3,2,1]?的 異或值之和?等于?(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4?。
請你將?nums2?中的元素重新排列,使得 異或值之和?最小?。

請你返回重新排列之后的 異或值之和?。

示例 1:

輸入:nums1 = [1,2], nums2 = [2,3]
輸出:2
解釋:將 nums2 重新排列得到 [3,2] 。
異或值之和為 (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2 。

示例 2:

輸入:nums1 = [1,0,3], nums2 = [5,3,4]
輸出:8
解釋:將 nums2 重新排列得到 [5,4,3] 。
異或值之和為 (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8 。

解題思路

假設n是數組的長度,這題可以看成nums1的前c個數字,在nums2中找出c個數字,一一對應,從而使得異或和最小,因此我們可以使用n位的二進制數即可表示nums2的不同取值狀態,第j位為1代表該情況下,nums2[j]已經被選擇了.

  • 例如對于nums1 = [1,2], nums2 = [2,3]
  • 01表示nums2[1]=3已經被選擇用于與nums1的前一個數字組成異或和了,dp[01]就是代表這種情況的最小異或和
  • 10表示nums2[0]=2已經被選擇用于與nums1的前一個數字組成異或和了
    所以dp[11]應該從dp[01]或者dp[10]轉移而來,并且需要加上新產生的異或值

狀態轉移

找出當前狀態的上一步狀態,進行轉移

  1. 遍歷nums2的每一個元素
  2. (1<<j)&i>0代表nums2[j]在當前狀態下已經被選擇,所以我們將這個選擇取消(將這一位置零),就是上一步的其中一種狀態dp[(1<<j)^i]
		for j := 0; j < n; j++ {if (1<<j)&i>0{val:=dp[(1<<j)^i]+(nums1[cnt(i)-1]^nums2[j])if val<dp[i]{dp[i]=val}}}

代碼

func minimumXORSum(nums1 []int, nums2 []int) (res int){n:=len(nums1)cnt := func(cur int) (res int) {for k := 0; k < 31; k++ {res+=1&curcur>>=1}return}mask:=1<<ndp := make([]int, mask)for i := 1; i < mask; i++{dp[i]=2e9}for i := 1; i < mask; i++ {for j := 0; j < n; j++ {if (1<<j)&i>0{val:=dp[(1<<j)^i]+(nums1[cnt(i)-1]^nums2[j])if val<dp[i]{dp[i]=val}}}}return dp[mask-1]
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390796.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390796.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390796.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

客戶細分模型_Avarto金融解決方案的客戶細分和監督學習模型

客戶細分模型Lets assume that you are a CEO of a company which have some X amount of customers in a city with 1000 *X population. Analyzing the trends/features of your customer and segmenting the population of the city to land new potential customers would …

用 Go 編寫一個簡單的 WebSocket 推送服務

用 Go 編寫一個簡單的 WebSocket 推送服務 本文中代碼可以在 github.com/alfred-zhon… 獲取。 背景 最近拿到需求要在網頁上展示報警信息。以往報警信息都是通過短信&#xff0c;微信和 App 推送給用戶的&#xff0c;現在要讓登錄用戶在網頁端也能實時接收到報警推送。 依稀記…

leetcode 231. 2 的冪

給你一個整數 n&#xff0c;請你判斷該整數是否是 2 的冪次方。如果是&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 如果存在一個整數 x 使得 n 2x &#xff0c;則認為 n 是 2 的冪次方。 示例 1&#xff1a; 輸入&#xff1a;n 1 輸出&#xff1a;tr…

Java概述、環境變量、注釋、關鍵字、標識符、常量

Java語言的特點 有很多小特點&#xff0c;重點有兩個開源&#xff0c;跨平臺 Java語言是跨平臺的 Java語言的平臺 JavaSE JavaME--Android JavaEE DK,JRE,JVM的作用及關系(掌握) (1)作用 JVM&#xff1a;保證Java語言跨平臺 &#xff0…

寫游戲軟件要學什么_為什么要寫關于您所知道的(或所學到的)的內容

寫游戲軟件要學什么Im either comfortably retired or unemployed, I havent decided which. What I do know is that I am not yet ready for decades of hard-won knowledge to lie fallow. Still driven to learn new technologies and to develop new projects, I see the …

leetcode 342. 4的冪

給定一個整數&#xff0c;寫一個函數來判斷它是否是 4 的冪次方。如果是&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 整數 n 是 4 的冪次方需滿足&#xff1a;存在整數 x 使得 n 4x 示例 1&#xff1a; 輸入&#xff1a;n 16 輸出&#xff1a;true …

梯度反傳_反事實政策梯度解釋

梯度反傳Among many of its challenges, multi-agent reinforcement learning has one obstacle that is overlooked: “credit assignment.” To explain this concept, let’s first take a look at an example…在許多挑戰中&#xff0c;多主體強化學習有一個被忽略的障礙&a…

三款功能強大代碼比較工具Beyond compare、DiffMerge、WinMerge

我們經常會遇到需要比較同一文件的不同版本&#xff0c;特別是代碼文件。如果人工去對比查看&#xff0c;勢必費時實力還會出現紕漏和錯誤&#xff0c;因此我們需要借助一些代碼比較的工具來自動完成這些工作。這里介紹3款比較流行且功能強大的工具。 1. Beyond compare這是一款…

shell腳本_Shell腳本

shell腳本In the command line, a shell script is an executable file that contains a set of instructions that the shell will execute. Its main purpose is to reduce a set of instructions (or commands) in just one file. Also it can handle some logic because it…

大數據與Hadoop

大數據的定義 大數據是指無法在一定時間內用常規軟件工具對其內容進行抓取、管理和處理的數據集合。 大數據的概念–4VXV 1,數據量大&#xff08;Volume&#xff09;2,類型繁多&#xff08;Variety &#xff09;3,速度快時效高&#xff08;Velocity&#xff09;4,價值密度低…

Arm匯編指令學習

ARM指令格式 ARM指令格式解析 opcode: 指令助記符,例如,MOV ,ADD,SUB等等 cond&#xff1a;指令條件碼表.下面附一張圖 {S}:是否影響CPSR的值. {.W .N}:指令寬度說明符,無論是ARM代碼還是Thumb&#xff08;armv6t2或更高版本&#xff09;代碼都可以在其中使用.W寬度說明符&…

facebook.com_如何降低電子商務的Facebook CPM

facebook.comWith the 2020 election looming, Facebook advertisers and e-commerce stores are going to continually see their ad costs go up as the date gets closer (if they haven’t already).隨著2020年選舉的臨近&#xff0c;隨著日期越來越近&#xff0c;Facebook…

Python中的If,Elif和Else語句

如果Elif Else聲明 (If Elif Else Statements) The if/elif/else structure is a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data.if / elif / else結構是控制程序流程的常用方法&#x…

Hadoop安裝及配置

Hadoop的三種運行模式 單機模式&#xff08;Standalone,獨立或本地模式&#xff09;:安裝簡單,運行時只啟動單個進程,僅調試用途&#xff1b;偽分布模式&#xff08;Pseudo-Distributed&#xff09;:在單節點上同時啟動namenode、datanode、secondarynamenode、resourcemanage…

漏洞發布平臺-安百科技

一個不錯的漏洞發布平臺&#xff1a;https://vul.anbai.com/ 轉載于:https://blog.51cto.com/antivirusjo/2093758

Android 微信分享圖片

private String APP_ID "00000000000000000"; //微信 APPID private IWXAPI iwxapi; private void regToWx() {iwxapi WXAPIFactory.createWXAPI(context, APP_ID, true);//這里context記得初始化iwxapi.registerApp(APP_ID); } IMServer.getDiskBitmap(IMServer.u…

蒙蒂霍爾問題_常見的邏輯難題–騎士和刀,蒙蒂·霍爾和就餐哲學家的問題解釋...

蒙蒂霍爾問題While not strictly related to programming, logic puzzles are a good warm up to your next coding session. You may encounter a logic puzzle in your next technical interview as a way to judge your problem solving skills, so its worth being prepare…

西格爾零點猜想_我從埃里克·西格爾學到的東西

西格爾零點猜想I finished reading Eric Siegel’s Predictive Analytics. And I have to say it was an awesome read. How do I define an awesome or great book? A book that changes your attitude permanently. You must not be the same person that you were before y…

C/C++實現刪除字符串的首尾空格

StdStringTrimTest.cpp #include <iostream> int main() {std::string str(" 字符串 String ");std::cout << str << std::endl;std::cout << str.size() << std::endl;str.erase(str.find_first_of( ), str.find_first_not_of…

assign復制對象_JavaScript標準對象:assign,values,hasOwnProperty和getOwnPropertyNames方法介紹...

assign復制對象In JavaScript, the Object data type is used to store key value pairs, and like the Array data type, contain many useful methods. These are some useful methods youll use while working with objects.在JavaScript中&#xff0c; Object數據類型用于存…