๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ‘œ ๋ณ‘ํ•ฉ (2023 KAKAO BLIND RECRUITMENT)

๐ŸŽฎinspirer9 2023. 3. 13. 09:00
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

๋‹น์‹ ์€ ํ‘œ ํŽธ์ง‘ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
ํ‘œ์˜ ํฌ๊ธฐ๋Š” 50 × 50์œผ๋กœ ๊ณ ์ •๋˜์–ด์žˆ๊ณ  ์ดˆ๊ธฐ์— ๋ชจ๋“  ์…€์€ ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฐ ์…€์€ ๋ฌธ์ž์—ด ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ์…€๊ณผ ๋ณ‘ํ•ฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„์—์„œ r๋ฒˆ์งธ, ์™ผ์ชฝ์—์„œ c๋ฒˆ์งธ ์œ„์น˜๋ฅผ (r, c)๋ผ๊ณ  ํ‘œํ˜„ํ•  ๋•Œ, ๋‹น์‹ ์€ ๋‹ค์Œ ๋ช…๋ น์–ด๋“ค์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  1. "UPDATE r c value"
    • (r, c) ์œ„์น˜์˜ ์…€์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
    • ์„ ํƒํ•œ ์…€์˜ ๊ฐ’์„ value๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  2. "UPDATE value1 value2"
    • value1์„ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ชจ๋“  ์…€์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
    • ์„ ํƒํ•œ ์…€์˜ ๊ฐ’์„ value2๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  3. "MERGE r1 c1 r2 c2"
    • (r1, c1) ์œ„์น˜์˜ ์…€๊ณผ (r2, c2) ์œ„์น˜์˜ ์…€์„ ์„ ํƒํ•˜์—ฌ ๋ณ‘ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
    • ์„ ํƒํ•œ ๋‘ ์œ„์น˜์˜ ์…€์ด ๊ฐ™์€ ์…€์ผ ๊ฒฝ์šฐ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.
    • ์„ ํƒํ•œ ๋‘ ์…€์€ ์„œ๋กœ ์ธ์ ‘ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ (r1, c1) ์œ„์น˜์˜ ์…€๊ณผ (r2, c2) ์œ„์น˜์˜ ์…€๋งŒ ์˜ํ–ฅ์„ ๋ฐ›์œผ๋ฉฐ, ๊ทธ ์‚ฌ์ด์— ์œ„์น˜ํ•œ ์…€๋“ค์€ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ๋‘ ์…€ ์ค‘ ํ•œ ์…€์ด ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ๋ณ‘ํ•ฉ๋œ ์…€์€ ๊ทธ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    • ๋‘ ์…€ ๋ชจ๋‘ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ๋ณ‘ํ•ฉ๋œ ์…€์€ (r1, c1) ์œ„์น˜์˜ ์…€ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    • ์ดํ›„ (r1, c1) ์™€ (r2, c2) ์ค‘ ์–ด๋Š ์œ„์น˜๋ฅผ ์„ ํƒํ•˜์—ฌ๋„ ๋ณ‘ํ•ฉ๋œ ์…€๋กœ ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.
  4. "UNMERGE r c"
    • (r, c) ์œ„์น˜์˜ ์…€์„ ์„ ํƒํ•˜์—ฌ ํ•ด๋‹น ์…€์˜ ๋ชจ๋“  ๋ณ‘ํ•ฉ์„ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.
    • ์„ ํƒํ•œ ์…€์ด ํฌํ•จํ•˜๊ณ  ์žˆ๋˜ ๋ชจ๋“  ์…€์€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ดˆ๊ธฐ์˜ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.
    • ๋ณ‘ํ•ฉ์„ ํ•ด์ œํ•˜๊ธฐ ์ „ ์…€์ด ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์„ ๊ฒฝ์šฐ (r, c) ์œ„์น˜์˜ ์…€์ด ๊ทธ ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  5. "PRINT r c"
    • (r, c) ์œ„์น˜์˜ ์…€์„ ์„ ํƒํ•˜์—ฌ ์…€์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    • ์„ ํƒํ•œ ์…€์ด ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ "EMPTY"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” UPDATE ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋นˆ ์…€์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

commands ํšจ๊ณผ
UPDATE 1 1 menu (1,1)์— "menu" ์ž…๋ ฅ
UPDATE 1 2 category (1,2)์— "category" ์ž…๋ ฅ
UPDATE 2 1 bibimbap (2,1)์— "bibimbap" ์ž…๋ ฅ
UPDATE 2 2 korean (2,2)์— "korean" ์ž…๋ ฅ
UPDATE 2 3 rice (2,3)์— "rice" ์ž…๋ ฅ
UPDATE 3 1 ramyeon (3,1)์— "ramyeon" ์ž…๋ ฅ
UPDATE 3 2 korean (3,2)์— "korean" ์ž…๋ ฅ
UPDATE 3 3 noodle (3,3)์— "noodle" ์ž…๋ ฅ
UPDATE 3 4 instant (3,4)์— "instant" ์ž…๋ ฅ
UPDATE 4 1 pasta (4,1)์— "pasta" ์ž…๋ ฅ
UPDATE 4 2 italian (4,2)์— "italian" ์ž…๋ ฅ
UPDATE 4 3 noodle (4,3)์— "noodle" ์ž…๋ ฅ

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” MERGE ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์…€์„ ๋ณ‘ํ•ฉํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

commands ํšจ๊ณผ
MERGE 1 2 1 3 (1,2)์™€ (1,3) ๋ณ‘ํ•ฉ
MERGE 1 3 1 4 (1,3)๊ณผ (1,4) ๋ณ‘ํ•ฉ

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๋ณ‘ํ•ฉํ•œ ์…€์€ "category" ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋ฉฐ (1,2), (1,3), (1,4) ์ค‘ ์–ด๋Š ์œ„์น˜๋ฅผ ์„ ํƒํ•˜๋”๋ผ๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” UPDATE ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์…€์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

commands ํšจ๊ณผ
UPDATE korean hansik "korean"์„ "hansik"์œผ๋กœ ๋ณ€๊ฒฝ
UPDATE 1 3 group (1,3) ์œ„์น˜์˜ ์…€ ๊ฐ’์„ "group"์œผ๋กœ ๋ณ€๊ฒฝ

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” UNMERGE ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์…€์˜ ๋ณ‘ํ•ฉ์„ ํ•ด์ œํ•˜๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

commands ํšจ๊ณผ
UNMERGE 1 4 ์…€ ๋ณ‘ํ•ฉ ํ•ด์ œ ํ›„ ์›๋ž˜ ๊ฐ’์€ (1,4)๊ฐ€ ๊ฐ€์ง

์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋“ค์ด ๋‹ด๊ธด 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด commands๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. commands์˜ ๋ช…๋ น์–ด๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ํ•˜์˜€์„ ๋•Œ, "PRINT r c" ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ 1์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ commands์˜ ๊ธธ์ด ≤ 1,000
  • commands์˜ ๊ฐ ์›์†Œ๋Š” ์•„๋ž˜ 5๊ฐ€์ง€ ํ˜•ํƒœ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
    1. "UPDATE r c value"
      • r, c๋Š” ์„ ํƒํ•  ์…€์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1~50 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
      • value๋Š” ์…€์— ์ž…๋ ฅํ•  ๋‚ด์šฉ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๊ธธ์ด 1~10 ์‚ฌ์ด์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    2. "UPDATE value1 value2"
      • value1์€ ์„ ํƒํ•  ์…€์˜ ๊ฐ’, value2๋Š” ์…€์— ์ž…๋ ฅํ•  ๋‚ด์šฉ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๊ธธ์ด 1~10 ์‚ฌ์ด์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    3. "MERGE r1 c1 r2 c2"
      • r1, c1, r2, c2๋Š” ์„ ํƒํ•  ์…€์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1~50 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
    4. "UNMERGE r c"
      • r, c๋Š” ์„ ํƒํ•  ์…€์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1~50 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
    5. "PRINT r c"
      • r, c๋Š” ์„ ํƒํ•  ์…€์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1~50 ์‚ฌ์ด์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
  • commands๋Š” 1๊ฐœ ์ด์ƒ์˜ "PRINT r c" ๋ช…๋ น์–ด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

commands result
["UPDATE 1 1 menu", "UPDATE 1 2 category", "UPDATE 2 1 bibimbap", "UPDATE 2 2 korean", "UPDATE 2 3 rice", "UPDATE 3 1 ramyeon", "UPDATE 3 2 korean", "UPDATE 3 3 noodle", "UPDATE 3 4 instant", "UPDATE 4 1 pasta", "UPDATE 4 2 italian", "UPDATE 4 3 noodle", "MERGE 1 2 1 3", "MERGE 1 3 1 4", "UPDATE korean hansik", "UPDATE 1 3 group", "UNMERGE 1 4", "PRINT 1 3", "PRINT 1 4"] ["EMPTY", "group"]
["UPDATE 1 1 a", "UPDATE 1 2 b", "UPDATE 2 1 c", "UPDATE 2 2 d", "MERGE 1 1 1 2", "MERGE 2 2 2 1", "MERGE 2 1 1 1", "PRINT 1 1", "UNMERGE 2 2", "PRINT 1 1"] ["d", "EMPTY"]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. (1,3) ์œ„์น˜์˜ ์…€์€ ๋น„์–ด์žˆ๊ณ  (1,4) ์œ„์น˜์˜ ์…€ ๊ฐ’์€ "group"์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ["EMPTY", "group"]์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋ชจ๋“  UPDATE ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

  • "MERGE 1 1 1 2" ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

  • "MERGE 2 2 2 1" ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

  • "MERGE 2 1 1 1" ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

  • "UNMERGE 2 2" ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ฐธ์กฐ์— ์ฐธ์กฐ์— ์ฐธ์กฐ์—... ๋ฐฐ์—ด์— ๋ฐฐ์—ด์— ๋ฐฐ์—ด์— ใ…‹ใ…‹ใ…‹
    • ์‹œ๊ฐ„๋งŒ ๋‚ญ๋น„ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.
      • ๋” ๋‹จ์ˆœํ•œ ๋ฐฉ๋ฒ•์„ ํ•ด๋ณด์ž
  • ๋ฌธ์ œ๋ฅผ ๋ณด๋‹ค๋ณด๋‹ˆ๊นŒ merge๊ฐ€ ๋ณ‘์‹ ๊ฐ™์€ ์กฐ๊ฑด์ด ๋‹ฌ๋ ค์žˆ๋„ค...
    • ๋‚œ r1 c1 r2 c2 ๋ฒ”์œ„ ๋‚ด ๋ชจ๋“  ์…€์˜ ์ฐธ์กฐ๋ฅผ ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ,
      • ์ง€๊ธˆ ๋‹ค์‹œ ๋ณด๋‹ˆ๊นŒ ๊ทธ๋ƒฅ 2์…€์˜ ์ฐธ์กฐ๋งŒ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ...
  • ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฉ”์ปค๋‹ˆ์ฆ˜๊ณผ ์ ‘๊ทผ์„ ํ•œ๋ฒˆ์— ๋๋‚ผ ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด์ž
    • ์ €์žฅ๊ณต๊ฐ„
      • ์‹œํŠธ ์ •๋ณด
        • sheet(51x51)๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ ,
          • ๊ฐ’ ์ฐธ์กฐ : test1 = [1,1]
          • ํ•ฉ๋ณ‘์…€ ์ฐธ์กฐ : test2 = [1,2,3,4]
      • ๊ฐ’ ์ •๋ณด
        • values = {}๋กœ ๋งŒ๋“ค๊ณ , ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ €์žฅ
          • ๊ฐ’์…€ : values[(1,1)] = "babo"
          • ํ•ฉ๋ณ‘์…€ : values[(1,2,3,4)] = "merged"
    • ๊ตฌํ˜„
      • ๋ช…๋ น์„ ์ปค๋งจ๋“œ ๋ณ„๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ ,
      • ๊ฐ ํ•จ์ˆ˜์—์„œ ๊ฐœ๋ณ„ ์ปค๋งจ๋“œ๋กœ ๋ถ„๊ธฐ
      • sheet์™€ value์—์„œ ๊ฐ’์„ ๊บผ๋‚ผ ๋•Œ๋Š” ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด๋กœ ์–ด๋–ค ์ข…๋ฅ˜์ธ์ง€ ํŒ๋ณ„ํ•œ๋‹ค.
        • ๊ฐ’ ์…€ : len(test1)
        • ํ•ฉ๋ณ‘ ์…€ : len(test2)
    • ๋ช…๋ น์–ด๋ณ„ ๊ตฌํ˜„
      • UPDATE r c value
        • ํ•ด๋‹น ์…€์— ์ฐธ์กฐ๊ฐ€ ์žˆ์œผ๋ฉด values์—์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•จ
        • ํ•ด๋‹น ์…€์— ์ฐธ์กฐ๊ฐ€ ์—†์œผ๋ฉด ์ฐธ์กฐ๋ฅผ ๋งŒ๋“ค๊ณ , values์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•จ
      • UPDATE value1 value2
        • values ํ…Œ์ด๋ธ” ์ „์ˆ˜ ๊ฒ€์‚ฌํ•ด์„œ value1์ด ์žˆ์œผ๋ฉด value2๋กœ ๋ณ€๊ฒฝ
        • ๋ชจ๋“  ์…€์˜ ๊ฐ’์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋๊นŒ์ง€ for๋ฌธ ๋Œ๋ฆฌ๋ฉด ๋จ
          • for k,v in list(values.items())๋กœ 
      • MERGE r1 c1 r2 c2
        • sheet์˜ ๋‘ ์…€ (r1, c1), (r2, c2)์˜ ์ฐธ์กฐ๋ฅผ ๋™์ผํ•œ ๊ฑธ๋กœ ๋ณ€๊ฒฝ
        • r1 c1 = r2 c2 ๋ฉด ์Šคํ‚ต
        • ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์— ๊ฐ’์ด ์žˆ๋Š” ์…€์ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•ด์„œ ๊ฐ’์„ r1 c1๋กœ. ๋‘์…€ ๋ชจ๋‘ ๊ฐ’์ด ์žˆ์œผ๋ฉด r1 c1 ๊ธฐ์ค€
      • UNMERGE r c
        • ํ•ด๋‹น ์œ„์น˜์— ์žˆ๋Š” ์…€์ด ๋ณ‘ํ•ฉ ์…€์ด๋ฉด sheet for๋ฌธ ๋Œ๋ ค์„œ ์ฐธ์กฐํ•ด์ œํ•˜๊ณ ,
        • values[r c]์— value[r1 c1 r2 c2] ๊ฐ’์„ ๋„ฃ๊ธฐ
      • ์–ด๋ผ? ๋œ๋‹ค!?

  • ์ด์ „์— ์žˆ๋˜ ๊ฐ’๋“ค ์ง€์šฐ์ง€ ์•Š์•„๋„ ๋˜๋Š” ๋“ฏ? ใ…‡ใ…‹ ๊ฐ€์ฆˆ์•„!
def solution(commands):
    answer = []
    sheet = [['']*51 for i in range(51)]
    values = {'':"EMPTY"}

    for cmd in commands:
        cmd = cmd.split(sep=' ')
        if cmd[0] == "UPDATE":
            if len(cmd) == 3: # ๋ฆฌ๋„ค์ž„
                source = cmd[1]
                target = cmd[2]
                for k,v in list(values.items()):
                    if v == source:
                        values[k] = target
            else: # ์—…๋ฐ์ดํŠธ
                r = int(cmd[1])
                c = int(cmd[2])
                v = cmd[3]
                source = sheet[r][c]
                if source == '': # ๋นˆ์…€
                    sheet[r][c] = (r,c)
                    values[(r,c)] = v
                else:
                    values[source] = v
        elif cmd[0] == "PRINT": # print("ํ”„๋ฆฐํŠธ")
            r = int(cmd[1])
            c = int(cmd[2])
            source = sheet[r][c]
            answer.append(values[source])
        elif cmd[0] == "MERGE":
            r1 = int(cmd[1])
            c1 = int(cmd[2])
            r2 = int(cmd[3])
            c2 = int(cmd[4])
            source1 = sheet[r1][c1]
            source2 = sheet[r2][c2]
            if source1 == '' and source2 == '': # ๋นˆ์…€, ๋นˆ์…€
                sheet[r1][c1] = (r1,c1)
                sheet[r2][c2] = (r2,c2)
            elif source1 == '': # ๋นˆ์…€ vs ๊ฐ’์…€
                sheet[r1][c1] = sheet[r2][c2]
            elif source2 == '': # ๊ฐ’์…€ vs ๋นˆ์…€
                sheet[r2][c2] = sheet[r1][c1]
            else: # ๊ฐ’์…€/๋ณ‘ํ•ฉ์…€ vs ๊ฐ‘์…€/๋ณ‘ํ•ฉ์…€
                for i in range(51):
                    for j in range(51):
                        if sheet[i][j] == source2:
                            sheet[i][j] = source1
        elif cmd[0] == "UNMERGE": # print("์–ธ๋จธ์ง€")
            r = int(cmd[1])
            c = int(cmd[2])
            source = sheet[r][c]
            v = values[source]
            for i in range(51):
                for j in range(51):
                    if sheet[i][j] == source:
                        sheet[i][j] = ''
            sheet[r][c] = (r,c)
            values[(r,c)] = v
    
    return answer
  • ์ž‰? ์™  ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ...

  • ๋จธ์ง€๋ž‘ ์–ธ๋จธ์ง€, ๊ฐ’ ๋„ฃ๊ธฐ. ๋‹ค ํ™•์ธํ–ˆ๋Š”๋ฐ... ํž ์™œ ์•ˆ๋ผ
  • ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 6๋ฒˆ 11๋ฒˆ์ด ์‹คํŒจํ•œ๋‹ค.

  • ๊ทธ๋ƒฅ ์ฒจ๋ถ€ํ„ฐ ๊ฐ’์„ ๋‹ค ๋„ฃ์–ด ๋†“๊ณ  ์‹œ์ž‘ํ•ด๋„ ๋˜๋Š”๊ตฌ๋‚˜... ใ…ก.ใ…ก;
def solution(commands):
    answer = []
    sheet = [[(i,j) for j in range(51)] for i in range(51)]
    values = {(i,j):'' for j in range(51) for i in range(51)}

    for cmd in commands:
        cmd = cmd.split(sep=' ')
        if cmd[0] == "UPDATE" and len(cmd) == 3: # ๋ฆฌ๋„ค์ž„
            source = cmd[1]
            target = cmd[2]
            for k,v in values.items():
                if v == source:
                    values[k] = target
        if cmd[0] == "UPDATE" and len(cmd) == 4: # ์—…๋ฐ์ดํŠธ
            r = int(cmd[1])
            c = int(cmd[2])
            v = cmd[3]
            source = sheet[r][c]
            values[source] = v
        elif cmd[0] == "PRINT": # print("ํ”„๋ฆฐํŠธ")
            r = int(cmd[1])
            c = int(cmd[2])
            source = sheet[r][c]
            if values[source] != '':
                answer.append(values[source])
            else:
                answer.append("EMPTY")
        elif cmd[0] == "MERGE":
            r1 = int(cmd[1])
            c1 = int(cmd[2])
            r2 = int(cmd[3])
            c2 = int(cmd[4])
            source1 = sheet[r1][c1]
            source2 = sheet[r2][c2]
            if source1 == source2:
                continue
            if values[source1] != '': # ๋นˆ์…€ vs ๊ฐ’์…€
                v_source1 = values[source1]
                for i in range(51):
                    for j in range(51):
                        if sheet[i][j] == source2:
                            sheet[i][j] = source1
            else:
                v_source2 = values[source2]
                for i in range(51):
                    for j in range(51):
                        if sheet[i][j] == source1:
                            sheet[i][j] = source2
        elif cmd[0] == "UNMERGE": # print("์–ธ๋จธ์ง€")
            r = int(cmd[1])
            c = int(cmd[2])
            source = sheet[r][c]
            v = values[source]
            for i in range(51):
                for j in range(51):
                    if sheet[i][j] == source:
                        sheet[i][j] = (i,j)
                        values[(i,j)] = ''
            values[(r,c)] = v
    
    return answer
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (1.54ms, 10.7MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (1.42ms, 10.7MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.76ms, 10.7MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.74ms, 10.6MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.16ms, 10.7MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (1.75ms, 10.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1.29ms, 10.8MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.55ms, 10.6MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (3.45ms, 10.8MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (4.13ms, 10.6MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (6.84ms, 10.6MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (15.11ms, 10.9MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (46.35ms, 10.8MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (43.27ms, 10.9MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (75.67ms, 10.8MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (64.56ms, 10.9MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (73.67ms, 10.9MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (67.36ms, 10.8MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (70.56ms, 10.9MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (153.35ms, 10.8MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (174.97ms, 11MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (80.95ms, 11MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด๋ฅผ ๋ณด๋ฉด
    • ํŠœํ”Œ์ด ์•„๋‹Œ ๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋ฐฉ์‹๋„ ์žˆ๊ณ 
def solution(commands):
    answer = []
    sheet = [[51 * i + j for j in range(51)] for i in range(51)]
    values = {i : '' for i in range(51*51)}

    for cmd in commands:
        cmd = list(cmd.split(sep=' '))
        if cmd[0] == "UPDATE" and len(cmd) == 3: # ๋ฆฌ๋„ค์ž„
            source = cmd[1]
            target = cmd[2]
            for k,v in values.items():
                if v == source:
                    values[k] = target
        elif cmd[0] == "UPDATE" and len(cmd) == 4:  # ์—…๋ฐ์ดํŠธ
            r = int(cmd[1])
            c = int(cmd[2])
            v = cmd[3]
            source = sheet[r][c]
            values[source] = v
        elif cmd[0] == "PRINT": # print("ํ”„๋ฆฐํŠธ")
            r = int(cmd[1])
            c = int(cmd[2])
            source = sheet[r][c]
            if values[source] != '':
                answer.append(values[source])
            else:
                answer.append("EMPTY")
        elif cmd[0] == "MERGE":
            r1 = int(cmd[1])
            c1 = int(cmd[2])
            r2 = int(cmd[3])
            c2 = int(cmd[4])
            source1 = sheet[r1][c1]
            source2 = sheet[r2][c2]
            if source1 == source2:
                continue
            if values[source1] != '':
                for i in range(51):
                    for j in range(51):
                        if sheet[i][j] == source2:
                            sheet[i][j] = source1
            else:
                for i in range(51):
                    for j in range(51):
                        if sheet[i][j] == source1:
                            sheet[i][j] = source2
        elif cmd[0] == "UNMERGE": # print("์–ธ๋จธ์ง€")
            r = int(cmd[1])
            c = int(cmd[2])
            source = sheet[r][c]
            v = values[source]
            for i in range(51):
                for j in range(51):
                    if sheet[i][j] == source:
                        sheet[i][j] = 51 * i + j
                        values[sheet[i][j]] = ''
            values[sheet[r][c]] = v
        
        # print(cmd)
        # for i in range(11):
        #     for j in range(11):
        #         print(sheet[i][j],end="|")
        #     print("")
        # print("="*10)
        # print(values)
        # print("="*10)
    return answer
  • ๊ทธ๋ƒฅ 2์ฐจ์› ๋ฐฐ์—ด์— ๋‚ด์šฉ๊ณผ ์ฐธ์กฐ๋ฅผ ๊ฐ™์ด ๋„ฃ๋Š” ๋ฐฉ์‹๋„ ๋˜๋Š”๊ตฌ๋‚˜.
def solution(commands):
    answer = []

    board = [[["",[(y,x)]] for x in range(51)] for y in range(51)]

    for c in commands:
        li = c.split()
        if li[0] == "UPDATE":
            if len(li) == 4:
                for r,c in board[int(li[1])][int(li[2])][1]:
                    board[r][c][0] = li[3]
            else:
                for y in range(1,51):
                    for x in range(1,51):
                        if board[y][x][0] == li[1]:
                            board[y][x][0] = li[2]

        if li[0] == "MERGE":
            r1,c1 = int(li[1]),int(li[2])
            r2,c2 = int(li[3]),int(li[4])
            temp_li = list(set(board[r1][c1][1]+board[r2][c2][1]))
            for r,c in board[r1][c1][1]:
                board[r][c][1] = temp_li
            for r,c in board[r1][c1][1]:
                board[r][c][1]= temp_li

            if board[r1][c1][0] != "":
                for r,c in board[r1][c1][1]:
                    board[r][c][0] = board[r1][c1][0]
            elif board[r2][c2][0] != "":
                for r,c in board[r2][c2][1]:
                    board[r][c][0] = board[r2][c2][0]

        if li[0] == "UNMERGE":

            for r,c in board[int(li[1])][int(li[2])][1]:
                if r == int(li[1]) and c == int(li[2]):
                    pass
                else:
                    board[r][c][1] = [(r,c)]
                    board[r][c][0] = ""
            board[int(li[1])][int(li[2])][1] = [(int(li[1]),int(li[2]))]

        if li[0] == "PRINT":
            r1,c1 = int(li[1]),int(li[2])
            if board[r1][c1][0] != "":
                answer.append(board[r1][c1][0])
            else:
                answer.append("EMPTY")
    return answer
  • ๊ทผ๋ฐ ๋”ฑํžˆ ์ž๋ฃŒํ˜•์ด๋‚˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณด๋‹ค๋Š” ๊ทธ๋ƒฅ ๊ตฌํ˜„ ๋ฌธ์ œ ๊ฐ™๋‹ค.
  • ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ๊นŒ ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ ๋ฌธ์ œ๋ผ๊ณ  ๋‚˜์˜ค๋˜๋ฐ...
    • (์‚ฌ์‹ค์€ ๋ญ”์ง€ ๋ชฐ๋ผ... ์†Œ๊ณค์†Œ๊ณค)
    • ๋‚˜์ค‘์— ๊ณต๋ถ€ํ•ด๋ณด์ž...
728x90
๋ฐ˜์‘ํ˜•