Skip to content

Commit

Permalink
알고리즘
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyeongchoi committed Mar 15, 2021
1 parent 42c8bd1 commit cc45867
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions argorithm/algorima1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from collections import deque

def bfs(board, i, j, visited):
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
val = board[i][j]
queue = deque([(i, j, 1)])
while queue:
x, y, cnt = queue.popleft()
for k in range(4):
nx, ny = x+dx[k], y+dy[k]
if not(0<=nx<4 and 0<=ny<4):
continue
if board[nx][ny]==val and visited[x][y]<2:
visited[x][y]+=1
queue.append((nx, ny, cnt+1))
return cnt

def solution(board):
visited = [[0]*4 for _ in range(4)]
answer = []
for i in range(4):
for j in range(4):
answer.append(bfs(board, i, j, visited))
if max(answer) > 1:
return max(answer)
else:
return -1

if __name__ == '__main__':
print(solution([[3,2,3,2],[2,1,1,2],[1,1,2,1],[4,1,1,1]]))
19 changes: 19 additions & 0 deletions argorithm/algorima2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def solution(sticker):
if len(sticker)==1:
return sticker[0]
arr = [0]*len(sticker)
arr[0] = sticker[0]
arr[1] = arr[0]
for i in range(2, len(sticker)-1):
arr[i] = max(arr[i-1], arr[i-2]+sticker[i])
result = max(arr)

arr = [0]*len(sticker)
arr[0] = 0
arr[1] = sticker[1]
for i in range(2, len(sticker)):
arr[i] = max(arr[i-1], arr[i-2]+sticker[i])
return max(result, max(arr))

if __name__ == '__main__':
print(solution([14,6,5,11,3,9,2,10]))

0 comments on commit cc45867

Please sign in to comment.