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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์–ด๋–ค ๊ฒŒ์ž„์—๋Š” ๋ถ•๋Œ€ ๊ฐ๊ธฐ๋ผ๋Š” ๊ธฐ์ˆ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ถ•๋Œ€ ๊ฐ๊ธฐ๋Š” t์ดˆ ๋™์•ˆ ๋ถ•๋Œ€๋ฅผ ๊ฐ์œผ๋ฉด์„œ 1์ดˆ๋งˆ๋‹ค x๋งŒํผ์˜ ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. t์ดˆ ์—ฐ์†์œผ๋กœ ๋ถ•๋Œ€๋ฅผ ๊ฐ๋Š” ๋ฐ ์„ฑ๊ณตํ•œ๋‹ค๋ฉด y๋งŒํผ์˜ ์ฒด๋ ฅ์„ ์ถ”๊ฐ€๋กœ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์—๋Š” ์ตœ๋Œ€ ์ฒด๋ ฅ์ด ์กด์žฌํ•ด ํ˜„์žฌ ์ฒด๋ ฅ์ด ์ตœ๋Œ€ ์ฒด๋ ฅ๋ณด๋‹ค ์ปค์ง€๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์ˆ ์„ ์“ฐ๋Š” ๋„์ค‘ ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋ฉด ๊ธฐ์ˆ ์ด ์ทจ์†Œ๋˜๊ณ , ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋Š” ์ˆœ๊ฐ„์—๋Š” ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ๋‹นํ•ด ๊ธฐ์ˆ ์ด ์ทจ์†Œ๋‹นํ•˜๊ฑฐ๋‚˜ ๊ธฐ์ˆ ์ด ๋๋‚˜๋ฉด ๊ทธ ์ฆ‰์‹œ ๋ถ•๋Œ€ ๊ฐ๊ธฐ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์—ฐ์† ์„ฑ๊ณต ์‹œ๊ฐ„์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.

๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›์œผ๋ฉด ์ •ํ•ด์ง„ ํ”ผํ•ด๋Ÿ‰๋งŒํผ ํ˜„์žฌ ์ฒด๋ ฅ์ด ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ˜„์žฌ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜๋ฉด ์บ๋ฆญํ„ฐ๊ฐ€ ์ฃฝ์œผ๋ฉฐ ๋” ์ด์ƒ ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋‹น์‹ ์€ ๋ถ•๋Œ€๊ฐ๊ธฐ ๊ธฐ์ˆ ์˜ ์ •๋ณด, ์บ๋ฆญํ„ฐ๊ฐ€ ๊ฐ€์ง„ ์ตœ๋Œ€ ์ฒด๋ ฅ๊ณผ ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ํŒจํ„ด์ด ์ฃผ์–ด์งˆ ๋•Œ ์บ๋ฆญํ„ฐ๊ฐ€ ๋๊นŒ์ง€ ์ƒ์กดํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

๋ถ•๋Œ€ ๊ฐ๊ธฐ ๊ธฐ์ˆ ์˜ ์‹œ์ „ ์‹œ๊ฐ„, 1์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰์„ ๋‹ด์€ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด bandage์™€ ์ตœ๋Œ€ ์ฒด๋ ฅ์„ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ health, ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„๊ณผ ํ”ผํ•ด๋Ÿ‰์„ ๋‹ด์€ 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด attacks๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ณต๊ฒฉ์ด ๋๋‚œ ์งํ›„ ๋‚จ์€ ์ฒด๋ ฅ์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ๋งŒ์•ฝ ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›๊ณ  ์บ๋ฆญํ„ฐ์˜ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜์–ด ์ฃฝ๋Š”๋‹ค๋ฉด -1์„ return ํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • bandage๋Š” [์‹œ์ „ ์‹œ๊ฐ„, ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰] ํ˜•ํƒœ์˜ ๊ธธ์ด๊ฐ€ 3์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • 1 ≤ ์‹œ์ „ ์‹œ๊ฐ„ = t ≤ 50
  • 1 ≤ ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰ = x ≤ 100
  • 1 ≤ ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ = y ≤ 100
  • 1 ≤ health ≤ 1,000
  • 1 ≤ attacks์˜ ๊ธธ์ด ≤ 100
  • attacks[i]๋Š” [๊ณต๊ฒฉ ์‹œ๊ฐ„, ํ”ผํ•ด๋Ÿ‰] ํ˜•ํƒœ์˜ ๊ธธ์ด๊ฐ€ 2์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • attacks๋Š” ๊ณต๊ฒฉ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.
  • attacks์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„์€ ๋ชจ๋‘ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
  • 1 ≤ ๊ณต๊ฒฉ ์‹œ๊ฐ„ ≤ 1,000
  • 1 ≤ ํ”ผํ•ด๋Ÿ‰ ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

bandage health attacks result
[5, 1, 5] 30 [[2, 10], [9, 15], [10, 5], [11, 5]] 5
[3, 2, 7] 20 [[1, 15], [5, 16], [8, 6]] -1
[4, 2, 7] 20 [[1, 15], [5, 16], [8, 6]] -1
[1, 1, 1] 5 [[1, 2], [3, 2]] 3

 

 

 

์†Œ์Šค ์ฝ”๋“œ
def solution(bandage, health, attacks):
    t, x, y = bandage
    max = health
    success = 0

    # ๊ณต๊ฒฉ ๋‹นํ•œ ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ
    attack_times = []
    for attack in attacks:
        attack_times.append(attack[0])

    # ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ๊นŒ์ง€ ์ƒ์กด์—ฌ๋ถ€ ํ™•์ธ
    for now in range(1,attack_times[-1]+1):
        # ๊ณต๊ฒฉ ๋‹นํ•˜๋ฉด ์ฒด๋ ฅ๊ฐ์†Œ ๋ฐ ์—ฐ์†์„ฑ๊ณต ์ดˆ๊ธฐํ™”
        if now in attack_times:
            idx = attack_times.index(now)
            health -= attacks[idx][1]
            success = 0

            # ๊ณต๊ฒฉ ๋‹นํ•ด์„œ ์ฒด๋ ฅ์ด ๋ฐ”๋‹ฅ๋‚˜๋Š” ๊ฒฝ์šฐ
            if health <= 0:
                return -1
                break

        # ๊ณต๊ฒฉ ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ๋ถ•๋Œ€์Šคํ‚ฌ
        else:
            # ์ฒด๋ ฅ์ด ์ตœ๋Œ€์น˜๋ณด๋‹ค ์ž‘์œผ๋ฉด ํšŒ๋ณต / ์ตœ๋Œ€์น˜์ด๊ฑฐ๋‚˜ ํฌ๋ฉด ์ตœ๋Œ€์น˜๋กœ ์ดˆ๊ธฐํ™”
            # ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ ์—ฐ์† ์„ฑ๊ณต
            if health < max:
                health += x
            else:
                health = max

            success += 1
            if success == t:
                health += y
                success = 0    

    return health

 

ํ’€์ด

  • ๋ถ•๋Œ€๊ฐ๊ธฐ ์Šคํ‚ฌ ๋ฆฌ์ŠคํŠธ์—์„œ ์‹œ์ „ ์‹œ๊ฐ„, ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ ์„ค์ •
    • t = ์‹œ์ „ ์‹œ๊ฐ„ (๋ถ•๋Œ€๊ฐ๊ธฐ ์Šคํ‚ฌ์˜ ์ตœ๋Œ€ ์ง„ํ–‰์‹œ๊ฐ„)
    • x = ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰ (1์ดˆ๋™์•ˆ ํšŒ๋ณต๋Ÿ‰)
    • y = ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ (t์‹œ๊ฐ„๋งŒํผ ๋ถ•๋Œ€๊ฐ๊ธฐ ์Šคํ‚ฌ์„ ์„ฑ๊ณตํ•  ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํšŒ๋ณต)
  • ์ฒด๋ ฅ์˜ ์ตœ๋Œ€์น˜ max, ์—ฐ์†์„ฑ๊ณต ํšŸ์ˆ˜ succes ์„ค์ •
  • ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ๋‹นํ•œ ์‹œ๊ฐ„์„ attack_times์— ์ถ”๊ฐ€
  • ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ์‹œ๊ฐ„๊นŒ์ง€ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ณต๊ฒฉ์—ฌ๋ถ€ ํ™•์ธ
    • ํ˜„์žฌ ์‹œ๊ฐ„์ด ๊ณต๊ฒฉ๋‹นํ•œ ์‹œ๊ฐ„ attack_times์— ์žˆ์œผ๋ฉด ์ฒด๋ ฅ๊ฐ์†Œ ๋ฐ ์—ฐ์†์„ฑ๊ณต ์ดˆ๊ธฐํ™”
      • ๊ณต๊ฒฉ์„ ๋‹นํ•ด์„œ ์ฒด๋ ฅ์ด 0๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด -1์„ ๋ฆฌํ„ดํ•˜๊ณ  ์ข…๋ฃŒ
    • ๊ณต๊ฒฉ๋‹นํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ๋ถ•๋Œ€๊ฐ๊ธฐ๋กœ ์ฒด๋ ฅ ํšŒ๋ณต
      • ์ฒด๋ ฅ์ด max๋ณด๋‹ค ์ž‘์œผ๋ฉด ํšŒ๋ณตํ•˜๊ณ  max๋ณด๋‹ค ํฌ๋ฉด max๋กœ ์ดˆ๊ธฐํ™”
      • ์—ฐ์† ์„ฑ๊ณตํ–ˆ์„ ๊ฒฝ์šฐ ์ถ”๊ฐ€ ํšŒ๋ณต

 

 

์กฐ๊ฑด์ด ๋งŽ๊ธด ํ•œ๋ฐ ๋ณต์žกํ•˜์ง€๋Š” ์•Š์•„์„œ ํ•˜๋‚˜์”ฉ ์ƒ๊ฐํ•˜๋ฉด์„œ ํ‘ธ๋‹ˆ๊นŒ ์˜์™ธ๋กœ ๊ธˆ๋ฐฉ ํ’€๋ฆฐ ๋ฌธ์ œ์˜€๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋Š” ์žฌ๋ฏธ์žˆ๋Š” ๋ฌธ์ œ ์ƒํ™ฉ์ด ๋งŽ์•„์„œ ์ข‹๋‹ค..ใ…Žใ…Ž....

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ง€๋‚˜๋‹ค๋‹ˆ๋Š” ๊ธธ์„ 'O', ์žฅ์• ๋ฌผ์„ 'X'๋กœ ๋‚˜ํƒ€๋‚ธ ์ง์‚ฌ๊ฐํ˜• ๊ฒฉ์ž ๋ชจ์–‘์˜ ๊ณต์›์—์„œ ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์‚ฐ์ฑ…์„ ํ•˜๋ คํ•ฉ๋‹ˆ๋‹ค. ์‚ฐ์ฑ…์€ ๋กœ๋ด‡ ๊ฐ•์•„์ง€์— ๋ฏธ๋ฆฌ ์ž…๋ ฅ๋œ ๋ช…๋ น์— ๋”ฐ๋ผ ์ง„ํ–‰ํ•˜๋ฉฐ, ๋ช…๋ น์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

["๋ฐฉํ–ฅ ๊ฑฐ๋ฆฌ", "๋ฐฉํ–ฅ ๊ฑฐ๋ฆฌ" … ]
์˜ˆ๋ฅผ ๋“ค์–ด "E 5"๋Š” ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ํ˜„์žฌ ์œ„์น˜์—์„œ ๋™์ชฝ์œผ๋กœ 5์นธ ์ด๋™ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๋ฅผ ๋จผ์ € ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๊ณต์›์„ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ์ค‘ ์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์œ„ ๋‘ ๊ฐ€์ง€์ค‘ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ํ•ด๋‹น๋œ๋‹ค๋ฉด, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” ํ•ด๋‹น ๋ช…๋ น์„ ๋ฌด์‹œํ•˜๊ณ  ๋‹ค์Œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
๊ณต์›์˜ ๊ฐ€๋กœ ๊ธธ์ด๊ฐ€ W, ์„ธ๋กœ ๊ธธ์ด๊ฐ€ H๋ผ๊ณ  ํ•  ๋•Œ, ๊ณต์›์˜ ์ขŒ์ธก ์ƒ๋‹จ์˜ ์ขŒํ‘œ๋Š” (0, 0), ์šฐ์ธก ํ•˜๋‹จ์˜ ์ขŒํ‘œ๋Š” (H - 1, W - 1) ์ž…๋‹ˆ๋‹ค.

๊ณต์›์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด park, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์ด ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด routes๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ๋ชจ๋“  ๋ช…๋ น์„ ์ˆ˜ํ–‰ ํ›„ ๋†“์ธ ์œ„์น˜๋ฅผ [์„ธ๋กœ ๋ฐฉํ–ฅ ์ขŒํ‘œ, ๊ฐ€๋กœ ๋ฐฉํ–ฅ ์ขŒํ‘œ] ์ˆœ์œผ๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ park์˜ ๊ธธ์ด ≤ 50
    • 3 ≤ park[i]์˜ ๊ธธ์ด ≤ 50
      • park[i]๋Š” ๋‹ค์Œ ๋ฌธ์ž๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ์‹œ์ž‘์ง€์ ์€ ํ•˜๋‚˜๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
        • S : ์‹œ์ž‘ ์ง€์ 
        • O : ์ด๋™ ๊ฐ€๋Šฅํ•œ ํ†ต๋กœ
        • X : ์žฅ์• ๋ฌผ
    • park๋Š” ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ž…๋‹ˆ๋‹ค.
  • 1 ≤ routes์˜ ๊ธธ์ด ≤ 50
    • routes์˜ ๊ฐ ์›์†Œ๋Š” ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” routes์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • routes์˜ ์›์†Œ๋Š” "op n"๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, op๋Š” ์ด๋™ํ•  ๋ฐฉํ–ฅ, n์€ ์ด๋™ํ•  ์นธ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • op๋Š” ๋‹ค์Œ ๋„ค ๊ฐ€์ง€์ค‘ ํ•˜๋‚˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
        • N : ๋ถ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • S : ๋‚จ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • W : ์„œ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • E : ๋™์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
      • 1 ≤ n ≤ 9

 

์ž…์ถœ๋ ฅ ์˜ˆ

park routes result
["SOO","OOO","OOO"] ["E 2","S 2","W 1"] [2,1]
["SOO","OXX","OOO"] ["E 2","S 2","W 1"] [0,1]
["OSO","OOO","OXO","OOO"] ["E 2","S 3","W 1"] [0,0]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(park, routes):
    # x = ์—ด ๋ฒˆํ˜ธ / y = ํ–‰ ๋ฒˆํ˜ธ
    # ํ–‰์—ด์˜ ์ตœ๋Œ€๊ฐ’ ์„ค์ •
    x_max = len(park[0])
    y_max = len(park)
    
    # ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    x = 0
    y = 0
    for i in range(y_max):          # i = ํ–‰ ๋ฒˆํ˜ธ = y๊ฐ’
        for j in range(x_max):      # j = ์—ด ๋ฒˆํ˜ธ = x๊ฐ’
            if park[i][j] == 'S':
                x = j
                y = i
                break
    
    # ์ขŒํ‘œ ์ด๋™
    for route in routes:
        # ๋ฐฉํ–ฅ๊ณผ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ
        direction, distance = route.split(" ")

        # ํ˜„์žฌ์˜ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ๋กœ ์ด๋™
        xx = x
        yy = y

        # ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๊ณต์›์„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๋Š”์ง€ ํ™•์ธ
        # step์ด ์ด๋™๊ฑฐ๋ฆฌ์™€ ๊ฐ™์•„์ง€๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ ์ขŒํ‘œ๋กœ ๋ณ€๊ฒฝ
        for step in range(1, int(distance)+1):
            # ๋™์ชฝ์œผ๋กœ ์ด๋™
            if direction == 'E' and xx != x_max-1 and park[yy][xx+1] != 'X':
                xx += 1
                if step == int(distance):
                    x = xx
            # ์„œ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'W' and xx != 0 and park[yy][xx-1] != 'X':
                xx -= 1
                if step == int(distance):
                    x = xx
            # ๋‚จ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'S' and yy != y_max-1 and park[yy+1][xx] != 'X':
                yy += 1
                if step == int(distance):
                    y = yy
            # ๋ถ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'N' and yy != 0 and park[yy-1][xx] != 'X':
                yy -= 1
                if step == int(distance):
                    y = yy

    return [y, x]

 

ํ’€์ด # 1

  • ์ขŒํ‘œ์—์„œ ๊ณ ๋ ค์‚ฌํ•ญ
    • x = ์—ด ๋ฒˆํ˜ธ / y = ํ–‰ ๋ฒˆํ˜ธ
    • x_max = ์—ด์˜ ๊ธธ์ด / y_max = ํ–‰์˜ ๊ธธ์ด
  • ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    • i = ํ–‰ ๋ฒˆํ˜ธ = y๊ฐ’ / j = ์—ด ๋ฒˆํ˜ธ / x๊ฐ’
    • ์œ„์˜ ๋‚ด์šฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์‹œ์ž‘์  S ์ฐพ๊ธฐ
  • ์ขŒํ‘œ ์ด๋™
    • ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž์˜ ๊ฐ’์€ ๋ฐฉํ–ฅ(direction), ๋’ค์˜ ๊ฐ’์€ ์ด๋™๊ฑฐ๋ฆฌ(distance)
    • ํ˜„์žฌ ์ขŒํ‘œ์™€ ๋™์ผํ•œ ์ž„์‹œ ์ขŒํ‘œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด๋™ (์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”)
      • ๋ฐฉํ–ฅ ํ™•์ธ
        • E = ๋™์ชฝ : x์ขŒํ‘œ + 1
        • W = ์„œ์ชฝ : x์ขŒํ‘œ - 1
        • S = ๋‚จ์ชฝ : y์ขŒํ‘œ + 1
        • N = ๋ถ์ชฝ : y์ขŒํ‘œ -1
      • ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ํ–‰์ด๋‚˜ ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธ
      • ํ•œ ์นธ ์ด๋™ํ–ˆ์„ ๋•Œ ์žฅ์• ๋ฌผ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
    • ๊ฐ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•œ ๊ฒฝ์šฐ ์ž„์‹œ ์ขŒํ‘œ๋ฅผ ํ•œ ์นธ ์ด๋™
    • step์ด ์ด๋™ ๊ฑฐ๋ฆฌ์— ๋„๋‹ฌํ•˜๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ ์ขŒํ‘œ๋กœ ๋ฐ”๊พธ๊ธฐ
    • x๋Š” ์—ด ๋ฒˆํ˜ธ, y๋Š” ํ–‰ ๋ฒˆํ˜ธ์ด๋ฏ€๋กœ [y,x] ๋ฅผ ๋ฐ˜ํ™˜

 

# 2
def solution(park, routes):
    # ํ–‰์—ด์˜ ์ตœ๋Œ€๊ฐ’ ์„ค์ •
    w, h = len(park[0]), len(park)

    # ๋ฐฉํ–ฅ๋ณ„๋กœ ์ด๋™ํ•ด์•ผํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅ
    move = {"N":(-1,0), "S":(1,0), "W":(0,-1), "E":(0,1)}
    
    # ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    x, y = 0, 0
    for i in range(h):
        for j in range(w):
            if park[i][j] == "S":
                x, y = i, j
                break
        
    # ์ขŒํ‘œ ์ด๋™
    for route in routes:
        # ๋ฐฉํ–ฅ๊ณผ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ
        direction, distance = route.split(" ")

        # ํ˜„์žฌ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฉด ํ•ด๋‹น๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
        dx, dy = x, y
        
        for idx in range(int(distance)):
            # ์ด๋™ํ•  ์œ„์น˜
            nx = x + move[direction][0]
            ny = y + move[direction][1]
        
            if 0 <= nx <= h-1 and 0 <= ny <= w-1 and (park[nx][ny]!="X"):
                x, y = nx, ny                    
            else:
                x, y = dx, dy
                break

    return [x,y]

 

ํ’€์ด #2

  • ์œ„์˜ ํ’€์ด์™€ ๋ฐ˜๋Œ€๋กœ x๋ฅผ ํ–‰ ๋ฒˆํ˜ธ, y๋ฅผ ํ–‰ ๋ฒˆํ˜ธ๋กœ ์ƒ๊ฐํ•œ ํ’€์ด
  • ํ‚ค๊ฐ€ ์ด๋™ ๋ฐฉํ–ฅ, ๊ฐ’์ด ์ด๋™ํ•ด์•ผํ•˜๋Š” ๊ฑฐ๋ฆฌ์ธ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • E = ๋™์ชฝ : y์ขŒํ‘œ + 1
    • W = ์„œ์ชฝ : y์ขŒํ‘œ - 1
    • S = ๋‚จ์ชฝ : x์ขŒํ‘œ + 1
    • N = ๋ถ์ชฝ : x์ขŒํ‘œ -1
  • ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
  • ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ dx, dy์— ์ €์žฅ (์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”)
  • ์ž„์‹œ ์ขŒํ‘œ nx, ny๋ฅผ ๋งŒ๋“ค์–ด ๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ–ˆ์„ ๊ฒฝ์šฐ์— ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๋ณ€๊ฒฝ

 

 

x์ขŒํ‘œ, y์ขŒํ‘œ ๋‚˜์˜ค๋ฉด ์ผ๋‹จ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‹œ์ž‘....๋‹ค์‹œ ์ฐจ๊ทผ์ฐจ๊ทผ ์ƒ๊ฐํ•ด๋ณด๊ฒ ๋‹ค๋ฉฐ ์ฝ”๋“œ 12938091๋ฒˆ ์ˆ˜์ •ํ–ˆ๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ’€์ด๊ฐ€ ์ข€ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” ์†๋„๊ฐ€ ์กฐ๊ธˆ ๋” ๋น ๋ฅด์ง€๋งŒ ๋จธ๋ฆฌ๊ฐ€ ๋”ฐ๋ผ์ฃผ์ง€ ์•Š์•„์„œ ํž˜๋“ค์—ˆ๋‹ค...ํœด...์–ด์จŒ๋“  ํ’€์—ˆ์œผ๋‹ˆ๊นŒ ์‰ฌ์–ด์•ผ๊ฒ ๋‹ค.๊ณต์› ์‚ฐ์ฑ…์„ ์™œ์ด๋ ‡๊ฒŒ ๋นก์„ธ๊ฒŒ ํ•˜์‹œ๋Š” ๊ฑฐ์—์š”... ๐Ÿƒ๐Ÿป ๐Ÿƒ๐Ÿป

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

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ˆซ์ž๋‚˜๋ผ ๊ธฐ์‚ฌ๋‹จ์˜ ๊ฐ ๊ธฐ์‚ฌ์—๊ฒŒ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ number๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์ง€์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์‚ฌ๋“ค์€ ๋ฌด๊ธฐ์ ์—์„œ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ๊ธฐ์‚ฌ๋Š” ์ž์‹ ์˜ ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์•ฝ์ˆ˜ ๊ฐœ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง„ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, ์ด์›ƒ๋‚˜๋ผ์™€์˜ ํ˜‘์•ฝ์— ์˜ํ•ด ๊ณต๊ฒฉ๋ ฅ์˜ ์ œํ•œ์ˆ˜์น˜๋ฅผ ์ •ํ•˜๊ณ , ์ œํ•œ์ˆ˜์น˜๋ณด๋‹ค ํฐ ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง„ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ด์•ผ ํ•˜๋Š” ๊ธฐ์‚ฌ๋Š” ํ˜‘์•ฝ๊ธฐ๊ด€์—์„œ ์ •ํ•œ ๊ณต๊ฒฉ๋ ฅ์„ ๊ฐ€์ง€๋Š” ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 15๋ฒˆ์œผ๋กœ ์ง€์ •๋œ ๊ธฐ์‚ฌ๋‹จ์›์€ 15์˜ ์•ฝ์ˆ˜๊ฐ€ 1, 3, 5, 15๋กœ 4๊ฐœ ์ด๋ฏ€๋กœ, ๊ณต๊ฒฉ๋ ฅ์ด 4์ธ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ์ด์›ƒ๋‚˜๋ผ์™€์˜ ํ˜‘์•ฝ์œผ๋กœ ์ •ํ•ด์ง„ ๊ณต๊ฒฉ๋ ฅ์˜ ์ œํ•œ์ˆ˜์น˜๊ฐ€ 3์ด๊ณ  ์ œํ•œ์ˆ˜์น˜๋ฅผ ์ดˆ๊ณผํ•œ ๊ธฐ์‚ฌ๊ฐ€ ์‚ฌ์šฉํ•  ๋ฌด๊ธฐ์˜ ๊ณต๊ฒฉ๋ ฅ์ด 2๋ผ๋ฉด, 15๋ฒˆ์œผ๋กœ ์ง€์ •๋œ ๊ธฐ์‚ฌ๋‹จ์›์€ ๋ฌด๊ธฐ์ ์—์„œ ๊ณต๊ฒฉ๋ ฅ์ด 2์ธ ๋ฌด๊ธฐ๋ฅผ ๊ตฌ๋งคํ•ฉ๋‹ˆ๋‹ค. ๋ฌด๊ธฐ๋ฅผ ๋งŒ๋“ค ๋•Œ, ๋ฌด๊ธฐ์˜ ๊ณต๊ฒฉ๋ ฅ 1๋‹น 1kg์˜ ์ฒ ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌด๊ธฐ์ ์—์„œ ๋ฌด๊ธฐ๋ฅผ ๋ชจ๋‘ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ฒ ์˜ ๋ฌด๊ฒŒ๋ฅผ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

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

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ number ≤ 100,000
  • 2 ≤ limit ≤ 100
  • 1 ≤ power ≤ limit

 

์ž…์ถœ๋ ฅ ์˜ˆ

number limit power result
5 3 2 10
10 3 2 21

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1 (์‹œ๊ฐ„ ์ดˆ๊ณผ)
def solution(number,limit,power):
    answer = 0
    knights = list(range(1,number+1))
    
    for knight in knights:
        cnt = 0
        for n in range(1,knight+1):
            if knight%n == 0:
                cnt += 1
        if cnt > limit:
            answer += power
        else:
            answer += cnt

    return answer

 

ํ’€์ด # 1

  • knights ๋ฆฌ์ŠคํŠธ์— ๊ฐ ๊ธฐ์‚ฌ๋“ค์˜ ๋ฒˆํ˜ธ ๋‹ด๊ธฐ
  • knights ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์•ฝ์ˆ˜ ๊ฐฏ์ˆ˜๋ฅผ cnt ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ
    • cnt๊ฐ€ limit๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ •ํ•ด์ง„ power๋ฅผ answer์— ๋”ํ•˜๊ธฐ
    • cnt๊ฐ€ limit๋ณด๋‹ค ์ž‘๋‹ค๋ฉด cnt๋ฅผ answer์— ๋”ํ•˜๊ธฐ

 

# 2
def solution(number,limit,power):
    knights = list(range(1,number+1))
    knights_score = []
    
    for knight in knights:
        cnt = 0
        for n in range(1,int(knight**(1/2))+1):
            if knight%n == 0:
                cnt += 1
                if n**2 != knight: 
                    cnt += 1
            if cnt > limit:
                cnt = power
                break
        knights_score.append(cnt)

    return sum(knights_score)

 

ํ’€์ด #2

  • knights ๋ฆฌ์ŠคํŠธ์— ๊ฐ ๊ธฐ์‚ฌ๋“ค์˜ ๋ฒˆํ˜ธ ๋‹ด๊ธฐ
  • knights ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ๊ฐ ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์•ฝ์ˆ˜ ๊ฐฏ์ˆ˜๋ฅผ cnt ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ
    • ์ด ๋•Œ, ๊ธฐ์‚ฌ ๋ฒˆํ˜ธ์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€์˜ ๋ฒ”์œ„ ์‚ฌ์šฉ (์•ฝ์ˆ˜๋Š” ์ง์ด ์žˆ์œผ๋ฏ€๋กœ ์ œ๊ณฑ๊ทผ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณตํ•˜์—ฌ ์ดํ›„์— ์ œ๊ณฑ)
    • ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๊ฐ€ limit์„ ๋„˜์–ด๊ฐ€๋ฉด for๋ฌธ์„ ์ข…๋ฃŒํ•˜์—ฌ ์—ฐ์‚ฐ์˜ ์ˆ˜ ์ค„์ด๊ธฐ
  • knights_score ๋ฆฌ์ŠคํŠธ์— ๊ฐ ๊ธฐ์‚ฌ์˜ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ
  • knights_score์˜ ํ•ฉ์„ ๋ฆฌํ„ด

 

 

์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ฌธ์ œ๊ฐ€ ๋‚˜์™”๋Š”๋ฐ์š”...? ์ œ์ถœํ•˜๊ณ  ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋œจ๋‹ˆ๊นŒ ์•„์ฐ”ํ•ด์„œ ์ธ์ƒ์˜ ์žฌ๋ฏธ๋ฅผ ๋Š๋‚„ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋กœ ํ–ˆ์„ ๋•Œ ํ…Œ์ŠคํŠธ1์— 3000ms ๊ฑธ๋ ธ๋Š”๋ฐ ๋‘ ๋ฒˆ์งธ ํ’€์ด๋กœ ํ•˜๋‹ˆ๊นŒ 50ms๊ฐ€ ๋‚˜์™”๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” ๋ธ”๋กœ๊ทธ ๊ธ€์„ ์ฐธ๊ณ ํ–ˆ๋Š”๋ฐ ์„ค๋ช…์„ ์ž˜ํ•ด์ฃผ์…”์„œ ์ดํ•ด๊ฐ€ ์ž˜ ๋๋‹ค.

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 ํ•จ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋‹ค...๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜ ์“ฐ๋Š”๊ฑฐ ์ตœ๊ณ ...!!

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๋ฒˆ์งธ ๋‹ค์งํ•˜๋Š” ์ค‘์ด๋‹ค. 

+ Recent posts