当前位置:网站首页>Backtracking - 491. Incremental subsequence
Backtracking - 491. Incremental subsequence
2022-07-26 12:24:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Give you an array of integers nums , Find and return all different increasing subsequences in the array , In increasing subsequence There are at least two elements . You can press In any order Return to the answer .
The array may contain duplicate elements , If two integers are equal , It can also be regarded as a special case of increasing sequence .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/increasing-subsequences
2 Title Example
Example 1:
Input :nums = [4,6,7,7]
Output :[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
Example 2:
Input :nums = [4,4,3,2,1]
Output :[[4,4]]
3 Topic tips
1 <= nums.length <= 15
-100 <= nums[i] <= 100
4 Ideas
Binary enumeration can be implemented recursively , List all subsequences , Then judge whether it is legal ., We can get such code :
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
public void dfs(int cur, int[] nums) {
if (cur == nums.length) {
// Judge whether it is legal , If the legal judgment is repeated , Add those that meet the conditions to the answer
if (isValid() && notVisited()) {
ans.add(new ArrayList<Integer>(temp));
}
return;
}
// If you select the current element
temp.add(nums[cur]);
dfs(cur + 1, nums);
temp.remove(temp.size() - 1);
// If you do not select the current element
dfs(cur + 1, nums);
}
This is a general template for recursively enumerating subsequences , That is, a temporary array temp To save the currently selected subsequence , Use cur To represent the subscript of the current position , stay dfs(cur,nums) Before the start ,[0, cur ―1] All elements in this interval have been considered , and [cur, n] The elements in this interval have not been considered . In execution dfs(cur,nums) when , We consider the cur Choose this position or not , If you select the current element , Then add the current element to temp in , Then recurse to the next position , At the end of recursion , Should be temp The last element of is deleted for backtracking ; If you don't select the current element , Recurses directly to the next location . Of course , If we simply enumerate like this , For each subsequence , We need to do it again O(n) The validity check and hash judgment of are repeated , During the execution of the whole program , We also need to use a space cost O(2") To maintain the hash value of the subsequence that has appeared . We can make some simple restrictions on choice and non choice , You can make the enumerated ones legal and not repeated :
The way to legitimize the sequence is very simple , That is to 「 choice 」 Make a limit , This element can be selected only when the current element is greater than or equal to the last selected element , All the elements enumerated in this way are legal
How to ensure that there is no repetition ? We need to give 「 No choice 」 Make a limit , Only when the current element is not equal to the last selected element , To consider not selecting the current element , Directly recurse the following elements . Because if there are two identical elements , We will consider four cases :
- The former is chosen , The latter was chosen
- The former is chosen , The latter is not chosen
- The former is not chosen , The latter was chosen
- The former is not chosen , The latter is not chosen
The second case and the third case are actually equivalent , After we limit it like this , Abandon the second , The third one is reserved , So the goal of weight removal is achieved .
Complexity analysis
- Time complexity :o(2n*n). You still need to do binary enumeration of subsequences , Although the enumerated sequence omits the process of judging legitimacy and hashing , But it still needs O(n) Time to add to the answer .
- Spatial complexity :O(n). The space cost of temporary arrays here is O(n), The space cost of stack space used by recursion is also O(n).
5 My answer
class Solution {
List<Integer> temp = new ArrayList<Integer>();
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> findSubsequences(int[] nums) {
dfs(0, Integer.MIN_VALUE, nums);
return ans;
}
public void dfs(int cur, int last, int[] nums) {
if (cur == nums.length) {
if (temp.size() >= 2) {
ans.add(new ArrayList<Integer>(temp));
}
return;
}
if (nums[cur] >= last) {
temp.add(nums[cur]);
dfs(cur + 1, nums[cur], nums);
temp.remove(temp.size() - 1);
}
if (nums[cur] != last) {
dfs(cur + 1, last, nums);
}
}
}
边栏推荐
猜你喜欢

3D point cloud course (VIII) -- feature point matching

连锁店收银系统如何帮助鞋店管理好分店?

FPGA入门学习(三)- 38译码器

Use the jsonobject object in fastjason to simplify post request parameter passing

羽毛球馆的两个基础设施你了解多少?

Some common writing methods and skills

Ds-24c/dc220v time relay

.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI

Pytest interface automation test framework | pytest configuration file

How does the chain store cashier system help shoe stores manage their branches?
随机推荐
Y9000p2022 reinstallation win10 problem
行业案例|指标中台如何助力银行业普惠金融可持续发展
一些常用的文章写作使用方法和技巧
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
Pytest interface automation test framework | setup and teardown functions of pytest
[MySQL constraint]
Use of strjoin function in MATLAB
Pytoch deep learning quick start tutorial -- mound tutorial notes (I)
全国职业院校技能大赛网络安全B模块wirshark数据包分析 wireshark0051.pcap
How much do you know about the two infrastructures of the badminton stadium?
SSJ-21B时间继电器
Jsj-3/ac220v time relay
Redisson分布式锁使用实例(一)
Introduction to FPGA (II) - one out of two selector
.eslintrc.js configuration description
MySQL之数据查询(聚合函数)
Problems and solutions in the learning process of file class
腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
剑指 Offer 24. 反转链表
Tencent cloud and smart industry business group (CSIG) adjusted the organizational structure and established the digital twin product department