Journey of LeetCode|DAY 25
Preface
This is a new day to continue my backtracking algorithm journey.
Learn something new and keep reviewing what I learnt before.
1. Increasing Subsequences
LeetCode Link: 491. Increasing Subsequences
Given an integer array nums, return all the different possible increasing subsequences of the given array with at least two elements. You may return the answer in any order.
The given array may contain duplicates, and two equal integers should also be considered a special case of increasing sequence.
Analysis and Solution
BackTrack
LeetCode C++ as followings BackTrack
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& nums, int startIndex) {
if (path.size() > 1) {
result.push_back(path);
}
int used[201] = {0}; // deduplication by array ; value range is [-100, 100]
for (int i = startIndex; i < nums.size(); i++) {
if ((!path.empty() && nums[i] < path.back())
|| used[nums[i] + 100] == 1) {
continue;
}
used[nums[i] + 100] = 1; // Record that this element has been used in this layer, and it cannot be used later in this layer
path.push_back(nums[i]);
backtracking(nums, i + 1);
path.pop_back();
}
}
public:
vector<vector<int>> findSubsequences(vector<int>& nums) {
result.clear();
path.clear();
backtracking(nums, 0);
return result;
}
};
2. Permutations
LeetCode Link: 46. Permutations
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Analysis and Solution
BackTrack
LeetCode C++ as followings BackTrack
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
void backtracking (vector<int>& nums, vector<bool>& used) {
// here indicates that get a result we want
if (path.size() == nums.size()) {
result.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (used[i] == true) continue; // skip if it was used before
used[i] = true;
path.push_back(nums[i]);
backtracking(nums, used);
path.pop_back();
used[i] = false;
}
}
vector<vector<int>> permute(vector<int>& nums) {
result.clear();
path.clear();
vector<bool> used(nums.size(), false);
backtracking(nums, used);
return result;
}
};
3. Permutations II
LeetCode Link: 47. Permutations II
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Analysis and Solution
BackTrack
LeetCode C++ as followings BackTrack
class Solution {
private:
vector<vector<int>> result;
vector<int> path;
void backtracking (vector<int>& nums, vector<bool>& used) {
// here indicates that get a result
if (path.size() == nums.size()) {
result.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++) {
// used[i - 1] == true,Indicates that nums[i - 1] on the same branch has been used
// used[i - 1] == false,indicates that nums[i - 1] on the same layer has been uesd
// skip if nums[i - 1] on the same layer hae been uesd
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
continue;
}
if (used[i] == false) {
used[i] = true;
path.push_back(nums[i]);
backtracking(nums, used);
path.pop_back();
used[i] = false;
}
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {//initialization
result.clear();
path.clear();
sort(nums.begin(), nums.end()); // sort for deduplication
vector<bool> used(nums.size(), false);
backtracking(nums, used);
return result;
}
};