-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/12973 """ # 풀이 과정 def solution(s): from collections import deque k = deque(list(s)) c = 0 check = [] while k: a = k.popleft() if check: if check[-1] == a: check.pop() while check: if len(check) >= 2: if check[-1] == check[-2]: check.pop() check.pop() else: break else: break else: check.append(a) else: check.append(a) return 1 if len(check) == 0 else 0
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/144853 """ """ SELECT BOOK_ID,date_format(PUBLISHED_DATE,'%Y-%m-%d') "PUBLISHED_DATE" from book where PUBLISHED_DATE like "2021%" and CATEGORY like "인문" order by PUBLISHED_DATE asc """
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/92343 """ # 풀이 과정 """ 양 모으기 # 조건 각 노드 방문시 양과 늑대가 따라옴 늑대가 양보다 같거나 많을 경우 양이 다 잡아먹힘 최대한 많은 양을 모아서 루트 노드로 돌아오기 그래프 형태로 볼 때 dfs 생각 방향 # info: i번 노드의 양 또는 늑대 여부 # edges: 연결된 두 노드를 나타냄 #생각방향 자식 노드를 거치려면 부모노드의 동물을 거쳐야한다 단순 생각 방향: 완탐 후 최대 양을 표현하면 된다 info를 숫자로 표현 후 순열로 한 후 모든 경우의 수를 표현가능 양을 최대로 모은 후 늑대가 있는 곳으로 가야한다 모든 노드들의 양이 최대 늑대 최소 값들을 구해야한다 dfs로 풀었던 등대 문제 생각 # dfs 사용 시 원복 개념 등산로 찾기에서 쓰인 개념 일부분 아이디어 사용!! """ v = [] result = [] def dfs(sheep, wolf, info, edges): global v, result if sheep > wolf: result.append(sheep) else: return for i, j in edges: # 부모노드 방문 여부 확인 if v[i] == 1 and v[j] == 0: v[j] = 1 # 늑대인 경우 if info[j] == 1: dfs(sheep, wolf + 1, info, edges) else: dfs(sheep + 1, wolf, info, edges) # 다른 부분 또한 방문 확인 위해 원복 v[j] = 0 def solution(info, edges): global v v = [0 for _ in range(len(info))] # 첫번째 방문 v[0] = 1 # sheep, wolf dfs(1, 0, info, edges) return max(result)
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/92341 """ # 풀이 과정 def solution(fees, records): # records:시각 차량 번호 내역 # fees:주차 요금 result = [] # 차량 번호가 작은거부터 요금 정산 max_time = 23 * 60 + 59 # 최대 시간 from collections import deque car_in = [] # 입차 car_out = [] # 출차 for a in records: k = a.split(" ") b = k[0].split(":") c = int(b[0]) * 60 + int(b[1]) if k[2] == "IN": car_in.append([int(k[1]), int(c)]) elif k[2] == "OUT": car_out.append([int(k[1]), int(c)]) car_in.sort(key=lambda x: (x[0], x[1])) car_out.sort(key=lambda x: (x[0], x[1])) car_in = deque(car_in) car_num = set([car_in[k][0] for k in range(len(car_in))]) car_num_check = {} # 차량 번호별 누적 시간 check for t in car_num: car_num_check[t] = 0 # 차량별 주차 시간 누적 계산 while car_in: v, w = car_in.popleft() if len(car_out) > 0: if car_out[0][0] == v: car_num_check[v] += car_out[0][1] - w del car_out[0] else: car_num_check[v] += max_time - w else: car_num_check[v] += max_time - w # print(car_num_check) car_num_check = sorted(car_num_check.items(), key=lambda item: item[0]) # print(car_num_check) for a, b in car_num_check: if b <= fees[0]: result.append(fees[1]) else: if (b - fees[0]) % fees[2] == 0: result.append(fees[1] + int((b - fees[0]) / fees[2]) * fees[3]) else: result.append(fees[1] + (int((b - fees[0]) / fees[2]) + 1) * fees[3]) return result
-
""" 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/12927 """ # 풀이 과정 import heapq def solution(n, works): # map(lambda x:x*x,works) new = [] for i in works: heapq.heappush(new, -i) while n > 0: k = heapq.heappop(new) k += 1 heapq.heappush(new, k) n -= 1 if new[0] >= 0: # print(new) return 0 return sum(list(map(lambda x: x * x, new)))
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/42584 """ # 풀이 과정 def solution(prices): p = prices result = [] for a in range(len(prices) - 1): num = p[a] count = 0 flag = False for b in range(a + 1, len(p)): count += 1 if p[b] < num: result.append(count) flag = True break if flag == False: result.append(count) result.append(0) return result
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/151136 """ """ 풀이 과정 # 반올림 함수 round SELECT round(avg(DAILY_FEE)) from CAR_RENTAL_COMPANY_CAR where CAR_TYPE ="SUV" """
-
""" 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/151138 """ """ 풀이 과정 select HISTORY_ID,CAR_ID,Date_format(START_DATE,"%Y-%m-%d") "START_DATE",Date_format(END_DATE,"%Y-%m-%d") "END_DATE", CASE when datediff(END_DATE, START_DATE) >= 29 then "장기 대여" else "단기 대여" END "RENT_TYPE" from CAR_RENTAL_COMPANY_RENTAL_HISTORY where START_DATE Like "2022-09%" order by HISTORY_ID desc """
-
출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/157343 풀이 과정 select * from CAR_RENTAL_COMPANY_CAR # %를 통하여 앞뒤로 네비게이션이 들어간 목록을 찾는다 where OPTIONS like "%네비게이션%" order by CAR_ID desc
-
""" 출처: 프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/87694 """ # 풀이 과정 """ 사각형들의 모든 점들을 알아낸 후 주어진 점을 이동시키는 것을 실행 그 중 사각형 내부에 있는 점 거르기+중복점 제거 생각 아이디어 그리고 매 순간 그 중 아이템이 있는 점에 도달하는 지 여부 확인+ 최단 거리 구하기 # 주어진 조건들 통하여 알 수 있는 정보: 주어진 점들의 좌표들, 선분들의 좌표, 외부에 있는 점들의 좌표, """ from collections import deque def solution(rectangle, characterX, characterY, itemX, itemY): # sol(내부의 선분등을 제대로 표현하기 힘듬) # # 내부에 있지 않은 점 # check=set([]) # for x1,y1,x2,y2 in rectangle: # new=[] # for v1,w1,v2,w2 in rectangle: # if v1<x1<v2 and w1<y1<w2: # new.append((x1,y1)) # if v1<x2<v2 and w1<y1<w2: # new.append((x2,y1)) # if v1<x1<v2 and w1<y2<w2: # new.append((x1,y2)) # if v1<x2<v2 and w1<y2<w2: # new.append((x2,y2)) # check=check|(set([(x1,y1),(x1,y2),(x2,y1),(x2,y2)])-set(new)) # 이동시킬 맵 m = [[0] * (101) for _ in range(101)] dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] # 사각형 위의 모든 점 new = [] for x1, y1, x2, y2 in rectangle: for x in [x1 * 2, x2 * 2]: for y in range(y1 * 2, (y2) * 2 + 1): new.append((x, y)) for y in [y1 * 2, y2 * 2]: for x in range(x1 * 2, (x2) * 2 + 1): new.append((x, y)) new = set(new) # 중복 제거 # 사각형 내부의 점 구분 check = [] for x1, y1, x2, y2 in rectangle: for x, y in new: if x1 * 2 < x < x2 * 2 and y1 * 2 < y < y2 * 2: check.append((x, y)) road = new - set(check) for x, y in road: m[x][y] = 1 # m[characterX][characterY]="start" # m[itemX][itemY]="destination" q = [(characterX * 2, characterY * 2)] # 방문처리 확인 v = [[0] * 101 for _ in range(101)] plus = [] # 거리 result = 0 # bfs while q: x, y = q.pop() # 방문 처리 v[x][y] = 1 if x == itemX * 2 and y == itemY * 2: return result // 2 for nx, ny in zip(dx, dy): if 0 <= x + nx <= 100 and 0 <= y + ny <= 100 and v[x + nx][y + ny] == 0 and m[x + nx][y + ny] == 1: plus.append((x + nx, y + ny)) if len(q) == 0: plus = list(set(plus)) q += plus result += 1 plus = []