Lv.4
-행렬과 연산
from collections import deque
def shiftrow(rc,rows,cols):
rows.appendleft(rows.pop())
cols[0].appendleft(cols[0].pop())
cols[1].appendleft(cols[1].pop())
def rotate(rc,rows,cols):
rows[len(rc) - 1].append(cols[1].pop())
cols[0].append(rows[len(rc) - 1].popleft())
rows[0].appendleft(cols[0].popleft())
cols[1].appendleft(rows[0].pop())
def solution(rc, operations):
answer = []
rows = deque(deque(i[1:-1]) for i in rc)
cols = [deque(rc[i][0] for i in range(len(rc))), deque(rc[i][len(rc[0]) - 1] for i in range(len(rc)))]
rc = deque(rc)
for i in range(len(operations)):
if operations[i] == 'ShiftRow':
shiftrow(rc,rows,cols)
else:
rotate(rc,rows,cols)
for i in range(len(rc)):
answer.append([])
answer[i].append(cols[0][i])
answer[i].extend(rows[i])
answer[i].append(cols[1][i])
return answer
-짝수행 만들기(실패)
Lv.3
-모두 0으로 만들기
from collections import deque
def solution(a, edges):
answer = 0
graph = [[] for i in range(len(a))]
for v1, v2 in edges:
graph[v1].append(v2)
graph[v2].append(v1)
route = []
visit = [0] * len(a)
visit[0] = 1
deq = deque([0])
while deq:
now_location = deq.popleft()
route.append(now_location)
for j in graph[now_location]:
if visit[j] == 0:
visit[j] = 1
deq.append(j)
visit = [0] * len(a)
for i in range(len(a)-1, -1, -1):
node = route[i]
visit[node] = 1
if a[node]:
for j in graph[node]:
if visit[j] == 0:
a[j] += a[node]
answer += abs(a[node])
a[node] = 0
return answer if a[0] == 0 else -1
-풍선 터트리기
def solution(a):
answer = 0
min_list = [[] for i in range(len(a))]
min_value = int(1000000000)
for i in range(len(a)):
if a[i] < min_value:
min_list[i].append(a[i])
min_value = a[i]
else:
min_list[i].append(min_value)
min_value = int(1000000000)
for i in range(len(a)-1, -1, -1):
if a[i] < min_value:
min_list[i].append(a[i])
min_value = a[i]
else:
min_list[i].append(min_value)
for i in range(len(a)):
if a[i] <= min_list[i][0] or a[i] <= min_list[i][1]:
answer += 1
return answer
-숫자게임(실패)