-
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
37-pknujsp #149
37-pknujsp #149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
def solution(coin, cards): | ||
a = set(cards[:len(cards) // 3]) | ||
b = set() | ||
t = len(cards) + 1 | ||
r = 1 | ||
|
||
for i in range(len(cards) // 3 + 1, len(cards), 2): | ||
c1, c2 = cards[i - 1], cards[i] | ||
b.add(c1) | ||
b.add(c2) | ||
|
||
removed = False | ||
# ํ์ฌ ๊ฐ์ง๊ณ ์๋ ์นด๋ ๋ชฉ๋ก ์ค n + 1์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ ํ์ธ | ||
for x in list(a): | ||
if t - x in a: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. t - x๋ฅผ ๋ฐ๋ก ๋ณ์๋ก ๋ด์ ํ์ ์์ด ๊ตฌํ์ด ๊ฐ๋ฅํ๊ตฐ์ .. ์ด๋ ๊ฒ ๋ณ์ ํ๋๋ฅผ ๋ ์ค์ผ ์ ์๊ตฐ์.. ๐ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ต ์ ์ฝ๋ฉํธ์ ๋ด์ฉ๊ณผ ๊ฐ์ ๋ถ๋ถ์ผ๋ก ๋ณด์๋ฉด ๋ฉ๋๋ค |
||
a.remove(t - x) | ||
a.remove(x) | ||
removed = True | ||
break | ||
|
||
if removed: | ||
r += 1 | ||
continue | ||
|
||
# ์ฝ์ธ์ผ๋ก ๊ตํํด์ ์ป์ ์ ์๋ ์นด๋ ์ค์์ n + 1์ด ๋๋ ๊ฒฝ์ฐ๋ฅผ ์ฐพ์์ผ ํจ | ||
# ์ฝ์ธ์ด ์์ผ๋ฏ๋ก ์ข ๋ฃ | ||
if not coin: | ||
break | ||
|
||
# `ํ์ฌ ๊ฐ๊ณ ์๋ ์นด๋ + ์ป์ ์ ์๋ ์นด๋` = n + 1์ด ๋๋ ๊ฒฝ์ฐ๋ฅผ ํ์ธ | ||
for x in list(b): | ||
if t - x in a: | ||
a.remove(t - x) | ||
b.remove(x) | ||
removed = True | ||
coin -= 1 | ||
break | ||
# ๋ง์ง๋ง ๋ฐฉ๋ฒ์ผ๋ก, ์ค์ง ๊ตํ์ผ๋ก ์ป์ ์ ์๋ ์นด๋ ๋ชฉ๋ก ์ค์์ n + 1์ด ๋๋ ๊ฒฝ์ฐ๋ฅผ ํ์ธ | ||
if not removed and coin >= 2: | ||
for x in list(b): | ||
if t - x in b: | ||
b.remove(t - x) | ||
b.remove(x) | ||
removed = True | ||
coin -= 2 | ||
break | ||
|
||
# n + 1์ ์ด๋ค ๊ฒฝ์ฐ์๋ ๋ชป ๋ง๋ค๋ฉด ์ข ๋ฃ | ||
if not removed: | ||
break | ||
r += 1 | ||
|
||
return r |
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.
์ค์ฑ๋ ์๋ ์ฝ๋๋ณด๋ฉด์ ์ฝ๋ ์์ฑํ๋ค๊ฐ ๊ถ๊ธํ๊ฒ ์๊ฒจ์ ์ฝ๋ฉํธ ๋จ๊น๋๋น
set์ด ์๋๋ผ list๋ก ๊ตฌํํด๋ ํต๊ณผ๋ ๋๋๋ฐ ์งํฉ์ผ๋ก ๊ตฌํํ์ ์ด์ ๊ฐ ์๊ฐ ๋ณต์ก๋ ๋๋ฌธ์ผ๊น์?
for ~ in์์ list์ ๊ฒฝ์ฐ์๋ ์๊ฐ๋ณต์ก๋๊ฐ O(n), set์ ๊ฒฝ์ฐ์๋ O(1)์ด์ฌ์ ์งํฉ์ผ๋ก ๊ตฌํํ์ ๊ฑด์ง ๊ถ๊ธํฉ๋๋ค!
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.
๋ค๋ค ๋ง์ํ์ ๊ทธ๋๋ก ์ ๋๋ค
์นด๋ ๋ชฉ๋ก์์
n + 1
์ด ๋๋ ์กฐํฉ์ ํ์ธํด๋ณผ๋๋ฆฌ์คํธ๋ก ํ๋ค๋ฉด ๋ชจ๋ ์นด๋๋ฅผ ๋ ์์ผ๋ก ๋ง๋ค์ด์ ์ผ์ผํ ๋ค ๋น๊ตํด์ผ ํ๋๋ฐ
์งํฉ์ด๋ผ๋ฉด ์์ผ๋ก ๋น๊ตํ ํ์์์ด ์นด๋ ํ๋์ฉ๋ง ํ์ธํด๋ณด๋ฉด์
n + 1
์ด ๊ฐ๋ฅํ ์นด๋์ ์กด์ฌ์ฌ๋ถ๋ฅผ ํ๋ฒ์ ํ์ธ๊ฐ๋ฅํ๋๊น์.