Skip to content

[YoungSeok-Choi] Week 7 Solutions #1452

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
40 changes: 40 additions & 0 deletions longest-substring-without-repeating-characters/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> map = new HashMap<>();
Map<Integer, Character> 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());
}
}
78 changes: 78 additions & 0 deletions number-of-islands/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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<Node> 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;
}
}
82 changes: 82 additions & 0 deletions reverse-linked-list/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}
}
54 changes: 54 additions & 0 deletions set-matrix-zeroes/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.List;

class Solution {

public List<Node> 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;
}
}
31 changes: 31 additions & 0 deletions unique-paths/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -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];
}
}