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
26 changes: 26 additions & 0 deletions container-with-most-water/dahyeong-yun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* TC : O(n)
* - 기둥 높이 배열의 길이 n에 대해 최악의 경우 전체를 순회 하므로 O(n)
* SC : O(1)
* - 별도로 유의미한 공간 생성이 없음
*/
class Solution {
/**
* 넗이의 조합을 다 해보고 max 값을 찾으면 됨
* 그 중에 안해도 되는 조합이 있음. 더 낮은 높이로 인해서 넓이가 결정되므로 더 높은 탑은 그냥 유지하고 다른 조합만 확인해보면 됨.
*/
public int maxArea(int[] height) {
int left = 0, right = height.length-1, max = 0;
while(left < right) {
int w = right - left;
int h = height[left] < height[right] ? height[left] : height[right];

int current = w * h;
max = Math.max(max, current);
Comment on lines +16 to +19

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

자바의 Math.min은 오버헤드가 적고 가독성을 챙길 수 있어서 내장함수 사용도 좋을 거 같습니다. 19라인에 Math.max 사용하고 있어서 Math.min 사용하면 일관성이 있을 거 같습니다.


if(height[left] > height[right]) right --;
else left ++;
}
return max;
}
}
30 changes: 30 additions & 0 deletions longest-increasing-subsequence/dahyeong-yun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* TC : O(n^2)
* - 숫자 배열의 길이가 n 일 때 바깥 쪽 루프의 인덱스가 하나씩 증가함에 따라 때 안쪽 루프는 각각 1번, 2번, n-1번 까지 순회함
* - 따라서 총 반복 횟수는 1 + 2 + ... + n-1
* - 등차급수 합계 공식에 따라 n(n-1) / 2 만큼 반복하므로 시간복잡도는 최고 차항만 고려하여 n^2
* SC : O(n)
* - 숫자 배열 길이 n 만큼의 추가 배열을 공간을 생성하므로 O(n)
*/
class Solution {
public int lengthOfLIS(int[] nums) {
int len = nums.length;
int[] dp = new int[len]; // 최대 길이는 input 의 길이 만큼임
Arrays.fill(dp, 1); // 모든 길이는 최소 1

for(int i=1; i<len; i++) {
for(int j=0; j<i; j++) {
if(nums[j] < nums[i]) { // 현재보다 작은 수인 경우, 현재 수를 끝에 붙이는 길이 케이스와 기존 최대값 비교
dp[i] = Math.max(dp[j] + 1, dp[i]);
}
}
}

int max = 1;
for(int i : dp) {
max = Math.max(i, max);
}

return max;
}
}
41 changes: 41 additions & 0 deletions spiral-matrix/dahyeong-yun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* TC : O(m * n)
* - matrix의 원소 수 m * n 만큼 순회 하므로 O(m * n)
* SC : O(m * n)
* - matrix의 원소 수 m * n 만큼의 리스트를 생성하므로 O(m * n)
*/
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int[] dRow = {0, 1, 0, -1};
int[] dCol = {1, 0, -1, 0};

int direction = 0;
List<Integer> list = new ArrayList<>();

int rowCount = matrix.length;
int colCount = matrix[0].length;

int row = 0, col = 0;
int totalElements = rowCount * colCount;

for (int i = 0; i < totalElements; i++) {
list.add(matrix[row][col]);
matrix[row][col] = 101; // 방문 처리 (제약조건: -100 <= val <= 100)

int nextRow = row + dRow[direction];
int nextCol = col + dCol[direction];

// 범위를 벗어나거나 이미 방문한 칸인 경우 방향 전환
if (nextRow < 0 || nextRow >= rowCount ||
nextCol < 0 || nextCol >= colCount ||
matrix[nextRow][nextCol] > 100) {

direction = (direction + 1) % 4;
}
Comment on lines +28 to +34

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

주석 내용도 이해가 쉽고 코드도 직관적인 거 같습니다.

101 을 방문 처리용 값으로 쓰시는데 VISITED 같은 상수로 선언해서 사용하면 더 좋을 거 같습니다.


row += dRow[direction];
col += dCol[direction];
}
return list;
}
}
37 changes: 37 additions & 0 deletions valid-parentheses/dahyeong-yun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* TC : O(n)
* - 문자열 s의 길이 만큼 순회 하므로 O(n)
* SC : O(n)
* - 문자열 길이 만큼의 char 배열을 생성하고, 최대 n/2 만큼의 스택 사용. 합산 1.5n 이므로 O(n)
*/
class Solution {
public boolean isValid(String s) {
Deque<Character> stack = new ArrayDeque<>();

for(char c : s.toCharArray()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

찾아보니 toCharArray가 내부적으로 문자열을 복제헤서 새로 만든다고 하네요.
인덱스 접근으로 s.chatAt(i) 처럼 하시면 불필요한 연산을 줄이실 수 있을 것 같습니다.

//여는 괄호의 경우 stack 에 쌓기
if(c == '{'
|| c == '('
|| c == '['
) {
stack.push(c);
} else {
// 닫는 괄호의 경우 stack이 비어있으면 안됨
if(stack.size() < 1) return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

사소하지만, stack에 isEmtpy()가 있다면 사용하시면 좀 더 가독성이 좋을 것 같아요.

else {
char peek = stack.peek();
if((peek == '(' && c == ')')
|| (peek == '{' && c == '}')
|| (peek == '[' && c == ']')
Comment on lines +23 to +25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

저도 리뷰 받은 사항인데, map에 key, value로 여는괄호 닫는괄호 쌍을 넣어두고 체크하면 코드가 매우 깔끔해질 것 같습니다.

) {
stack.pop();
} else {
return false;
}
}
}
}

return stack.size() == 0;
}
}
Loading