当前位置:网站首页>522. longest special sequence II / Sword finger offer II 101 Split equal sum subset
522. longest special sequence II / Sword finger offer II 101 Split equal sum subset
2022-06-27 14:11:00 【Biqiliang】
522. The longest special sequence II【 Medium question 】【 A daily topic 】
Ideas :【 enumeration 】
Procedures are written in code comments , Refer to the official solution . But my execution time is 2ms~
Code :
class Solution {
public int findLUSlength(String[] strs) {
/** * For a string in the string array strs[i] Come on If a subsequence of it sub It's a special sequence , That is, it is not a subsequence of other strings in the array * So this string strs[i] It is also a special sequence * prove : Reduction to absurdity * hypothesis sub Is a special sequence , that sub It does not appear as a subsequence in any other string * So here you are sub Add any characters , It still doesn't appear in other strings as a subsequence * Because the topic requires finding the longest special sequence , So we just need to enumerate every string in the array , Filter out the longest string that matches the special sequence , That is, the longest special sequence we are looking for * So now the problem is reduced to two steps : * 1. Traverse each string , Determine whether it is a special sequence * 2. If the string meets the special sequence requirements , Update the longest special sequence length max, The function finally returns max * For the first 1 Step by step , Can be converted to , Determine whether the current string is a subsequence of other strings in the array except itself , yes It indicates that the current string is a special sequence , no Is not a special sequence */
int max = -1,n = strs.length;
// Double loop traversal string array , Screening strs[i] Whether it is a special sequence
for (int i = 0; i < n; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) {
if (i == j){
continue;
}
String s1 = strs[i],s2 = strs[j];
int n1 = s1.length(),n2 = s2.length();
//s1 Length ratio of s2 Also long ,s1 It can't be s2 The subsequence , Don't go on judging
if (n1 > n2){
continue;
}else if (n1 == n2){
//s1 The length of s2 equal
// If s1 And s2 Unequal , be s1 No s2 The subsequence
// If s1 And s2 equal , be s1 yes s2 The subsequence , The location of the sign is flase, Inner layer for Loop exit
if (s1.equals(s2)){
flag = false;
break;
}else {
continue;
}
}
// Judge s1 Whether it is s2 The subsequence ( here n1 < n2)
int p1 = 0,p2 = 0;
while (p1 < n1 && p2 < n2){
// If p1 Character and p2 The characters are equal , be p1 And p2 Shift right
if (s1.charAt(p1) == s2.charAt(p2)){
p1++;
p2++;
}else {
// otherwise , Only will p2 Move right
p2++;
}
}
// If s1 End of normal traversal , explain s1 yes s2 The subsequence , The location of the sign is false, Inner layer for Loop exit
if (p1 == n1){
flag = false;
break;
}
}
// After screening ,strs[i] Passed the test , It's a special sequence , Update the length of the longest special sequence
if (flag){
max = Math.max(max,strs[i].length());
}
}
return max;
}
}
The finger of the sword Offer II 101. To divide into equal and subsets 【 Simple questions 】
Ideas :【 Dynamic programming 】
First order dp And second order dp, Write according to the answer ~, This is a dynamic programming problem. I really didn't expect .
Code :【 Second order dp】
class Solution {
public boolean canPartition(int[] nums) {
// It should be divided into two parts , Then the array length is at least 2,
int n = nums.length;
if (n < 2){
return false;
}
// It should be divided into two parts of equal sum , The sum of each part must be half of the total , So you should first find the sum of the arrays sum, In this way, the goal and
int sum = 0,max = 0;
for (int num : nums) {
max = Math.max(max,num);
sum += num;
}
// If sum Is odd , Then the array can not be divided into elements and equal parts
if(sum % 2 != 0){
return false;
}
int target = sum / 2;
//max > target, It is impossible to divide an array into elements and equal parts
if (max > target){
return false;
}
//max == target It must be divided into two parts ,max As part of , The rest of the elements are another part
if (max == target){
return true;
}
// Definition dp Array dp[i][j] Represents array subscript [0,i) Within the scope of , Select whether several elements can make elements and reach j Then we just need to find dp[n-1][target] Is it true that will do
boolean[][] dp = new boolean[n][target+1];
// The boundary conditions : j=0 when ,dp[i][0] = true ; i=0 when ,dp[0][nums[0]] = true.
dp[0][nums[0]] = true;
for (int i = 0; i < n; i++) {
dp[i][0] = true;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= target; j++) {
if (j >= nums[i]){
// No choice nums[i] Elements , be dp[i][j] Depending on dp[i-1][j]
// choice nums[i] Elements , be dp[i][j] Depending on dp[i-1][j-nums[i]]
dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]];
}else {
// because j < nums[i] It is impossible to choose nums[i] Elements
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n-1][target];
}
}
Code :【 First order dp】
class Solution {
public boolean canPartition(int[] nums) {
// It should be divided into two parts , Then the array length is at least 2,
int n = nums.length;
if (n < 2){
return false;
}
// It should be divided into two parts of equal sum , The sum of each part must be half of the total , So you should first find the sum of the arrays sum, In this way, the goal and
int sum = 0,max = 0;
for (int num : nums) {
max = Math.max(max,num);
sum += num;
}
// If sum Is odd , Then the array can not be divided into elements and equal parts
if(sum % 2 != 0){
return false;
}
int target = sum / 2;
//max > target, It is impossible to divide an array into elements and equal parts
if (max > target){
return false;
}
//max == target It must be divided into two parts ,max As part of , The rest of the elements are another part
if (max == target){
return true;
}
// Definition dp Array dp[j] Indicates that the current array is within the optional range , Select whether several elements can make elements and reach j, Then we find that when the optional range is the entire array ,dp[target] Is it true that will do
boolean[] dp = new boolean[target+1];
// The boundary conditions :j = 0 when , Must be true
dp[0] = true;
for (int num : nums) {
for (int j = target; j >= num; j--) {
/*if (j >= nums[i]){ // choice nums[i] Elements , be dp[j] Depending on dp[j-nums[i]] // No choice nums[i] Elements , be dp[j] Immobility dp[j] = dp[j] || dp[j-nums[i]]; }else { //j < nums[i] when , It is impossible to choose nums[i] therefore dp[j] Keep still dp[j] = dp[j]; } So the transfer equations are summarized as follows */
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
}
}
边栏推荐
- 类模板中可变参的逐步展开
- [microservices sentinel] hotspot rules | authorization rules | cluster flow control | machine list
- Dynamic Networks and Conditional Computation论文简读和代码合集
- Leetcode 724. 寻找数组的中心下标(可以,一次过)
- SFINAE
- Half find (half find)
- Awk concise tutorial
- 为什么 Oracle 云客户必须在Oracle Cloud 季度更新发布后自行测试?
- Redis持久化
- 简析国内外电商的区别
猜你喜欢

PostgreSQL 15新版本特性解读(含直播问答、PPT资料汇总)

芯片供给过剩之际,进口最多的中国继续减少进口,美国芯片慌了

Tsinghua & Shangtang & Shanghai AI & CUHK proposed Siamese image modeling, which has both linear probing and intensive prediction performance

以前国产手机高傲定价扬言消费者爱买不买,现在猛降两千求售

Axi bus

AcWing 第57 场周赛

Practice of constructing ten billion relationship knowledge map based on Nebula graph

巧用redis实现点赞功能,它不比mysql香吗?

buuctf misc 百里挑一

Completely solve the problem of Chinese garbled code in Web Engineering at one time
随机推荐
Acwing game 57
美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越
[WUSTCTF2020]girlfriend
Openssf security plan: SBOM will drive software supply chain security
Pytorch learning 3 (test training model)
反射学习总结
【高等数学】从法向量到第二类曲面积分
CMOS level circuit analysis
海量数据!秒级分析!Flink+Doris构建实时数仓方案
Shell concise tutorial
JVM parameter setting and analysis
打印输出数(递归方法解决)
Summary and Thinking on interface test automation
enable_if
事务的四大特性
基于Vue+Node+MySQL的美食菜谱食材网站设计与实现
A method to realize automatic renaming of pictures uploaded by WordPress
AXI總線
How ASP connects Excel
Rereading the classic: the craft of research (1)