notes

APS

  • ์†์ฝ”๋”ฉ์„ ์ง‘์š”ํ•˜๊ฒŒ ์™„๋ฒฝํžˆ ํ•ด๋‚ด๊ณ ์„œ ์ฝ”๋”ฉํ•˜์ž..

  • ์กฐ๊ฑด๋ฌธ์€ ๊ผญ else: ๋„ ๊ณ ๋ คํ•˜์ž. if ๋ฌธ ์ผ๋•Œ๋งŒ ๊ณ ๋ คํ•˜๋Š” ํ๋ฆ„์ด computitational thinking ์— ๋งž์ง€๋งŒ, ๋†“์น  ์ˆ˜ ์žˆ๋‹ค.

    • ํ‡ด์‚ฌ ๋‹ค์ด๋‚˜๋ฏน ์—์„œ else: ๋ฅผ ์•ˆ ํ•ด๋„ ๋œ๋‹ค๊ณ  ์ฐฉ๊ฐํ•จ

  • ์šฐ์„ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์„ ๊ทธ๋ ค์„œ ํŒŒ์•…ํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์ž. ๋ฌธ์ œ ์ˆœ์„œ๊ฐ€ ์•„๋‹ˆ๋ผ

    • ๊ณตํ†ต๋˜๋Š” ์กฐ๊ฑด์„ ํŒŒ์•…ํ•˜์ž.

    • ๊ทธ๋ ค๋†“์œผ๋ฉด ๋ญ๋ถ€ํ„ฐ ์ง€์›Œ๊ฐ€์•ผ ์šฐ์„ ์ˆ˜ํ–‰์„ ์•Œ๊ธฐ ์‰ฝ๋‹ค

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

  • ใ„นใ…‡.. ๋ฌธ์žฅ์— ์ •๋‹ต ์กฐ๊ฑด์ด ์žˆ๋‹ค..

  • ๊ตฌํ˜„์€ ์ข…์ด์—

  • aps ๋ฅผ ํ•  ๋•Œ ์ƒ๊ฐํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ๊ตฌํ˜„ํ•  ๋•Œ ๋‹ค๋ฅธ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ํŽธํ•  ์ˆ˜ ์žˆ๋‹ค. aps ํ•˜๋ฉด์„œ ๊ตฌํ˜„๊ฐ€๋Šฅ์„ฑ๋„ ๋™์‹œ์— ๊นŠ์ด ์ƒ๊ฐํ•˜์ž.

  • ๋ณ€์ˆ˜์„ค์ •์„ ํ•˜๊ณ , ๋ฌธ์ œ๋ฅผ ํ•œ๋ฌธ์žฅ ํ•œ๋ฌธ์žฅ ์ฝ์–ด๊ฐ€๋ฉด์„œ ๋ณ€์ˆ˜๋ฅผ ๋น„๊ตํ•˜์ž.

    • Ex) ๋ฑ€ ๋ฌธ์ œ์—์„œ ๋ฌธ์ œ์— ์‚ฌ๊ณผ๊ฐ€ ์—†์–ด์ง„๋‹ค๊ณ  ๋ช…์‹œํ•ด์žˆ๋Š” ๊ฒƒ์„ ๋ณด๊ณ , ์ฝ”๋“œ์—์„œ ์‚ฌ๊ณผ๋ฅผ remove ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

  • ๋ฌธ์ œ๊ฐ€ ๋งŽ์ด ๋ณต์žกํ•  ์‹œ

    • while True:
              ์›ํ•˜๋Š” ์ง€์  ๋„๋‹ฌ์‹œ
                      ์ˆ˜ํ–‰
                      exit()
  • ๋ณ€์ˆ˜์„ค์ •์„ ์ ๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•˜์ž. ๊ณผ๋„ํ•œ ์ธ๋ฑ์‹ฑ ใ„ดใ„ด

  • ๋‚ด๊ฐ€ ํ•„์ˆ˜์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผํ•  ๊ฒƒ๋งŒ ๊ตฌํ˜„ํ•˜์ž. ๊ณ„์‚ฐ์€ ์ปดํ“จํ„ฐ๊ฐ€

  • ๊ตฌํ˜„์„ ํ•ด๋‚˜๊ฐ€๋ฉด์„œ ์ค‘๊ฐ„ ๋””๋ฒ„๊น…์„ ํ•˜์ž

  • ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’ ์—…๋ฐ์ดํŠธ ์‹œ

    • min_answer = int(1e9)
      max_answer = -int(1e9)
  • ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ str ์ผ ๋•Œ ๋” ํŽธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • string[0] ์€ " ์ด๋‹ค.

DP

  • ์Œ DP ํ’€ ๋•Œ DP ํ…Œ์ด๋ธ”์„ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑํ•˜๋Š”์ง€๊ฐ€ ์ข€ ๋” ์ค‘์š”ํ•จ. ์–ด๋–ค์‹์œผ๋กœ ํ…Œ์ด๋ธ”์„ ๊ตฌ์„ฑํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ƒํ™ฉ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ.

For loop

  • In list ๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์ž => range over ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

    • for value in list:
  • ๋ฐ˜๋ณต๋ฌธ range ๋ฅผ ํ™œ์šฉํ•˜์ž

    • for i in range(a,b,c):
      
      # a ๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๋ฉฐ
      # b ์ „๊นŒ์ง€ ํ•ด๋‹น๋˜๊ณ 
      # c ๋‹จ์œ„๋กœ ๋Š˜์–ด๋‚œ๋‹ค
  • split ๋œ ์ •๋ณด๋“ค์„ ์ž…๋ ฅ๋ฐ›๋Š” ๋™์‹œ์— ์ž๋ฃŒ๋ฅผ ๋งŒ๋“ค์ž

    • graph = [[] for _ in range(n+1)]
      for i in range(m):
          a,b = map(int,input().split())
          graph[a].append(b)
  • for ๋ฌธ์—์„œ List ๋ฅผ ํŽธ์ง‘ํ•  ๋•Œ ์ฃผ์˜ !

    • for v in list:
          list.append(v + n)
      
      # ์œ„์ฒ˜๋Ÿผ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์•ˆ์—์„œ list ๊ฐ€ ํŽธ์ง‘๋˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด๋‹ค
      
      length = len(list)
          for i in range(length):
              list.append(list[i] + n)
      
      # ๋ฒˆ๊ฑฐ๋กญ๋”๋ผ๋„ ์ด๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜์ž
  • for ๋ฌธ ์•ˆ์—์„œ ์‚ฌ์šฉํ•œ ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น for ๋ฌธ ์œ„์—์„œ ์ดˆ๊ธฐํ™”

List - Based

  • Duque ์˜ rotate ์˜ต์…˜์€ ์‹œ๊ณ„, ๋ฐ˜์‹œ๊ณ„ ํšŒ์ „์ด ๊ฐ€๋Šฅํ•จ;;

  • ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ํ•  ๋•Œ, 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ์œ„์น˜๊ฐ€ ๊ณ„์† ๋ณ€ํ•œ๋‹ค๋ฉด, [x, y] ํ˜•ํƒœ์˜ element ๋ฅผ ๊ฐ–๋Š” 1์ฐจ์› ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์ž.

    • => 2์ฐจ์› ๋ฐฐ์—ด์ƒ์˜ ์ •๋ณด๊ฐ€ ๊ฒน์น˜๊ฑฐ๋‚˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์˜ค๋ฅ˜๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.

    • el_locs = [[x, y], [x, y], [x, y]...]
    • # ํ•ด๋‹น ์œ„์น˜๊ฐ€ v_locs ์•ˆ์— ์žˆ๋Š”์ง€ ์—†๋Š”์ง€
      if [x, y] in el_locs:
  • Queue ๋‚˜ Stack ์— ์ธ์ ‘๋…ธ๋“œ๋ฅผ push ํ•  ๋•Œ ๋…ธ๋“œ ์ž์ฒด์— ์ •๋ณด๋ฅผ ๋‹ด์•„ ๊ธฐ์กด ๋…ธ๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •๋ณด๋ฅผ ๋ถ€์—ฌํ•˜์—ฌ ์ธ์ ‘๋…ธ๋“œ๋ฅผ push ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

    • # ex)
      data = []
      for y in range(n):
          for x in range(n):
              if arr[y][x]:
                  data.append((arr[y][x],0,y,x))
      
      # ๊ทธ ์ด์ „ ๋…ธ๋“œ๋กœ ์ธํ•ด ์ง„ํ–‰๋œ ์ธ์ ‘๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…ํ•  ๋•Œ ๊ธฐ์กด ๋…ธ๋“œ์˜ ์‹œ๊ฐ„ +1 ์„ ํ•ด์ฃผ๋Š” ๊ฐœ๋…            
      while q:
          virus,time, y,x = q.popleft()
          if time == s:
              break
          for i in range(4):
              ny = y + dy[i]
              nx = x + dx[i]
      
              if 0 <= ny < n and 0 <= nx < n:
                  if not arr[ny][nx]:
                      arr[ny][nx] = virus
                      q.append((virus, time+1, ny,nx))
  • Permutation / Combination

    • from itertools import permutations
      from itertools import combinations
      
      a = []
      b = []
      items= [1,2,3,4]
      
      list(combinations(items, m))
      list(permutations(items, m))
  • Least common multiple

    • Arr = list(map(int,list(input().split())))
      
      n = 1
      while n%Arr[0] or n%Arr[1] or n%Arr[2]:
          n += 1
      
      print(n)

2 - dimensional array

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

  • 90 ๋„ ํšŒ์ „

    • list(zip(*arr[::-1]))
  • split ๋œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ

    • graph = [[] for _ in range(n+1)]
      for i in range(m):
          a,b = map(int,input().split())
          graph[a].append(b)
  • ๊ธฐ๋ณธ์ ์œผ๋กœ nx, ny ์˜ ์กฐ๊ฑด๋ฌธ์„ ํ•  ๋•Œ๋Š” ์ธ๋ฑ์Šค ๋ฒ”์œ„ ๋จผ์ € ๊ฒ€ํ† ํ•ด์•ผํ•œ๋‹ค.

     if 0 > nx or r <= nx or 0 > ny or c <= ny or a[nx][ny] == 1:
            d = (d + 1) % 4
            nx = x + dx[d]
            ny = y + dy[d]
    
    ๋งŒ์•ฝ ์กฐ๊ฑด๋ฌธ์—์„œ a[nx][ny] ๋ฅผ ๋จผ์ € ์ˆ˜ํ–‰ํ•œ๋‹ค๋ฉด ์ธ๋ฑ์Šค ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€๋งŒ,
    ์•ž์—์„œ ๋ฒ”์œ„๊ฐ€ ๋ฒ—์–ด๋‚œ ๊ฒƒ์ด ํ•˜๋‚˜๋ผ๋„ ๋ฐœ๊ฒฌ๋˜๋ฉด or ๋ฌธ์ด๊ธฐ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
  • ํšŒ์ „

    list(zip(*arr[::-1]))

Recursion

  • ํ•จ์ˆ˜๋‚ด์—์„œ ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ƒ๊ฐํ•˜๊ณ  ์ธ์ž ์„ค์ •์„ ํ•˜์ž

Hash

  • ๋”•์…”๋„ˆ๋ฆฌ ๊ตฌํ˜„

    # key ๋ฅผ ์ˆซ์ž ์ˆœ์„œ๋Œ€๋กœ ํ•  ๋•Œ
    
    dic = dict()
    lst = [chr(i) for i in range(ord('A'), ord('Z') + 1)] # Value ๋กœ ๊ฐ€์งˆ ๊ฐ’๋“ค์˜ ๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„
    
    for idx, char in enumerate(lst):
        dic[char] = idx + 1 # ์—ฌ๊ธฐ์„œ ์ˆœ์„œ ๋ฐ”๊ฟ”์ฃผ๋ฉด ํ‚ค์™€ ๋ฐธ๋ฅ˜๊ฐ€ ๋ฐ”๋€๋‹ค

Last updated