Skip to content

Commit af257ad

Browse files
authored
Merge pull request DaleStudy#1524 from Tessa1217/main
[Tessa1217] Week 09 Solutions
2 parents 84105a9 + 2e53834 commit af257ad

File tree

5 files changed

+335
-0
lines changed

5 files changed

+335
-0
lines changed

β€Žlinked-list-cycle/Tessa1217.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
/**
13+
* headλŠ” μ—°κ²° 리슀트의 ν—€λ“œλ‹€.
14+
* νŠΉμ • λ…Έλ“œκ°€ 리슀트 λ‚΄μ˜ λ‹€λ₯Έ λ…Έλ“œμ™€ next 포인트λ₯Ό 톡해 μ—°κ²°λ˜μ–΄ μžˆμ„ λ•Œ 이 λ¦¬μŠ€νŠΈμ—λŠ” 사이클이 μžˆλ‹€κ³  ν•  수 μžˆλ‹€.
15+
* posλŠ” next 포인터와 μ—°κ²°λœ λ…Έλ“œμ˜ 인덱슀λ₯Ό λ‚˜νƒ€λ‚΄λŠ”λ° 쓰이며 posλŠ” νŒŒλΌλ―Έν„°λ‘œ μ£Όμ–΄μ§€μ§€ μ•ŠλŠ”λ‹€.
16+
* μ£Όμ–΄μ§„ λ¦¬μŠ€νŠΈμ— 사이클이 μžˆλ‹€λ©΄ trueλ₯Ό κ·Έλ ‡μ§€ μ•Šλ‹€λ©΄ falseλ₯Ό λ°˜ν™˜ν•˜μ„Έμš”.
17+
* */
18+
public class Solution {
19+
20+
// μ‹œκ°„λ³΅μž‘λ„: O(n), κ³΅κ°„λ³΅μž‘λ„: O(1)
21+
public boolean hasCycle(ListNode head) {
22+
if (head == null) {
23+
return false;
24+
}
25+
26+
ListNode next = head;
27+
ListNode furtherNext = head;
28+
29+
while (furtherNext != null && furtherNext.next != null) {
30+
next = next.next;
31+
furtherNext = furtherNext.next.next;
32+
if (next == furtherNext) {
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
// head 없을 λ•ŒκΉŒμ§€ 계속 탐색 반볡 : μ‹œκ°„λ³΅μž‘λ„, κ³΅κ°„λ³΅μž‘λ„ O(n)
41+
// public boolean hasCycle(ListNode head) {
42+
43+
// Set<ListNode> nodeSet = new HashSet<>();
44+
45+
// while (head != null) {
46+
// if (nodeSet.contains(head)) {
47+
// return true;
48+
// }
49+
// nodeSet.add(head);
50+
// head = head.next;
51+
// }
52+
53+
// return false;
54+
55+
// }
56+
}
57+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
3+
// μ‹œκ°„λ³΅μž‘λ„: O(n)
4+
public int maxProduct(int[] nums) {
5+
6+
if (nums == null || nums.length == 0) {
7+
return 0;
8+
}
9+
10+
// μ΅œλŒ€ κ³± μΌ€μ΄μŠ€
11+
int maxProduct = nums[0];
12+
// μ΅œμ†Œ κ³± μΌ€μ΄μŠ€
13+
int minProduct = nums[0];
14+
// μ΅œλŒ€ κ°’
15+
int max = nums[0];
16+
17+
// DP둜 ν’€μ΄ν•˜μ˜€μ§€λ§Œ 음수 * 음수의 λ°˜λ‘€ μΌ€μ΄μŠ€ λ°œμƒν•˜μ—¬ ν•΄λ‹Ή ν’€μ΄λ‘œ μˆ˜μ •
18+
// Test Case : [-2, 3, -4] => dp둜 풀이 μ‹œ 3이 λ°˜ν™˜λ˜λŠ” 문제 μžˆμ—ˆμŒ
19+
20+
for (int i = 1; i < nums.length; i++) {
21+
22+
int current = nums[i];
23+
24+
// 음수일 경우 : μ΅œμ†Œ κ³±κ³Ό μ΅œλŒ€ κ³± μœ„μΉ˜ λ°”κΏˆ
25+
if (current < 0) {
26+
int tempMax = maxProduct;
27+
maxProduct = Math.max(current, minProduct * current);
28+
minProduct = Math.min(current, tempMax * current);
29+
} else {
30+
maxProduct = Math.max(current, maxProduct * current);
31+
minProduct = Math.min(current, minProduct * current);
32+
}
33+
34+
max = Math.max(max, maxProduct);
35+
36+
}
37+
38+
return max;
39+
40+
}
41+
}
42+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Solution {
5+
6+
// μ‹œκ°„λ³΅μž‘λ„: O(n), κ³΅κ°„λ³΅μž‘λ„ O(1)
7+
public String minWindow(String s, String t) {
8+
9+
int strLength = s.length();
10+
int targetLength = t.length();
11+
12+
// λͺ©ν‘œ λΆ„μžμ—΄μ΄ μ£Όμ–΄μ§„ λ¬Έμžμ—΄λ³΄λ‹€ κΈΈλ‹€λ©΄ λΆ€λΆ„ λ¬Έμžμ—΄λ‘œ λ³Ό 수 μ—†μŒ
13+
if (targetLength > strLength) {
14+
return "";
15+
}
16+
17+
// λͺ©ν‘œ λ¬Έμžμ—΄μ— ν•„μš”ν•œ λ¬Έμžμ™€ κ·Έ 개수λ₯Ό λ‹΄λŠ” λ§΅
18+
Map<Character, Integer> charMap = new HashMap<>();
19+
20+
for (char c : t.toCharArray()) {
21+
charMap.put(c, charMap.getOrDefault(c, 0) + 1);
22+
}
23+
24+
// 투 포인터 μ„ μ–Έ
25+
int left = 0;
26+
int right = 0;
27+
int minWindowLength = Integer.MAX_VALUE;
28+
int minWindowStart = 0; // μ΅œμ†Œ μœˆλ„μš° μ‹œμž‘ μœ„μΉ˜
29+
int remaining = targetLength;
30+
31+
while (right < strLength) {
32+
33+
Character end = s.charAt(right);
34+
35+
if (charMap.containsKey(end)) {
36+
charMap.put(end, charMap.get(end) - 1);
37+
if (charMap.get(end) >= 0) {
38+
remaining--;
39+
}
40+
}
41+
42+
// target λ¬Έμžμ—΄μ˜ λͺ¨λ“  문자λ₯Ό μ°Ύμ•˜λ‹€λ©΄ left 포인터 μ΄λ™ν•˜λ©΄μ„œ
43+
// μ΅œμ†Œ μœˆλ„μš° μ°ΎκΈ° μ‹œμž‘
44+
while (remaining == 0) {
45+
if (right - left + 1 < minWindowLength) {
46+
minWindowLength = right - left + 1;
47+
minWindowStart = left;
48+
}
49+
50+
char startChar = s.charAt(left);
51+
if (charMap.containsKey(startChar)) {
52+
charMap.put(startChar, charMap.get(startChar) + 1);
53+
if (charMap.get(startChar) > 0) {
54+
remaining++;
55+
}
56+
}
57+
58+
left++;
59+
}
60+
61+
right++;
62+
63+
}
64+
65+
return minWindowLength == Integer.MAX_VALUE ? ""
66+
: s.substring(minWindowStart, minWindowStart + minWindowLength);
67+
68+
}
69+
}
70+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
6+
int[] dx = {1, -1, 0, 0};
7+
int[] dy = {0, 0, 1, -1};
8+
// boolean[][] visited;
9+
int width, height;
10+
11+
// μ‹œκ°„λ³΅μž‘λ„, κ³΅κ°„λ³΅μž‘λ„: O(m * n)
12+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
13+
height = heights.length;
14+
width = heights[0].length;
15+
16+
boolean[][] pacific = new boolean[height][width];
17+
boolean[][] atlantic = new boolean[height][width];
18+
19+
/**
20+
* κ°€μž₯μžλ¦¬μ— λ„λ‹¬ν•˜λŠ” 경둜λ₯Ό 일일이 νƒμƒ‰ν•˜λŠ” 것이 μ•„λ‹Œ
21+
κ°€μž₯μžλ¦¬λΆ€ν„° 물이 흐λ₯Ό 수 μžˆλŠ” 경둜λ₯Ό μ—­μœΌλ‘œ 탐색
22+
*/
23+
24+
// height check
25+
for (int i = 0; i < height; i++) {
26+
dfs(i, 0, heights, pacific);
27+
dfs(i, width - 1, heights, atlantic);
28+
}
29+
30+
// width check
31+
for (int j = 0; j < width; j++) {
32+
dfs(0, j, heights, pacific);
33+
dfs(height - 1, j, heights, atlantic);
34+
}
35+
36+
List<List<Integer>> answer = new ArrayList<>();
37+
for (int i = 0; i < height; i++) {
38+
for (int j = 0; j < width; j++) {
39+
if (pacific[i][j] && atlantic[i][j]) {
40+
answer.add(List.of(i, j));
41+
}
42+
}
43+
}
44+
45+
return answer;
46+
}
47+
48+
private void dfs(int x, int y, int[][] heights, boolean[][] visited) {
49+
visited[x][y] = true;
50+
for (int i = 0; i < 4; i++) {
51+
int nx = x + dx[i];
52+
int ny = y + dy[i];
53+
if (nx >= 0 && nx < height && ny >= 0 && ny < width) {
54+
if (!visited[nx][ny] && heights[nx][ny] >= heights[x][y]) {
55+
dfs(nx, ny, heights, visited);
56+
}
57+
}
58+
}
59+
}
60+
61+
// public List<List<Integer>> pacificAtlantic(int[][] heights) {
62+
// height = heights.length;
63+
// width = heights[0].length;
64+
// visited = new boolean[height][width];
65+
66+
// List<List<Integer>> answer = new ArrayList<>();
67+
// for (int i = 0; i < height; i++) {
68+
// for (int j = 0; j < width; j++) {
69+
// WaterFlow checkWaterFlow = dfs(i, j, new WaterFlow(), heights);
70+
// if (checkWaterFlow.crossBothOceans()) {
71+
// List<Integer> coordinate = List.of(i, j);
72+
// answer.add(coordinate);
73+
// }
74+
// }
75+
// }
76+
// return answer;
77+
// }
78+
79+
// private WaterFlow dfs(int x, int y, WaterFlow checkWaterFlow, int[][] heights) {
80+
// /**
81+
// 0, 0 | 0, 1 | 0, 2 | 0, 3 | 0, 4
82+
// 1, 0 | 2, 0 | 3, 0 | 4, 0
83+
// */
84+
// if (x == 0 || y == 0) {
85+
// checkWaterFlow.crossPacificFlow();
86+
// }
87+
// /**
88+
// 4, 0 | 4, 1 | 4, 2 | 4, 3 | 4, 4
89+
// 3, 4 | 2, 4 | 1, 4 | 0, 4
90+
// */
91+
// if (x == height - 1 || y == width - 1) {
92+
// checkWaterFlow.crossAtlanticFlow();
93+
// }
94+
95+
// if (checkWaterFlow.crossBothOceans()) {
96+
// return checkWaterFlow;
97+
// }
98+
99+
// visited[x][y] = true;
100+
101+
// for (int i = 0; i < 4; i++) {
102+
// int nx = x + dx[i];
103+
// int ny = y + dy[i];
104+
// if (nx >= 0 && nx < height && ny >= 0 && ny < width) {
105+
// if (!visited[nx][ny] && heights[nx][ny] <= heights[x][y]) {
106+
// dfs(nx, ny, checkWaterFlow, heights);
107+
// }
108+
// }
109+
// }
110+
// visited[x][y] = false;
111+
112+
// return checkWaterFlow;
113+
// }
114+
115+
// static class WaterFlow {
116+
117+
// private boolean pacificFlow;
118+
119+
// private boolean atlanticFlow;
120+
121+
// WaterFlow() {
122+
// pacificFlow = false;
123+
// atlanticFlow = false;
124+
// }
125+
126+
// public void crossPacificFlow() {
127+
// pacificFlow = true;
128+
// }
129+
130+
// public void crossAtlanticFlow() {
131+
// atlanticFlow = true;
132+
// }
133+
134+
// public boolean crossBothOceans() {
135+
// return pacificFlow && atlanticFlow;
136+
// }
137+
138+
// }
139+
}
140+

β€Žsum-of-two-integers/Tessa1217.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** +, - λΆ€ν˜Έ μ“°μ§€ μ•Šκ³  두 수의 ν•© κ΅¬ν•˜κΈ° */
2+
class Solution {
3+
4+
// λΉ„νŠΈ μ—°μ‚°μœΌλ‘œ 계산 μˆ˜ν–‰
5+
public int getSum(int a, int b) {
6+
while (b != 0) {
7+
// μ™Όμͺ½ μ‰¬ν”„νŠΈν•˜μ—¬ 자리 μ˜¬λ¦Όμ— λŒ€ν•œ 계산 μˆ˜ν–‰
8+
int bit = (a & b) << 1;
9+
// XOR μ—°μ‚°μœΌλ‘œ 자리 올림 μ—†λŠ” λ§μ…ˆ μˆ˜ν–‰
10+
a = a ^ b;
11+
b = bit;
12+
}
13+
return a;
14+
}
15+
16+
/**
17+
* ν’€λ¦¬κΈ°λŠ” ν•˜μ§€λ§Œ, 이런 식은 νŽΈλ²•μΌ 것 같은...?
18+
* Class Integer 내뢀에 sum μ—°μ‚°: Adds two integers together as per the + operator.
19+
* + operator μ“΄λ‹€κ³  λ˜μ–΄μžˆμŒ...
20+
* */
21+
// public int getSum(int a, int b) {
22+
// return Integer.sum(a, b);
23+
// }
24+
25+
}
26+

0 commit comments

Comments
Β (0)