Skip to content

[bky373] 9th Week Soultions #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2024
Merged
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
45 changes: 45 additions & 0 deletions clone-graph/bky373.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8주차 풀이와 비슷하게, 필드를 이용하는 방법이 굉장히 깔끔하고 좋네요 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/

/*
* time: O(N+M), where N is the number of nodes and M is the number of edges.
* space: O(N), N is the space for the `visited` hash map.
*/
class Solution {
private Map<Node, Node> visited = new HashMap<>();

public Node cloneGraph(Node node) {
if (node == null) {
return node;
}

if (visited.containsKey(node)) {
return visited.get(node);
}

Node cloned = new Node(node.val, new ArrayList());
visited.put(node, cloned);

for (Node neighbor : node.neighbors) {
cloned.neighbors.add(cloneGraph(neighbor));
}
return cloned;
}
}
46 changes: 46 additions & 0 deletions course-schedule/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* time: O(V + E), where V is the number of courses (vertices) and E is the number of prerequisites (edges).
* space: O(V + E), where V is for the map and sets and E is for call stacks, which is bounded by the number of courses and prerequisites.
*/
class Solution {

Map<Integer, List<Integer>> courseToPrerequisites = new HashMap<>();
Set<Integer> traversing = new HashSet<>();
Set<Integer> finished = new HashSet<>();

public boolean canFinish(int numCourses, int[][] prerequisites) {
for (int[] prerequisite : prerequisites) {
courseToPrerequisites.computeIfAbsent(prerequisite[0], key -> new ArrayList<>())
.add(prerequisite[1]);
}

for (int i = 0; i < numCourses; i++) {
if (!dfs(i)) {
return false;
}
}
return true;
}

private boolean dfs(int course) {
if (traversing.contains(course)) {
return false;
}
if (finished.contains(course)) {
return true;
}

traversing.add(course);
if (courseToPrerequisites.containsKey(course)) {
List<Integer> requisites = courseToPrerequisites.get(course);
for (int req : requisites) {
if (!dfs(req)) {
return false;
}
}
}
traversing.remove(course);
finished.add(course);
return true;
}
}
58 changes: 58 additions & 0 deletions design-add-and-search-words-data-structure/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* time:
* - add: O(N)
* - search: O(N)
* - N is the length of the word.
* space: O(M)
* - M is the number of nodes.
*/
public class WordDictionary {

private Node root;

public WordDictionary() {
root = new Node();
}

public void addWord(String word) {
Node node = root;
for (char ch : word.toCharArray()) {
if (!node.subNodes.containsKey(ch)) {
node.subNodes.put(ch, new Node());
}
node = node.subNodes.get(ch);
}
node.isEndOfWord = true;
}

public boolean search(String word) {
return helper(root, word);
}

public boolean helper(Node node, String word) {
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!node.subNodes.containsKey(ch)) {
if (ch == '.') {
for (char x : node.subNodes.keySet()) {
Node child = node.subNodes.get(x);
if (helper(child, word.substring(i + 1))) {
return true;
}
}
}
return false;
} else {
node = node.subNodes.get(ch);
}
}
return node.isEndOfWord;
}

private class Node {

Map<Character, Node> subNodes = new HashMap<>();
boolean isEndOfWord;
}

}
58 changes: 58 additions & 0 deletions number-of-islands/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* time: O(M * N)
* - M is the number of rows
* - N is the number of columns
* space: O(min(M, N))
* - M is the number of rows
* - N is the number of columns
* in worst case where the grid is filled with lands, the size of queue can grow up to min(𝑀,𝑁).
*/
class Solution {

private char[][] grid;

public int numIslands(char[][] grid) {
this.grid = grid;
int numOfIslands = 0;

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
numOfIslands++;
bfs(i, j);
}
}
}
return numOfIslands;
}

private void bfs(int i, int j) {
Queue<Integer> que = new LinkedList<>();
que.add(i);
que.add(j);
grid[i][j] = 0;

int[] yDir = {0, -1, 0, 1};
int[] xDir = {1, 0, -1, 0};

while (!que.isEmpty()) {
int y = que.poll();
int x = que.poll();

for (int d = 0; d < 4; d++) {
int ny = y + yDir[d];
int nx = x + xDir[d];

if (ny < 0 || ny >= grid.length || nx < 0 || nx >= grid[0].length) {
continue;
}

if (grid[ny][nx] == '1') {
grid[ny][nx] = '0';
que.add(ny);
que.add(nx);
}
}
}
}
}
66 changes: 66 additions & 0 deletions pacific-atlantic-water-flow/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* time: O(M*N)
* space: O(M*N)
* - M is the number of rows
* - N is the number of columns
*/
class Solution {

int[][] heights;
int rLen;
int cLen;
int[] rDirs = {0, -1, 0, 1};
int[] cDirs = {1, 0, -1, 0};

public List<List<Integer>> pacificAtlantic(int[][] heights) {
this.heights = heights;
this.rLen = heights.length;
this.cLen = heights[0].length;

boolean[][] pacific = new boolean[rLen][cLen];
boolean[][] atlantic = new boolean[rLen][cLen];

for (int i = 0; i < rLen; i++) {
dfs(i, 0, pacific);
dfs(i, cLen - 1, atlantic);
}

for (int i = 0; i < cLen; i++) {
dfs(0, i, pacific);
dfs(rLen - 1, i, atlantic);
}

List<List<Integer>> both = new ArrayList<>();
for (int i = 0; i < rLen; i++) {
for (int j = 0; j < cLen; j++) {
if (pacific[i][j] && atlantic[i][j]) {
both.add(List.of(i, j));
}
}
}
return both;
}

private void dfs(int r, int c, boolean[][] visited) {
visited[r][c] = true;

for (int d = 0; d < 4; d++) {
int nr = r + rDirs[d];
int nc = c + cDirs[d];

if (nr < 0 || nr > rLen - 1 || nc < 0 || nc > cLen - 1) {
continue;
}

if (visited[nr][nc]) {
continue;
}

if (heights[nr][nc] < heights[r][c]) {
continue;
}

dfs(nr, nc, visited);
}
}
}