Skip to content

Conversation

@jhm9595
Copy link
Owner

@jhm9595 jhm9595 commented Oct 10, 2022

Time Complexity : ..?

@jhm9595 jhm9595 requested a review from bigelephant29 October 10, 2022 06:46
public List<String> generateParenthesis(int n) {

List<String> strList = new ArrayList<String>();
strList.add("");
Copy link
Collaborator

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);
}
}
}
Copy link
Collaborator

@bigelephant29 bigelephant29 Oct 16, 2022

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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()));
Copy link
Collaborator

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.

Copy link
Collaborator

@bigelephant29 bigelephant29 Oct 16, 2022

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++)

Copy link
Collaborator

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++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants