2018 3

[Programmers][Python] KAKAO 2018 프렌즈 4블록

자주나오는 유형. 규칙에따라 블록 부수고 중력작용. def bomb_block(b, m, n): block = set() for i in range(m - 1): for j in range(n - 1): if b[i][j] and b[i][j] == b[i][j + 1] == b[i + 1][j] == b[i + 1][j + 1]: for dx, dy in [(0, 0), (1, 0), (0, 1), (1, 1)]: nx, ny = i + dx, j + dy block.add((nx, ny)) if not block: return 0, 0 for x, y in block: b[x][y] = 0 new_board = [[0] * n for _ in range(m)] for j in range(n): idx..

[프로그래머스][Python] 2018카카오블라인드1차: 캐시

내풀이. deque하면 pop안에 인덱스가 안들어 간다는걸 몰랐다. 그래서 결국 그냥 큐로 바꿈. def solution(cacheSize, cities): cache = [] total_cost = 0 for city in cities: lo_city = city.lower() if lo_city not in cache: cache.append(lo_city) total_cost += 5 if len(cache) > cacheSize: cache.pop(0) else: cache.append(cache.pop(cache.index(lo_city))) total_cost += 1 return total_cost 남의 코드 볼 것 :maxlen으로 크기제한, remove로 바로 없애기. # 남의 풀이 de..

[백준][Python] 2018 수들의 합

간격 컨트롤이 아닌 내부 요소들의 합으로 컨트롤하는 슬라이딩 윈도우. import sys input = sys.stdin.readline N = int(input()) prex_sum, left = 0, 0 answer = 0 # 슬라이딩 윈도우 for right in range(1,N+1): # 결론적으로 end는 기존 슬라이딩 윈도우 처럼 일정 간격이 고정이 아니라 # 일정 합 이상이되면 알아서 그 간격이 줄어드는 시스템이므로 한번 순회로 전부 조사 가능. while left < N and prex_sum < N: prex_sum += left+1 left += 1 if prex_sum == N: answer += 1 prex_sum -= right print(answer)