-
Notifications
You must be signed in to change notification settings - Fork 2
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
6-mjj111 #196
6-mjj111 #196
Conversation
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.
ํํ ์ด ๋ฌธ์ ์ ์ต๋์ ์ฅ๋ฒฝ์ ์์ด์๊ตฐ์.... ๋ฌธ์ ์๋ชป ์ดํดํด์ "์ด๊ฑธ ์ด์ผ ํ์ง?" ํ๋ค์ ใ ใ
๋จ์ํ ๋ฐฑํธ๋ํน ๋ฐฉ์์ผ๋ก ๋ฌธ์์ด ์กฐํฉ์ ์์ฑํด์ ๊ฒ์ฌํ๊ธฐ์ ๋ฑ๋ด๋ ์๊ฐ์ด๊ณผ ๋ ๊ฒ ๊ฐ์์ ์ด์ฐ์ ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํ๋ ์์ผ๋ก ํธ๋ ๋๋ค์...
#include <unordered_set>
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
words = unordered_set<string>(wordDict.begin(), wordDict.end());
return dfs(s);
}
private:
unordered_set<string> words;
unordered_map<string, bool> checked;
bool dfs(const string& s) { // ๋ถ๋ถ ๋ฌธ์์ด์ ๋ง๋ค์ด๊ฐ๋ฉฐ ํ์ธ
if(words.count(s) > 0) { // wordDict์ ์๋ ๋ฌธ์์ด์ด๋ฉด ๊ฐ๋ฅ
return true;
} else if(checked.count(s) > 0) { // ์ ์ ํ์ธํด๋ณธ ๋ฌธ์์ด์ด๋ฉด ์บ์ฑ๋ ๊ฒฐ๊ณผ ๋ฐํ
return checked[s];
} else {
for(int i = 1; i < s.size(); ++i) { // ๋ถ๋ถ ๋ฌธ์์ด ์์ฑ
string substr = s.substr(0, i);
// ๋ ๋ถ๋ถ ๋ฌธ์์ด [...i] [i...]์ด ๋ชจ๋ dict์ ์๋ค๋ฉด
if(words.count(substr) > 0 && dfs(s.substr(i))) {
return checked[s] = true; // true
}
}
return checked[s] = false; // ์๋๋ผ๋ฉด false
}
}
};
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(object):
def DP(self, s, wordDict, end):
s = s[end:]
if len(s) == 0:
return True
else:
for end in range(1, len(s)+1):
if s[:end] in wordDict and self.DP(s, wordDict, end):
return True
return False
def wordBreak(self, s, wordDict):
return self.DP(s, wordDict, 0)
35/47์์ ์๊ฐ์ด๊ณผ๊ฐ ๊ฑธ๋ฆฌ๋๋ฐ ๋ญ๊ฐ ๋ ๊ณ ์น๊ณ ์ถ์๋ฐ ์ฐ์ ๋ฆฌ๋ทฐ์ฌ๋ฆฝ๋๋ค..!
์ง์น ๋๋ก ์ง์ณ๋ฒ๋ ธ์ต๋๋ค....
๋ช
์ค๋ PR ๋ณด๋ ์ ๊ฐ ์ธ๋ฑ์ค ์ฒ๋ฆฌ๋ฅผ ๋๋ฌด ๋ง์ด ํด์ ์๊ฐ์ด๊ณผ๊ฐ ๋๊ฑธ์ง๋? ๋ผ๋ ์๊ฐ์ด ๋ค์์ต๋๋ค.๐๐ญ(์ฌ์ค ์ ๋ชจ๋ฅด๊ฒ ์ด์..)
์ด ๋ฌธ์ ๋ฅผ 20๋ถ๋ง์ ๋๋ฑํ์๋ค๋.. ์ ๋ ๊ฑฐ์ ํ์๊ฐ ๋ฐ์ ๋ถ์ก์๋ ๊ฒ ๊ฐ๋ค์..
์๊ณ ๋ง์ผ์
จ์ต๋๋ค!!!
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.
T, F๋ฅผ ์ ๋ฐ ์์ผ๋ก ์ ์ฅํด์ ํ์ธํ ์ ์๋ค๋... ์์ ์๋นกํ๋ฐ์...?!๐ฎ๐
Word Break - LeetCode |
๐ ๋ฌธ์ ๋งํฌ
LeetCode - WordBreak
โ๏ธ ์์๋ ์๊ฐ
20๋ถ ์์
โจ ์๋ ์ฝ๋ ๋ฐ ๋ฌธ์ ์ค๋ช
ํด๋น ๋ฌธ์ ๋ ์ฃผ์ด์ง ๋ฌธ์์ด s๋ฅผ ๋จ์ด ์ฌ์ (wordDict)์ ์๋ ๋จ์ด๋ค๋ก ์กฐํฉํ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ๋ฐํํ๋ฉด ๋ฉ๋๋ค.
๋ฌธ์์ด s์ ๊ฐ ์ธ๋ฑ์ค(ํ๋ผ๋ฏธํฐ i )๋ฅผ ์ํํ๋ฉฐ, ํด๋น ์ธ๋ฑ์ค์์ ๋๋ ์ ์๋ ๋จ์ด๋ค์ ๊ฒ์ฌํฉ๋๋ค.
private boolean dp(int i) {
๊ฐ ๋จ์ด๊ฐ ๋๋ ์ ์๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด, ๊ทธ ์ด์ ์ ๋ถ๋ถ ๋ฌธ์์ด๋ ์ ํจํ์ง ํ์ธํ์ฌ
if (i >= word.length() - 1 && dp(i - word.length())) {
dp ๋ฐฐ์ด์ ๊ฐฑ์ ํฉ๋๋ค.
์ต์ข ์ ์ผ๋ก dp[len(s)]์ ๊ฐ์ด True์ธ์ง ํ์ธํ์ฌ, ์ฃผ์ด์ง ๋ฌธ์์ด s๋ฅผ ๋จ์ด ์ฌ์ ์ ๋จ์ด๋ค๋ก ๊ตฌ์ฑํ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ๋ฐํํฉ๋๋ค.
โจ ์ฌ์ด ์ค๋ช (์ฐธ๊ณ )
์๋ ๊ทธ๋ฆผ์ dp[i]๊ฐ ์ฐธ์ด ๋๋ ์กฐ๊ฑด์ ๋ํ๋ ๋๋ค!
์์ฝํ์๋ฉด, s ๋ฌธ์์ด์ i ์์น์์ ๋๋๋ ๋ถ๋ถ ๋ฌธ์์ด์ด wordDict์ ์๋ ์ด๋ค ๋จ์ด์ ๊ฐ๊ณ ,
๊ทธ ๋จ์ด๊ฐ ์์๋๊ธฐ ์ ์ ๋ถ๋ถ ๋ฌธ์์ด๋ wordDict์ ์๋ ๋จ์ด๋ค๋ก ๋๋ ์ ์๋ค๋ฉด!
dp[i]๋ ์ฐธ์ด ๋๋ ๋ฐฉ์์ ๋๋ค.