-
Notifications
You must be signed in to change notification settings - Fork 1
9-dohyeondol1 #33
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
9-dohyeondol1 #33
Conversation
|
์ผ๋ถ๋ฌ ํ์๋์ ๋ฌธ์ ์ค๋ช
์ ๋ณด์ง ์๊ณ ๋ฐ๋ก ๋ฌธ์ ๋ฅผ ํ์ด๋ดค๋๋ฐ, ๋น์ทํ๊ฒ ํผ ๊ฒ ๊ฐ์์. ์ ๋ ํ์ด์ฌ์ ํ์ด์ฌ
def solution(n, lost, reserve):
lost_set = set(lost) - set(reserve)
reserve_set = set(reserve) - set(lost)
for i in sorted(reserve_set):
if i-1 in lost_set:
lost_set.remove(i-1)
elif i+1 in lost_set:
lost_set.remove(i+1)
return n-len(lost_set)ํ๋ก๊ทธ๋๋จธ์ค๋ ์ฒ์์ด๋ค์ ใ ใ ๋์ Pypy3์ด ์๋๊ฒ ์์ฝ๋ค์ |
|
@froglike6 |
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.
ํจ์๋ฅผ ๊ตฌํํ๋ ๋ฌธ์ ๋ ์ฒ์ ๋ด์ ์ ์ ํ๋ค์! ์์ ์ํ ๋ณด๋ ๊ธฐ๋ถ์ด๋ค์ฉ ์ํ ๊ณต๋ถํ ๋ ํ๋ฉด ๋์ ๋ ๊ฑฐ ๊ฐ์์
for (reserve ํฌ๊ธฐ๋งํผ ๋ฐ๋ณต) { ์ ์ฌ๋์ด lost์ ์๋์ง ํ์ธ โ ์์ผ๋ฉด ์ ๊ฑฐ ์๋๋ฉด ๋ท ์ฌ๋์ด lost์ ์๋์ง ํ์ธ โ ์์ผ๋ฉด ์ ๊ฑฐ } return n - ๋จ์ lost ํฌ๊ธฐ
์ ๋ ์ฐ์ ์๋ ์ฝ๋๋ฅผ ์ ๋ฐ ์์ผ๋ก ์ฐ๊ณ ํด๋ดค๋๋ฐ ํ์๋์ด ๋ง์ํ์
๋ ํ๋ ์ฃผ์ํด์ผ ํ ์ฌํญ์ ์ฌ๋ฒ์ ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์์ด ์ฒด์ก๋ณต์ ๋๋ ๋นํ ๊ฒฝ์ฐ์ ๋๋ค.
์ด ๊ฒฝ์ฐ ํด๋น ํ์์ ๊ทธ๋ฅ ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์๊ณผ ๋๊ฐ๊ธฐ ๋๋ฌธ์, ๊ฒ์ฌ์์ ์ ์ธํด์ผ ํฉ๋๋ค
์ด ๋ถ๋ถ์ ๋ชป์ง์ผฐ๋ค์..
๊ตฌํ์ ํ๋ค๊ฐ ๋ชปํ๊ฒ ์ด์ ์งํผํฐํํ
๋งก๊ฒผ์ด๋ค ใ
ใ
...;;
C
#include <stdio.h>
#include <stdbool.h>
int solution(int n, int lost[], int lostSize, int reserve[], int reserveSize) {
bool isLost[31] = { false }; // ํ์ ๋ฒํธ๋ 1~30
bool isReserve[31] = { false };
// ์ด๊ธฐ ์ธํ
for (int i = 0; i < lostSize; i++)
isLost[lost[i]] = true;
for (int i = 0; i < reserveSize; i++)
isReserve[reserve[i]] = true;
// ์ฌ๋ถ๋ ์๊ณ ์์ด๋ฒ๋ฆฐ ์ฌ๋ ์ฒ๋ฆฌ (์๊ธฐ ์์ ์ด ํด๊ฒฐ)
for (int i = 1; i <= n; i++) {
if (isLost[i] && isReserve[i]) {
isLost[i] = false;
isReserve[i] = false;
}
}
// ์ฌ๋ถ์ ์ ๋๋ ๋ค ์ฌ๋์๊ฒ ๋น๋ ค์ฃผ๊ธฐ
for (int i = 1; i <= n; i++) {
if (isReserve[i]) {
if (i > 1 && isLost[i - 1]) {
isLost[i - 1] = false;
} else if (i < n && isLost[i + 1]) {
isLost[i + 1] = false;
}
}
}
// ๋จ์ lost ์ ์ธ๊ธฐ
int lostCount = 0;
for (int i = 1; i <= n; i++) {
if (isLost[i]) lostCount++;
}
return n - lostCount;
}
hadongun
left a comment
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.
๋งค์ฐ ๋จ์ํ๊ฒ ์๊ฐํ๋๋ฐ ์๊ฐ๋ณด๋ค ๊ณ ๋ คํ ์กฐ๊ฑด์ด ๋ง์๋ค์ฅ.. ์ฌ๋ฒ ๊ฐ์ ธ์จ ํ์์ด ๋๋ ๋นํ๋ ๋ถ๋ถ์ ๊ฐ๊ณผํ๊ณ ์์ด์ ์ฝ์ง ์์์ต๋๋ต
ํํ
def solution(n, lost, reserve):
isborrow = [False]*n
max_learn = n
fixed_lost = []
fixed_reserve = []
for i in range(len(lost)):
if lost[i] not in reserve:
fixed_lost.append(lost[i])
for j in range(len(reserve)):
if reserve[j] not in lost:
fixed_reserve.append(reserve[j])
max_learn -= len(fixed_lost)
for k in range(len(fixed_reserve)):
isborrow[fixed_reserve[k] - 1] = True
for i in range(len(fixed_lost)):
idx = fixed_lost[i] - 1
if idx - 1 >= 0 and isborrow[idx - 1]:
max_learn += 1
isborrow[idx - 1] = False
continue
if idx + 1 < n and isborrow[idx + 1]:
max_learn += 1
isborrow[idx + 1] = False
return max_learn
๊ตฌํ์ ๊ณ์ ์ฐ์ตํ๋ฉด ๋์ฃ ! ์ค์ํ ๊ฑด ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ์ด๋ค ์ ๊ทผ์ผ๋ก ํด๊ฒฐํด์ผ ํ๋์ง๋ฅผ ๋น ๋ฅด๊ฒ ํ์
ํ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
|
๋์ค๋๋ ์ค์๋์ฒ๋ผ ์ฌ๋ฒ์ ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์์ด ์ฒด์ก๋ณต์ ๋๋ ๋นํ ๊ฒฝ์ฐ๋ฅผ ์ฝ๊ฒ ํด๊ฒฐํ์ง ๋ชปํ ๊ฒ ๊ฐ๋ค์.. |
๐ ๋ฌธ์ ๋งํฌ
์ ๋ง ์ฌ์ฐ๋๊น ์๋ ์๋์ฝ๋๋ฅผ ๋ณด์ง ์๊ณ ์ง์ ํ์ด์ ์ฝ๋๋ฅผ ๋ฆฌ๋ทฐ์ ๊ณต์ ํด์ฃผ์๋ฉด ์ข๊ฒ ์ต๋๋ค..!
์ ๊ฐ ์๊ฐ์ง ๋ชปํ ์๋ก์ด ํ์ด๋ฅผ ๋ง๋ ์ ์์ง ์์๊น ํ๋ ๊ธฐ๋๋ฅผ ์๊ณ ...
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ
์ฌ์ด ๋ฌธ์ ์ธ๋ฐ ๋๋ฌด ์๊ฐ์ ์ค๋ ์ผ์ต๋๋ค;;
โจ ์๋ ์ฝ๋
ํ์ด ์์ฒด๋ ์ด๋ ค์ธ ๊ฒ ์์ต๋๋ค.
์ ๋ต(์ฒด์ก์์ ์ ๋ค์ ์ ์๋ ํ์์ ์ต๋๊ฐ)์ (์ ์ฒด ์ธ์ - ์ฒด์ก๋ณต์ด ์๋ ์ธ์)์ผ๋ก ์ธํ ํด๋๊ณ ,
์ฒด์ก๋ณต์ด ์๋ ์น๊ตฌ์ ์, ๋ค ๋ฒํธ์ ์ฌ๋ฒ์ ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ์น๊ตฌ๊ฐ ์๋ค๋ฉด ์ฒด์ก๋ณต์ ๋น๋ฆฌ๊ณ ์ ๋ต์ ์ธ์ ํ๋ช ์ ์ถ๊ฐํฉ๋๋ค.
์ด ๋ ๊ฒ์ฌ๋ ์์ ์ฐ์ ์ํด์ ์งํํฉ๋๋ค.
์ฒด์ก๋ณต์ ๋น๋ฆด ์ ์๋ ํ์์ ์ต๋๊ฐ์ ๊ตฌํ๋ ค๋ฉด,
๋ค์์ผ๋ก ์ฒด์ก๋ณต์ ๋น๋ฆด ํ์์ ์ ๋ฒํธ ํ์์ด ์ฒด์ก๋ณต์ ๋น๋ ค์ค ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํด์ผ ํ๊ธฐ ๋๋ฌธ์ด์ฃ .
๋ ํ๋ ์ฃผ์ํด์ผ ํ ์ฌํญ์ ์ฌ๋ฒ์ ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์์ด ์ฒด์ก๋ณต์ ๋๋ ๋นํ ๊ฒฝ์ฐ์ ๋๋ค.
์ด ๊ฒฝ์ฐ ํด๋น ํ์์ ๊ทธ๋ฅ ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์๊ณผ ๋๊ฐ๊ธฐ ๋๋ฌธ์, ๊ฒ์ฌ์์ ์ ์ธํด์ผ ํฉ๋๋ค.
์ ๋ ์ฃผ์ด์ง ๋ฐฐ์ด์ ๊ทธ๋๋ก ์ฌ์ฉํ๊ณ ์
std::vector.erase()๋ฅผ ์ฌ์ฉํด lost์ reserve์์ ๋ฒํธ๊ฐ ๊ฐ์(=์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์์ด ์ฒด์ก๋ณต์ ๋๋ ๋นํ ๊ฒฝ์ฐ)๊ฐ๋ค์ ์ ๊ฑฐํ์์ต๋๋ค.๊ทธ ํ lost ๋ฐฐ์ด์ ๊ฐ(ํ์ ๋ฒํธ)์ -1, +1 ์์๋๋ก ์ฒด์ก๋ณต์ ๋น๋ ค์ค ์ ์๋ ํ์์ด ์๋ ์ง ๊ฒ์ฌํ๊ณ
์๋ค๋ฉด ์ ๋ต์ +1๋ช ํ๋ ๋ฐ๋ณต๋ฌธ์ ๋๋ ค ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์์ต๋๋ค.
์๋ ์ฝ๋
์ ๊ฐ ์๋ํ๋ ๋ฐฉ๋ฒ์
vector.erase()๋ฅผ ์ฌ์ฉํ์ฌ ์ง์๋๊ฐ๋ฉด์ ์ ๋ต์ ++ํ๊ธฐ์ด๋ ๊ฒ ๋๊ฐ์ง ์ ๋๋ค.
๋ค๋ฅธ ์ฌ๋๋ค์ ํ์ด๋ฅผ ๋ณด๋ ์ข ๋ ๋ค์ํ ํ์ด๊ฐ ์๋๋ผ๊ตฌ์?
์ฌ๋ฌ๋ถ๋ค์ ์ด๋ป๊ฒ ํธ์ จ๋์ง ๊ถ๊ธํ๋ค์.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ