-
Notifications
You must be signed in to change notification settings - Fork 0
Leet code22 #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Leet code22 #66
Conversation
| public List<String> generateParenthesis(int n) { | ||
|
|
||
| List<String> strList = new ArrayList<String>(); | ||
| strList.add(""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List<String> strList = new ArrayList<String>(List.of(""));
| list.add(str1 + str2); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest you to split the i==1 case.
for(String str1 : generate(strList, n-1)) {
list.add("(" + str1 + ")");
}
for(int i = 1 ; i < n; i++) {
for(String str1 : generate(strList, n-i)) {
for(String str2 : generate(strList, i)) {
list.add(str1 + str2);
}
}
}
In this way, the goal of the code is more clear. This is better for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, in this way, you don't need the above if(n==1). I think you can combine these two cases together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind that we should always try to find a generic solution which covers edge cases like n==1.
| } | ||
| } | ||
|
|
||
| map.put(n, list.stream().distinct().collect(Collectors.toList())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The distinct() here is very risky because it indicates that you put duplicate answers into the list.
When you have duplicate answers, you're very likely doing something wrongly (or super inefficiently).
You should think about a solution which doesn't generate duplicate answers.
If the duplicate answers are inevitable, make sure the number of duplicate answers is controllable.
I'm not pretty sure about this problem, but I guess there are some solutions that completely avoid duplicate answers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class Solution {
public:
vector<string> ans;
void dfs(string &s, int c1, int c2) {
if(c1 + c2 == 0) {
ans.push_back(s);
} else {
if(c1 > 0) {
s += '(';
dfs(s, c1-1, c2+1);
s.pop_back();
}
if(c2 > 0) {
s += ')';
dfs(s, c1, c2-1);
s.pop_back();
}
}
}
vector<string> generateParenthesis(int n) {
string tmp = "";
dfs(tmp, n, 0);
return ans;
}
};
This is the code which I wrote several years ago when I worked on this problem. (In C++)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wrote the code based on your solution.
class Solution {
private:
unordered_map<int, vector<string>> answers;
unordered_map<int, vector<string>> single_segment;
public:
Solution() {
// initialization
answers[1].push_back("()");
single_segment[1] = answers[1];
}
vector<string> generateParenthesis(int n) {
if(answers.find(n) != answers.end()) {
return answers[n];
}
vector<string> current_answers;
for(auto answer: generateParenthesis(n-1)) {
current_answers.push_back("(" + answer + ")");
}
single_segment[n] = current_answers;
for(int i = 1; i < n; i++) {
for(auto answer_first: generateParenthesis(n-i)) {
for(auto answer_second: single_segment[i]) {
current_answers.push_back(answer_first + answer_second);
}
}
}
return answers[n] = current_answers;
}
};
I think in this solution we don't need distinct() function call. This was written in C++.
Time Complexity : ..?