본문 바로가기

해커랭크4

[HACKERRANK] - DFS: Connected Cell in a Grid www.hackerrank.com/challenges/ctci-connected-cell-in-a-grid/problem DFS: Connected Cell in a Grid | HackerRank Find the largest connected region in a 2D Matrix. www.hackerrank.com DFS 문제. def maxRegion(grid): maxR = 0 for y in range(len(grid)): for x in range(len(grid[0])): if grid[y][x] == 1: maxR = max(maxR, 1+search(grid,y,x)) return maxR def search(grid, y,x): grid[y][x] = 2 xway,yway = [-1,.. 2021. 2. 7.
[HACKERRANK] - BFS: Shortest Reach in a Graph www.hackerrank.com/challenges/ctci-bfs-shortest-reach/problem BFS: Shortest Reach in a Graph | HackerRank Implement a Breadth First Search (BFS). www.hackerrank.com BFS 문제. from collections import deque class Graph: def __init__(self,n): self.n = n self.nodes = {i:[] for i in range(n)} def connect(self,a,b): self.nodes[a].append(b) self.nodes[b].append(a) def find_all_distances(self,s): depths =.. 2021. 2. 7.
[HACKERRANK] - Find the nearest clone www.hackerrank.com/challenges/find-the-nearest-clone/problem Find the nearest clone | HackerRank Find the shortest path length between any two nodes with the given condition. www.hackerrank.com 주어진 val과 같은 color를 가진 node 간의 최소 거리를 출력하는 문제. 처음에 문제를 node[val]과 같은 color를 가진 node를 찾는 문제로 잘못 이해해서 풀이하는데 쓸데없는 시간이 걸렸다. BFS로 풀면 쉽게 풀리는데 아래와 같이 코드를 짜도 통과한다. from collections import deque def findShortest(gr.. 2021. 2. 6.
[HACKERRANK] - Roads and Libraries www.hackerrank.com/challenges/torque-and-development/problem Roads and Libraries | HackerRank Help the ruler of HackerLand determine the cheapest way to give his citizens access to libraries. www.hackerrank.com 마을의 갯수 n과 도로 건설 비용 c_road, 도서관 건설 비용 c_lib, 연결 가능한 도로가 주어졌을 때, 모든 마을이 도서관을 이용할 수 있도록 도로 및 도서관을 건설할 때 드는 최소 비용을 구하는 문제 한 집단의 최소비용은 해당 집단의 마을 수를 nn이라고 할 때, min(nn*c_lib, c_lib + (nn-1)*c_ro.. 2021. 2. 4.