From be76c8403be3cdef36603f3d551b8835401e8b38 Mon Sep 17 00:00:00 2001 From: mintheon Date: Sun, 29 Dec 2024 22:26:38 +0900 Subject: [PATCH 1/4] merge two sorted lists solved --- merge-two-sorted-lists/mintheon.java | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 merge-two-sorted-lists/mintheon.java diff --git a/merge-two-sorted-lists/mintheon.java b/merge-two-sorted-lists/mintheon.java new file mode 100644 index 000000000..d6ee5a9e0 --- /dev/null +++ b/merge-two-sorted-lists/mintheon.java @@ -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; + } +} From ced98a8acd237b4af23021707947237fde17d66e Mon Sep 17 00:00:00 2001 From: mintheon Date: Sun, 29 Dec 2024 22:36:54 +0900 Subject: [PATCH 2/4] missing number solved --- missing-number/mintheon.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 missing-number/mintheon.java diff --git a/missing-number/mintheon.java b/missing-number/mintheon.java new file mode 100644 index 000000000..611f19888 --- /dev/null +++ b/missing-number/mintheon.java @@ -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; + } +} From 389da1ea3f534e6a88f5e35e64361300547c9517 Mon Sep 17 00:00:00 2001 From: mintheon Date: Sat, 4 Jan 2025 02:56:00 +0900 Subject: [PATCH 3/4] word search solved --- word-search/mintheon.java | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 word-search/mintheon.java diff --git a/word-search/mintheon.java b/word-search/mintheon.java new file mode 100644 index 000000000..674d6789c --- /dev/null +++ b/word-search/mintheon.java @@ -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) { + 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; + } +} From 05a50eccc3b823f29fe0d17b56566a1684ef8b23 Mon Sep 17 00:00:00 2001 From: mintheon Date: Sat, 4 Jan 2025 03:31:27 +0900 Subject: [PATCH 4/4] palindromic substrings solved --- palindromic-substrings/mintheon.java | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 palindromic-substrings/mintheon.java diff --git a/palindromic-substrings/mintheon.java b/palindromic-substrings/mintheon.java new file mode 100644 index 000000000..1ee5fb9ed --- /dev/null +++ b/palindromic-substrings/mintheon.java @@ -0,0 +1,33 @@ +class Solution { + /** + 시간복잡도: O(n^2) + 공간복잡도: 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; + } +}