Skip to content

[sora0319] WEEK 09 solutions #1537

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 5 commits into from
Jun 2, 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
20 changes: 20 additions & 0 deletions linked-list-cycle/sora0319.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

23 changes: 23 additions & 0 deletions maximum-product-subarray/sora0319.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}

87 changes: 87 additions & 0 deletions minimum-window-substring/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
public class sora0319 {
public class Solution {
public String minWindow(String s, String t) {
if (s.length() < t.length()) return "";

Map<Character, Integer> tCounts = new HashMap<>();
Map<Character, Integer> 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<Character, Integer> window, Map<Character, Integer> target) {
for (Map.Entry<Character, Integer> 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;
}
}
}


67 changes: 67 additions & 0 deletions pacific-atlantic-water-flow/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
public class sora0319 {
public class Solution {
private int[][] heights;
private int rows, cols;

public List<List<Integer>> pacificAtlantic(int[][] heights) {
this.heights = heights;
this.rows = heights.length;
this.cols = heights[0].length;
List<List<Integer>> result = new ArrayList<>();

for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Set<String> visitedPac = new HashSet<>();
Set<String> 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<String> 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<String> 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;
}
}

}

13 changes: 13 additions & 0 deletions sum-of-two-integers/sora0319.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}