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-kjs254 #22

Merged
merged 2 commits into from
Mar 5, 2024
Merged

6-kjs254 #22

merged 2 commits into from
Mar 5, 2024

Conversation

kjs254
Copy link
Member

@kjs254 kjs254 commented Feb 27, 2024

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

๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ

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

1h

์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณด๋‹จ ๊ตฌํ˜„ ์ž์ฒด์— ์‹œ๊ฐ„์„ ๋งŽ์ด ๋บ๊ฒผ๋„ค์š”..ใ…Ž

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œํ•ด์„

๋ฌธ์ œ๊ฐ€ ๋˜๊ฒŒ ๊ธธ์ง€๋งŒ ์š”์•ฝํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๊ด„ํ˜ธ๋กœ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด s๋ฅผ x๋ฒˆ ์ˆœํ™˜ ์‹œํ”„ํŠธํ•œ๋‹ค.

  2. x๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘ ๊ด„ํ˜ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    • (x = s์˜ ๊ธธ์ด - 1)

๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ํ•œ๋ฒˆ์— ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

image

์œ„ ์˜ˆ์‹œ์—์„  0, 2, 4๋ฒˆ ์‹œํ”„ํŠธํ•œ ๋ฐฐ์—ด์ด ๊ด„ํ˜ธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฏ€๋กœ 3์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ์„ ๊ฐ๊ฐ ๊ตฌํ˜„ํ•˜์—ฌ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.

1) ๊ด„ํ˜ธ ์กฐ๊ฑด ํŒ๋… ํ•จ์ˆ˜

  • ๊ด„ํ˜ธ ๋ฌธ์ž์—ด bracket์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š” func()์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

  • ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

2) ๋ฌธ์ž์—ด ์‹œํ”„ํŠธ(ํšŒ์ „)

  • ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.



2. ์ˆ˜๋„์ฝ”๋“œ

๊ด„ํ˜ธ ์กฐ๊ฑด ํŒ๋… ํ•จ์ˆ˜

  1. ์—ฌ๋Š” ๊ด„ํ˜ธ : ์Šคํƒ์— ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  2. ๋‹ซ๋Š” ๊ด„ํ˜ธ : ์Šคํƒ๊ณผ ๋น„๊ตํ•˜์—ฌ ์ง์ด๋ผ๋ฉด ์Šคํƒ์„ pop()ํ•ฉ๋‹ˆ๋‹ค.

    • ์ง์ด ์•„๋‹ˆ๋ผ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋งˆ์ง€๋ง‰์— ์Šคํƒ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

pseudocode๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

def(bracket):
    stk ์„ ์–ธ
    for b in bracket:
        if b==์—ฌ๋Š” ๊ด„ํ˜ธ:
            stk.append(b)
        
        if b==๋‹ซ๋Š” ๊ด„ํ˜ธ:
            if stk is empty:
                retrun False

            if stk[-1], b๊ฐ€ ์ง์ด๋ผ๋ฉด:
                stk.pop()
            else:
                return False

    if stk is empty:
        return True

์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

def func(bracket):
    stk = []
    for b in bracket:
        if b in ["[","{","("]:
            stk.append(b)
        elif b in ["]","}",")"]:
            if not stk:
                return False
            if stk[-1]=="[" and b=="]":
                stk.pop()
            elif stk[-1]=="{" and b=="}":
                stk.pop()
            elif stk[-1]=="(" and b==")":
                stk.pop()
            else:
                return False
    return not stk

์ƒ๊ฐ๋ณด๋‹ค ์ง€์ €๋ถ„ํ•ด์„œ ๋†€๋ž์Šต๋‹ˆ๋‹ค..

์‹œํ”„ํŠธ(ํšŒ์ „)๋Š” ์ธ๋ฑ์‹ฑ ๋ฐ ์Šฌ๋ผ์ด์‹ฑ์„ ํ†ตํ•ด ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

l = len(s)
    for i in range(l):
        x = s[i:]+s[:i] 

0๋ฒˆ ๋ถ€ํ„ฐ l-1๋ฒˆ ์‹œํŠธํ”„ ๋œ ๋ฌธ์ž์—ด์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ „์ฒด์ฝ”๋“œ

def func(bracket):
    stk = []
    for b in bracket:
        if b in ["[","{","("]:
            stk.append(b)
        elif b in ["]","}",")"]:
            if not stk:
                return False
            if stk[-1]=="[" and b=="]":
                stk.pop()
            elif stk[-1]=="{" and b=="}":
                stk.pop()
            elif stk[-1]=="(" and b==")":
                stk.pop()
            else:
                return False
    return not stk

def solution(s):
    answer = 0
    l = len(s)
    for i in range(l):
        x = s[i:]+s[:i]
        if func(x):
            answer +=1
        
    return answer

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์Šคํƒ์„ ๋ฐฐ์šธ ๋•Œ ๊ด„ํ˜ธ ํŒ๋… ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐœ๋…์„ ์žก์•˜๋˜๊ฒŒ ๊ธฐ์–ต๋‚˜์„œ ํ•œ๋ฒˆ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ƒ๊ฐ๋งŒํผ ๊ฐ„๋‹จํžˆ ๊ตฌํ˜„ํ•˜์ง„ ๋ชปํ–ˆ๊ณ  ์‹œ๊ฐ„๋„ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š”๊ฒŒ ๋ณด์—ฌ์„œ GPTํ•œํ…Œ ๋ชจ๋ฒ”๋‹ต์•ˆ์„ ๋ณด์—ฌ๋‹ฌ๋ผ ํ•˜๋‹ˆ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ง๊ด€์ ์œผ๋กœ ๊ตฌํ˜„์„ ์ž˜ ํ•˜๋”๋ผ๊ตฌ์š”.

์ œ๊ฐ€ ๊ธฐ๋ณธ ๊ฐœ๋…์ด ์•ฝํ•ด์„œ ์Šคํƒ์˜ ๊ฐœ๋…์„ ์Šต๋“ํ•˜๊ธฐ์—” ์•„์ฃผ ์ข‹์€ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ›‘ commitํ•œ ๊ฒƒ์„ ๋ณด๋Š”๋ฐ ๋ฌด์–ธ๊ฐ€ ์ž˜๋ชป๋œ ๊ฒƒ ๊ฐ™๋„ค์š”.
๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ.py ํŒŒ์ผ์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋‚˜์˜ต๋‹ˆ๋‹ค.

โœ”๏ธ ๊ทธ๊ฒƒ๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ, PR์— ์ž‘์„ฑํ•ด์ฃผ์‹  ๋‚ด์šฉ์€ ์ •๋ง ์ž˜ ์งœ์—ฌ์ง„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
ํŠนํžˆ, x = s[i:]+s[:i] ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๊ฒŒ ์ข‹์•˜๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. c++์—์„œ๋Š” ๊ทธ๊ฒŒ ์•ˆ๋˜์„œ,,,Python์ด ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ๊ฒƒ์€ ์ •๋ง ์ข‹์€ ๊ฒƒ ๊ฐ™๋„ค์š”

โœ”๏ธ ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ์งง๊ฒŒ ์ž˜ ํ’€์ดํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

@kjs254
Copy link
Member Author

kjs254 commented Mar 1, 2024

๐Ÿ›‘ commitํ•œ ๊ฒƒ์„ ๋ณด๋Š”๋ฐ ๋ฌด์–ธ๊ฐ€ ์ž˜๋ชป๋œ ๊ฒƒ ๊ฐ™๋„ค์š”. ๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ.py ํŒŒ์ผ์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋‚˜์˜ต๋‹ˆ๋‹ค.

โœ”๏ธ ๊ทธ๊ฒƒ๊ณผ๋Š” ๋ณ„๊ฐœ๋กœ, PR์— ์ž‘์„ฑํ•ด์ฃผ์‹  ๋‚ด์šฉ์€ ์ •๋ง ์ž˜ ์งœ์—ฌ์ง„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํŠนํžˆ, x = s[i:]+s[:i] ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ฑ์‹ฑ๊ณผ ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๊ฒŒ ์ข‹์•˜๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. c++์—์„œ๋Š” ๊ทธ๊ฒŒ ์•ˆ๋˜์„œ,,,Python์ด ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ๊ฒƒ์€ ์ •๋ง ์ข‹์€ ๊ฒƒ ๊ฐ™๋„ค์š”

โœ”๏ธ ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ์งง๊ฒŒ ์ž˜ ํ’€์ดํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ.py ๋ฅผ ๋น„์›Œ๋‘” ์ฑ„๋กœ ์˜ฌ๋ ค๋ฒ„๋ ธ๋„ค์š” ๋ฐ”๋กœ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์˜ ์žฅ์ ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์ฃผ๋ฌด๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์‹œ๊ฐ„๋ณต์žก๋„์—์„œ ์ž์ฃผ ๊ฑธ๋ฆฌ๋Š” ์ ์ด ๋ฌธ์ œ๊ฒ ๋„ค์š”.

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒ๊ฐ ๊ณผ์ •์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜์…”์„œ ์ดํ•ดํ•˜๊ธฐ๋„ ์ •๋ง ์ข‹์•˜์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

Comment on lines +6 to +16
elif b in ["]","}",")"]:
if not stk:
return False
if stk[-1]=="[" and b=="]":
stk.pop()
elif stk[-1]=="{" and b=="}":
stk.pop()
elif stk[-1]=="(" and b==")":
stk.pop()
else:
return False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ถ€๋ถ„์€ ๊ด„ํ˜ธ๊ฐ€ ์ง์ด ์ง€์–ด์ง€๋Š” ๊ณผ์ •์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์ด๋ผ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฑด ๋‹ค ๋˜‘๊ฐ™๋„ค์š”.
์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ์ด๋Ÿฐ ๊ณผ์ •์ด ๋‚˜์˜ค๋ฉด ํ•œ๊บผ๋ฒˆ์— ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค.
๋‹ค๋งŒ ์ด๋Ÿฐ ๋ฐฉ์‹๋„ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ ธ์„œ ์ฝ๊ธฐ ํŽธํ•˜๋„ค์š”.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ถ€๋ถ„์€ ๊ด„ํ˜ธ๊ฐ€ ์ง์ด ์ง€์–ด์ง€๋Š” ๊ณผ์ •์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์ด๋ผ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฑด ๋‹ค ๋˜‘๊ฐ™๋„ค์š”. ์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ์ด๋Ÿฐ ๊ณผ์ •์ด ๋‚˜์˜ค๋ฉด ํ•œ๊บผ๋ฒˆ์— ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ด๋Ÿฐ ๋ฐฉ์‹๋„ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ ธ์„œ ์ฝ๊ธฐ ํŽธํ•˜๋„ค์š”.

์ €๋„ ๋ฌถ์–ด์„œ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ์ง€ ๊ฐ์ด ์ž˜ ์•ˆ์žกํ˜€์„œ ๊ทธ๋ƒฅ ์ผ์ผ์ด ์ ์—ˆ์Šต๋‹ˆ๋‹ค...

๋‚˜์ค‘์— ์•Œ์•„๋ณด๋‹ˆ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ฌถ์–ด์„œ ์ง์„ ์ง€์œผ๋ฉด ๊น”๋”ํ•˜๊ฒŒ ๊ตฌํ˜„๋˜๋”๋ผ๊ตฌ์š”

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YIM2UL2ET ๋ง์”€๋Œ€๋กœ ๋‹จ์ˆœํ•˜๊ฒŒ and๋กœ ์„ธ ์กฐ๊ฑด์„ ๋ฌถ์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

bracket ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด ์ด์ œ stack์ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋– ์˜ค๋ฅด๋„ค์š”.

์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š

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.

4 participants