Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ballinaMaze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<iostream>
#include<bits/stdc++.h>
#include<vector>
using namespace std;
/*
We keep rolling the ball in all directions until it hits a wall, starting from the current position.
Each time we land on a new stopping cell, we check if it's the destination and then DFS from there.
We mark visited cells as -1 to avoid revisiting the same paths again.
*/
class Solution{
vector<vector<int>> dirs;
int m, n;

bool dfs(vector<vector<int>>& maze, int i, int j, vector<int>& destination){
if(destination[0] == i && destination[1] == j) return true;
if(maze[i][j] == -1) return false;

maze[i][j] = -1;
for(auto& dir : dirs){
int r = dir[0] + i;
int c = dir[1] + j;

while(r >= 0 && c >= 0 && r < m && c < n && maze[r][c] != 1){
r += dir[0];
c += dir[1];
}

r -= dir[0];
c -= dir[1];

if(dfs(maze, r, c, destination)) return true;
}
return false;
}
public:
bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination){
dirs = {{-1, 0}, {1,0}, {0,1}, {0, -1}};
m = maze.size();
n = maze[0].size();

return dfs(maze, start[0], start[1], destination);
}
};
25 changes: 25 additions & 0 deletions townJudge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
#include<bits/stdc++.h>
#include<vector>
using namespace std;
/*
We use an array to count trust levels: losing trust (out-degree) subtracts, gaining trust (in-degree) adds.
The town judge should be trusted by everyone else but trust no one.
So we just find the person with an in-degree of n−1.
*/
class Solution{
public:
int findJudge(int n, vector<vector<int>>& trust){
vector<int> inDegrees(n+1);

for(auto& it : trust){
inDegrees[it[0]]--;
inDegrees[it[1]]++;
}

for(int i = 0; i <= n; i++){
if(inDegrees[i] == n-1) return i;
}
return -1;
}
};