Skip to content

Commit af257ad

Browse files
authored
Merge pull request #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+
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)