https://school.programmers.co.kr/learn/courses/30/lessons/131128

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋‘ ์ •์ˆ˜ X, Y์˜ ์ž„์˜์˜ ์ž๋ฆฌ์—์„œ ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ •์ˆ˜ k(0 ≤ k ≤ 9)๋“ค์„ ์ด์šฉํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์ˆ˜๋ฅผ ๋‘ ์ˆ˜์˜ ์ง๊ฟ์ด๋ผ ํ•ฉ๋‹ˆ๋‹ค(๋‹จ, ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ •์ˆ˜ ์ค‘ ์„œ๋กœ ์ง์ง€์„ ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค). X, Y์˜ ์ง๊ฟ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด, ์ง๊ฟ์€ -1์ž…๋‹ˆ๋‹ค. X, Y์˜ ์ง๊ฟ์ด 0์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด, ์ง๊ฟ์€ 0์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, X = 3403์ด๊ณ  Y = 13203์ด๋ผ๋ฉด, X์™€ Y์˜ ์ง๊ฟ์€ X์™€ Y์—์„œ ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” 3, 0, 3์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์ˆ˜์ธ 330์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ˆ์‹œ๋กœ X = 5525์ด๊ณ  Y = 1255์ด๋ฉด X์™€ Y์˜ ์ง๊ฟ์€ X์™€ Y์—์„œ ๊ณตํ†ต์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” 2, 5, 5๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์ˆ˜์ธ 552์ž…๋‹ˆ๋‹ค(X์—๋Š” 5๊ฐ€ 3๊ฐœ, Y์—๋Š” 5๊ฐ€ 2๊ฐœ ๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ ๋‚จ๋Š” 5 ํ•œ ๊ฐœ๋Š” ์ง ์ง€์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.)
๋‘ ์ •์ˆ˜ X, Y๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, X, Y์˜ ์ง๊ฟ์„ returnํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ X, Y์˜ ๊ธธ์ด(์ž๋ฆฟ์ˆ˜) ≤ 3,000,000์ž…๋‹ˆ๋‹ค.
  • X, Y๋Š” 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • X, Y์˜ ์ง๊ฟ์€ ์ƒ๋‹นํžˆ ํฐ ์ •์ˆ˜์ผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

X Y result
"100" "2345" "-1"
"100" "203045" "0"
"100" "123450" "10"
"12321" "42531" "321"
"5525" "1255" "552"

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (์‹œ๊ฐ„ ์ดˆ๊ณผ)
def solution(X, Y):
    answer = ""

    inter = []

    for x in X:
        if x in Y:
            inter.append(int(x))
            Y = Y.replace(x,"",1)  

    inter.sort(reverse=True) 
    if len(inter) == 0:
        answer = "-1"
    elif inter[0] == 0:
        answer = "0"
    else:
        for i in inter:
            answer += str(i)
        
    return answer

 

ํ’€์ด # 1

  • ๊ต์ง‘ํ•ฉ ์›์†Œ๋ฅผ ๋‹ด์„ inter ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • X๋ฅผ ๋Œ๋ฉด์„œ X์˜ ์›์†Œ๊ฐ€ Y์— ์žˆ์œผ๋ฉด inter ๋ฆฌ์ŠคํŠธ์— ํ•ด๋‹น ์›์†Œ ์ถ”๊ฐ€ํ•˜๊ณ  Y์—์„œ ํ•ด๋‹น ์›์†Œ ์‚ญ์ œ
    • replace("๋ฐ”๊พธ๊ธฐ ์ „ ๋ฌธ์ž", "๋ฐ”๊พผ ํ›„์˜ ๋ฌธ์ž", ๋ฐ”๊พธ๊ณ  ์‹ถ์€ ๋ฌธ์ž ๊ฐฏ์ˆ˜)
  • inter ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • inter๊ฐ€ ์—†์„ ๊ฒฝ์šฐ "-1", 0์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” "0"์„ ์ถœ๋ ฅ
  • inter๊ฐ€ ์žˆ๊ณ  0์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๋ฌธ์ž๋ฅผ answer์— ์ถ”๊ฐ€

 

# 2
def solution(X, Y):
    answer = ""

    numberX = [0]*10
    numberY = [0]*10
    
    # ๊ฐ ์ˆซ์ž๊ฐ€ ๋ช‡ ๊ฐœ์”ฉ ์žˆ๋Š”์ง€ ์นด์šดํŠธ
    for x in X:
        numberX[int(x)] += 1
    
    for y in Y:
        numberY[int(y)] += 1


    # ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ์ถ”๊ฐ€
    for idx in range(9,-1,-1):
        if numberX[idx] > 0 and numberY[idx] > 0:
            cnt = min(numberX[idx], numberY[idx])
            answer += (str(idx) * cnt)

    if answer == "":
        answer = "-1"
    elif answer[0] == "0":
        answer = "0"

        
    return answer

 

ํ’€์ด #2

  • X์™€ Y์˜ ๊ฐ ์ˆซ์ž ๊ฐฏ์ˆ˜๋ฅผ numberX์™€ numberY์— ์นด์šดํŠธ (๊ฐ ์ˆซ์ž = ์ธ๋ฑ์Šค)
  • sort๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ํฐ ์ˆ˜๋ถ€ํ„ฐ ๋ฐ˜๋ณต๋ฌธ
    • numberX์™€ numberY์— ๋‘˜ ๋‹ค ์กด์žฌํ•˜๋ฉด ๋‘˜ ์ค‘์— ์ž‘์€ ์ˆ˜๋งŒํผ answer์— ์ถ”๊ฐ€
  • ๊ต์ง‘ํ•ฉ์ด ์—†๊ฑฐ๋‚˜ 0์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ

 

 

๋ฌธ์ œ ์ž์ฒด๋Š” ๋‹จ์ˆœํ•œ๋ฐ ์ œํ•œ ์‚ฌํ•ญ์„ ๋ณด๋ฉด ์ •์ˆ˜๊ฐ€ ์ƒ๋‹นํžˆ ํด ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ๋‹ค. sort๋ฅผ ์ด์šฉํ•˜๋ฉด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฏ€๋กœ sort๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  for๋ฌธ๋„ 10๊ฐœ ์ˆซ์ž ๋‚ด์—์„œ ๋Œ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์˜€๋‹ค. ํ†ต๊ณผ๋Š” ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋„ ๊ณ ๋ คํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฒŒ ์ค‘์š”ํ•œ๋ฐ....์–ด๋ ต๊ตฌ๋งŒ...

+ Recent posts