diff --git a/linked-list-cycle/sora0319.java b/linked-list-cycle/sora0319.java new file mode 100644 index 000000000..07c5d18de --- /dev/null +++ b/linked-list-cycle/sora0319.java @@ -0,0 +1,20 @@ +public class sora0319 { + public class Solution { + public boolean hasCycle(ListNode head) { + ListNode back = head; + ListNode front = head; + + while (front != null && front.next != null) { + back = back.next; + front = front.next.next; + + if (back == front) { + return true; + } + } + + return false; + } + } +} + diff --git a/maximum-product-subarray/sora0319.java b/maximum-product-subarray/sora0319.java new file mode 100644 index 000000000..9ac85cd34 --- /dev/null +++ b/maximum-product-subarray/sora0319.java @@ -0,0 +1,23 @@ +public class sora0319 { + public class Solution { + public int maxProduct(int[] nums) { + int result = nums[0]; + int max = 1; + int min = 1; + + for (int num : nums) { + int tempMax = Math.max(Math.max(max * num, min * num), num); + int tempMin = Math.min(Math.min(max * num, min * num), num); + + max = tempMax; + min = tempMin; + + result = Math.max(result, max); + } + + return result; + } + } + +} + diff --git a/minimum-window-substring/sora0319.java b/minimum-window-substring/sora0319.java new file mode 100644 index 000000000..63efb617a --- /dev/null +++ b/minimum-window-substring/sora0319.java @@ -0,0 +1,87 @@ +public class sora0319 { + public class Solution { + public String minWindow(String s, String t) { + if (s.length() < t.length()) return ""; + + Map tCounts = new HashMap<>(); + Map wCounts = new HashMap<>(); + + // tCounts 초기화 + for (char ch : t.toCharArray()) { + if (tCounts.containsKey(ch)) { + tCounts.put(ch, tCounts.get(ch) + 1); + } else { + tCounts.put(ch, 1); + } + } + + int minLow = 0; + int minHigh = s.length(); + int low = 0; + boolean found = false; + + for (int high = 0; high < s.length(); high++) { + char ch = s.charAt(high); + if (wCounts.containsKey(ch)) { + wCounts.put(ch, wCounts.get(ch) + 1); + } else { + wCounts.put(ch, 1); + } + + while (isExist(wCounts, tCounts)) { + if (high - low < minHigh - minLow) { + minLow = low; + minHigh = high; + found = true; + } + + char lowChar = s.charAt(low); + if (tCounts.containsKey(lowChar)) { + int count = wCounts.get(lowChar); + if (count == 1) { + wCounts.remove(lowChar); + } else { + wCounts.put(lowChar, count - 1); + } + } else { + if (wCounts.containsKey(lowChar)) { + int count = wCounts.get(lowChar); + if (count == 1) { + wCounts.remove(lowChar); + } else { + wCounts.put(lowChar, count - 1); + } + } + } + + low++; + } + } + + if (found) { + return s.substring(minLow, minHigh + 1); + } else { + return ""; + } + } + + private boolean isExist(Map window, Map target) { + for (Map.Entry entry : target.entrySet()) { + char ch = entry.getKey(); + int required = entry.getValue(); + + if (!window.containsKey(ch)) { + return false; + } + + int count = window.get(ch); + if (count < required) { + return false; + } + } + return true; + } + } +} + + diff --git a/pacific-atlantic-water-flow/sora0319.java b/pacific-atlantic-water-flow/sora0319.java new file mode 100644 index 000000000..47a69f5b1 --- /dev/null +++ b/pacific-atlantic-water-flow/sora0319.java @@ -0,0 +1,67 @@ +public class sora0319 { + public class Solution { + private int[][] heights; + private int rows, cols; + + public List> pacificAtlantic(int[][] heights) { + this.heights = heights; + this.rows = heights.length; + this.cols = heights[0].length; + List> result = new ArrayList<>(); + + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) { + Set visitedPac = new HashSet<>(); + Set visitedAtl = new HashSet<>(); + + if (dfsPac(row, col, visitedPac) && dfsAtl(row, col, visitedAtl)) { + result.add(Arrays.asList(row, col)); + } + } + } + return result; + } + + private boolean dfsPac(int row, int col, Set visited) { + String key = row + "," + col; + if (visited.contains(key)) return false; + visited.add(key); + + if (row == 0 || col == 0) return true; + + int[][] moved = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} }; + for (int[] m : moved) { + int r = row + m[0]; + int c = col + m[1]; + if (isRange(r, c) && heights[row][col] >= heights[r][c]) { + if (dfsPac(r, c, visited)) return true; + } + } + return false; + } + + private boolean dfsAtl(int row, int col, Set visited) { + String key = row + "," + col; + if (visited.contains(key)) return false; + visited.add(key); + + if (row == rows - 1 || col == cols - 1) return true; + + int[][] directions = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} }; + for (int[] dir : directions) { + int r = row + dir[0]; + int c = col + dir[1]; + if (isRange(r, c) && heights[row][col] >= heights[r][c]) { + if (dfsAtl(r, c, visited)) return true; + } + } + return false; + } + + private boolean isRange(int r, int c) { + return r >= 0 && r < rows && c >= 0 && c < cols; + } + } + +} + diff --git a/sum-of-two-integers/sora0319.java b/sum-of-two-integers/sora0319.java new file mode 100644 index 000000000..f7f35086a --- /dev/null +++ b/sum-of-two-integers/sora0319.java @@ -0,0 +1,13 @@ +public class sora0319 { + class Solution { + public int getSum(int a, int b) { + while (b != 0) { + int carry = a & b; + a = a ^ b; + b = carry << 1; + } + return a; + } + } +} +