Skip to content

Commit 9295f4c

Browse files
authored
Merge pull request #1537 from sora0319/main
[sora0319] WEEK 09 solutions
2 parents b88b698 + 18a30cc commit 9295f4c

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

linked-list-cycle/sora0319.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class sora0319 {
2+
public class Solution {
3+
public boolean hasCycle(ListNode head) {
4+
ListNode back = head;
5+
ListNode front = head;
6+
7+
while (front != null && front.next != null) {
8+
back = back.next;
9+
front = front.next.next;
10+
11+
if (back == front) {
12+
return true;
13+
}
14+
}
15+
16+
return false;
17+
}
18+
}
19+
}
20+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class sora0319 {
2+
public class Solution {
3+
public int maxProduct(int[] nums) {
4+
int result = nums[0];
5+
int max = 1;
6+
int min = 1;
7+
8+
for (int num : nums) {
9+
int tempMax = Math.max(Math.max(max * num, min * num), num);
10+
int tempMin = Math.min(Math.min(max * num, min * num), num);
11+
12+
max = tempMax;
13+
min = tempMin;
14+
15+
result = Math.max(result, max);
16+
}
17+
18+
return result;
19+
}
20+
}
21+
22+
}
23+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
public class sora0319 {
2+
public class Solution {
3+
public String minWindow(String s, String t) {
4+
if (s.length() < t.length()) return "";
5+
6+
Map<Character, Integer> tCounts = new HashMap<>();
7+
Map<Character, Integer> wCounts = new HashMap<>();
8+
9+
// tCounts 초기화
10+
for (char ch : t.toCharArray()) {
11+
if (tCounts.containsKey(ch)) {
12+
tCounts.put(ch, tCounts.get(ch) + 1);
13+
} else {
14+
tCounts.put(ch, 1);
15+
}
16+
}
17+
18+
int minLow = 0;
19+
int minHigh = s.length();
20+
int low = 0;
21+
boolean found = false;
22+
23+
for (int high = 0; high < s.length(); high++) {
24+
char ch = s.charAt(high);
25+
if (wCounts.containsKey(ch)) {
26+
wCounts.put(ch, wCounts.get(ch) + 1);
27+
} else {
28+
wCounts.put(ch, 1);
29+
}
30+
31+
while (isExist(wCounts, tCounts)) {
32+
if (high - low < minHigh - minLow) {
33+
minLow = low;
34+
minHigh = high;
35+
found = true;
36+
}
37+
38+
char lowChar = s.charAt(low);
39+
if (tCounts.containsKey(lowChar)) {
40+
int count = wCounts.get(lowChar);
41+
if (count == 1) {
42+
wCounts.remove(lowChar);
43+
} else {
44+
wCounts.put(lowChar, count - 1);
45+
}
46+
} else {
47+
if (wCounts.containsKey(lowChar)) {
48+
int count = wCounts.get(lowChar);
49+
if (count == 1) {
50+
wCounts.remove(lowChar);
51+
} else {
52+
wCounts.put(lowChar, count - 1);
53+
}
54+
}
55+
}
56+
57+
low++;
58+
}
59+
}
60+
61+
if (found) {
62+
return s.substring(minLow, minHigh + 1);
63+
} else {
64+
return "";
65+
}
66+
}
67+
68+
private boolean isExist(Map<Character, Integer> window, Map<Character, Integer> target) {
69+
for (Map.Entry<Character, Integer> entry : target.entrySet()) {
70+
char ch = entry.getKey();
71+
int required = entry.getValue();
72+
73+
if (!window.containsKey(ch)) {
74+
return false;
75+
}
76+
77+
int count = window.get(ch);
78+
if (count < required) {
79+
return false;
80+
}
81+
}
82+
return true;
83+
}
84+
}
85+
}
86+
87+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
public class sora0319 {
2+
public class Solution {
3+
private int[][] heights;
4+
private int rows, cols;
5+
6+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
7+
this.heights = heights;
8+
this.rows = heights.length;
9+
this.cols = heights[0].length;
10+
List<List<Integer>> result = new ArrayList<>();
11+
12+
for (int row = 0; row < rows; row++) {
13+
for (int col = 0; col < cols; col++) {
14+
Set<String> visitedPac = new HashSet<>();
15+
Set<String> visitedAtl = new HashSet<>();
16+
17+
if (dfsPac(row, col, visitedPac) && dfsAtl(row, col, visitedAtl)) {
18+
result.add(Arrays.asList(row, col));
19+
}
20+
}
21+
}
22+
return result;
23+
}
24+
25+
private boolean dfsPac(int row, int col, Set<String> visited) {
26+
String key = row + "," + col;
27+
if (visited.contains(key)) return false;
28+
visited.add(key);
29+
30+
if (row == 0 || col == 0) return true;
31+
32+
int[][] moved = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
33+
for (int[] m : moved) {
34+
int r = row + m[0];
35+
int c = col + m[1];
36+
if (isRange(r, c) && heights[row][col] >= heights[r][c]) {
37+
if (dfsPac(r, c, visited)) return true;
38+
}
39+
}
40+
return false;
41+
}
42+
43+
private boolean dfsAtl(int row, int col, Set<String> visited) {
44+
String key = row + "," + col;
45+
if (visited.contains(key)) return false;
46+
visited.add(key);
47+
48+
if (row == rows - 1 || col == cols - 1) return true;
49+
50+
int[][] directions = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
51+
for (int[] dir : directions) {
52+
int r = row + dir[0];
53+
int c = col + dir[1];
54+
if (isRange(r, c) && heights[row][col] >= heights[r][c]) {
55+
if (dfsAtl(r, c, visited)) return true;
56+
}
57+
}
58+
return false;
59+
}
60+
61+
private boolean isRange(int r, int c) {
62+
return r >= 0 && r < rows && c >= 0 && c < cols;
63+
}
64+
}
65+
66+
}
67+

sum-of-two-integers/sora0319.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class sora0319 {
2+
class Solution {
3+
public int getSum(int a, int b) {
4+
while (b != 0) {
5+
int carry = a & b;
6+
a = a ^ b;
7+
b = carry << 1;
8+
}
9+
return a;
10+
}
11+
}
12+
}
13+

0 commit comments

Comments
 (0)