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

29-9kyo-hwang #114

Merged
merged 3 commits into from
Feb 26, 2024
Merged

29-9kyo-hwang #114

merged 3 commits into from
Feb 26, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

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

2022 KAKAO BLIND RECRUITMENT ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ

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

20๋ถ„ + ํ•ด์„ค๋ณด๊ณ  1์‹œ๊ฐ„ 40๋ถ„

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

0. ์นด์นด์˜ค๋Š” ์‹ ์ธ๊ฐ€?

์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋”ฑ ๋ดค์„ ๋• "? ๊ทธ๋ƒฅ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ฐ’ ๋ฐ”๊พธ๋ฉด ๋๋‚˜๋Š” ๊ฑฐ ์•„๋‹Œ๊ฐ€?" ํ–ˆ์ง€๋งŒ ๋‚œ์ด๋„๊ฐ€ Lv.3์ธ ๊ฒƒ์„ ๋ณด๊ณ  ๋ญ”๊ฐ€ ์ž˜๋ชป๋จ์„ ๊นจ๋‹ฌ์•˜๋‹ค.

์ผ๋‹จ ๊ทธ๋ƒฅ ๋ฌด์‹ํ•˜๊ฒŒ ์ˆœํšŒํ•˜๋Š” ์ฝ”๋“œ๋ผ๋„ ๋‚ด๋ณด์ž ํ•ด์„œ ์ œ์ถœํ•ด๋ดค์ง€๋งŒ ์—ญ์‹œ ํšจ์œจ์„ฑ์€ ์‹น ๋‹ค ๋‚ ๋ ค๋จน์—ˆ๋‹ค. ์•”๋งŒ ์งฑ๊ตฌ๋ฅผ ๊ตด๋ ค๋ด๋„ "๋‚ด๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ์ง€์‹์œผ๋กœ ์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‚˜?"์— ๋Œ€ํ•œ ๋‹ต์ด ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค.

"์ด๊ฑด ๋‚ด๊ฐ€ ๋ชจ๋ฅด๋Š” ์˜์—ญ์ธ๊ฐ€...?" ์‹ถ์–ด์„œ ๊ถ๊ธˆ์ฆ์„ ์ฐธ์ง€ ๋ชปํ•˜๊ณ  ์นด์นด์˜ค ๊ณต์‹ ํ•ด์„ค๊ธ€์„ ๋ดค๋Š”๋ฐ, ์—ญ์‹œ ์ „ํ˜€ ์ƒ์ƒ๋„ ๋ชปํ•œ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.
image
ํšจ์œจ์„ฑ ํ†ต๊ณผํ•œ ๋น„์œจ์ด 1%๋Œ€์ธ ๊ฑธ ๋ณด๋‹ˆ, ๋‚˜๋งŒ ๊ทธ๋Ÿฐ ๊ฑด ์•„๋‹ˆ๊ตฌ๋‚˜ ์‹ถ์–ด์„œ ์•ˆ๋„์˜ ํ•œ์ˆจ์„ ์‰ฌ์—ˆ๋‹ค.

1. ๋ฌธ์ œ ํŒŒ์•…

๊ฑด๋ฌผ์˜ ๋‚ด๊ตฌ๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด board์™€ ์ ์˜ ๊ณต๊ฒฉ ํ˜น์€ ์•„๊ตฐ์˜ ํšŒ๋ณต ์Šคํ‚ฌ์„ ๋‚˜ํƒ€๋‚ด๋Š” 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด skill์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค.
์ ์˜ ๊ณต๊ฒฉ ํ˜น์€ ์•„๊ตฐ์˜ ํšŒ๋ณต ์Šคํ‚ฌ์ด ๋ชจ๋‘ ๋๋‚œ ๋’ค ํŒŒ๊ดด๋˜์ง€ ์•Š์€ ๊ฑด๋ฌผ์˜ ๊ฐœ์ˆ˜๋ฅผ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์ด ํฌ๊ธฐ๊ฐ€ 4 x 5์ธ ๋งต์— ๋‚ด๊ตฌ๋„๊ฐ€ 5์ธ ๊ฑด๋ฌผ๋“ค์ด ์žˆ๋Š” ์ƒํƒœ๋ฅผ ๊ฐ€์ •ํ•ด๋ณด์ž.
image

์ ์ด ๋งต์˜ (0,0)๋ถ€ํ„ฐ (3,4)๊นŒ์ง€ ๊ณต๊ฒฉํ•˜์—ฌ 4๋งŒํผ ๊ฑด๋ฌผ์˜ ๋‚ด๊ตฌ๋„๋ฅผ ๋‚ฎ์ท„๋‹ค. ๋งต์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™”ํ•œ๋‹ค.
image

์ ์ด ๋งต์˜ (2,0)๋ถ€ํ„ฐ (2,3)๊นŒ์ง€ ๊ณต๊ฒฉํ•˜์—ฌ 2๋งŒํผ ๊ฑด๋ฌผ์˜ ๋‚ด๊ตฌ๋„๋ฅผ ๋‚ฎ์ถ”๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด 4๊ฐœ์˜ ๊ฑด๋ฌผ์ด ํŒŒ๊ดด๋œ๋‹ค.
image

์•„๊ตฐ์ด ๋งต์˜ (1,0)๋ถ€ํ„ฐ (3,1)๊นŒ์ง€ ํšŒ๋ณตํ•˜์—ฌ 2๋งŒํผ ๊ฑด๋ฌผ์˜ ๋‚ด๊ตฌ๋„๋ฅผ ๋†’์ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด 2๊ฐœ์˜ ๊ฑด๋ฌผ์ด ํŒŒ๊ดด๋˜์—ˆ๋‹ค๊ฐ€ ๋ณต๊ตฌ๋˜๊ณ  2๊ฐœ์˜ ๊ฑด๋ฌผ๋งŒ ํŒŒ๊ดด๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋กœ ๋ณ€ํ•œ๋‹ค.
image

์ ์ด ๋งต์˜ (0,1)๋ถ€ํ„ฐ (3,3)๊นŒ์ง€ ๊ณต๊ฒฉํ•˜์—ฌ 1๋งŒํผ ๊ฑด๋ฌผ์˜ ๋‚ด๊ตฌ๋„๋ฅผ ๋‚ฎ์ถ”๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด 8๊ฐœ์˜ ๊ฑด๋ฌผ์ด ๋” ํŒŒ๊ดด๋˜์–ด ์ด 10๊ฐœ์˜ ๊ฑด๋ฌผ์ด ํŒŒ๊ดด๋œ ์ƒํƒœ๊ฐ€ ๋œ๋‹ค. ์ด ๋•Œ ๋‚ด๊ตฌ๋„๊ฐ€ 0 ์ดํ•˜๊ฐ€ ๋œ ์ด๋ฏธ ํŒŒ๊ดด๋œ ๊ฑด๋ฌผ๋„, ๊ณต๊ฒฉ์„ ๋ฐ›์œผ๋ฉด ๊ณ„์†ํ•ด์„œ ๋‚ด๊ตฌ๋„๊ฐ€ ํ•˜๋ฝํ•œ๋‹ค.
image

์ตœ์ข…์ ์œผ๋กœ ์ด 10๊ฐœ์˜ ๊ฑด๋ฌผ์ด ํŒŒ๊ดด๋˜์ง€ ์•Š์•˜๋‹ค.

2. ํ’€์ด

๊ฒฐ๊ตญ 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ตฌ๊ฐ„์˜ ๋ณ€ํ™”๋ฅผ ์–ด๋–ป๊ฒŒ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ง€๊ฐ€ ๊ด€๊ฑด์ด๋‹ค. ์œ„์—์„œ ์–˜๊ธฐํ•œ ๊ฒƒ์ฒ˜๋Ÿผ brute-force๋กœ ํ’€ ๊ฒฝ์šฐ ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋ชจ๋‘ ๋งž์ถœ ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ $O(N * M * K)$ (K: ์Šคํ‚ฌ ๊ฐœ์ˆ˜)๊ฐ€ ๋˜์–ด ํšจ์œจ์„ฑ ํ…Œ์ผ€์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด์— ์•ž์„œ, ์šฐ์„ ์€ 1์ฐจ์› ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๊ตฌ๊ฐ„ ๋ณ€ํ™”๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด๊ฒ ๋‹ค.
[1, 2, 4, 8, 9] ๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, 0๋ฒˆ ์›์†Œ๋ถ€ํ„ฐ 3๋ฒˆ ์›์†Œ๊นŒ์ง€ 2๋งŒํผ ๋นผ์„œ [-1, 0, 2, 6, 9]๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค. ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ๋ฐฉ๋ฒ•์€ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ, ์ด๋Š” $O(M)$ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค.

$O(M)$ ์„ $O(1)$ ๋กœ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์€ ๋ˆ„์  ํ•ฉ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • ์œ„ ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ, 0๋ฒˆ ์›์†Œ๋ถ€ํ„ฐ 3๋ฒˆ ์›์†Œ๊นŒ์ง€ 2์”ฉ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ [-2, 0, 0, 0, 2]๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ๋ˆ„์ ํ•ฉ ์‹œํ‚ค๋ฉด [-2, -2, -2, -2, 0]์ด ๋œ๋‹ค.
  • ๋ˆ„์ ํ•ฉ์‹œํ‚จ ๋ฆฌ์ŠคํŠธ๋ฅผ ์›๋ณธ ๋ฆฌ์ŠคํŠธ [1, 2, 4, 8, 9]์™€ ๋”ํ•˜๋ฉด [-1, 0, 2, 6, 9]๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰ a๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ b๋ฒˆ์งธ ์›์†Œ๊นŒ์ง€ c๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ฃผ๊ณ  ์‹ถ์œผ๋ฉด, ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์˜ a๋ฒˆ์งธ ์›์†Œ์— c๋ฅผ ๋”ํ•˜๊ณ  b + 1๋ฒˆ์งธ ์›์†Œ์— c๋ฅผ ๋นผ๋ฉด ๋œ๋‹ค.

์ด์ œ ์ด ๋‚ด์šฉ์„ 2์ฐจ์›์œผ๋กœ ํ™•์žฅํ•ด๋ณด์ž. ํ•œ ๋ฒˆ (0, 0)๋ถ€ํ„ฐ (2, 2)๊นŒ์ง€ n๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

1์ฐจ์› ๋ฐฐ์—ด์—์„œ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ, ๊ฐ ํ–‰๋งˆ๋‹ค ๋”ฐ๋กœ ๋ˆ„์ ํ•ฉ์„ ์ ์šฉ์‹œํ‚ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

n 0 0 -n
n 0 0 -n
n 0 0 -n

์ด๊ฑฐ๋ฅผ ์—ด ๋ฐฉํ–ฅ์— ๋Œ€ํ•ด ํ™•์ธํ•ด๋ณด์ž.

n
n
n

์ด๋Š” ๊ฒฐ๊ตญ

 n
 0
 0
-n

๊ณผ ๊ฐ™๋‹ค. ๋˜ํ•œ

-n
-n
-n

์ด๋Š” ๊ฒฐ๊ตญ

-n
 0
 0
 n

๊ณผ ๊ฐ™๋‹ค. ๊ฒฐ๊ตญ

n 0 0 -n
n 0 0 -n
n 0 0 -n

์ด๋ฅผ ๋‹ค์‹œ ํ‘œํ˜„ํ•ด๋ณด๋ฉด

 n 0 0 -n
 0 0 0  0
 0 0 0  0 
-n 0 0  n

๊ณผ ๊ฐ™๋‹ค. ์ด๋ฅผ ํ–‰๊ณผ ์—ด์— ๋Œ€ํ•ด ๊ฐ๊ฐ ๋ˆ„์ ํ•ฉ ์‹œํ‚ค๋ฉด

n n n 0
n n n 0
n n n 0
0 0 0 0

์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” (0, 0)๋ถ€ํ„ฐ (2, 2)๊นŒ์ง€ n๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ฃผ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์™„์„ฑ๋˜์—ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด skill์˜ ํ•œ ์›์†Œ๋ฅผ $O(1)$ ๋งŒ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ K๊ฐœ์˜ skill์— ๋Œ€ํ•ด ๋ชจ๋‘ ์ ์šฉํ•˜๋ฏ€๋กœ $O(K)$ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค. ๋˜ํ•œ ํ•ด๋‹น ๋ฐฐ์—ด์„ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด๋กœ ๊ณผ์ •์ด ํ•„์š”ํ•œ๋ฐ, ์ด๋•Œ ๊ฐ๊ฐ ํ–‰ ๋ฐฉํ–ฅ๊ณผ ์—ด ๋ฐฉํ–ฅ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ $O(N * M)$ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ $O(K + N * M)$ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฑธ๋ฆฌ๋ฉด์„œ, ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

2๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์— ํ•œ ๋ฒˆ ์ ์šฉํ•ด๋ณด์ž.

  1. (1, 1)๋ถ€ํ„ฐ (2, 2)๊นŒ์ง€ -4๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค˜์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ (1, 1)๊ณผ (3, 3)์—” -4, (1, 3)๊ณผ (3, 1)์—” 4๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
0  0  0  0
0 -4  0  4
0  0  0  0
0  4  0 -4
  1. (0, 0)๋ถ€ํ„ฐ (1, 1)๊นŒ์ง€ -2๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค˜์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ (0, 0)๊ณผ (2, 2)์—” -2, (0, 2)์™€ (2, 0)์—” 2๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
-2  0  2  0
 0 -4  0  4
 2  0 -2  0
 0  4  0 -4
  1. (2, 0)๋ถ€ํ„ฐ (2, 0)๊นŒ์ง€ +100๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค˜์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ (2, 0)๊ณผ (3, 1)์—” 100, (2, 1)๊ณผ (3, 0)์—” -100๋งŒํผ ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
  -2    0  2  0
   0   -4  0  4
 102 -100 -2  0
-100  104  0 -4

์ด์ œ ์ด ๋ฐฐ์—ด์„ ๋จผ์ € ๊ฐ€๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ๋ˆ„์ ํ•ฉํ•œ๋‹ค.

  -2 -2  0 0
   0 -4 -4 0
 102  2  0 0
-100  4  4 0

๋‹ค์Œ์œผ๋กœ ์„ธ๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ๋ˆ„์ ํ•ฉํ•œ๋‹ค.

 -2 -2  0 0
 -2 -6 -4 0
100 -4 -4 0
  0  0  0 0

๋งˆ์ง€๋ง‰์œผ๋กœ ์ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์›๋ณธ๊ณผ ํ•ฉ์ณ์ค€๋‹ค.

1 2 3     -2 -2  0     -1  0 3
4 5 6  +  -2 -6 -4  =   2 -1 2
7 8 9    100 -4 -4    107  4 5

๋”ฐ๋ผ์„œ 0๋ณด๋‹ค ํฐ ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” 6๊ฐœ์ด๋‹ค.

3. ์ฝ”๋“œ ํ™•์ธ

int solution(vector<vector<int>> Board, vector<vector<int>> Skills) 
{
    const int N = Board.size(), M = Board[0].size();
    vector<vector<int>> WeightMatrix = GetWeightMatrix(N, M, Skills);
    
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            Board[i][j] += WeightMatrix[i][j];
        }
    }
    
    int Answer = 0;
    for(const vector<int>& Row : Board)
    {
        for(const int& Col : Row)
        {
            Answer += (Col > 0);
        }
    }
    
    return Answer;
}

๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง„ board ์ •๋ณด์™€ skill ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ , GetWeightMatrix()๋ฅผ ํ˜ธ์ถœํ•ด ๋ˆ„์ ํ•ฉ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜๋ฐ›๋Š”๋‹ค.
์ด๋ฅผ ์›๋ณธ board ์ •๋ณด์™€ ํ•ฉ์นœ ๋’ค, 0๋ณด๋‹ค ํฐ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

vector<vector<int>> GetWeightMatrix(const int N, const int M, const vector<vector<int>>& InSkills)
{
    vector WeightMatrix(N + 1, vector(M + 1, 0));
    for(const vector<int>& InSkill : InSkills)
    {
        FSkill Skill(InSkill);
        if(Skill.Type == static_cast<int>(ESkillType::ATTACK))
        {
            WeightMatrix[Skill.R1][Skill.C1] -= Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C2 + 1] -= Skill.Degree;
            
            WeightMatrix[Skill.R1][Skill.C2 + 1] += Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C1] += Skill.Degree;
        }
        else if(Skill.Type == static_cast<int>(ESkillType::HEAL))
        {
            WeightMatrix[Skill.R1][Skill.C1] += Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C2 + 1] += Skill.Degree;
            
            WeightMatrix[Skill.R1][Skill.C2 + 1] -= Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C1] -= Skill.Degree;
        }
    }

    ...
}

๋ˆ„์ ํ•ฉ ๋ฆฌ์ŠคํŠธ๋Š” ์›๋ฆฌ์— ๋”ฐ๋ผ N๊ณผ M๋ณด๋‹ค ํ•œ ์นธ์”ฉ ๋” ํฌ๊ฒŒ ๊ณต๊ฐ„์„ ์žก์•„์ค€๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ์˜ ์Šคํ‚ฌ์„ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•œ๋‹ค.

  • ๋งŒ์•ฝ ํ•ด๋‹น ์Šคํ‚ฌ์ด ์ ์˜ ๊ณต๊ฒฉ์ด๋ผ๋ฉด Degree๋งŒํผ (r1, c1)๊ณผ (r2 + 1, c2 + 1)๋Š” ๋นผ์ค˜์•ผ ํ•˜๊ณ  (r1, c2 + 1)๊ณผ (r2 + 1, c1)์—๋Š” ๋”ํ•ด์ค˜์•ผ ํ•œ๋‹ค.
  • ๋ฐ˜๋Œ€๋กœ ์•„๊ตฐ์˜ ํšŒ๋ณต์ด๋ผ๋ฉด Degree๋งŒํผ (r1, c1)๊ณผ (r2 + 1, c2 + 1)๋Š” ๋”ํ•ด์ค˜์•ผ ํ•˜๊ณ  (r1, c2 + 1)๊ณผ (r2 + 1, c1)์—๋Š” ๋นผ์ค˜์•ผ ํ•œ๋‹ค.
vector<vector<int>> GetWeightMatrix(const int N, const int M, const vector<vector<int>>& InSkills)
{
    ...
    for(int i = 0; i <= N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            WeightMatrix[i][j + 1] += WeightMatrix[i][j];
        }
    }
    
    for(int j = 0; j <= M; ++j)
    {
        for(int i = 0; i < N; ++i)
        {
            WeightMatrix[i + 1][j] += WeightMatrix[i][j];
        }
    }
       
    return WeightMatrix;
}

์ด์ œ ๋ชจ๋“  ์Šคํ‚ฌ์— ๋Œ€ํ•ด ์ •๋ณด ๊ธฐ๋ก์„ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด, ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ€๋กœ ๋ฐฉํ–ฅ๊ณผ ์„ธ๋กœ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ๊ฐ ๋ˆ„์ ํ•ฉ์„ ์ง„ํ–‰ํ•ด์ค€๋‹ค.
๋ˆ„์ ํ•ฉ์ด ์™„๋ฃŒ๋˜๋ฉด ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

4. ์ „์ฒด ์ฝ”๋“œ

#include <string>
#include <vector>

using namespace std;

struct FSkill
{
    int Type, R1, C1, R2, C2, Degree;
    
    FSkill(const vector<int>& InSkill) : Type(InSkill[0]), R1(InSkill[1]), C1(InSkill[2]), R2(InSkill[3]), C2(InSkill[4]), Degree(InSkill[5]) { }
};

enum class ESkillType
{
    NONE,
    ATTACK,
    HEAL,
};

vector<vector<int>> GetWeightMatrix(const int N, const int M, const vector<vector<int>>& InSkills)
{
    vector WeightMatrix(N + 1, vector(M + 1, 0));
    for(const vector<int>& InSkill : InSkills)
    {
        FSkill Skill(InSkill);
        if(Skill.Type == static_cast<int>(ESkillType::ATTACK))
        {
            WeightMatrix[Skill.R1][Skill.C1] -= Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C2 + 1] -= Skill.Degree;
            
            WeightMatrix[Skill.R1][Skill.C2 + 1] += Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C1] += Skill.Degree;
        }
        else if(Skill.Type == static_cast<int>(ESkillType::HEAL))
        {
            WeightMatrix[Skill.R1][Skill.C1] += Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C2 + 1] += Skill.Degree;
            
            WeightMatrix[Skill.R1][Skill.C2 + 1] -= Skill.Degree;
            WeightMatrix[Skill.R2 + 1][Skill.C1] -= Skill.Degree;
        }
    }
    
    for(int i = 0; i <= N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            WeightMatrix[i][j + 1] += WeightMatrix[i][j];
        }
    }
    
    for(int j = 0; j <= M; ++j)
    {
        for(int i = 0; i < N; ++i)
        {
            WeightMatrix[i + 1][j] += WeightMatrix[i][j];
        }
    }
       
    return WeightMatrix;
}

int solution(vector<vector<int>> Board, vector<vector<int>> Skills) 
{
    const int N = Board.size(), M = Board[0].size();
    vector<vector<int>> WeightMatrix = GetWeightMatrix(N, M, Skills);
    
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            Board[i][j] += WeightMatrix[i][j];
        }
    }
    
    int Answer = 0;
    for(const vector<int>& Row : Board)
    {
        for(const int& Col : Row)
        {
            Answer += (Col > 0);
        }
    }
    
    return Answer;
}

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

์นด์นด์˜ค์—๋Š” ์ฒœ์žฌ๋“ค๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ธ๊ฐ€...? ๋ˆ„์ ํ•ฉ์„ ์ด๋ ‡๊ฒŒ ์‘์šฉํ•œ๋‹ค๋Š” ๊ฑธ ์ƒ์ƒ์กฐ์ฐจ ๋ชปํ–ˆ๋‹ค.
ํ—ˆํ—ˆ... ๋ฒฝ์ด ์ข€ ๋Š๊ปด์ง„๋‹ค.

# Conflicts:
#	9-kyo-hwang/Graph Traversal/1039 ๊ตํ™˜.cpp
#	9-kyo-hwang/README.md
Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

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

๋‚œ์ด๋„๊ฐ€ ๋†’์€ ๋ฌธ์ œ์ผ ์ˆ˜๋ก ์ด๋ ‡๊ฒŒ ํšจ์œจ์„ ๋”ฐ์ ธ์„œ ์ตœ์ ํ™”ํ•˜๋Š” ๊ณผ์ •์ด ๋” ์ค‘์š”ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”.. ๊ทธ๋ฆฌ๊ณ  ๋Š๊ปด์ง€์…จ๋‹ค๋Š” ๊ทธ ๋ฒฝ, ์ €๋Š” ์„ ๋ฐฐ๋‹˜ํ•œํ…Œ ๋Š๋ผ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค๐Ÿ˜„๐Ÿ˜‚ ์ด๋ฒˆ ๋ฌธ์ œ๋„ ๋Œ€๋‹จํžˆ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๐Ÿ”ฅ๐Ÿ˜Š

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.

์ €๋„ ๋ฌธ์ œ ์„ค๋ช…์„ ๋ณด๊ณ  '๋ธŒ๋ฃจํŠธํฌ์Šค๋กœ ํ‘ธ๋Š” ๊ฑด๊ฐ€?' ์‹ถ์—ˆ๋Š”๋ฐ ํšจ์œจ์„ฑ ํ…Œ์ผ€๋„ ํ†ต๊ณผํ•ด์•ผ ํ•˜๋Š”๊ตฐ์š”...
์ €์—๊ฒ ๊ทธ๋ƒฅ ๋ฒฝ์ด ์•„๋‹ˆ๊ณ  ๊ฑฐ์ธ๋“ค์˜ ๋ฒฝ์ด๋„ค์š”...๐Ÿ˜‚
์„ ๋ฐฐ๋‹˜๋„ ์ €์—๊ฒ ์—„์ฒญ๋‚œ ๋ฒฝ์ด๋ž๋‹ˆ๋‹ค.๐Ÿ‘
์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!!

@9kyo-hwang 9kyo-hwang merged commit daf60df into main Feb 26, 2024
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.

3 participants