Skip to content
Merged
14 changes: 14 additions & 0 deletions Hongjoo/lv2/다음큰숫자.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
https://programmers.co.kr/learn/courses/30/lessons/12911
"""
def solution(n):
answer= 0
bin_flag= True
one = list(str(bin(n))).count("1")
while bin_flag :
n+=1
bin_flag = not(str(bin(n))[2:].count("1") == one)
answer=n


return answer
39 changes: 39 additions & 0 deletions Hongjoo/lv2/캐시.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
https://school.programmers.co.kr/learn/courses/30/lessons/17680
"""

def solution(cacheSize, cities):
answer = 0
#1. 모든 도시이름 소문자 -> 대문자로 통일
for c in range(len(cities)):
cities[c]=cities[c].upper()
stack = []
# +예외처리 : chachSize가 0일때 -> answer = 5*dB개수
if cacheSize <= 0 :
return (5*len(cities))
# 2. City 개수만큼 반복
for i in range(len(cities)):
city = cities[i]
cache_miss = True
# 2-1 . Stack에 있을때(cache hit)
for s in range(len(stack)):
if city == stack[s][1] : # stack에 있음
answer += 1 # cache hit 시간 추가
cache_miss = False
stack[s][0] = answer # 데이터 사용 시간 업데이트

#2-2. 위 cache 탐색 후에도 Cache miss 발생
if cache_miss : # Cache miss 남
# [1] cache miss 발생& stack이 not full
answer += 5
if len(stack) < cacheSize:
stack.append([answer,city])
#[2] cache miss 발생 & stack 이 full 한 경우 -> replace 교체 (recently cache)
else :
# stack내에 검색 시간이 가장 이른 db 버리기
stack.sort(key = lambda x : x[0])
stack.pop(0)
# 새로운 data로 교체
stack.append([answer,city])

return answer
41 changes: 41 additions & 0 deletions Hongjoo/백준/숨바꼭질3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
https://www.acmicpc.net/problem/13549

[BOJ] 숨바꼭질3/골드5/
#promblem
- 이동 방법[t, x] -> [t+1 , x-1] ,[t+1 , x+1] # 걷기/ ``[t , 2*x]``
#flow : BFS (최단 시간 x , 최단 동작 횟수) or DFS
1. node= [현 위치 x , 현 시간 t]
2.이웃한 node - x+1 , x-1 , 2*x
(if , nx = 2*x -> t else :걷기 -> t+1)
IDEA
- 같은 level -> 이동(걷기+ 순간이동) 횟수 동일함
- 궁극적으로 최단 시간 = 최단 경로 , 순간이동을 가장 많이 해야함 (순간이동 우선순위가 높음)
=> 2*x -> x-1, x+1 순으로 다음 node 탐색하기
-> 중복되지만 순간이동으로 도달한 경우 vs 걸어서 도달한 경우를 비교할 때,
Comment on lines +11 to +15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 이번 문제에 이 순간이동 관련한 아이디어를 떠올리기가 힘들었는데... 잘 생각해서 정리해주신것 같아요!

"""

import sys
from collections import deque
input = sys.stdin.readline
MAX = 100001
N , K = map(int, input().split())
visited = [MAX] * (MAX+1) # 방문 여부
# 2. BFS로 N-> K 의 모든 경로 찾기
q = deque([N])
visited[N]= 0

while q :
cx= q.popleft()
ct = visited[cx]
if cx == K:
break
for nx in (2*cx , cx-1 , cx+1 ) :
if 0 <= nx < MAX and visited[nx] >= MAX : # nx 의 제한 조건
# 시간 업데이트
if nx == 2*cx :
visited[nx] = visited[cx]
else :
visited[nx] = visited[cx] +1
q.append(nx)
Comment on lines +26 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

큐에 좌표만 넣고 시간은 visited 배열에 따로 관리하면서 깔끔하고 가독성 좋은 코드가 된것 같습니다. 👍

print(visited[K])
41 changes: 41 additions & 0 deletions Hongjoo/백준/숨바꼭질4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys
from collections import deque
input = sys.stdin.readline
N, K = map(int, input().split())
MAX = 100001
# answer = []
#[1] 최단 시간 찾기
visited = [[MAX, MAX] for _ in range(MAX+1)]# level 기입

t= 0
next_node = [N]
q = deque([N])
visited[N] = [t,N] # 도착 시간 , 이전 node 위치 확인

#(예외처리) 출발지 = 도착지 같은 경우
if K != N :
while q :
t+=1
next_node = len(q)
for i in range(next_node) : # 현재 level의 node개수만큼 반복
cx = q.popleft()
# print(f"cx {cx} , {next_node}")
# 만약 목적 달성시 , 끝
for nx in [cx -1 , cx+1 , cx*2]:
if 0<= nx <= MAX and visited[nx][0]>= MAX :
q.append(nx)
visited[nx] = [t, cx]
# 현재 q -> 다음 level 의 노드만 남아있는 상태
# 만약 K을 도달한 경우-> 최단 시간 저장
if visited[K][0]< MAX :
break
print(t)
#[2] 역추적 - 최단 시간 경우 , 경로 추적
re_visited = [K]
pt = K
while pt != N :
_ , pt =visited[pt]
re_visited.append(pt)

print(" ".join(map(str,list(reversed(re_visited)))))

36 changes: 36 additions & 0 deletions Hongjoo/백준/숨박꼭질.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
from collections import deque
input = sys.stdin.readline
MAX = 100000
# 1. N, K 입력변수 입력 받기
N ,K = map(int, input().split())
# 2. N과 K 가 같을 때 , 예외처리
if N == K :
print(0)
exit()

# 3. BFS 로 K까지 도달하는데 최단 거리 출력하기
q = deque([N])
visited = [False] * (MAX + 1)
visited[N] = True
lv = 1
#[1] while 을 최단거리(level) 만큼 반복
while q:
level_size = len(q)
next_lv = []
#[2] 현 lv 의 노드만 BFS 탐색 & 다음 lv 후보군 노드을 enqueue
for _ in range(level_size) :
cx = q.popleft()
for nx in (cx-1 , cx+1 ,2*cx) :
if 0 <= nx <= MAX:
if nx == K : # 목적지 도착
print(lv)
exit()
# 첫 방문
if not visited[nx] :
q.append(nx)
next_lv.append(nx)
Comment on lines +21 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 여기서 왜 2중 for문을 쓰셨는지 알 수 있을까요???

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# print(f"##{lv}= {level_size} => {next_lv}")
lv +=1
for x in next_lv :
visited[x] = True
46 changes: 46 additions & 0 deletions Hongjoo/백준/숨박꼭질2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
# [BOJ] 숩박꼭질 / 골드 4
https://www.acmicpc.net/problem/12851

-문제
goa) 수빈 -> 동생 위치까지 도달하는데 최단"시간" & 경우의 수 (미친?)
# 조건
i. 수빈의 위치 N , 동생 K (1차원)
2. 이동 방법 (현 위치:x ) => 3가지
(1) 걷기 : x-1 , x+1
(2) 순간이동 : 2*x
- 유형: BFS
field[x] = [[걸리는 시간,경우의 수]]

5 17
"""
Comment on lines +1 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지난 주 풀이도 함께 올려주셨네요! 👍
지난 주 발제자료로 다양한 풀이를 추가해두었는데 참고해주세요!
https://github.com/AlgorithmStudy-Allumbus/codingtest_algorithm_study/blob/main/_WeeklyChallenges/W26-%5BGraph-BFS%5D/Study_BOJ_12851_%EC%88%A8%EB%B0%94%EA%BC%AD%EC%A7%882.py

import sys
from collections import deque
N , K = map(int, sys.stdin.readline().split())
INF = 1000001
field = [[INF,0]]*INF# field[x] = 최단 시간
# BFS 특 : 먼저 방문한 경우가 최단 시간임
q = deque([N])
field[N] = [0,1] # 최단시간 , 경우의 수

while q :
x = q.popleft() # 현 위치
ct , cn =field[x] # 현 위치에서 최단 시간 , 경우의 수
# if field[K][0] < ct : # 종료 조건 :
# break
for nx in [x-1 , x+1 , 2*x]:
if 0<= nx < INF:
#[1] 첫 방문인 경우 : field[nx][1] == 0 -> 최단 시간 업데이트 & q 에 넣기
if field[nx][1] == 0 :
field[nx] = [ct +1 ,cn]
q.append(nx) # 위치 업데이트
# print(f"update {nx} => field[nx] : {field[nx]}")
#[2] 중복 방문인 경우(최단 시간이 같을때)-> field[x][1]누적 경로의 수 추가
elif field[nx][0] == ct +1 :
field[nx][1] += cn
# print(f"## duplicate :{nx} - {field[nx]}")
# 최단 시간이 더 큼 -> 암 것도 없음

print(field[K][0])
print(field[K][1])

48 changes: 48 additions & 0 deletions Hongjoo/백준/숨박꼭질4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sys
from collections import deque
input = sys.stdin.readline
N, K = map(int, input().split())
MAX = 100000
# answer = []
#[1] 최단 시간 찾기
visited = [MAX]* (MAX+1) # level 기입
t= 0
next_node = [N]
q = deque([N])
visited[N] = t
while q :
t+=1
next_node = len(q)
for i in range(next_node) : # 현재 level의 node개수만큼 반복
cx = q.popleft()
# 만약 목적 달성시 , 끝
for nx in [cx -1 , cx+1 , cx*2]:
if 0<= nx <= MAX and visited[nx]>= MAX :
q.append(nx)
visited[nx] = t
# 현재 q -> 다음 level 의 노드만 남아있는 상태
# 만약 K을 도달한 경우-> 최단 시간 저장
if visited[K] <MAX :
# answer.append(t)
break

#[2]역추적K-> N - backtracking , DFS
re_visited = []
stack = deque([[K,t]])
while stack :
cx, ct = stack.pop()
if cx not in re_visited :
re_visited.append(cx)
if cx == N :
break
if cx %2==0 : #짝수면
ad_li = [cx-1,cx+1 , cx//2]
else :
ad_li = [cx-1, cx+1]
# print("##",ad_li , type(ad_li))
for nx in ad_li :
if 0<=nx <=MAX and nx not in re_visited and visited[nx] == ct-1:
stack.append([nx,ct-1])
print(t)
print(" ".join(map(str,list(reversed(re_visited)))))

60 changes: 60 additions & 0 deletions Hongjoo/백준/이모티콘.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
https://www.acmicpc.net/problem/14226
# 이모티콘 / 골드 4
# goal ) 화면에 S개 이모티콘 만드는데 드는 최소시간 구하기
- 3가지 작업(1sec)로 화면 1개 -> S 개
(1) 화면 속 모든 이모티콘 -> 클립보드에 "복사" <덮어쓰기 - ctrl+c>
(2) 클립보드 속 모든 이모티콘 -> 화면에 "붙여넣기" <추가- ctrl+v>
- 클립보드가 반드시 비어있지 않아야 붙여 넣기 가능
- 클립 보드 속 이모티콘은 일부만 삭제 , 복사 불가
- 붙여넣기 시, 클립보드에 있는 이모티콘 개수가 화면에 추가됨
(3) 화면 속 이모티콘 1개 삭제 <삭제 - delete>
# FLOW : BFS
i.
field = [screen : 화면 속 이모티콘 개수 , Backup: 클립보드 속 이모티콘 개수]
ii. 3가지 작업
[copy]
- Bacup[i] = Screen[i-1] 개수
[put]
if backup[i-1] !=0
- screen[i] = backup[i-1] + screen[i-1]
[delete]
- screen[i] = screen[i-1]
"""
import sys
from collections import deque
input = sys.stdin.readline

S = int(input())
visited = [[False]*1002 for _ in range(1002)]
visited[1][0] = True# visited [screen 개수][클립보드 개수] 조합 시 방문 여부 T/F

q = deque([[1,0,0]]) # screen 개수
# 3가지 각 종류의 작업 이후 화면 속 & 클립보드 속 이모티콘 개수
def function(num , screen , backup ):
if num == 0 : # copy
return screen , screen
elif num == 1 : # put
return screen + backup , backup
elif num == 2: # delete
return screen -1 , backup
# 2. BFS 작업
while q:
cs , cb , ct =q.popleft()
# 목표 달성시-> 끝내기
if cs == S :
break
if cb == 0 :
next_f = [0,2]
else :
next_f = [0,1,2]

for d in next_f :
ns , nb = function(d , cs , cb )
nt = ct+1
#BFS 화면 적합성
if 1<= ns <=1001 and not visited[ns][nb] :
q.append([ns, nb , nt])
visited[ns][nb] = True

print(ct)