diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 0000000..98e7c87 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,16 @@ +class Solution: + def findJudge(self, n: int, trust: List[List[int]]) -> int: + if n == 1 and not trust: return 1 + indegree = defaultdict(int) #[indegree, + outdegree = defaultdict(int) #outdegree + + for i, j in trust: + indegree[j] += 1 + outdegree[i] += 1 + + for _n in range(1, n+1): + if indegree[_n] == n-1 and outdegree[_n] == 0: + return _n + return -1 + +#TC V+E where V is no of people and E is number of relations diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 0000000..32acf20 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,57 @@ +class Solution: + def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool: + q = deque() + ROWS, COLS = len(maze), len(maze[0]) + + def isValidCell(curi, curj): + if not 0 <= curi < ROWS or not 0 <= curj < COLS: + return False + return True + + seen = set() + q.append(start) + + while q: + curi, curj = q.popleft() + + + if not isValidCell(curi, curj) or maze[curi][curj] == 1 or (curi, curj) in seen: + continue + + seen.add((curi, curj)) + #print(curi, curj) + + #4 directions while loop right? + #left 0, -1 + newi, newj = curi, curj + while isValidCell(newi, newj) and maze[newi][newj] != 1: + newj -= 1 + newj+=1 + if [newi, newj] == destination:return True + if (newi, newj) not in seen: q.append((newi, newj)) + + #right 0, 1 + newi, newj = curi, curj + while isValidCell(newi, newj) and maze[newi][newj] != 1: + newj += 1 + newj-=1 + if [newi, newj] == destination:return True + if (newi, newj) not in seen: q.append((newi, newj)) + + #top -1, 0 + newi, newj = curi, curj + while isValidCell(newi, newj) and maze[newi][newj] != 1: + newi -= 1 + newi+=1 + if [newi, newj] == destination:return True + if (newi, newj) not in seen: q.append((newi, newj)) + + #bottom 1, 0 + newi, newj = curi, curj + while isValidCell(newi, newj) and maze[newi][newj] != 1: + newi += 1 + newi-=1 + if [newi, newj] == destination:return True + if (newi, newj) not in seen: q.append((newi, newj)) + + return False