Simple_PS

  • Lv2 프로그래머스(Programmers)[Python][파이썬] 연속된 부분 수열의 합
    """ 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/178870 """ # 풀이 과정 def solution(sequence, k): a = 0 b = 0 c = 0 result = [] while True: if a == 0 and b == 0 and c == 0: c += sequence[a] if c == k: result.append([a, b]) else: if c < k: b += 1 if b > len(sequence) - 1: break c += sequence[b] else: c -= sequence[a] if a > len(sequence) - 1: break a += 1 if c == k: result.append([a, b]) result.sort(key=lambda x: ([x[1] - x[0], x[0]])) return result[0]
  • Lv3 프로그래머스(Programmers)[Python][파이썬] 블록 이동하기
    """ 프로그래머스 출처:https://school.programmers.co.kr/learn/courses/30/lessons/60063 """ # 풀이 과정 """ bfs """ from collections import deque def arround(x, y, check_x, check_y): check = [[(x + 1, y), (x - 1, y)], [(x, y + 1), (x, y - 1)]] for i in check: if (check_x, check_y) in i: continue else: d = i k = [] for nx, ny in d: if nx != x: k.append((nx, ny, nx, check_y)) elif ny != y: k.append((nx, ny, check_x, ny)) return k def solution(board): m = len(board) n = len(board[0]) # 이동 dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] # 회전8,이동4 robot = ((0, 0), (0, 1)) q = deque([((0, 0), (0, 1), 0)]) visit = set([]) result = float("inf") while q: z = q.popleft() if (m - 1, n - 1) in z: result = min(result, z[2]) continue x1, x2 = z[0][0], z[0][1] y1, y2 = z[1][0], z[1][1] count = z[2] if not ((z[0]), (z[1])) in visit or not ((z[1]), (z[0])) in visit: visit.add(((z[0]), (z[1]))) visit.add(((z[1]), (z[0]))) else: continue # 이동 for nx, ny in zip(dx, dy): if 0 <= nx + x1 < m and 0 <= ny + x2 < n and 0 <= nx + y1 < m and 0 <= ny + y2 < n: if board[nx + x1][ny + x2] == 0 and board[nx + y1][ny + y2] == 0 and not ((nx + x1, ny + x2), ( nx + y1, ny + y2)) in visit and not ((nx + y1, ny + y2), (nx + x1, ny + x2)) in visit: q.append(((nx + x1, ny + x2), (nx + y1, ny + y2), count + 1)) # visit.add(((nx+x1,ny+x2),(nx+y1,ny+y2))) # visit.add(((nx+y1,ny+y2),(nx+x1,ny+x2))) check = arround(x1, x2, y1, y2) for nx, ny, mx, my in check: if 0 <= nx < m and 0 <= ny < n: if board[nx][ny] == 0 and board[mx][my] == 0 and not ((nx, ny), (x1, x2)) in visit and not ((nx, ny), ( x1, x2)) in visit: q.append(((x1, x2), (nx, ny), count + 1)) # visit.add(((x1,x2),(nx,ny))) # visit.add(((nx,ny),(x1,x2))) check = arround(y1, y2, x1, x2) for nx, ny, mx, my in check: if 0 <= nx < m and 0 <= ny < n: if board[nx][ny] == 0 and board[mx][my] == 0 and not ((nx, ny), (y1, y2)) in visit and not ((nx, ny), ( y1, y2)) in visit: q.append(((y1, y2), (nx, ny), count + 1)) # visit.add(((y1,y2),(nx,ny))) # visit.add(((nx,ny),(y1,y2))) return result
  • Lv2 프로그래머스(Programmers)[Python][파이썬] 메뉴 리뉴얼
    """ 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/72411 """ # 풀이 과정 def solution(orders, course): from collections import Counter from itertools import combinations # menu=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") result = [] k = {} for a in orders: b = list(a) for c in course: d = list(combinations(b, c)) for e in d: e = sorted(list(e)) e = "".join(e) if e in k: k[e] += 1 else: k[e] = 1 set_menu = {} for f in k: if k[f] < 2: continue else: set_menu[f] = k[f] course_menu = [] for g in course: t = [] for h in set_menu: if len(h) == g: if len(t) == 0: t.append(h) else: if set_menu[h] > set_menu[t[-1]]: t.clear() t.append(h) elif set_menu[h] == set_menu[t[-1]]: t.append(h) course_menu = course_menu + t # course_menu=sorted(course_menu, key=len,reverse=True) course_menu.sort() for i in orders: last_check = [] for j in course_menu: if set(i) | set(j) == set(i) and j not in result: last_check.append(j) # print(last_check) result = result + last_check result.sort() return result
  • Lv3 프로그래머스(Programmers)[Python][파이썬] 불량사용자
    """ 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/64064 """ # 풀이 과정 from collections import deque def solution(user_id, banned_id): # 불량사용자 아이디 항목별 종류 check = [[] for _ in range(len(banned_id))] if len(banned_id) == 0: return 0 for num in range(len(banned_id)): for j in user_id: ban = banned_id[num] if len(ban) == len(j): for i in range(len(ban)): if ban[i] != "*" and ban[i] != j[i]: break elif ban[i] == "*": continue else: check[num].append(j) else: continue if len(banned_id) == 1: return len(set(check[0])) result = [] q = deque([]) # 기본 큐 만들기 for k in check[0]: q.append([k]) for ban in range(1, len(banned_id)): add_mem = [] for new in check[ban]: for before in q: add_mem.append(before + [new]) q = deque([]) q += add_mem count = len(q) while count > 0: t = q.popleft() count -= 1 if len(t) != len(set(t)): continue else: q.append(t) result = [] for r in add_mem: k = set(r) if len(r) == len(k): if len(result) == 0: result.append(k) else: for n in result: if len(n - k) == 0: break else: result.append(k) return len(result)
  • Lv2 프로그래머스(Programmers)[Python][파이썬] 멀쩡한 사각형
    """ 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/62048 """ # 풀이 과정 def solution(w, h): result = 0 if w == 1 or h == 1: return 0 if h < w: for k in range(h): result += int((k * w / h)) elif h == w: for k in range(w): return w * h - w else: for k in range(w): result += int(k * h / w) return result * 2
  • Lv3 프로그래머스(Programmers)[Python][파이썬] 부대 복귀
    """ 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/132266 """ # lv3 부대 복귀 # 내 풀이 # 조건 # 각 부대 지역 고유번호 지정 # 임무 완료 후 최단 시간으로 부대 복귀 # 적군의 방해로 지역이 막혀 부대 복귀 불가능한 대원 발생 가능 # 접근 사고 방향 # 최단 거리를 구하는 조건이라 다익스트라 알고리즘 떠올려야 하나 익숙하지 않아 dfs로 1차 접근 # dfs 방문 여부로 인한 최단거리 구성의 어려움으로 인한 bfs로 선회 후 재구성: # >1차: 시간초과 발생 # 2차 생각 전환 # 방향전환: 꼭 출발점에서 목적지를 찾아가야하나? 목적지에서 시작점까지를 돌면 source마다 할 필요 없이 반복 필요 없음 # 도착점에서 갈 수 없는 곳 모두 -1로 생각! # 목표:최단 시간을 담은 값을 리턴 # 총 지역수: n , 길 정보 roads , 부대원 위치: sources 강철부대 지역:destination from collections import deque from collections import defaultdict import sys sys.setrecursionlimit(10 ** 6) # 최대 깊이 설정 m = defaultdict(list) def solution(n, roads, sources, destination): check = [-1 for _ in range(n + 1)] # 각 부대별 최단 거리 정리 # 도착점으로부터 시작으로 사고 전환! 도착 못하면 -1 result = [] r = roads s = sources d = destination # 위치별 정리 for i, j in r: m[i].append(j) m[j].append(i) v = [False for _ in range(n + 1)] # 방문 여부 q = deque([[d, 0]]) new = [] # 거리에 따라 추가할 부분 while q: x, t = q.popleft() v[x] = True # 방문 check[x] = t new += m[x] if len(q) == 0: new = list(set(new)) # 중복 제거 if len(new) == 0: break for u in [y for y in new if v[y] == False]: q.append([u, t + 1]) if len(q) == 0: break new = [] # 그 다음 순서를 담기 위해서 초기화!! for z in s: result.append(check[z]) return result
  • Lv2 프로그래머스(Programmers)[Python][파이썬] 마법의 엘리베이터
    """ 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/148653 """ # 풀이 과정 def solution(storey): x = list(str(storey)) x = list(map(int, x)) count = 0 if len(x) == 1: return x[0] if x[0] <= 5 else 10 - x[0] + 1 for k in range(len(x) - 1, 0, -1): a = 10 - x[k] b = x[k] if b < 5: count += b elif b == 5: if k - 1 >= 0 and x[k - 1] >= 5: x[k - 1] = x[k - 1] + 1 count += b else: count += b else: count += a if k - 1 >= 0: x[k - 1] += 1 if x[0] <= 5: return count + x[0] else: return count + 10 - x[0] + 1
  • Lv3 프로그래머스(Programmers)[Python][파이썬] 보석 쇼핑
    """ 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/67258 """ # 풀이 과정 """ 투포인터 """ import heapq from collections import defaultdict from collections import deque from collections import Counter def solution(gems): # 종류 check = len(set(gems)) start, end = 0, 0 my = defaultdict(int) result = float("inf") result_start, result_end = float("inf"), float("inf") my[gems[start]] += 1 while start <= len(gems) - 1: if len(my) != check: if end < len(gems) - 1: end += 1 my[gems[end]] += 1 else: break else: if end - start < result: result = end - start result_start, result_end = start, end if start == end: return [start + 1, end + 1] else: my[gems[start]] -= 1 if my[gems[start]] == 0: del my[gems[start]] start += 1 # 마지막 if not result_start <= len(gems) and not result_end <= len(gems): return [1, len(gems)] return [result_start + 1, result_end + 1]
  • Lv2 프로그래머스(Programmers)[Python][파이썬] 리코쳇 로봇
    """ 출처:프로그래머스, https://school.programmers.co.kr/learn/courses/30/lessons/169199 """ # 풀이 과정 def solution(board): from collections import deque check = deque() finish = [] m, n = len(board), len(board[0]) check_board = [[" "] * n for k in range(m)] # 최소 거리를 담는 보드 for x in range(len(board)): # 출발 및 목적지 위치 확인 for y in range(len(board[0])): if board[x][y] == "R": check.append([x, y, 0]) if board[x][y] == "G": finish.append([x, y]) if check and finish: break f_x, f_y = finish[0][0], finish[0][1] dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] while check: a, b, c = check.popleft() for v, w in zip(dx, dy): new_a = a new_b = b while True: if 0 <= new_a + v < len(board) and 0 <= new_b + w < len(board[0]) and board[new_a + v][ new_b + w] != "D": new_a += v new_b += w else: if check_board[new_a][new_b] == " ": check.append([new_a, new_b, c + 1]) check_board[new_a][new_b] = c + 1 break else: if check_board[new_a][new_b] > c + 1: check.append([new_a, new_b, c + 1]) check_board[new_a][new_b] = c + 1 break return -1 if check_board[f_x][f_y] == " " else check_board[f_x][f_y]
  • Lv3 프로그래머스(Programmers)[Python][파이썬] 베스트 앨범
    """ 출처:프로그래머스 https://school.programmers.co.kr/learn/courses/30/lessons/42579 """ # 내 풀이 from collections import defaultdict def solution(genres, plays): g = defaultdict(int) g_num = defaultdict(list) for i in range(len(genres)): g[genres[i]] += plays[i] g_num[genres[i]].append((plays[i], i)) check = [] for k in g_num.keys(): g_num[k].sort(key=lambda x: (-x[0], x[1])) check.append((g[k], k)) check.sort(reverse=True) result = [] for i, j in check: n = 0 for count, num in g_num[j]: result.append(num) n += 1 if n == 2: break return result
  • << 9 10 11 12 13 14 15 16 17 18 19 >>