"์ƒ์ƒ ์ด์ƒ์˜ ํŒŒ๊ดด๋ ฅ" 2024๋…„ ๋„คํŠธ์›Œํฌ ํŠธ๋ Œ๋“œ๋ฅผ ์ฃผ๋„ํ•  3๊ฐ€์ง€ ์•„์ด๋””์–ด (๋งํฌ)

 

๊ธฐ์‚ฌ ๋‚ด์šฉ ์š”์•ฝ
VPN ๋Œ€์‹  ๋ฌด์—‡์„ ์“ธ ์ˆ˜ ์žˆ์„๊นŒ? ๋‹ค์Œ์˜ 3๊ฐ€์ง€ ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ์ƒˆ๋กœ์šด ๊ฐœ๋…์€ ์ „ํ†ต์ ์ธ ์„œ๋น„์Šค ์ œ๊ณต์—…์ฒด์˜ ๊ฐœ๋…๊ณผ ๋„คํŠธ์›Œํฌ์˜ ๋ฏธ๋ž˜๋ฅผ ๋ฐ”๊ฟ” ๋†“์„ ์ˆ˜๋„ ์žˆ๋‹ค.
์ฒซ ๋ฒˆ์งธ๋Š” ํด๋ผ์šฐ๋“œ ์šฐ์„  ๋ณด์ด์ง€ ์•Š๋Š” ๋„คํŠธ์›Œํฌ(invisible network)๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์„œ๋น„์Šค ๊ฐœ๋…์€ ์„œ๋น„์Šค๊ฐ€ ์•„์˜ˆ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ชจ๋‘๊ฐ€ ์ธํ„ฐ๋„ท์„ ์‚ฌ์šฉํ•ด ํด๋ผ์šฐ๋“œ์— ์ ‘์†ํ•˜๊ณ  ํด๋ผ์šฐ๋“œ๋Š” ๋ฐ์ดํ„ฐ์„ผํ„ฐ์— ์—ฐ๊ฒฐ๋œ๋‹ค. ๊ธฐ์กด VPN์˜ ์ž„๋ฌด ์ค‘ ์ผ๋ถ€๋Š” ์ธํ„ฐ๋„ท ์•ก์„ธ์Šค๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋˜๊ณ  ์ผ๋ถ€๋Š” ํด๋ผ์šฐ๋“œ ์ œ๊ณต์—…์ฒด ์ธํ”„๋ผ ๋‚ด์— ์œ„์น˜ํ•œ๋‹ค.
๋‘ ๋ฒˆ์งธ๋Š” 5G ๋„คํŠธ์›Œํฌ ์Šฌ๋ผ์ด์‹ฑ์— ์˜์กดํ•˜๋Š” ์–ธ๋”๋„ท(undernet)์ด๋‹ค. ์–ธ๋”๋„ท์€ ์„œ๋น„์Šค ์ œ๊ณต์—…์ฒด๊ฐ€ ๊ด€์‹ฌ์žˆ๋Š” ๊ฐœ๋…๊ณผ ์‚ฌ์šฉ์ž๊ฐ€ ๊ด€์‹ฌ์žˆ๋Š” ๊ฐœ๋…์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์ธํ„ฐ๋„ท์— ์—ฐ๊ฒฐ์€ ๋˜์ง€๋งŒ ๊ทธ ์•„๋ž˜์— ์œ„์น˜ํ•œ ๋„คํŠธ์›Œํฌ๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์–ธ๋”๋„ท์—์„œ 5G ๋„คํŠธ์›Œํฌ ์Šฌ๋ผ์ด์‹ฑ ๊ฐœ๋…์€ ํŠธ๋ž˜ํ”ฝ ๋ถ„ํ• ์„ ํ†ตํ•ด ๋” ๋‚˜์€ QoS๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋Š” ์„œ๋ธŒ๋„ท์„ ๋งŒ๋“ ๋‹ค.
์„ธ ๋ฒˆ์งธ๋Š” ๋Œ€์•ˆ ํŒŒ์ด๋ฒ„ ์•ก์„ธ์Šค ์ œ๊ณต์—…์ฒด๋ฅผ ํ†ตํ•œ ์ŠคํŠธ๋ ˆ์ธ์ € ๋„คํŠธ์›Œํฌ(stranger network)๋‹ค. ๊ธฐ์—…์šฉ ์„œ๋น„์Šค๋ฅผ ์œ„ํ•œ ์•ก์„ธ์Šค ๋„คํŠธ์›Œํฌ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ†ต์‹  ์‚ฌ์—…์ž๋‚˜ ์ผ€์ด๋ธ” ์‚ฌ์—…์ž๊ฐ€ ์ œ๊ณตํ–ˆ์ง€๋งŒ ์ฃผ๋กœ ํŒŒ์ด๋ฒ„ ์—ฐ๊ฒฐ์„ ์ œ๊ณตํ•˜๋Š” ์•ก์„ธ์Šค ์ œ๊ณต์—…์ฒด๊ฐ€ ๋‹ค์ˆ˜ ์ฆ๊ฐ€ํ–ˆ๋‹ค. ์ด๋Ÿฐ ์—…์ฒด์˜ ๋ฌธ์ œ๋Š” ํ˜‘์†Œํ•œ ์ง€๋ฆฌ์  ๋ฒ”์œ„์ด๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•  ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๊ด€๋ฆฌํ˜• ์„œ๋น„์Šค ์ œ๊ณต์—…์ฒด(MSP)๋ฅผ ์•ก์„ธ์Šค ํ†ตํ•ฉ์—…์ฒด๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์œ„์˜ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์„œ๋น„์Šค๋ฅผ ์ „ํ†ต์ ์ธ ๋ฉ”์ปค๋‹ˆ์ฆ˜๊ณผ ๋น„์ฆˆ๋‹ˆ์Šค ๋ชจ๋ธ์—์„œ ๋ถ„๋ฆฌํ•˜์—ฌ '์„œ๋น„์Šค ์ œ๊ณต์—…์ฒด'์˜ ํ•ต์‹ฌ์„ ๋ฌด๋„ˆ๋œจ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์—… ์ธก๋ฉด์—์„œ ์—‰๋šฑํ•ด๋ณด์ธ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋Œ€๋ถ€๋ถ„์˜ ๊ธฐ์—…์ด ๋„คํŠธ์›Œํฌ ์„œ๋น„์Šค๊ฐ€ 5๋…„ ๋‚ด์— ์ง€๊ธˆ๊ณผ๋Š” ๋‹ค๋ฅธ ๋ชจ์Šต์ด ๋  ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ–ˆ๋‹ค. ์—‰๋šฑํ•˜๋‹ค๊ณ  ํ•˜์ง€๋งŒ ์‚ฌ์‹ค์€ ํ˜„์‹ค์ ์ธ ๋ณ€ํ™”์ผ์ง€๋„ ๋ชจ๋ฅธ๋‹ค.

 

 

ํ•ต์‹ฌ ์šฉ์–ด
  • VPN(Virtual Private Network, ๊ฐ€์ƒ ์‚ฌ์„ค๋ง) ์ด๋ž€?
    • ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์„ค๋ง์— ์—ฐ๊ฒฐ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ์ธํ„ฐ๋„ท์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ธํ„ฐ๋„ท ๋ณด์•ˆ ์„œ๋น„์Šค
    • ์ธํ„ฐ๋„ท ํ†ต์‹ ์„ ์•”ํ˜ธํ™”ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ•๋ ฅํ•œ ์ต๋ช…์„ฑ์„ ์ œ๊ณต
    • ๊ณต์šฉ WiFi์—์„œ ์Šค๋ˆ„ํ•‘์„ ๋ฐฉ์ง€ํ•˜๊ณ  ์ธํ„ฐ๋„ท ๊ฒ€์—ด์„ ์šฐํšŒํ•˜๊ฑฐ๋‚˜ ์›๊ฒฉ ์ž‘์—…์„ ์œ„ํ•ด ๊ธฐ์—… ๋‚ด๋ถ€ ๋„คํŠธ์›Œํฌ์— ์—ฐ๊ฒฐ
  • ํด๋ผ์šฐ๋“œ(Cloud) ๋ž€?
    • ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ƒํ™”๋œ ์„œ๋ฒ„, ์„œ๋ฒ„์—์„œ ์ž‘๋™ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” IT ํ™˜๊ฒฝ
  • ๋„คํŠธ์›Œํฌ ์Šฌ๋ผ์ด์‹ฑ(Network Slicing) ์ด๋ž€?
    • 5G์˜ ํ•ต์‹ฌ ๊ธฐ์ˆ ๋กœ ๋„คํŠธ์›Œํฌ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋‚˜๋ˆ„๋Š” ๊ธฐ์ˆ 
    • ๋ฌผ๋ฆฌ์ ์ธ ํ•˜๋‚˜์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ฐ€์ƒํ™”ํ•ด ๋ถ„ํ• ํ•˜์—ฌ ๋‹ค์ˆ˜์˜ ๋„คํŠธ์›Œํฌ์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • ๋™์ผ ๋„คํŠธ์›Œํฌ์—์„œ ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค๋“ค์ด ๋…๋ฆฝ์ ์œผ๋กœ ๋‹ค๋ฅธ ์„œ๋น„์Šค์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์œผ๋ฉด์„œ ํ’ˆ์งˆ ๋ณด์žฅ
  • ํŒŒ์ด๋ฒ„ ์ฑ„๋„(Fiber Channel, FC) ์ด๋ž€?
    • ๋ฐ์ดํ„ฐ ์Šคํ† ๋ฆฌ์ง€ ๋ฐ ๋ฐ์ดํ„ฐ ๋„คํŠธ์›Œํ‚น ๊ธฐ์ˆ ์˜ ๊ธฐ๋Šฅ์„ ํ†ตํ•ฉํ•˜๋Š” ๊ณ ์† ์ง๋ ฌ ์ธํ„ฐํŽ˜์ด์Šค ์ œ๊ณต
  • MSP(Managed Service Provider) ๋ž€?
    • ํด๋ผ์šฐ๋“œ ์ธํ”„๋ผ ๊ด€๋ฆฌ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—…์ฒด

โ€ป Reference โ€ป

https://www.cloudflare.com/ko-kr/learning/access-management/what-is-a-vpn/ (VPN์˜ ๊ฐœ๋… / ์ž‘๋™๋ฐฉ์‹ / ์žฅ๋‹จ์ )

https://www.itworld.co.kr/news/313556 (VPN ๊ด€๋ จ ๊ธฐ์‚ฌ)

https://rb.gy/dhme7a (ํด๋ผ์šฐ๋“œ์˜ ๊ฐœ๋… / ์œ ํ˜• / ์žฅ๋‹จ์ ) 

https://www.cloudflare.com/ko-kr/learning/cloud/what-is-a-virtual-private-cloud/ (๊ฐ€์ƒ ํ”„๋ผ์ด๋น— ํด๋ผ์šฐ๋“œ=VPC์˜ ๊ฐœ๋…)

https://www.kukinews.com/newsView/kuk202004220223 (๋„คํŠธ์›Œํฌ ์Šฌ๋ผ์ด์‹ฑ ๊ด€๋ จ ๊ธฐ์‚ฌ)

http://www.ktword.co.kr/test/view/view.php?m_temp1=2645 (ํŒŒ์ด๋ฒ„ ์ฑ„๋„์˜ ๊ฐœ๋… ๋ฐ ํŠน์ง•) 

https://library.gabia.com/contents/infrahosting/11317/ (MSP์˜ ๊ฐœ๋… / ์—ญํ•  / CSP์™€ ๋น„๊ต)


 

์ผ๋‹จ ๋ชจ๋ฅด๋Š” ์šฉ์–ด๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ์ฝ๊ธฐ๊ฐ€ ์ข€ ์–ด๋ ค์šด ๊ธฐ์‚ฌ์˜€๋‹ค. ๋„คํŠธ์›Œํฌ ๊ณต๋ถ€์˜ ํ•„์š”์„ฑ์„ ๋Š๋ผ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ฐ ์šฉ์–ด์˜ ๊ฐœ๋…์„ ์ฐพ์•„๋ณด๋ฉด์„œ ์–ด๋Š ์ •๋„ ์ดํ•ดํ•˜๊ธฐ๋Š” ํ–ˆ์ง€๋งŒ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๋ ค๋ฉด ๋„คํŠธ์›Œํฌ ๊ณต๋ถ€๋ฅผ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค...โœ๏ธโœ๏ธ

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๊ฐœ ์ˆซ์ž ๋‚ด์—์„œ ๋Œ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์˜€๋‹ค. ํ†ต๊ณผ๋Š” ํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ ๊ฐ™์ง€๋Š” ์•Š๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋„ ๊ณ ๋ คํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฒŒ ์ค‘์š”ํ•œ๋ฐ....์–ด๋ ต๊ตฌ๋งŒ...

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋‚˜๋งŒ์˜ ์นด์นด์˜ค ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ์ง€๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ 4๊ฐœ ์ง€ํ‘œ๋กœ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์„ฑ๊ฒฉ์€ ๊ฐ ์ง€ํ‘œ์—์„œ ๋‘ ์œ ํ˜• ์ค‘ ํ•˜๋‚˜๋กœ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

์ง€ํ‘œ ๋ฒˆํ˜ธ ์„ฑ๊ฒฉ ์œ ํ˜•
1๋ฒˆ ์ง€ํ‘œ ๋ผ์ด์–ธํ˜•(R), ํŠœ๋ธŒํ˜•(T)
2๋ฒˆ ์ง€ํ‘œ ์ฝ˜ํ˜•(C), ํ”„๋กœ๋„ํ˜•(F)
3๋ฒˆ ์ง€ํ‘œ ์ œ์ด์ง€ํ˜•(J), ๋ฌด์ง€ํ˜•(M)
4๋ฒˆ ์ง€ํ‘œ ์–ดํ”ผ์น˜ํ˜•(A), ๋„ค์˜คํ˜•(N)

 

4๊ฐœ์˜ ์ง€ํ‘œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์„ฑ๊ฒฉ ์œ ํ˜•์€ ์ด 16(=2 x 2 x 2 x 2)๊ฐ€์ง€๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "RFMN"์ด๋‚˜ "TCMA"์™€ ๊ฐ™์€ ์„ฑ๊ฒฉ ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฒ€์‚ฌ์ง€์—๋Š” ์ด n๊ฐœ์˜ ์งˆ๋ฌธ์ด ์žˆ๊ณ , ๊ฐ ์งˆ๋ฌธ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ 7๊ฐœ์˜ ์„ ํƒ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋งค์šฐ ๋น„๋™์˜
  • ๋น„๋™์˜
  • ์•ฝ๊ฐ„ ๋น„๋™์˜
  • ๋ชจ๋ฅด๊ฒ ์Œ
  • ์•ฝ๊ฐ„ ๋™์˜
  • ๋™์˜
  • ๋งค์šฐ ๋™์˜

๊ฐ ์งˆ๋ฌธ์€ 1๊ฐ€์ง€ ์ง€ํ‘œ๋กœ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๋ฅผ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์–ด๋–ค ํ•œ ์งˆ๋ฌธ์—์„œ 4๋ฒˆ ์ง€ํ‘œ๋กœ ์•„๋ž˜ ํ‘œ์ฒ˜๋Ÿผ ์ ์ˆ˜๋ฅผ ๋งค๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์„ ํƒ์ง€ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜
๋งค์šฐ ๋น„๋™์˜ ๋„ค์˜คํ˜• 3์ 
๋น„๋™์˜ ๋„ค์˜คํ˜• 2์ 
์•ฝ๊ฐ„ ๋น„๋™์˜ ๋„ค์˜คํ˜• 1์ 
๋ชจ๋ฅด๊ฒ ์Œ ์–ด๋–ค ์„ฑ๊ฒฉ ์œ ํ˜•๋„ ์ ์ˆ˜๋ฅผ ์–ป์ง€ ์•Š์Šต๋‹ˆ๋‹ค
์•ฝ๊ฐ„ ๋™์˜ ์–ดํ”ผ์น˜ํ˜• 1์ 
๋™์˜ ์–ดํ”ผ์น˜ํ˜• 2์ 
๋งค์šฐ ๋™์˜ ์–ดํ”ผ์น˜ํ˜• 3์ 

 

์ด๋•Œ ๊ฒ€์‚ฌ์ž๊ฐ€ ์งˆ๋ฌธ์—์„œ ์•ฝ๊ฐ„ ๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ ์–ดํ”ผ์น˜ํ˜•(A) ์„ฑ๊ฒฉ ์œ ํ˜• 1์ ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฒ€์‚ฌ์ž๊ฐ€ ๋งค์šฐ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•  ๊ฒฝ์šฐ ๋„ค์˜คํ˜•(N) ์„ฑ๊ฒฉ ์œ ํ˜• 3์ ์„ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋„ค์˜คํ˜•์ด ๋น„๋™์˜, ์–ดํ”ผ์น˜ํ˜•์ด ๋™์˜์ธ ๊ฒฝ์šฐ๋งŒ ์ฃผ์–ด์ง€์ง€ ์•Š๊ณ , ์งˆ๋ฌธ์— ๋”ฐ๋ผ ๋„ค์˜คํ˜•์ด ๋™์˜, ์–ดํ”ผ์น˜ํ˜•์ด ๋น„๋™์˜์ธ ๊ฒฝ์šฐ๋„ ์ฃผ์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๊ฐ ์„ ํƒ์ง€๋Š” ๊ณ ์ •์ ์ธ ํฌ๊ธฐ์˜ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋งค์šฐ ๋™์˜๋‚˜ ๋งค์šฐ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด 3์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
๋™์˜๋‚˜ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด 2์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
์•ฝ๊ฐ„ ๋™์˜๋‚˜ ์•ฝ๊ฐ„ ๋น„๋™์˜ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด 1์ ์„ ์–ป์Šต๋‹ˆ๋‹ค.
๋ชจ๋ฅด๊ฒ ์Œ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด ์ ์ˆ˜๋ฅผ ์–ป์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๊ฒ€์‚ฌ ๊ฒฐ๊ณผ๋Š” ๋ชจ๋“  ์งˆ๋ฌธ์˜ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๋ฅผ ๋”ํ•˜์—ฌ ๊ฐ ์ง€ํ‘œ์—์„œ ๋” ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์„ฑ๊ฒฉ ์œ ํ˜•์ด ๊ฒ€์‚ฌ์ž์˜ ์„ฑ๊ฒฉ ์œ ํ˜•์ด๋ผ๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ํ•˜๋‚˜์˜ ์ง€ํ‘œ์—์„œ ๊ฐ ์„ฑ๊ฒฉ ์œ ํ˜• ์ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด, ๋‘ ์„ฑ๊ฒฉ ์œ ํ˜• ์ค‘ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๋น ๋ฅธ ์„ฑ๊ฒฉ ์œ ํ˜•์„ ๊ฒ€์‚ฌ์ž์˜ ์„ฑ๊ฒฉ ์œ ํ˜•์ด๋ผ๊ณ  ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

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

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ survey์˜ ๊ธธ์ด ( = n) ≤ 1,000
  • survey์˜ ์›์†Œ๋Š” "RT", "TR", "FC", "CF", "MJ", "JM", "AN", "NA" ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • survey[i]์˜ ์ฒซ ๋ฒˆ์งธ ์บ๋ฆญํ„ฐ๋Š” i+1๋ฒˆ ์งˆ๋ฌธ์˜ ๋น„๋™์˜ ๊ด€๋ จ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • survey[i]์˜ ๋‘ ๋ฒˆ์งธ ์บ๋ฆญํ„ฐ๋Š” i+1๋ฒˆ ์งˆ๋ฌธ์˜ ๋™์˜ ๊ด€๋ จ ์„ ํƒ์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฐ›๋Š” ์„ฑ๊ฒฉ ์œ ํ˜•์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • choices์˜ ๊ธธ์ด = survey์˜ ๊ธธ์ด
  • choices[i]๋Š” ๊ฒ€์‚ฌ์ž๊ฐ€ ์„ ํƒํ•œ i+1๋ฒˆ์งธ ์งˆ๋ฌธ์˜ ์„ ํƒ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • 1 ≤ choices์˜ ์›์†Œ ≤ 7
choices ๋œป
1 ๋งค์šฐ ๋น„๋™์˜
2 ๋น„๋™์˜
3 ์•ฝ๊ฐ„ ๋น„๋™์˜
4 ๋ชจ๋ฅด๊ฒ ์Œ
5 ์•ฝ๊ฐ„ ๋™์˜
6 ๋™์˜
7 ๋งค์šฐ ๋™์˜

 

 

์ž…์ถœ๋ ฅ ์˜ˆ

survey choices result
["AN", "CF", "MJ", "RT", "NA"] [5, 3, 2, 7, 5] "TCMA"
["TR", "RT", "TR"] [7, 1, 3] "RCJA"

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(survey, choices):
    answer = ''

    types = [["R","T"],["C","F"],["J","M"],["A","N"]]
    scores = [[0,0],[0,0],[0,0],[0,0]]

    # survey ๊ธฐ๋ฐ˜ ์ ์ˆ˜ ๊ณ„์‚ฐ
    n = len(survey)
    for idx in range(n):
        front = survey[idx][0]
        back = survey[idx][1]
        if "R" in survey[idx]:
            if choices[idx] < 4:
                scores[0][types[0].index(front)] += (4-choices[idx])
            else:
                scores[0][types[0].index(back)] += (choices[idx]-4)
        elif "C" in survey[idx]:
            if choices[idx] < 4:
                scores[1][types[1].index(front)] += (4-choices[idx])
            else:
                scores[1][types[1].index(back)] += (choices[idx]-4)
        elif "J" in survey[idx]:
            if choices[idx] < 4:
                scores[2][types[2].index(front)] += (4-choices[idx])
            else:
                scores[2][types[2].index(back)] += (choices[idx]-4)
        elif "A" in survey[idx]:
            if choices[idx] < 4:
                scores[3][types[3].index(front)] += (4-choices[idx])
            else:
                scores[3][types[3].index(back)] += (choices[idx]-4)          



    # scores๋ฅผ ๋ณด๊ณ  ์ ์ˆ˜๊ฐ€ ๋†’์€ ์œ ํ˜• ์ถœ๋ ฅ 
    for idx in range(4):
        if scores[idx][0] >= scores[idx][1]:
            answer += types[idx][0]
        else:
            answer += types[idx][1]

    return answer

 

ํ’€์ด # 1

  • ๊ฐ ํƒ€์ž…๊ณผ ์ ์ˆ˜๋ฅผ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์ƒ์„ฑ
  • survey ๊ธธ์ด๋งŒํผ ์ธ๋ฑ์Šค๋ฅผ ๋Œ๋ฉด์„œ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
    • survey์—์„œ ์•ž๊ธ€์ž์™€ ๋’ท๊ธ€์ž๋ฅผ ๊ตฌ๋ถ„
    • ์–ด๋–ค ๊ธ€์ž๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  socres์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
    • ์ ์ˆ˜๊ฐ€ 4๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— 4๋ณด๋‹ค ํฌ๋ฉด ๋’ท๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
  • ์•ž์—์„œ ๊ณ„์‚ฐํ•œ scores๋ฅผ ๋Œ๋ฉด์„œ ์ ์ˆ˜๊ฐ€ ๋†’์€ ๊ฐ’์˜ type์„ answer๋กœ ์ถœ๋ ฅ

 

# 2
def solution(survey, choices):
    answer = ''

    scores = {"A":0, "N":0, "C":0, "F":0, "M":0, "J":0, "R":0, "T":0}
    for idx, choice in enumerate(choices):
        if choice - 4 > 0:
            scores[survey[idx][1]] += choice - 4
        elif choice - 4 < 0:
            scores[survey[idx][0]] += 4 - choice

    if scores["R"] >= scores["T"]:
        answer += "R"
    else:
        answer += "T"

    if scores["C"] >= scores["F"]:
        answer += "C"
    else:
        answer += "F"

    if scores["J"] >= scores["M"]:
        answer += "J"
    else:
        answer += "M"

    if scores["A"] >= scores["N"]:
        answer += "A"
    else:
        answer += "N"
    

    return answer

 

ํ’€์ด #2

  • ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์— ์œ ํ˜• ๊ฐ’์— ์ ์ˆ˜๋ฅผ ํ‘œ๊ธฐ
  • ์ ์ˆ˜๊ฐ€ 4๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— 4๋ณด๋‹ค ํฌ๋ฉด ๋’ท๊ธ€์ž์˜ ์ธ๋ฑ์Šค์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
  • RT, CF, JM, AN์˜ ์ ์ˆ˜๋ฅผ ๊ฐ๊ฐ ๋น„๊ตํ•ด์„œ answer๋กœ ์ถœ๋ ฅ

 

# 3
def solution(survey, choices):
    types = {"RT":0,"CF":0,"JM":0,"AN":0}
    
    for A,B in zip(survey,choices):
        if A not in types.keys():
            A = A[::-1]
            types[A] -= B-4
        else:
            types[A] += B-4

    answer = ''    
    for name in types.keys():
        if types[name] > 0:
            answer += name[1]
        elif types[name] < 0:
            answer += name[0]
        else:
            answer += sorted(name)[0]

    return answer

 

ํ’€์ด #3

  • ์ง์ด ๋˜๋Š” ์œ ํ˜•๋ผ๋ฆฌ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋กœ ๊ตฌ์„ฑ
  • survey์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์— ํ‚ค๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๊ณ  ์ ์ˆ˜ ๋นผ๊ธฐ (์ˆœ์„œ ๋ณ€๊ฒฝ O)
    • ์ ์ˆ˜๊ฐ€ ๋†’์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฏ€๋กœ ์–‘์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋นผ๊ธฐ
    • ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์Œ์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
  • survey์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค์ธ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ ์ ์ˆ˜ ๋”ํ•˜๊ธฐ (์ˆœ์„œ ๋ณ€๊ฒฝ X)
    • ์ ์ˆ˜๊ฐ€ ๋†’์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฏ€๋กœ ์–‘์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋”ํ•˜๊ธฐ
    • ์ ์ˆ˜๊ฐ€ ๋‚ฎ์œผ๋ฉด (์ ์ˆ˜-4)๊ฐ€ 0๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์Œ์ˆ˜ = ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— ์ ์ˆ˜ ๋นผ๊ธฐ
  • ์œ„์˜ ๋ฐฉ์‹๋Œ€๋กœ ์ ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ  ๋นผ๋ฉด ์ ์ˆ˜๋Š” ๊ฒฐ๊ตญ ํ‚ค์˜ ๋’ท ๋ฌธ์ž ๊ธฐ๋ฐ˜
    • ์ ์ˆ˜๊ฐ€ 0๋ณด๋‹ค ํฌ๋ฉด ๋’ท ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  0๋ณด๋‹ค ์ž‘์€๋ฉด ์•ž ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅ

 

 

๋ฌธ์ œ์— ๋Œ€ํ•ด ์„ค๋ช…์ด ์ž์„ธํ•ด์„œ ์ดํ•ด๋Š” ์ž˜ ๋˜์—ˆ๋Š”๋ฐ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋‹ˆ๊นŒ ์ข€ ๋ณต์žกํ•ด์กŒ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋Š” ์ ์ˆ˜๊ฐ€ 4๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž์— ์žˆ๋Š” ๋ฌธ์ž์— +3, 4๋ณด๋‹ค ํฌ๋ฉด ๋’ค์— ์žˆ๋Š” ๋ฌธ์ž์— +3์ด ๋˜๋Š” ๊ฒƒ์„ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ’€์ด ๊ธฐ๋ฐ˜์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ๋‘ ๋ฒˆ์งธ ํ’€์ด๊นŒ์ง€ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๋‹ค ๋ณด๋‹ˆ ์„ธ ๋ฒˆ์งธ ํ’€์ด๊ฐ€ ์ง„์งœ ์”ฝํฌ๋น…์ด์–ด์„œ ์ถ”๊ฐ€ํ–ˆ๋‹ค....๋ฌธ์ž์—ด์„ ๋ฐ”๊พธ๋ฉด์„œ ์ ์ˆ˜ ๋”ํ•˜๊ธฐ? ๋„๋Œ€์ฒด ์ด๋Ÿฐ ์ƒ๊ฐ์€ ์–ด๋–ป๊ฒŒ ํ•˜์‹œ๋Š”๊ฑฐ์—์š”? ํ•ด์„ํ•˜๊ธฐ๋„ ์–ด๋ ค์› ๋Š”๋ฐ...์ฒœ์žฌ ์•„๋‹ˆ์‹ ๊ฐ€์š”...? 

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

๋จธ์“ฑ์ด๋Š” ๊ตฌ์Šฌ์„ ์นœ๊ตฌ๋“ค์—๊ฒŒ ๋‚˜๋ˆ„์–ด์ฃผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ์Šฌ์€ ๋ชจ๋‘ ๋‹ค๋ฅด๊ฒŒ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ๋จธ์“ฑ์ด๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ตฌ์Šฌ์˜ ๊ฐœ์ˆ˜ balls์™€ ์นœ๊ตฌ๋“ค์—๊ฒŒ ๋‚˜๋ˆ„์–ด ์ค„ ๊ตฌ์Šฌ ๊ฐœ์ˆ˜ share์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, balls๊ฐœ์˜ ๊ตฌ์Šฌ ์ค‘ share๊ฐœ์˜ ๊ตฌ์Šฌ์„ ๊ณ ๋ฅด๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ balls ≤ 30
  • 1 ≤ share ≤ 30
  • ๊ตฌ์Šฌ์„ ๊ณ ๋ฅด๋Š” ์ˆœ์„œ๋Š” ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • share ≤ balls

 

์ž…์ถœ๋ ฅ ์˜ˆ

balls share result
3 2 3
5 3 10

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
def solution(balls, share):
    answer = 1
    
    ball_list = list(range(balls,1,-1))
    share_list = list(range(1,share+1))
    
    for idx in range(1,share+1):
        answer *= ball_list[idx-1]
        answer = int(answer/share_list[idx-1])
    
    
    return answer

 

ํ’€์ด # 1

  • ๊ณต์˜ ๊ฐฏ์ˆ˜์—์„œ 1์”ฉ ๋นผ์„œ ball_list์— ๋‹ด๊ณ , ๊ณต์œ ํ•  ๊ณต์˜ ๊ฐฏ์ˆ˜๋ฅผ 1๋ถ€ํ„ฐ 1์”ฉ ๋”ํ•ด share_list์— ๋‹ด๊ธฐ
  • share ์ˆซ์ž ๋ฒ”์œ„์—์„œ ball_list์˜ ์›์†Œ๋Š” ๊ณฑํ•˜๊ณ  share_list์˜ ์›์†Œ๋Š” ๋‚˜๋ˆ„๊ธฐ

 

# 2
def solution(balls, share):
    answer = 1
    cnt = 0

    while cnt < share:
        answer *= (balls-cnt)
        answer = int(answer/(cnt+1))
        cnt += 1    
    
    return answer

 

ํ’€์ด #2

  • cnt๊ฐ€ share์˜ ์ˆซ์ž๋ณด๋‹ค ์ž‘์„๋™์•ˆ ๊ณฑํ•˜๊ธฐ์™€ ๋‚˜๋ˆ„๊ธฐ๋ฅผ ์‹คํ–‰
    • balls-cnt๋ฅผ ๊ณฑํ•˜๊ธฐ
    • ์œ„์˜ ์ˆซ์ž์—์„œ cnt+1์„ ๋‚˜๋ˆ„๊ธฐ 

 

# 3
import math

def solution(balls, share):
    return math.comb(balls, share)

 

ํ’€์ด #3

  • math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ combination ํ•จ์ˆ˜ ์‚ฌ์šฉ

 

 

์ฒ˜์Œ์— ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๊ณ  ๋‚˜๋ˆ„๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํ•˜๋‹ค๊ฐ€ ์ž˜ ์•ˆ๋˜๊ธธ๋ž˜ ๋ฆฌ์ŠคํŠธ๋กœ ํ’€์–ด๋ดค๋Š”๋ฐ ์—ญ์‹œ๋‚˜ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋‹ค. ๊ทธ๋ž˜์„œ ์ˆซ์ž๋ฅผ ์กฐ์ž‘ํ•ด์„œ while๋ฌธ์— ๋„ฃ์—ˆ๋”๋‹ˆ ๊ฒฐ๊ณผ๋Š” ๋‚˜์™”์ง€๋งŒ ๋ญ”๊ฐ€ ๋” ๋น ๋ฅด๊ฒŒ ๋‚˜์™”์œผ๋ฉด ์ข‹๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ค๋ณด๋‹ˆ math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— combination ํ•จ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋‹ค...๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์“ฐ๋Š”๊ฑฐ ์ตœ๊ณ ...!!

๊ตฌ๊ธ€, AI ์–ธ์–ด๋ชจ๋ธ '์ œ๋ฏธ๋‚˜์ด' ๊ณต๊ฐœ…"GPT-4·์‚ฌ๋žŒ ๋Šฅ๋ ฅ ๋„˜์–ด" (๋งํฌ)

 

๊ธฐ์‚ฌ ๋‚ด์šฉ ์š”์•ฝ
๊ตฌ๊ธ€์ด ์˜คํ”ˆ AI ์ฑ—GPT์˜ LLM์ธ 'GPT'์™€ ๊ฐ™์€ AI ๋ชจ๋ธ์ธ '์ œ๋ฏธ๋‚˜์ด'(Gemini)๋ฅผ ๊ณต๊ฐœํ–ˆ๋‹ค. ์ด๋ฏธ์ง€๋ฅผ ์ธ์‹ํ•˜๊ณ  ์Œ์„ฑ์œผ๋กœ ๋งํ•˜๊ฑฐ๋‚˜ ๋“ค์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ฝ”๋”ฉ ๋Šฅ๋ ฅ๊นŒ์ง€ ๊ฐ–์ถ˜ '๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI'๋กœ ๋งŒ๋“ค์–ด์กŒ๋‹ค. ๋˜ํ•œ ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ํ’€๊ฑฐ๋‚˜ ๋ถ„์„ํ•˜๋Š” ์ถ”๋ก  ๋Šฅ๋ ฅ๋„ ๊ฐ–์ถ”์—ˆ๋‹ค.
๋จธ์‹ ๋Ÿฌ๋‹ ๊ทœ๋ชจ์— ๋”ฐ๋ผ ์šธํŠธ๋ผ(Ultra), ํ”„๋กœ(Pro), ๋‚˜๋…ธ(Nano) 3๊ฐœ์˜ ๋ชจ๋ธ๋กœ ์ถœ์‹œ๋œ๋‹ค. ํ”„๋กœ๋Š” ๊ตฌ๊ธ€ '๋ฐ”๋“œ'์— ํƒ‘์žฌ๋˜์—ˆ๊ณ  ์šธํŠธ๋ผ๋Š” ๋‚ด๋…„ ์ดˆ ์žฅ์ฐฉ๋  ์˜ˆ์ •์ด๋‹ค. ๋‚˜๋…ธ๋Š” ์˜จ๋””๋ฐ”์ด์Šค ํ˜•ํƒœ๋กœ ์Šค๋งˆํŠธํฐ์— ํƒ‘์žฌ๋œ๋‹ค.
์ œ๋ฏธ๋‚˜์ด๋Š” ๊ตฌ๊ธ€์ด ์ž์ฒด ๊ฐœ๋ฐœํ•œ AI์นฉ(TPU v4, v5e)์œผ๋กœ ํ•™์Šตํ–ˆ๋‹ค. ๊ตฌ๊ธ€์€ ์ตœ์ฒจ๋‹จ AI ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ตœ์‹  ์นฉ(ํด๋ผ์šฐ๋“œ TPU v5p)๋„ ๊ณต๊ฐœํ•˜๋ฉฐ ๊ฐœ๋ฐœ์„ ๊ฐ€์†ํ™”ํ•  ์˜ˆ์ •์ด๋‹ค.

 

 

ํ•ต์‹ฌ ์šฉ์–ด
  • ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI๋ž€?
    • ํ…์ŠคํŠธ, ์ด๋ฏธ์ง€, ์Œ์„ฑ ๋“ฑ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” AI ๊ธฐ์ˆ 
  • ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI์™€ ๊ธฐ์กด AI์˜ ์ฐจ์ด์ 
    • ๊ธฐ์กด AI
      • ํ…์ŠคํŠธ๋‚˜ ์ž์—ฐ์–ด๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ ์ค‘์ 
      • ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋‚˜ ํ†ต๊ณ„, ํ…์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์ง€๋งŒ ์ธ๊ฐ„๊ณผ ๊ฐ™์€ ์‚ฌ๊ณ ๋Š” ๋ถˆ๊ฐ€๋Šฅ
    • ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI
      • ํ…์ŠคํŠธ, ์Œ์„ฑ, ์ด๋ฏธ์ง€, ์˜์ƒ ๋“ฑ ์„œ๋กœ ๋‹ค๋ฅธ ์–‘์‹์˜ ๋ฐ์ดํ„ฐ ๊ฐ„ ๊ด€๊ณ„์„ฑ์„ ํ•™์Šตํ•˜๊ณ  ์ดํ•ดํ•˜๋ฉฐ ํ‘œํ˜„
      • OpenAI์˜ ๋‹ฌ๋ฆฌ(DALL-E2)๋Š” 'AI ์‹œ์Šคํ…œ์ด ์ธ๋ฅ˜๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณด๊ณ  ์ดํ•ดํ•˜๋Š”์ง€ ๋„์›€๋˜๊ณ ์ž' ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI ํ™œ์šฉ

โ€ป Reference โ€ป

https://www.samsungsds.com/kr/insights/multi-modal-ai.html (๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI ๊ฐœ๋…)

https://weekly.chosun.com/news/articleView.html?idxno=28553 (๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI ๊ด€๋ จ ๊ธฐ์‚ฌ)

https://www.aitimes.com/news/articleView.html?idxno=154737 (๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI ๊ด€๋ จ ๊ธฐ์‚ฌ)

 


 

AI ๊ธฐ์ˆ ์ด ๋ฐœ์ „ํ•˜๋ฉด์„œ ์„ฑ๋Šฅ์ด ๋” ์ข‹์€ ๋ชจ๋ธ์„ ์ถœ์‹œํ•˜๊ธฐ ์œ„ํ•œ ๊ฒฝ์Ÿ์ด ๊ฐ€์†ํ™”๋˜๊ณ  ์žˆ๋‹ค. ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด๋Š” ๋ชจ๋ธ์€ ๋‹จ์ˆœํžˆ ๋‹ต๋งŒ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ๋„˜์–ด ์„ธ์ƒ์„ ์ดํ•ดํ•˜๊ณ  ๋‹ต์„ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค. '์ œ๋ฏธ๋‚˜์ด'(Gemini) ์ถœ์‹œ๋กœ ์–ด๋–ค ๋‹ค๋ฅธ ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ AI๊ฐ€ ๋“ฑ์žฅํ• ์ง€ ๊ธฐ๋Œ€๋œ๋‹ค.

 

 

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์„ ๋ถ„ 3๊ฐœ๊ฐ€ ํ‰ํ–‰ํ•˜๊ฒŒ ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ ์„ ๋ถ„์˜ ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ๊ฐ€ [[start, end], [start, end], [start, end]] ํ˜•ํƒœ๋กœ ๋“ค์–ด์žˆ๋Š” 2์ฐจ์› ๋ฐฐ์—ด lines๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋‘ ๊ฐœ ์ด์ƒ์˜ ์„ ๋ถ„์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

lines๊ฐ€ [[0, 2], [-3, -1], [-2, 1]]์ผ ๋•Œ ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์„ ๋ถ„์ด ๋‘ ๊ฐœ ์ด์ƒ ๊ฒน์นœ ๊ณณ์€ [-2, -1], [0, 1]๋กœ ๊ธธ์ด 2๋งŒํผ ๊ฒน์ณ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • lines์˜ ๊ธธ์ด = 3
  • lines์˜ ์›์†Œ์˜ ๊ธธ์ด = 2
  • ๋ชจ๋“  ์„ ๋ถ„์€ ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ์ž…๋‹ˆ๋‹ค.
  • lines์˜ ์›์†Œ๋Š” [a, b] ํ˜•ํƒœ์ด๋ฉฐ, a, b๋Š” ๊ฐ๊ฐ ์„ ๋ถ„์˜ ์–‘ ๋์  ์ž…๋‹ˆ๋‹ค.
  • -100 ≤ a < b ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

lines result
[[0, 1], [2, 5], [3, 9]] 2
[[-1, 1], [1, 3], [3, 9]] 0
[[0, 5], [3, 9], [1, 10]] 8

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(lines):
    answer = 0
    count = [0]*200
    
    starts = []
    ends = []

    for line in lines:
        start, end = line

        starts.append(start)
        ends.append(end)

        for num in range(start, end):
            count[num+100] += 1

    for cnt in count[100+min(starts):100+max(ends)+1]:
        if cnt > 1:
            answer += 1

    return answer

 

ํ’€์ด # 1

  • ์›์†Œ๊ฐ€ 0์ธ ๊ธธ์ด 200์˜ ๋ฆฌ์ŠคํŠธ count ์ƒ์„ฑ
    • ์„ ๋ถ„์ด ์  (-100,100) ๋ฒ”์œ„์— ์žˆ์œผ๋ฏ€๋กœ ๊ธธ์ด๊ฐ€ 200
  • ๊ฐ ์„ ๋ถ„์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ขŒํ‘œ๋ฅผ ๋‹ด์„ starts / ends ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • linelist ๋Œ๊ธฐ
    • ๊ฐ ์„ ๋ถ„์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ขŒํ‘œ ์ถ”๊ฐ€
    • ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ ์‚ฌ์ด ์ˆซ์ž์— +1 (count ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„๊ฐ€ -100~100์ด๋ฏ€๋กœ ์ธ๋ฑ์Šค์— +100)
  • ์‹œ์ž‘ ์ขŒํ‘œ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜์™€ ๋ ์ขŒํ‘œ ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋ฒ”์œ„์—์„œ count๊ฐ€ 2๋ณด๋‹ค ํฌ๋ฉด ์„ ๋ถ„์ด ๊ฒน์น˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ +1

 

# 2
def solution(lines):
    s1 = set(i for i in range(lines[0][0], lines[0][1]))
    s2 = set(i for i in range(lines[1][0], lines[1][1]))
    s3 = set(i for i in range(lines[2][0], lines[2][1]))
    
    return len((s1 & s2) | (s2 & s3) | (s1 & s3))

 

ํ’€์ด #2

  • ๊ฐ ๋ผ์ธ์˜ ์‹œ์ž‘ ์ขŒํ‘œ๋ถ€ํ„ฐ ๋ ์ขŒํ‘œ๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์„ธํŠธ๋กœ ์ƒ์„ฑ (s1 / s2 / s3)
  • ์„ ๋ถ„๋ผ๋ฆฌ์˜ ๊ต์ง‘ํ•ฉ(&)๋“ค์˜ ํ•ฉ์ง‘ํ•ฉ(|) ๊ธธ์ด๋ฅผ ์ถœ๋ ฅ

 

 

(-100,100) ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฑธ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค๊ณ  ๋‚˜๋‹ˆ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๊ตณ์ด ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ์˜ min/max๋Š” ๊ตฌํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์„ ๊ฑฐ ๊ฐ™๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด์ฒ˜๋Ÿผ ์„ธํŠธ์—์„œ ํ•ฉ์ง‘ํ•ฉ๊ณผ ๊ต์ง‘ํ•ฉ์„ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ธ๋ฐ...๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋‚ด๋Š” ๊ฒŒ ํ•ญ์ƒ ์–ด๋ ต๋‹ค...

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ •์ˆ˜ n์„ ๊ธฐ์ค€์œผ๋กœ n๊ณผ ๊ฐ€๊นŒ์šด ์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ n์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋” ํฐ ์ˆ˜๋ฅผ ์•ž์— ์˜ค๋„๋ก ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numlist์™€ ์ •์ˆ˜ n์ด ์ฃผ์–ด์งˆ ๋•Œ numlist์˜ ์›์†Œ๋ฅผ n์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ€๊นŒ์šด ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ n ≤ 10,000
  • 1 ≤ numlist์˜ ์›์†Œ ≤ 10,000
  • 1 ≤ numlist์˜ ๊ธธ์ด ≤ 100
  • numlist๋Š” ์ค‘๋ณต๋œ ์›์†Œ๋ฅผ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

numlist n result
[1, 2, 3, 4, 5, 6] 4 [4, 5, 3, 6, 2, 1]
[10000,20,36,47,40,6,10,7000] 30 [36, 40, 20, 47, 10, 6, 7000, 10000]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(numlist, n):
    # Key = ์ˆซ์ž / Value = ์ฐจ์ด
    num_diff = {}
    for num in numlist:
        num_diff[num] = abs(num-n)   

    rank = dict(sorted(num_diff.items(), key=lambda x:(x[1],-x[0])))
    
    return list(rank.keys())

 

ํ’€์ด # 1

  • num_diff ๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • Key๊ฐ’์— numlist์˜ ์ˆซ์ž / Value๊ฐ’์— numlist ์ˆซ์ž์™€ n์˜ ์ฐจ์ด ์ž…๋ ฅ
  • rank๋ผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • num_diff์˜ Value(= numlist ์ˆซ์ž์™€ n์˜ ์ฐจ์ด) ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ → x[1]
    • Value๊ฐ€ ๊ฐ™๋‹ค๋ฉด Key๊ฐ€ ํฐ ๊ฐ’ ๋จผ์ € ์˜ค๋„๋ก ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ → -x[0]
  • ์ •๋ ฌ๋œ rank ๋”•์…”๋„ˆ๋ฆฌ์˜ Key๊ฐ’(=numlist์˜ ์ˆซ์ž) ์ถœ๋ ฅ

 

# 2
def solution(numlist, n):
    numlist.sort(key=lambda x:(abs(n-x), -x))
    return numlist

 

ํ’€์ด #2

  • numlist ์ˆซ์ž์™€ n์˜ ์ฐจ์ด ์˜ค๋ฆ„์ฐจ์ˆœ / numlist ์ˆซ์ž ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
  • ์ •๋ ฌ๋œ numlist ์ถœ๋ ฅ

 

 

์ฒซ ๋ฒˆ์งธ ํ’€์ด๋Š” '์ฐจ์ด๊ฐ’ ๋งŒ๋“ค๊ธฐ → ์ฐจ์ด๊ฐ’๊ณผ ์ˆซ์ž๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์— ๋„ฃ๊ธฐ → ์ฐจ์ด๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ → ์ •๋ ฌ๋œ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ํ‚ค ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ' ์˜ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ณ  ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” '์ฐจ์ด๊ฐ’ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ'๊ฐ€ ๋์ด๋‹ค. ์›๋ฆฌ?๋Š” ๊ฐ™์ง€๋งŒ ์–ผ๋งˆ๋‚˜ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋Š”์ง€์— ๋”ฐ๋ผ์„œ ์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ๊น”๋”ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค๊ณ  3928490๋ฒˆ์งธ ๋‹ค์งํ•˜๋Š” ์ค‘์ด๋‹ค. 

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์–‘์˜ ์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. n × n ๋ฐฐ์—ด์— 1๋ถ€ํ„ฐ n^2๊นŒ์ง€ ์ •์ˆ˜๋ฅผ ์ธ๋ฑ์Šค [0][0]๋ถ€ํ„ฐ ์‹œ๊ณ„๋ฐฉํ–ฅ ๋‚˜์„ ํ˜•์œผ๋กœ ๋ฐฐ์น˜ํ•œ ์ด์ฐจ์› ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ n ≤ 30

 

์ž…์ถœ๋ ฅ ์˜ˆ

n result
4 [[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]
5 [[1, 2, 3, 4, 5], [16, 17, 18, 19, 6], [15, 24, 25, 20, 7], [14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]

 

 

์†Œ์Šค ์ฝ”๋“œ
def solution(n):
    if n == 1:
        return [[1]]

    answer = [[0 for j in range(n)] for i in range(n)]

    i = 0       # ํ–‰
    j = 0       # ์—ด
    dir = 'r'   # ๋ฐฉํ–ฅ
    
    for num in range(n*n):
        answer[i][j] = num+1
        if dir == 'r':
            j += 1
            if j == n-1 or answer[i][j+1] != 0:
                dir = 'd'
        elif dir == 'd':
            i += 1
            if i == n-1 or answer[i+1][j] != 0:
                dir = 'l'
        elif dir == 'l':
            j -= 1
            if j == 0 or answer[i][j-1] != 0:
                dir = 'u'
        else:
            i -= 1
            if i == 0 or answer[i-1][j] != 0:
                dir = 'r'            
    
    
    return answer

 

ํ’€์ด

  • 1 x 1 ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ์— [[1]]์„ ์ถœ๋ ฅ
  • ํ–‰ ๋ฒˆํ˜ธ = i / ์—ด ๋ฒˆํ˜ธ = j / ๋ฐฉํ–ฅ = dir (r:์˜ค๋ฅธ์ชฝ → d:์•„๋ž˜์ชฝ  → l:์™ผ์ชฝ → u:์œ„์ชฝ)
  • n x n ๋ฒ”์œ„๋ฅผ ๋Œ๋ฉด์„œ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์ฑ„์›Œ์ฃผ๋Š” for๋ฌธ
    • ๋ฐฉํ–ฅ์ด ์˜ค๋ฅธ์ชฝ์ธ ๊ฒฝ์šฐ : ์—ด ํ•œ ์นธ ์ด๋™ / ๋งˆ์ง€๋ง‰ ์—ด์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
    • ๋ฐฉํ–ฅ์ด ์•„๋ž˜์ชฝ์ธ ๊ฒฝ์šฐ : ํ–‰ ํ•œ ์นธ ์ด๋™ / ๋งˆ์ง€๋ง‰ ํ–‰์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
    • ๋ฐฉํ–ฅ์ด ์™ผ์ชฝ์ธ ๊ฒฝ์šฐ : ์—ด ํ•œ ์นธ ์•ž์œผ๋กœ ์ด๋™ / ์ฒซ ๋ฒˆ์งธ ์—ด์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ
    • ๋ฐฉํ–ฅ์ด ์œ„์ชฝ์ธ ๊ฒฝ์šฐ : ํ–‰ ํ•œ ์นธ ์œ„๋กœ ์ด๋™ / ์ฒซ ๋ฒˆ์งธ ํ–‰์ด๊ฑฐ๋‚˜ ์นธ์ด ์ฑ„์›Œ์ ธ ์žˆ์œผ๋ฉด ๋ฐฉํ–ฅ ๋ณ€๊ฒฝ

 

 

์ด๊ฑฐ ๋ ˆ๋ฒจ0 ๋งž์•„์š”..? ๊ทธ๋ ‡๋‹ค๋ฉด ๋‚ด ์‹ค๋ ฅ์ด ๋งˆ์ด๋„ˆ์Šค์ธ๊ฐ€๋ณด๋‹ค....์ด๋Ÿฐ ์ €๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ํ•ด๋ณด๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰ํ•ด์„œ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ์•ž์œผ๋กœ ๋” ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค...ํ”Œ๋Ÿฌ์Šค ์‹ค๋ ฅ์ด ๋  ๋•Œ๊นŒ์ง€ ํ™”์ดํŒ…..!!

 

+ Recent posts