Skip to content
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

[mintheon] Week 4 #813

Merged
merged 4 commits into from
Jan 4, 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
36 changes: 36 additions & 0 deletions merge-two-sorted-lists/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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 {
/**
시간복잡도: O(N)
공간복잡도: O(1)
*/
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode answer = new ListNode(-1);
ListNode node = answer;

while(list1 != null && list2 != null) {
if(list1.val < list2.val) {
node.next = list1;
list1 = list1.next;
} else {
node.next = list2;
list2 = list2.next;
}

node = node.next;
}

node.next = list1 != null ? list1 : list2;

return answer.next;
}
}
17 changes: 17 additions & 0 deletions missing-number/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/**
시간복잡도: O(n)
공간복잡도: O(1)
*/
public int missingNumber(int[] nums) {
int n = nums.length;
int expectedSum = n * (n + 1) / 2;

int arraySum = 0;
for(int i = 0; i < nums.length; i++) {
arraySum += nums[i];
}

return expectedSum - arraySum;
}
}
33 changes: 33 additions & 0 deletions palindromic-substrings/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
/**
시간복잡도: O(n^2)
Copy link
Contributor

Choose a reason for hiding this comment

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

사실상 삼중 for statement인데, O(n^3)으로 볼 수도 있을까요? 의견 궁금합니다

Copy link
Member Author

Choose a reason for hiding this comment

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

사실상 삼중 for statement인데, O(n^3)으로 볼 수도 있을까요? 의견 궁금합니다

for 루프가 3개라고 해서 무조건 n^3는 아니지만, 해당 코드는 말씀하신대로 n^3의 경우로 보이네요!
결국 while내 호출이 될때마다 최대 O(n) 만큼의 작업이 추가 수행되는 부분을 놓쳤네요.

공간복잡도: O(1)
-> 모든 부분 문자열을 확인하는 방식 대신 좀 더 최적화한 방식으로 다시 풀어볼 것.
*/
public int countSubstrings(String s) {
int count = 0;

for(int i = 0; i < s.length(); i++) {
for(int j = i; j < s.length(); j++) {
if(isPalindrom(s, i, j)) {
count++;
}
}
}

return count;
}

private boolean isPalindrom(String text, int left, int right) {
while(left < right) {
if(text.charAt(left) != text.charAt(right)) {
return false;
}

left++;
right--;
}

return true;
}
}
45 changes: 45 additions & 0 deletions word-search/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
// BFS로 변환해서 다시 풀어볼 것
char[][] board;
boolean[][] visited;
int[] moveX = {-1, 1, 0, 0};
int[] moveY = {0, 0, -1, 1};
int row, col;

public boolean exist(char[][] board, String word) {
this.board = board;
this.row = board.length;
this.col = board[0].length;
this.visited = new boolean[row][col];

for(int y = 0; y < row; y++) {
for(int x = 0; x < col; x++) {
if(this.hasWord(y, x, word, 0)) {
return true;
}
}
}

return false;
}

private boolean hasWord(int y, int x, String word, int index) {
Copy link
Contributor

Choose a reason for hiding this comment

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

String word도 함수의 argument로 넘기는 대신 class 변수로 등록해놓고 사용할 수 있을까요?

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

if(x < 0 || x >= col || y < 0 || y >= row || visited[y][x] || board[y][x] != word.charAt(index)) {
return false;
}

this.visited[y][x] = true;
for(int i = 0; i < 4; i++) {
if(this.hasWord(y + moveY[i], x + moveX[i], word, index + 1)) {
return true;
}
}

this.visited[y][x] = false;
return false;
}
}
Loading