-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/12911 """ # 풀이 과정 def solution(n): a = bin(n) b = a[2:] c = b.count("1") k = n while True: k += 1 t = bin(k) v = t[2:].count("1") if v == c: return k answer = 0 return answer
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/42884 """ # 풀이 과정 from collections import defaultdict from collections import deque import heapq def solution(routes): check = [] car_start = defaultdict(set) car_end = defaultdict(set) for c in range(len(routes)): check.append(routes[c][0]) check.append(routes[c][1]) car_start[routes[c][0]].add(c) car_end[routes[c][1]].add(c) start = min(check) end = max(check) check.sort() now = set([]) result = 0 check_car = set([]) # 이전에 발견되던게 지금 발견 안되면 그 지점은 무조건 카메라 설치 필수 for time in check: now = now | car_start[time] # 빠지는 차량 발생 if len(car_end[time]) > 0: if len(car_end[time] - check_car) > 0: result += 1 check_car = check_car | car_end[time] | now return result
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/42583 """ # 풀이과정 def solution(bridge_length, weight, truck_weights): from collections import deque b_l = bridge_length # 최대 수용 가능 차량 수 w = weight # 무게 t_w = deque(truck_weights) now = 0 # 시간 count_w = 0 # 다리 위 차량 무게 count_c = 0 # 차량 수 check = deque([]) while t_w: k = t_w.popleft() if len(check) == 0: check.append([now, k]) count_w += k count_c += 1 else: if now - check[0][0] == b_l: # 차가 다리를 벗어나는 시점 i, j = check.popleft() count_w -= j count_c -= 1 if count_w + k <= w and count_c + 1 <= b_l: check.append([now, k]) count_w += k count_c += 1 else: t_w.appendleft(k) now += 1 # print(check) continue if count_w + k <= w and count_c + 1 <= b_l: check.append([now, k]) count_w += k count_c += 1 else: t_w.appendleft(k) now += 1 # print(check) return check[-1][0] + b_l + 1
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/77486 """ # 내 풀이 """ 생각방향:등비급수처럼 리스트를 만든 후 맨 위에 지속적으로 더해주는 형태 목표:enroll에 적힌 판매원들의 수익을 출력 """ from collections import defaultdict import math # 판매원 수익 명단 dic check = [] name = [] # 추천인 분배 def divide(e, r, n, p): global check, name # 1원 미만으로 분배금이 떨어질 경우 분배금 발생 x while n >= 1: # 등록자 번호 위치 k = name[p] # 추천인이 없는 경우 if r[k] == "-": break # 추천인이 있는 경우 else: check[r[k]] += math.ceil(n * (0.9)) n = int(n * (0.1)) # 더 위의 상위순번 추천자 시작 p = r[k] return 0 def solution(enroll, referral, seller, amount): global check, name check = defaultdict(int) name = defaultdict(int) for i, j in enumerate(enroll): check[j] = 0 name[j] = i # 구성원 e = enroll # 추천인 찾기 r = referral # 판매 집계 데이터의 판매원 이름 s = seller # 판매 집계 데이터 a = amount for i in range(len(a)): n = a[i] * 100 # 판매량*단가 p = s[i] # 판매원 # 처음 판매자 수익금 가져가기 check[p] += math.ceil((0.9) * (n)) # 분배할 금액 n = int(n * (0.1)) # 구성원 명단,추천인 명단,남은 분배금액, 최초 판매자 divide(e, r, n, p) result = [] for f in enroll: result.append(check[f]) return result
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/17677 """ # 풀이 과정 def solution(str1, str2): # a,b 모두 공집합일 경우 자카드 유사도 1로 정의 eng = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" eng = list(eng) str1 = list(str1.upper()) str2 = list(str2.upper()) a = [] # str1 집합 b = [] # str2 집합 u = 0 n = 0 for i in range(len(str1) - 1): new = "" if str1[i] in eng: new += str1[i] else: continue if str1[i + 1] in eng: new += str1[i + 1] else: continue a.append(new) for i in range(len(str2) - 1): new = "" if str2[i] in eng: new += str2[i] else: continue if str2[i + 1] in eng: new += str2[i + 1] else: continue if new in a: u += 1 n += 1 k = a.index(new) del a[k] else: u += 1 u += len(a) if u == 0 and n == 0: return 65536 else: return int((n / u) * 65536)
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/43162 """ # 풀이 과정 from collections import defaultdict from collections import deque import copy def solution(n, computers): network = defaultdict(set) result = [] for i in range(len(computers)): network[i].add(i) for j in range(len(computers[0])): if computers[i][j] == 1: network[i].add(j) network[j].add(i) check = deque([i for i in range(n)]) visit = set([]) new = set([]) while check: start = check.popleft() if start in visit: continue q = deque([start]) while q: v = q.popleft() visit.add(v) new.add(v) for end in network[v]: if not end in visit and not end in new: q.append(end) net = copy.deepcopy(new) result.append(net) return len(result)
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/42586 """ # 풀이 과정 def solution(progresses, speeds): from collections import deque p = deque(progresses) s = deque(speeds) result = [] count = 0 # 작업 개수 check = len(p) # 1사이클 while p: count += 1 a = p.popleft() b = s.popleft() a += b p.append(a) s.append(b) if count == check: finish = 0 for f in list(p): if f >= 100: p.popleft() s.popleft() finish += 1 else: break if finish != 0: result.append(finish) check = len(p) count = 0 else: check = len(p) count = 0 return result
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/12979 """ # 풀이 과정 from collections import deque def solution(n, stations, w): before = set(stations) # 기지국 설치 new = set([]) s = deque(stations) min_network = 0 max_network = 0 result = 0 # 시작부분 범위 내에 없는 경우 if s[0] - w > 1: max_network = 1 + 2 * w new.add(1 + w) result += 1 # 기지국 개설 후 시작 else: max_network = s.popleft() + w while max_network < n: if len(s) > 0: # 이미 설치된 곳의 전파가 드는 권역에 있는 경우(연장하기) if s[0] - w <= max_network <= s[0] + w: max_network = s[0] + w s.popleft() # 전파가 드는 권역이 아닌 경우(최대한 넓은 범위를 커버하게하기) elif s[0] - w >= max_network + 1: result += 1 new.add(max_network + 1 + w) max_network = max_network + 1 + 2 * w # 이미 최대로 설치된 곳의 최대치보다 범위가 현재 넓은 경우 elif s[0] + w < max_network: while True and s: if s[0] + w <= max_network: s.popleft() else: break else: result += 1 new.add(max_network + 1 + w) max_network = max_network + 1 + 2 * w # print(s,max_network) return len(new - before)
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/138476 """ #풀이과정 def solution(k, tangerine): count = {} for x in tangerine: if not str(x) in count: count[str(x)] = 1 else: count[str(x)] += 1 count = sorted(count.items(), key=lambda x: -x[1]) result = [] for y, z in count: if k > 0: result.append(y) k -= z else: break return len(result)
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/challenges?order=recent&levels=3&languages=python3&page=3 """ # 풀이 과정 from collections import deque # 기둥 유지 가능 여부 확인 def check_up(x, y, check): # 그 아래 기둥이 있는 경우 if (x, y - 1, x, y) in check: # check.add((x,y,x,y+1)) return True # 그 아래 보가 있을 경우 elif (x - 1, y, x, y) in check or (x, y, x + 1, y) in check: # check.add((x,y,x,y+1)) return True else: return set([(x, y, x, y + 1)]) # 보 유지 가능 여부 확인 def check_right(x, y, check): # 밑에 기둥이 있거나, 양쪽 보가 존재가 할 경우 if (x, y - 1, x, y) in check or (x + 1, y - 1, x + 1, y) in check: # check.add((x,y,x+1,y)) return True # 양쪽 보가 존재할 경우 elif (x - 1, y, x, y) in check and (x + 1, y, x + 2, y) in check: # check.add((x,y,x+1,y)) return True else: return set([(x, y, x + 1, y)]) def solution(n, build_frame): # 기둥,보 설치 확인 check = set([]) for x, y, a, b in build_frame: # 설치 1 if b == 1: # 기둥 if a == 0: # 바닥인경우 if y == 0: check.add((x, y, x, y + 1)) else: # 그 아래 기둥이 있는 경우 if (x, y - 1, x, y) in check: check.add((x, y, x, y + 1)) # 그 아래 보가 있을 경우 elif (x - 1, y, x, y) in check or (x, y, x + 1, y) in check: check.add((x, y, x, y + 1)) # 보 else: if y != 0: # 밑에 기둥이 있거나, 양쪽 보가 존재가 할 경우 if (x, y - 1, x, y) in check or (x + 1, y - 1, x + 1, y) in check: check.add((x, y, x + 1, y)) # 양쪽 보가 존재할 경우 elif (x - 1, y, x, y) in check and (x + 1, y, x + 2, y) in check: check.add((x, y, x + 1, y)) else: q = deque([]) up = [(0, 1, 0, 2), (-1, 1, 0, 1), (0, 1, 1, 1)] # 기둥 right = [(1, 0, 2, 0), (1, 0, 1, 1), (-1, 0, 0, 0), (0, 0, 0, 1)] # 보 # 기둥 if a == 0: k = set([(x, y, x, y + 1)]) check = check - k # 기둥 제거 # 제거되는지 여부 판단 for a, b, c, d in up: if (x + a, y + b, x + c, y + d) in check: # 기둥 if d - b == 1: flag = check_up(x + a, y + b, check) if flag != True: check = check | k break else: flag = check_right(x + a, y + b, check) if flag != True: check = check | k break else: k = set([(x, y, x, y + 1)]) check = check - k # 기둥 제거 """ for a,b,c,d in up: if (x+a,y+b,x+c,y+d) in check: q.append((x+a,y+b,x+c,y+d)) """ # 보 제거 여부 판단 else: k = set([(x, y, x + 1, y)]) check = check - k # 기둥 제거 # 제거되는지 여부 판단 for a, b, c, d in right: if (x + a, y + b, x + c, y + d) in check: # 기둥 if d - b == 1: flag = check_up(x + a, y + b, check) if flag != True: check = check | k break else: flag = check_right(x + a, y + b, check) if flag != True: check = check | k break else: k = set([(x, y, x + 1, y)]) check = check - k # 기둥 제거 """ for a,b,c,d in right: if (x+a,y+b,x+c,y+d) in check: q.append((x+a,y+b,x+c,y+d)) break else: k=set([(x,y,x+1,y)]) check=check-k # 보 제거 """ """ for a,b,c,d in right: if (x+a,y+b,x+c,y+d) in check: q.append((x+a,y+b,x+c,y+d)) """ # 기둥 또는 보를 제거함으로써 주변 제거할 기둥 보 탐색 """ while q: k=q.popleft() # 기둥 if k[3]-k[1]!=0: shape="기둥" exist=check_up(k[0],k[1],check) # 보 else: shape="보" exist=check_right(k[0],k[1],check) if exist==True: continue else: check=check-exist if shape=="기둥": for a,b,c,d in up: if (k[0]+a,k[1]+b,k[0]+c,k[1]+d) in check: q.append((k[0]+a,k[1]+b,k[0]+c,k[1]+d)) else: for a,b,c,d in right: if (k[0]+a,k[1]+b,k[0]+c,k[1]+d) in check: q.append((k[0]+a,k[1]+b,k[0]+c,k[1]+d)) """ # print(check,"check") # 마무리 단계 재 구조화 result = [] for a, b, c, d in check: if d - b == 1: s = 0 else: s = 1 result.append([a, b, s]) result.sort() return result