[2nd verseion]4th LeetCode:22. Generate Parentheses

Shu-Yu Huang
1 min readApr 17, 2020

Solution2

In the last time, solution 1 solved “Generate Parentheses”, but it exceeded the time limit.

Now let’s try another method:
Adding pairs of parentheses “()” once a time.
This will make sure the is no un-paired parenthesis.

The execution time is now reduced but it still exceeds the limit.

So I think I should avoid using recursive, which may reduce the time complexity.
Also, suggested by PercyJiang’s try I used list(set(XXX)) to reduce the time complexity of excluding repeated sequences of parentheses.

https://leetcode.com/problems/generate-parentheses/discuss/602997/Python-3-adding-parentheses-pairs-with-loop

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Shu-Yu Huang
Shu-Yu Huang

Written by Shu-Yu Huang

AI engineer in Taiwan AI Academy| Former process engineer in TSMC| Former Research Assistance in NYMU| Studying few-shot-learning and GAN

No responses yet

Write a response