ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 12760 ์ตœํ›„์˜ ์Šน์ž๋Š” ๋ˆ„๊ตฌ? with Python
    PS 2022. 3. 10. 18:04
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 12760 ์ตœํ›„์˜ ์Šน์ž๋Š” ๋ˆ„๊ตฌ?

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์ตœ์ข… ํ”Œ๋ ˆ์ด์–ด N๋ช…์ด ๋‚จ์•„์žˆ๋‹ค. ๊ฐ ํ”Œ๋ ˆ์ด์–ด๋Š” M์žฅ์”ฉ์˜ ์ˆซ์ž๊ฐ€ ์ ํžŒ ์นด๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ,
      ์ด๋“ค์€ ๋งค ํ„ด ์ž์‹ ์ด ๊ฐ€์ง„ ์นด๋“œ ์ค‘ ๊ฐ€์žฅ ํฐ ์นด๋“œ๋ฅผ ๋‘๊ณ  ๋น„๊ต๋ฅผ ํ•˜๋Š”๋ฐ, ๊ทธ ์นด๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ 1์ ์„ ํš๋“ํ•œ๋‹ค.

    2. ๊ทธ ํ„ด์— ์‚ฌ์šฉ๋œ ์นด๋“œ๋Š” ๋ฒ„๋ฆฌ๊ธฐ๋กœ ํ•œ๋‹ค. (๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ํ”Œ๋ ˆ์ด์–ด๋Š” ์—ฌ๋Ÿฌ ๋ช…์ผ ์ˆ˜ ์žˆ๋‹ค.)

    3.โ€ŠM๋ฒˆ์˜ ๊ฒฝ๊ธฐ ํ›„ ๊ฐ€์žฅ ๋งŽ์€ ์ ์ˆ˜๋ฅผ ํš๋“ํ•œ ํ”Œ๋ ˆ์ด์–ด๋Š” ๋ช‡ ๋ฒˆ ํ”Œ๋ ˆ์ด์–ด์ธ์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.

    1. 2 <= N <= 100, 1 <= M <= 100
      1 <= ์นด๋“œ์— ์ ํžŒ ์ˆซ์ž <= 100

    2. ๊ฐ€์žฅ ๋งŽ์€ ์ ์ˆ˜๋ฅผ ํš๋“ํ•œ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ช…์ผ ๊ฒฝ์šฐ, ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ํ”Œ๋ ˆ์ด์–ด๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

    3. ์ •๋ ฌ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    
    n, m = map(int, stdin.readline().split())
    d = {x: 0 for x in range(1, n + 1)}
    players = []
    for i in range(n):
        cards = list(map(int, stdin.readline().split()))
        cards.sort()
        players.append(list(reversed(cards)))
    
    mx = [0 for _ in range(m)]
    
    for i in range(n):
        for j in range(m):
            mx[j] = max(mx[j], players[i][j])
    
    for i in range(n):
        for j in range(m):
            if mx[j] == players[i][j]:
                d[i + 1] += 1
    
    mx_cnt = max(d.values())
    res = []
    for key, item in d.items():
        if item == mx_cnt:
            res.append(key)
    res.sort()
    print(*res)

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    5 3
    5 4 3
    3 4 5
    3 5 4
    4 5 3
    3 4 4

    ์‹คํ–‰๊ฒฐ๊ณผ

    1 2 3 4

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. d ๋ณ€์ˆ˜๋Š” dictionary ์ด๋ฉฐ, ๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜์ด๋‹ค.

    2. N ๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฐ์ž ๋“ค๊ณ  ์žˆ๋Š” ์นด๋“œ์˜ ์ˆซ์ž๋“ค์ด ์ž…๋ ฅ๋  ๋•Œ, ๊ทธ ์ˆซ์ž๋“ค์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ players์— ๋„ฃ์–ด์ค€๋‹ค.

    3. ๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์นด๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ€์žฅ ํฐ ์นด๋“œ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

    4. ๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์นด๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ mx[j] ๊ฐ€ i ๋ฒˆ ํ”Œ๋ ˆ์ด์–ด์˜[j]๋ฒˆ์งธ ์นด๋“œ์™€ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด, d[i + 1] ์— + 1์„ ํ•ด์ค€๋‹ค.

    5. d ์— ์ €์žฅ๋œ ๊ฐ’ ์ค‘, ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ณจ๋ผ๋‚ธ ๋’ค, mx_cnt ์— ์ €์žฅํ•œ๋‹ค.

    6. d๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ mx_cnt ์™€ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง„ ํ‚ค๋ฅผ res์— ์ €์žฅํ•ด์ค€๋‹ค.
      res๋ฅผ ์ถœ๋ ฅํ•  ๋•, ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ ๋ฒˆ ์ˆ˜ํ–‰ํ•ด์ค€ ๋’ค ์ถœ๋ ฅํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋˜ ์ •๋ ฌ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
    2. ์นด๋“œ์˜ ์ˆซ์ž๋ฅผ ํฐ ์ˆœ์„œ๋Œ€๋กœ ๋ณด์•„์•ผํ•˜๋Š” ๊ฒƒ์„ ๋– ์˜ฌ๋ฆฌ์ง€ ๋ชปํ•ด ์กฐ๊ธˆ ํ—ค๋งธ์Šต๋‹ˆ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.