๐Ÿ”ฅ TIL - Day 37

Kim Dae Hyunยท2021๋…„ 10์›” 25์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
44/93

์ด ๋ฌธ์ œ๋Š” ๊ธฐ์–ตํ•˜์ž !!

๐Ÿ“Œ ์ƒค์˜ค๋ฏธ ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ

2์ฐจ์›์˜ ๊ณต๊ฐ„์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ๊ฐ€ ํ•œ ๋ฒˆ์˜ ๋™์ž‘์œผ๋กœ ์ฒญ์†Œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ”์œ„์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ๋ผ.

  • 0 ์€ ์ฒญ์†Œ๋˜์ง€ ์•Š์€ ๊ณต๊ฐ„ (์ฒญ์†Œ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ๊ณต๊ฐ„)
  • 1์€ ๋ฒฝ

์ด๋™์กฐ๊ฑด

  • ์™ผ์ชฝ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ์™ผ์ชฝ๋ฐฉํ–ฅ์œผ๋กœ ์ฒญ์†Œ๊ณต๊ฐ„ ํƒ์ƒ‰
  • ๋™,์„œ,๋‚จ,๋ถ ๋ชจ๋‘ ์ฒญ์†Œํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ํ˜„์žฌ ๋ฐฉํ–ฅ์„ ๋ฐ”๋ผ๋ณด๊ณ  ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ ์ด๋™
  • ๋™,์„œ,๋‚จ,๋ถ ๋ชจ๋‘ ์ฒญ์†Œ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ  ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์ด ๋ฒฝ์ด๋ผ๋ฉด ๋™์ž‘์ข…๋ฃŒ

์ž…๋ ฅ์€ ์‹œ์ž‘์œ„์น˜ x,y์ขŒํ‘œ์™€ ๋ฐฉํ–ฅ์ด ์ฃผ์–ด์ง„๋‹ค.

๋ฐฉํ–ฅ

  • ๋ถ: 0
  • ๋™: 1
  • ๋‚จ: 2
  • ์„œ: 3

์ฒ˜์Œ์—๋Š” ํ‰๋ฒ”ํ•œ BFS ์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ค‘๊ฐ„์ค‘๊ฐ„ ๊ณ ๋ คํ•ด์•ผ ๋˜๋Š” ๊ฒŒ ๋„ˆ๋ฌด ๋งŽ์•˜๋‹ค ใ… ใ… 

๋ฝ€์ธํŠธ 1
์ด๋™์ขŒํ‘œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑํ–ˆ์„ ๋•Œ ๊ฐ ์œ„์น˜์—์„œ ์™ผ์ชฝ์œผ๋กœ ํšŒ์ „ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ

#     ๋ถ  ๋™  ๋‚จ  ์„œ
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]

# ๋ถ -> ์„œ: 0 -> 3
# ๋™ -> ๋ถ: 1 -> 0
# ๋‚จ -> ๋™: 2 -> 1
# ์„œ -> ๋‚จ: 3 -> 2
def rotate(d):
    return (d + 3) % 4

๋ฝ€์ธํŠธ2
๋ฐ˜๋Œ€๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ

# ๋ถ -> ๋‚จ: 0 -> 2
# ๋™ -> ์„œ: 1 -> 3
# ๋‚จ -> ๋ถ: 2 -> 0
# ์„œ -> ๋™: 3 -> 1
def goback(d):
    return (d + 2) % 4

BFS์—์„œ ํšŒ์ „๊ฐ™์€ ๊ฒƒ๋“ค์ด ๋‚˜์˜ค๋ฉด ๋งŽ์ด ์–ด๋ ต๊ฒŒ ๋Š๊ปด์ง€๊ณค ํ–ˆ๋Š”๋ฐ ์ข‹์€ ํ’€์ด๋ฒ•์„ ํ•˜๋‚˜ ๋ฐฐ์›Œ๊ฐ„๋‹ค ใ…Ž (๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ !!!!!!!!!!!!!!!!!!!!!!!!!)

์ „์ฒด์ฝ”๋“œ

current_r, current_c, current_d = 7, 4, 0
current_room_map = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 1, 1, 1, 1, 0, 1],
    [1, 0, 0, 1, 1, 0, 0, 0, 0, 1],
    [1, 0, 1, 1, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 1, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 1, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
#    ๋ถ,๋™,๋‚จ,์„œ
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]

# ์™ผ์ชฝ ํšŒ์ „ ๋ฉ”์„œ๋“œ
# ๋ถ -> ์„œ 0 -> 3
# ๋™ -> ๋ถ 1 -> 0
# ๋‚จ -> ์„œ 2 -> 3
# ์„œ ->๋‚จ  3 -> 2
def rotate(d):
    return (d + 3) % 4

# ํ˜„์žฌ ๋ฐฉํ–ฅ ๊ธฐ์ค€ ๋ฐ˜๋Œ€์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋ฉ”์„œ๋“œ
# ๋ถ -> ๋‚จ 0 -> 2
# ๋™ -> ์„œ 1 -> 3
# ๋‚จ -> ๋ถ 2 -> 0
# ์„œ -> ๋™ 3 -> 1
def go_back(d):
    return (d+2) % 4


def get_count_of_departments_cleaned_by_robot_vacuum(r, c, d, room_map):
    n = len(room_map)
    m = len(room_map[0])
    res = 0
    if room_map[r][c] == 0:
        res += 1
        room_map[r][c] = -1
    q = [(r,c,d)]

    while q:
        r, c, d = q.pop(0)
        d_idx = d
        for i in range(4):
            d_idx = rotate(d_idx)

            nr = r + dr[d_idx]
            nc = c + dc[d_idx]

            if 0 <= nr < n and 0 <= nc < m and room_map[nr][nc] == 0:
                room_map[nr][nc] = -1
                q.append((nr, nc, d_idx))
                res += 1
                break # break๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด 4๋ฐฉํ–ฅ ์ค‘ ์ฒญ์†Œ ๊ฐ€๋Šฅํ•œ ์˜์—ญ์ด ์žˆ์Œ์—๋„ ๋ฐ˜๋ณต๋ฌธ์˜ ๋งˆ์ง€๋ง‰์—์„œ ์ฒญ์†Œ๋ถˆ๊ฐ€๋Šฅ์ด๊ณ  ๋’ท์ชฝ๋„ ๋ง‰ํ˜€์žˆ๋‹ค๋ฉด ์ข…๋ฃŒ๋˜์–ด๋ฒ„๋ฆผ

            elif i == 3: # 4๋ฒˆ์งธ ๋ฐฉํ–ฅ ํƒ์ƒ‰์‹œ(๋งˆ์ง€๋ง‰ ํƒ์ƒ‰) ์œ„ if๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด ์ฒญ์†Œ๊ฐ€๋Šฅํ•œ ์ง€์—ญ์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋’ค์ชฝ ์ด๋™ํ•œ๋‹ค (๋ฐฉํ–ฅ์œ ์ง€)
                d_idx = go_back(d)
                nr = r + dr[d_idx]
                nc = c + dc[d_idx]
                q.append((nr, nc, d))

                if room_map[nr][nc] == 1: # ๋’ค์ชฝ๋ฐฉํ–ฅ์ด ๋ฒฝ์œผ๋กœ ๋ง‰ํžŒ ๊ฒฝ์šฐ
                    return res

    return res


# 57 ๊ฐ€ ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!
print(get_count_of_departments_cleaned_by_robot_vacuum(current_r, current_c, current_d, current_room_map))


๐Ÿ“Œ ๋ฐฑ์ค€ 2609 ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

์ฝ”ํ…Œ์— ์ด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ์ž˜ ๋‚˜์˜ค์ง„ ์•Š๊ฒ ์ง€๋งŒ ํ˜ธ์˜ค์˜ค์˜ค์˜ฅ์‹œ๋‚˜ ๋‚˜์™”์„ ๋•Œ ๋ชป ํ’€๋ฉด ๋„ˆ๋ฌด ์ฐฝํ”ผํ•˜๋‹ˆ๊นŒ ...

์ตœ๋Œ€๊ณต์•ฝ์ˆ˜? โžก๏ธ ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•!!

์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•

  • a๊ฐ€ b๋ณด๋‹ค ํด ๋•Œ, a๋ฅผ b๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ๊ฒฝ์šฐ b๊ฐ€ a์™€ b์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์ด๋‹ค.
  • ์žฌ๊ท€๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅ (์žฌ๊ท€๊ฐ€ ๋˜๋‹ˆ๊นŒ ๋‹น์—ฐํžˆ ๋ฐ˜๋ณต๋ฌธ๋„ ๋จ!)
def get_gcd(a, b):
    if b == 0: 
        return a

    return get_gcd(b, a % b)

์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜? โžก๏ธ (๋‘ ์ˆ˜์˜ ๊ณฑ) / ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜

print((a*b)//gcd)

์ „์ฒด์ฝ”๋“œ

def get_gcd(a, b):
    if b == 0:
        return a

    return get_gcd(b, a % b)


a, b = map(int, input().split())
gcd = get_gcd(a, b)
print(gcd)
print((a*b)//gcd)


๐Ÿ“Œ ๋ฐฑ์ค€ 2805 ๋‚˜๋ฌด ์ž๋ฅด๊ธฐ

๊ฐ’์„ ๊ฐ€์ง€๊ณ  ํ•˜๋Š” ์ด๋ถ„ํƒ์ƒ‰
๋ณดํ†ต ์ด๋ถ„ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜๋Š” ๋งŽ์€ ๋ฌธ์ œ๋“ค์ด ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„๊ฐ’์„ ์žก๊ณ  ํ•œ ์ชฝ์œผ๋กœ ํƒ์ƒ‰ ์˜์—ญ์„ ์ค„์—ฌ๊ฐ€๋Š” ๋ฐฉ์‹์ธ๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ํŠน์ • ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ด๋ถ„ํƒ์ƒ‰์„ ํ–ˆ๋‹ค. (์ด๊ฒƒ๋„ ๋ณดํ†ต์ธ๊ฐ€ .. ?)

๋ชฉ์žฌ์ ˆ๋‹จ๊ธฐ์˜ ๋†’์ด (mid)๋ฅผ ์ตœ๋Œ€๊ฐ’ ์ชฝ ํ˜น์€ ์ตœ์†Œ๊ฐ’ ์ชฝ์œผ๋กœ ์ค„์—ฌ๊ฐ€๋ฉฐ ํƒ์ƒ‰ํ•œ๋‹ค.


n, m = map(int, input().split())
trees = list(map(int, input().split()))

# minh: 1 (๋‚˜๋ฌด์˜ ์ตœ์†Œ๋†’์ด๋Š” 1)
# maxh: ํ˜„์žฌ ๋‚˜๋ฌด ์ค‘ ์ตœ๊ณ ๋†’์ด
minh, maxh = 1, max(trees)

while minh <= maxh:
    mid = (minh + maxh) // 2 # ๋ชฉ์žฌ์ ˆ๋‹จ๊ธฐ์˜ ๋†’์ด
    tmp = 0 # ๋ฒŒ๋ชฉ๋˜๋Š” ๋‚˜๋ฌด๋“ค์˜ ๊ธธ์ด์˜ ํ•ฉ

    for t in trees:
        if t > mid: # ๋ฒŒ๋ชฉ๋˜๋Š” ๋†’์ด์— ํ•ด๋‹นํ•˜๋Š” ๋‚˜๋ฌด๋งŒ ๋ฒŒ๋ชจ๋˜๋„๋ก
            tmp = tmp + (t - mid)

    # mid์˜ ๋†’์ด๋กœ ๋ฒŒ๋ชฉํ•œ ๊ฒฐ๊ณผ ๋ชฉํ‘œ๋กœ ํ•œ ๊ธธ์ด๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
    if tmp >= m:
        minh = mid + 1 # ์ตœ์†Œ๊ฐ’์„ ์ค‘๊ฐ„๊ฐ’+1๋กœ ๊ฐฑ์‹ ํ•ด์„œ ๋” ๋†’์€ ๋†’์ด์—์„œ ๋ฒŒ๋ชฉ๋˜์–ด ๋” ์ž‘์€ ๊ธธ์ด๊ฐ€ ๋ฒŒ๋ชฉ๋˜๋„๋ก ํ•œ๋‹ค.
    else:
        maxh = mid - 1 # ์ตœ๋Œ€๊ฐ’์„ ์ค‘๊ฐ„๊ฐ’+1๋กœ ๊ฐฑ์‹ ํ•ด์„œ ๋” ๋‚ฎ์€ ๋†’์ด์—์„œ ๋ฒŒ๋ชฉ๋˜์–ด ๋” ํฐ ๊ธธ์ด๊ฐ€ ๋ฒŒ๋ชฉ๋˜๋„๋ก ํ•œ๋‹ค.

print(maxh)
profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€