Skip to content

[jinhyungrhee] week 06 solutions #1437

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 6 commits into from
May 10, 2025
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
32 changes: 32 additions & 0 deletions container-with-most-water/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {

/**
* time-complexity : O(n)
* space-complexity : O(1)
*/

public int maxArea(int[] height) {

int left = 0;
int right = height.length - 1;

int w = 0, h = 0, currSize = 0, maxSize = 0;

while (left < right) {

w = right - left;
h = Math.min(height[left], height[right]);

currSize = w * h;
maxSize = Math.max(currSize, maxSize);

if (height[left] < height[right]) {
left++;
} else {
right--;
}

}
return maxSize;
}
}
75 changes: 75 additions & 0 deletions design-add-and-search-words-data-structure/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class TrieNode {
boolean word;
TrieNode[] children;

TrieNode() {
this.word = false;
this.children = new TrieNode[27];
}
}

class WordDictionary {
/**
* space-complexity : O(N * L) (w.c)
* - N : 단어 수, L : 평균 길이
* - 단어 하나 추가 시 최대 L개의 TrieNode 생성
* - (w.c) 모든 단어가 중복 없이 추가되면 (N*L)개의 노드 필요
*/
TrieNode root;
public WordDictionary() {
this.root = new TrieNode();
}

/**
* addWord() Time-complexity : O(L)
* - 각 문자마다 TrieNode를 따라 내려가며 필요한 경우 새 노드 생성
* - 한 단어당 최대 L개의 노드 생성 및 접근
*/
public void addWord(String word) {

TrieNode curr = root;
for (char c : word.toCharArray()) {
if (curr.children[c - 'a'] == null) {
curr.children[c - 'a'] = new TrieNode();
}
curr = curr.children[c - 'a'];
}
curr.word = true;
}

/**
* search() Time-complexity :
* - '.'가 없는 경우 : O(L)
* -> 일반적인 문자는 한 경로만 탐색
* - '.'가 있는 경우 : O(26^L) (w.c)
* -> 현재 노드의 모든 자식에 대해 DFS 수행
*
*/
public boolean search(String word) {
return dfs(word, 0, root);
}

public boolean dfs(String word, int index, TrieNode node) {

if (index == word.length()) {
return node.word;
}

char c = word.charAt(index);

if (c == '.') {
for (TrieNode child : node.children) {
if (child != null && dfs(word, index + 1, child)) {
return true;
}
}
return false;

} else {

TrieNode next = node.children[c - 'a'];
return next != null && dfs(word, index + 1, next);

}
}
}
27 changes: 27 additions & 0 deletions longest-increasing-subsequence/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.*;
class Solution {
/**
* time-complexity : O(n^2)
* space-complexity : O(n)
*/
public int lengthOfLIS(int[] nums) {

int[] dp = new int[nums.length];
Arrays.fill(dp, 1);

for (int i = 1 ; i < nums.length; i++) {
for (int j = i; j >= 0; j--) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}

int maxVal = 0;
for (int num : dp) {
if (num > maxVal) maxVal = num;
}

return maxVal;
}
}
53 changes: 53 additions & 0 deletions spiral-matrix/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.*;
class Solution {
/**
* time-complexity : O(n * m)
* space-complexity : O(1) (excluding the output List)
*/
public List<Integer> spiralOrder(int[][] matrix) {

List<Integer> result = new ArrayList<>();

int left = 0;
int right = matrix[0].length - 1;
int top = 0;
int bottom = matrix.length - 1;

while (left <= right && top <= bottom) {

// 위쪽 행을 순회한 후, 상단 경계를 1 증가
for (int y = left; y <= right; y++) {
result.add(matrix[top][y]);
}
top++;

// 상단 인덱스가 하단 인덱스 보다 커지면 순회 중단
if (top > bottom) break;

// 우측 열을 순회한 후, 우측 경계를 1 감소
for (int x = top; x <= bottom; x++) {
result.add(matrix[x][right]);
}
right--;

// 우측 인덱스가 좌측 인덱스보다 작아지면 순회 중단
if (right < left) break;

// 아래쪽 행을 순회한 후, 하단 경계를 1 감소
for (int y = right; y >= left; y--) {
result.add(matrix[bottom][y]);
}
bottom--;

// 왼쪽 열을 순회한 후, 좌측 경계를 1 증가
for (int x = bottom; x >= top; x--) {
result.add(matrix[x][left]);
}
left++;

}
return result;

}

}
31 changes: 31 additions & 0 deletions valid-parentheses/jinhyungrhee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.*;
class Solution {
/**
* time-complexity : O(n)
* space-complexity : O(n)
*/
public boolean isValid(String s) {

Deque<Character> stack = new ArrayDeque<>();
Map<Character, Character> table = new HashMap<>();
table.put(')', '(');
table.put(']', '[');
table.put('}', '{');

for (int i = 0; i < s.length(); i++) {
if (table.containsKey(s.charAt(i))) {

if ((table.get(s.charAt(i))).equals(stack.peek())) {
stack.pop();
} else {
stack.push(s.charAt(i));
}

} else {
stack.push(s.charAt(i));
}
}

return stack.isEmpty();
}
}