[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํŠœํ”Œ/ ํŒŒ์ด์ฌ/ Python/ 2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ
Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํŠœํ”Œ/ ํŒŒ์ด์ฌ/ Python/ 2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

โœ… ',{'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง‘ํ•ฉ๋ณ„๋กœ ๋Š์–ด์ค€๋‹ค(split)

โœ… ๋ฌธ์ž ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค(sort)

โœ… ์ง‘ํ•ฉ์— ๋“ค์–ด ์žˆ๋Š” ์›์†Œ๋“ค์„ ๊บผ๋‚ด์–ด ์ˆซ์ž์ธ ๊ฒฝ์šฐ์—๋Š” answer์— ์žˆ๋Š” ์ง€ ํ™•์ธ ํ›„ ์—†์œผ๋ฉด ๋‹ด์•„์ค€๋‹ค.

โœ… ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๋‹ค ๋ณด๋‹ˆ, ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ™œ์šฉํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ๋‹ค. ํŒŒ์ด์ฌ์˜ re(regular expression) ๋ชจ๋“ˆ์„ ์‚ฌ์šฉ, findall() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋ฌธ์ œ์—์„œ๋Š” i๋ฅผ ๋ฐ”๋กœ re.findall("\d+", i)๋กœ ํ‘œํ˜„ํ•ด ์ˆซ์ž๊ฐ’๋“ค๋งŒ ์ฐพ์•„์„œ ๋ฆฌ์ŠคํŠธ์— ๋‹ด์•„์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸŽซcode )

def solution(s):
    answer = []
    a = s.split(',{')
    a.sort(key = len)
    tmp = ''    
    for i in a:
        for k in i:
            if k == ',':
                if int(tmp) not in answer:
                    answer.append(int(tmp))
                tmp = ''
            elif k !='{' and k != '}':
                tmp += k
        else:
            if int(tmp) not in answer:
                answer.append(int(tmp))
            tmp = ''
    return answer

 

๐Ÿ“Œ description )

๋ฌธ์ œ์ถœ์ฒ˜ : https://programmers.co.kr/learn/courses/30/lessons/64065

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํŠœํ”Œ

"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]

programmers.co.kr

 

[๋ฌธ์ œ ์„ค๋ช…]

์…€์ˆ˜์žˆ๋Š” ์ˆ˜๋Ÿ‰์˜ ์ˆœ์„œ์žˆ๋Š” ์—ด๊ฑฐ ๋˜๋Š” ์–ด๋–ค ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฅด๋Š” ์š”์†Œ๋“ค์˜ ๋ชจ์Œ์„ ํŠœํ”Œ(tuple)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. n๊ฐœ์˜ ์š”์†Œ๋ฅผ ๊ฐ€์ง„ ํŠœํ”Œ์„ n-ํŠœํ”Œ(n-tuple)์ด๋ผ๊ณ  ํ•˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • (a1, a2, a3, ..., an)

ํŠœํ”Œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ฑ์งˆ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ์ค‘๋ณต๋œ ์›์†Œ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ex : (2, 3, 1, 2)
  2. ์›์†Œ์— ์ •ํ•ด์ง„ ์ˆœ์„œ๊ฐ€ ์žˆ์œผ๋ฉฐ, ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ํŠœํ”Œ์ž…๋‹ˆ๋‹ค. ex : (1, 2, 3) ≠ (1, 3, 2)
  3. ํŠœํ”Œ์˜ ์›์†Œ ๊ฐœ์ˆ˜๋Š” ์œ ํ•œํ•ฉ๋‹ˆ๋‹ค.

์›์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ n๊ฐœ์ด๊ณ , ์ค‘๋ณต๋˜๋Š” ์›์†Œ๊ฐ€ ์—†๋Š” ํŠœํ”Œ (a1, a2, a3, ..., an)์ด ์ฃผ์–ด์งˆ ๋•Œ(๋‹จ, a1, a2, ..., an์€ ์ž์—ฐ์ˆ˜), ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง‘ํ•ฉ ๊ธฐํ˜ธ '{', '}'๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • {{a1}, {a1, a2}, {a1, a2, a3}, {a1, a2, a3, a4}, ... {a1, a2, a3, a4, ..., an}}

์˜ˆ๋ฅผ ๋“ค์–ด ํŠœํ”Œ์ด (2, 1, 3, 4)์ธ ๊ฒฝ์šฐ ์ด๋Š”

  • {{2}, {2, 1}, {2, 1, 3}, {2, 1, 3, 4}}

์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ์ง‘ํ•ฉ์€ ์›์†Œ์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ์ƒ๊ด€์—†์œผ๋ฏ€๋กœ

  • {{2}, {2, 1}, {2, 1, 3}, {2, 1, 3, 4}}
  • {{2, 1, 3, 4}, {2}, {2, 1, 3}, {2, 1}}
  • {{1, 2, 3}, {2, 1}, {1, 2, 4, 3}, {2}}

๋Š” ๋ชจ๋‘ ๊ฐ™์€ ํŠœํ”Œ (2, 1, 3, 4)๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

ํŠน์ • ํŠœํ”Œ์„ ํ‘œํ˜„ํ•˜๋Š” ์ง‘ํ•ฉ์ด ๋‹ด๊ธด ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, s๊ฐ€ ํ‘œํ˜„ํ•˜๋Š” ํŠœํ”Œ์„ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

[์ œํ•œ์‚ฌํ•ญ]

  • s์˜ ๊ธธ์ด๋Š” 5 ์ด์ƒ 1,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • s๋Š” ์ˆซ์ž์™€ '{', '}', ',' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ˆซ์ž๊ฐ€ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • s๋Š” ํ•ญ์ƒ ์ค‘๋ณต๋˜๋Š” ์›์†Œ๊ฐ€ ์—†๋Š” ํŠœํ”Œ์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • s๊ฐ€ ํ‘œํ˜„ํ•˜๋Š” ํŠœํ”Œ์˜ ์›์†Œ๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • return ํ•˜๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ 500 ์ดํ•˜์ธ ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

[์ž…์ถœ๋ ฅ ์˜ˆ]

sresult
"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4]
"{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4]
"{{20,111},{111}}" [111, 20]
"{{123}}" [123]
"{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]

์ž…์ถœ๋ ฅ ์˜ˆ์— ๋Œ€ํ•œ ์„ค๋ช…์ž…์ถœ๋ ฅ ์˜ˆ #1

๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

(111, 20)์„ ์ง‘ํ•ฉ ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„ํ•˜๋ฉด {{111}, {111,20}}์ด ๋˜๋ฉฐ, ์ด๋Š” {{20,111},{111}}๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

(123)์„ ์ง‘ํ•ฉ ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„ํ•˜๋ฉด {{123}} ์ž…๋‹ˆ๋‹ค.

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

(3, 2, 4, 1)์„ ์ง‘ํ•ฉ ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด ํ‘œํ˜„ํ•˜๋ฉด {{3},{3,2},{3,2,4},{3,2,4,1}}์ด ๋˜๋ฉฐ, ์ด๋Š” {{4,2,3},{3},{2,3,4,1},{2,3}}๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•