leetcode 1035. 不相交的線(dp)

在兩條獨立的水平線上按給定的順序寫下 nums1 和 nums2 中的整數。

現在,可以繪制一些連接兩個數字 nums1[i] 和 nums2[j] 的直線,這些直線需要同時滿足滿足:

nums1[i] == nums2[j]
且繪制的直線不與任何其他連線(非水平線)相交。
請注意,連線即使在端點也不能相交:每個數字只能屬于一條連線。

以這種方法繪制線條,并返回可以繪制的最大連線數。

示例 1:
在這里插入圖片描述

輸入:nums1 = [1,4,2], nums2 = [1,2,4]
輸出:2
解釋:可以畫出兩條不交叉的線,如上圖所示。
但無法畫出第三條不相交的直線,因為從 nums1[1]=4 到 nums2[2]=4 的直線將與從 nums1[2]=2 到 nums2[1]=2 的直線相交。
示例 2:

輸入:nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
輸出:3
示例 3:

輸入:nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
輸出:2

解題思路

這題本質上就匹配最長公共子序列,只不過是把字母換成了數字,但是方法上是一樣的

代碼

class Solution {public int maxUncrossedLines(int[] nums1, int[] nums2) {int n=nums1.length,m=nums2.length;int[][] dp = new int[n+1][m+1];for (int i=1;i<=n;i++)for (int j = 1; j <=m; j++)dp[i][j]=nums1[i-1]==nums2[j-1]?dp[i-1][j-1]+1:Math.max(dp[i-1][j],dp[i][j-1]);return dp[n][m];}}

結果

在這里插入圖片描述

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

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

相關文章

SPI和RAM IP核

學習目的&#xff1a; &#xff08;1&#xff09; 熟悉SPI接口和它的讀寫時序&#xff1b; &#xff08;2&#xff09; 復習Verilog仿真語句中的$readmemb命令和$display命令&#xff1b; &#xff08;3&#xff09; 掌握SPI接口寫時序操作的硬件語言描述流程&#xff08;本例僅…

個人技術博客Alpha----Android Studio UI學習

項目聯系 這次的項目我在前端組&#xff0c;負責UI&#xff0c;下面簡略講下學到的內容和使用AS過程中遇到的一些問題及其解決方法。 常見UI控件的使用 1.TextView 在TextView中&#xff0c;首先用android:id給當前控件定義一個唯一標識符。在活動中通過這個標識符對控件進行事…

數據科學家數據分析師_站出來! 分析人員,數據科學家和其他所有人的領導和溝通技巧...

數據科學家數據分析師這一切如何發生&#xff1f; (How did this All Happen?) As I reflect on my life over the past few years, even though I worked my butt off to get into Data Science as a Product Analyst, I sometimes still find myself begging the question, …

leetcode 810. 黑板異或游戲

黑板上寫著一個非負整數數組 nums[i] 。Alice 和 Bob 輪流從黑板上擦掉一個數字&#xff0c;Alice 先手。如果擦除一個數字后&#xff0c;剩余的所有數字按位異或運算得出的結果等于 0 的話&#xff0c;當前玩家游戲失敗。 (另外&#xff0c;如果只剩一個數字&#xff0c;按位異…

react-hooks_在5分鐘內學習React Hooks-初學者教程

react-hooksSometimes 5 minutes is all youve got. So in this article, were just going to touch on two of the most used hooks in React: useState and useEffect. 有時只有5分鐘。 因此&#xff0c;在本文中&#xff0c;我們僅涉及React中兩個最常用的鉤子&#xff1a; …

分析工作試用期收獲_免費使用零編碼技能探索數據分析

分析工作試用期收獲Have you been hearing the new industry buzzword — Data Analytics(it was AI-ML earlier) a lot lately? Does it sound complicated and yet simple enough? Understand the logic behind models but dont know how to code? Apprehensive of spendi…

select的一些問題。

這個要怎么統計類別數呢&#xff1f; 哇哇哇 解決了。 之前怎么沒想到呢&#xff1f;感謝一樓。轉載于:https://www.cnblogs.com/AbsolutelyPerfect/p/7818701.html

html5語義化標記元素_語義HTML5元素介紹

html5語義化標記元素Semantic HTML elements are those that clearly describe their meaning in a human- and machine-readable way. 語義HTML元素是以人類和機器可讀的方式清楚地描述其含義的元素。 Elements such as <header>, <footer> and <article> …

重學TCP協議(12)SO_REUSEADDR、SO_REUSEPORT、SO_LINGER

1. SO_REUSEADDR 假如服務端出現故障&#xff0c;主動斷開連接以后&#xff0c;需要等 2 個 MSL 以后才最終釋放這個連接&#xff0c;而服務重啟以后要綁定同一個端口&#xff0c;默認情況下&#xff0c;操作系統的實現都會阻止新的監聽套接字綁定到這個端口上。啟用 SO_REUSE…

殘疾科學家_數據科學與殘疾:通過創新加強護理

殘疾科學家Could the time it takes for you to water your houseplants say something about your health? Or might the amount you’re moving around your neighborhood reflect your mental health status?您給植物澆水所需的時間能否說明您的健康狀況&#xff1f; 還是…

POJ 3660 Cow Contest [Floyd]

POJ - 3660 Cow Contest http://poj.org/problem?id3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is un…

Linux 網絡相關命令

1. telnet 1.1 檢查端口是否打開 執行 telnet www.baidu.com 80&#xff0c;粘貼下面的文本&#xff08;注意總共有四行&#xff0c;最后兩行為兩個空行&#xff09; telnet [domainname or ip] [port]例如&#xff1a; telnet www.baidu.com 80 如果這個網絡連接可達&…

JSON.parseObject(String str)與JSONObject.parseObject(String str)的區別

一、首先來說說fastjson fastjson 是一個性能很好的 Java 語言實現的 JSON 解析器和生成器&#xff0c;來自阿里巴巴的工程師開發。其主要特點是&#xff1a; ① 快速&#xff1a;fastjson采用獨創的算法&#xff0c;將parse的速度提升到極致&#xff0c;超過所有基于Java的jso…

jQuery Ajax POST方法

Sends an asynchronous http POST request to load data from the server. Its general form is:發送異步http POST請求以從服務器加載數據。 其一般形式為&#xff1a; jQuery.post( url [, data ] [, success ] [, dataType ] )url : is the only mandatory parameter. This…

spss23出現數據消失_改善23億人口健康數據的可視化

spss23出現數據消失District Health Information Software, or DHIS2, is one of the most important sources of health data in low- and middle-income countries (LMICs). Used by 72 different LMIC governments, DHIS2 is a web-based open-source platform that is used…

01-hibernate注解:類級別注解,@Entity,@Table,@Embeddable

Entity Entity:映射實體類 Entity(name"tableName") name:可選&#xff0c;對應數據庫中一個表&#xff0c;若表名與實體類名相同&#xff0c;則可以省略。 注意&#xff1a;使用Entity時候必須指定實體類的主鍵屬性。 第一步&#xff1a;建立實體類&#xff1a; 分別…

leetcode 1707. 與數組中元素的最大異或值

題目 給你一個由非負整數組成的數組 nums 。另有一個查詢數組 queries &#xff0c;其中 queries[i] [xi, mi] 。 第 i 個查詢的答案是 xi 和任何 nums 數組中不超過 mi 的元素按位異或&#xff08;XOR&#xff09;得到的最大值。換句話說&#xff0c;答案是 max(nums[j] XO…

MySQL基礎入門學習【2】數據類型

數據類型&#xff1a;指列、存儲過程參數、表達式和局部變量的數據特征&#xff0c;它決定了數據的存儲格式&#xff0c;代表了不同的信息類型 &#xff08;1&#xff09; 整型(按存儲范圍分類)&#xff1a;TINYINT&#xff08;1字節&#xff09; SAMLLINT&#xff08;2字節&am…

昆西·拉森的凈資產是多少?

People ask me how much I get paid all the time. It comes up on podcast interviews, Quora questions, and face-to-face discussions.人們問我&#xff0c;我一直得到多少報酬。 它來自播客訪談&#xff0c;Quora問題和面對面的討論。 And people search this question a…

COVID-19研究助理

These days scientists, researchers, doctors, and medical professionals face challenges to develop answers to their high priority scientific questions.如今&#xff0c;科學家&#xff0c;研究人員&#xff0c;醫生和醫學專家面臨著挑戰&#xff0c;無法為其高度優先…