본문 바로가기
반응형

SWE/코테137

HackerRank - AND xor OR 문제 이해 안감 ㅜ 문제 : https://www.hackerrank.com/challenges/and-xor-or/problem sample input에서 3,5,2 가 뭔지 모르겠음 5 9 6 3 5 2 2022. 3. 5.
HackerRank - Queue using Two Stacks 문제 : https://www.hackerrank.com/challenges/queue-using-two-stacks/problem 해결 코드 : 6문제 time limit 나옴 ㅠㅠ (6/15) stack1 = [] stack2 = [] def enque(num): stack1.append(num) def deque() : #pop from stack1 #push to stack2 while len(stack1) : stack2.append(stack1.pop()) #deque stack2.pop() #reset while len(stack2) : #pop from stack2 #push to stack1 stack1.append(stack2.pop()) def printFront(): print(st.. 2022. 3. 5.
HackerRank - Castle on the Grid 문제 : https://www.hackerrank.com/challenges/castle-on-the-grid/problem 코드 : BFS의 응용 버전 두번째 코드 (성공!) from collections import deque def minimumMoves(grid, startX, startY, goalX, goalY): n = len(grid) m = len(grid[0]) maximum = n*m # n*m graph graph = [[maximum]*m for _ in range(n)] graph[startX][startY] = 0 # for debugging def printGraph(): print('====printGraph=====') for i in range(n): for j in r.. 2022. 3. 5.
python - BFS - problem solving example 강의: https://youtu.be/7C9RgOcvkvo 코드: # BFS example from collections import deque # 1. input n, m = map(int, input.split()) graph=[] for i in range(n): graph[i].append(list(map(int, input()))) # 2. find shortest path # top, bottom, left, right dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] def bfs(r,c): queue = deque() queue.append(r,c) while queue: r,c = queue.popleft() for d in range(4): nr = r + dr[d] .. 2022. 3. 5.
반응형