Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

6-mjj111 #196

wants to merge 1 commit into from

Conversation

mjj111
Copy link
Collaborator

@mjj111 mjj111 commented Jul 3, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

LeetCode - WordBreak

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

20๋ถ„ ์†Œ์š”

โœจ ์ˆ˜๋„ ์ฝ”๋“œ ๋ฐ ๋ฌธ์ œ ์„ค๋ช…

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s๋ฅผ ๋‹จ์–ด ์‚ฌ์ „(wordDict)์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ์กฐํ•ฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    private boolean dp(int i) {
        if (i < 0) return true;

        if (memo[i] == -1) {
            for (String word: wordDict) {
                if (i >= word.length() - 1 && dp(i - word.length())) {
                    if (s.substring(i - word.length() + 1, i + 1).equals(word)) {
                        memo[i] = 1;
                        break;
                    }
                }
            }
        }

        if (memo[i] == -1) {
            memo[i] = 0;
        }

        return memo[i] == 1;
    }

๋ฌธ์ž์—ด s์˜ ๊ฐ ์ธ๋ฑ์Šค(ํŒŒ๋ผ๋ฏธํ„ฐ i )๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, ํ•ด๋‹น ์ธ๋ฑ์Šค์—์„œ ๋๋‚  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
private boolean dp(int i) {

๊ฐ ๋‹จ์–ด๊ฐ€ ๋๋‚  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด, ๊ทธ ์ด์ „์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด๋„ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•˜์—ฌ
if (i >= word.length() - 1 && dp(i - word.length())) {

dp ๋ฐฐ์—ด์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

  if (s.substring(i - word.length() + 1, i + 1).equals(word)) {
      memo[i] = 1;
      break;
  }

์ตœ์ข…์ ์œผ๋กœ dp[len(s)]์˜ ๊ฐ’์ด True์ธ์ง€ ํ™•์ธํ•˜์—ฌ, ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s๋ฅผ ๋‹จ์–ด ์‚ฌ์ „์˜ ๋‹จ์–ด๋“ค๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

โœจ ์‰ฌ์šด ์„ค๋ช…(์ฐธ๊ณ )

์•„๋ž˜ ๊ทธ๋ฆผ์€ dp[i]๊ฐ€ ์ฐธ์ด ๋˜๋Š” ์กฐ๊ฑด์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค!
์š”์•ฝํ•˜์ž๋ฉด, s ๋ฌธ์ž์—ด์˜ i ์œ„์น˜์—์„œ ๋๋‚˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด wordDict์— ์žˆ๋Š” ์–ด๋–ค ๋‹จ์–ด์™€ ๊ฐ™๊ณ ,
๊ทธ ๋‹จ์–ด๊ฐ€ ์‹œ์ž‘๋˜๊ธฐ ์ „์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด๋„ wordDict์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค๋ฉด!
dp[i]๋Š” ์ฐธ์ด ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

image

image

image

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a 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
        }
    }
};

Copy link
Member

@xxubin04 xxubin04 left a 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๋ถ„๋งŒ์— ๋š๋”ฑํ•˜์‹œ๋‹ค๋‹ˆ.. ์ €๋Š” ๊ฑฐ์˜ ํ•œ์‹œ๊ฐ„ ๋ฐ˜์€ ๋ถ™์žก์•˜๋˜ ๊ฒƒ ๊ฐ™๋„ค์š”..
์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!!!

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

Successfully merging this pull request may close these issues.

None yet

3 participants