diff --git a/longest-substring-without-repeating-characters/YoungSeok-Choi.java b/longest-substring-without-repeating-characters/YoungSeok-Choi.java new file mode 100644 index 000000000..6fea20a6b --- /dev/null +++ b/longest-substring-without-repeating-characters/YoungSeok-Choi.java @@ -0,0 +1,40 @@ +import java.util.HashMap; +import java.util.Map; + +class Solution { + public int lengthOfLongestSubstring(String s) { + int mx = 0; + int autoIncrement = 0; + Map map = new HashMap<>(); + Map reverseMap = new HashMap<>(); + + for (char c : s.toCharArray()) { + ++autoIncrement; + if (map.containsKey(c)) { + mx = Math.max(mx, map.size()); + + int start = map.get(c); + // NOTE: 중복 문자를 만나는 경우, 중복된 문자 이전에 Map에 들어온 원소는 모두 제거, (e.g. "sadvdf" 라는 입력이 + // 들어왔을 때 sad까지만 제거가 되고 v는 남아있어야 함.) + for (int i = start; i >= 1; i--) { + if (reverseMap.containsKey(i)) { + char target = reverseMap.get(i); + reverseMap.remove(i); + map.remove(target); + + } else { + break; + } + } + + map.put(c, autoIncrement); + reverseMap.put(autoIncrement, c); + } else { + map.put(c, autoIncrement); + reverseMap.put(autoIncrement, c); + } + } + + return Math.max(mx, map.size()); + } +} diff --git a/number-of-islands/YoungSeok-Choi.java b/number-of-islands/YoungSeok-Choi.java new file mode 100644 index 000000000..0f1698aa7 --- /dev/null +++ b/number-of-islands/YoungSeok-Choi.java @@ -0,0 +1,78 @@ +import java.util.LinkedList; +import java.util.Queue; + +class Solution { + + public int[] dx = { 1, 0, -1, 0 }; + public int[] dy = { 0, 1, 0, -1 }; + public int cnt = 0; + public int w = 0; + public int h = 0; + public boolean[][] visit; + public Queue q = new LinkedList<>(); + + public int numIslands(char[][] grid) { + w = grid.length; + h = grid[0].length; + visit = new boolean[w][h]; + + for (int i = 0; i < w; i++) { + for (int j = 0; j < h; j++) { + if (grid[i][j] == '1') { + cnt++; + // dfs(grid, i, j); + bfs(grid, i, j); + } + } + } + + return cnt; + } + + public void dfs(char[][] grid, int x, int y) { + if (x < 0 || x >= w || y < 0 || y >= h || grid[x][y] == '0' || visit[x][y]) { + return; + } + + visit[x][y] = true; + grid[x][y] = '0'; + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + dfs(grid, nx, ny); + } + } + + public void bfs(char[][] grid, int x, int y) { + + q.add(new Node(x, y)); + + while (!q.isEmpty()) { + Node p = q.poll(); + + for (int i = 0; i < 4; i++) { + int nx = p.x + dx[i]; + int ny = p.y + dy[i]; + + if (x < 0 || x >= w || y < 0 || y >= h || grid[x][y] == '0' || visit[x][y]) { + continue; + } + + grid[nx][ny] = '0'; + q.add(new Node(nx, ny)); + } + } + } +} + +class Node { + int x; + int y; + + public Node(int x, int y) { + this.x = x; + this.y = y; + } +} diff --git a/reverse-linked-list/YoungSeok-Choi.java b/reverse-linked-list/YoungSeok-Choi.java new file mode 100644 index 000000000..8fd6bb6ae --- /dev/null +++ b/reverse-linked-list/YoungSeok-Choi.java @@ -0,0 +1,82 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode reverseList(ListNode head) { + int idx = 0; + Map idxMap = new HashMap<>(); + + if (head == null || head.next == null) { + return null; + } + + while (true) { + idxMap.put(idx++, head.val); + + if (head.next == null) { + break; + } + + head = head.next; + } + + ListNode resHead = new ListNode(); + ListNode cur = resHead; + + for (int i = idx - 1; i >= 0; i--) { + cur.val = idxMap.get(i); + + if (i != 0) { + cur.next = new ListNode(); + cur = cur.next; + } + } + + return resHead; + } + +} + +// O(n) + 변수 하나로 직관적으로 문제를 해결할 수 있다.. +class AnotherSolution { + public ListNode reverstList(ListNode head) { + ListNode prev = null; + ListNode cur = head; + + while (cur.next != null) { + ListNode next = cur.next; + cur.next = prev; + prev = cur; + cur = next; + } + + return prev; + } +} + +class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} diff --git a/set-matrix-zeroes/YoungSeok-Choi.java b/set-matrix-zeroes/YoungSeok-Choi.java new file mode 100644 index 000000000..2d643feaf --- /dev/null +++ b/set-matrix-zeroes/YoungSeok-Choi.java @@ -0,0 +1,54 @@ +import java.util.ArrayList; +import java.util.List; + +class Solution { + + public List point = new ArrayList<>(); + + public void setZeroes(int[][] matrix) { + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] == 0) { + point.add(new Node(i, j)); + } + } + } + + for (Node n : point) { + int x = n.x; + int y = n.y; + + int nx = n.x; + while (nx > 0) { + matrix[--nx][y] = 0; + } + + int ny = n.y; + while (ny > 0) { + matrix[x][--ny] = 0; + } + + int nnx = n.x; + while (nnx < matrix.length - 1) { + matrix[++nnx][y] = 0; + } + + int nny = n.y; + while (nny < matrix[0].length - 1) { + matrix[x][++nny] = 0; + } + } + } + +} + +class Node { + int x; + int y; + + public Node(int x, int y) { + this.x = x; + this.y = y; + } +} diff --git a/unique-paths/YoungSeok-Choi.java b/unique-paths/YoungSeok-Choi.java new file mode 100644 index 000000000..3a19db189 --- /dev/null +++ b/unique-paths/YoungSeok-Choi.java @@ -0,0 +1,31 @@ +// NOTE: 2 * X 부터 모든 경우를 세어보니, 3 * 3의 격자가 나왔을 때 2 * 3, 3 * 2 경우를 더한 값이 나오는 것을 알 수 있었음 +// +) 3 * 4 격자에 대한 답은 3 * 3 격자의 답 + 2 * 4 격자의 답을 더하면 만들어지는 것을 알수 있었고, 아래와 같은 점화식을 만들 수 있었음 +// memo[m][n] = memo[m][n - 1] + memo[m - 1][n] +class Solution { + public int uniquePaths(int m, int n) { + int[][] memo = new int[101][101]; + + if (m == 1 || n == 1) { + return 1; + } + + memo[2][2] = 2; + for (int i = 2; i < 3; i++) { + for (int j = 3; j < 101; j++) { + memo[i][j] = j; + memo[j][i] = j; + } + } + + for (int i = 3; i < 101; i++) { + memo[i][i] = memo[i][i - 1] + memo[i - 1][i]; + for (int j = i + 1; j < 101; j++) { + int val = memo[i][j - 1] + memo[i - 1][j]; + memo[i][j] = val; + memo[j][i] = val; + } + } + + return memo[m][n]; + } +}