Skip to content

Commit 0038b5f

Browse files
authored
Merge pull request #2572 from hyeri0903/main
[hyeri0903] WEEK 09 Solutions
2 parents f8f15fb + fb649e9 commit 0038b5f

5 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
public class Solution {
13+
public boolean hasCycle(ListNode head) {
14+
/**
15+
1.prob: linked list ์ด๋ฉด true, ์•„๋‹ˆ๋ฉด false return
16+
2.constraints
17+
- # of nodes min = 0, max = 10000
18+
- pos: -1 or valid index
19+
- pos๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์ง€ ์•Š์Œ
20+
3.solution
21+
- pointer 2๊ฐœ๋ฅผ ๋‘๊ณ  1๊ฐœ๋Š” 2์นธ์”ฉ, 1๊ฐœ๋Š” 1์นธ์”ฉ ์ด๋™ํ•˜๋ฉฐ ๊ฒฐ๊ตญ ๋งŒ๋‚˜๋Š”์ง€ ์ฒดํฌ
22+
๋งŒ๋‚˜๋ฉด true, ์•ˆ ๋งŒ๋‚˜๋ฉด false return
23+
*/
24+
25+
ListNode slow = head;
26+
ListNode fast = head;
27+
28+
while(fast != null && fast.next != null) {
29+
slow = slow.next;
30+
fast = fast.next.next;
31+
32+
//๋…ธ๋“œ๊ฐ€ ๋™์ผํ•˜๋ฉด linked list
33+
if(slow == fast) {
34+
return true;
35+
}
36+
}
37+
return false;
38+
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int maxProduct(int[] nums) {
3+
/**
4+
1.problem: subarray ์ค‘์— ๊ฐ€์žฅ ํฐ product return
5+
2.constraints
6+
- num.length min = 1
7+
- value min = -10, max=10
8+
3.solutions
9+
- bruteforce, time: O(n^2), space: O(1)
10+
- min, max value ์ถ”์ , time: O(n), space: O(1)
11+
*/
12+
int n = nums.length;
13+
if (n == 1) return nums[0];
14+
15+
// int maxValue = Integer.MIN_VALUE;
16+
// for(int i = 0; i < n; i++) {
17+
// int curValue = 1;
18+
// for(int j = i; j < n; j++) {
19+
// curValue *= nums[j];
20+
// maxValue = Math.max(maxValue, curValue);
21+
// }
22+
// }
23+
24+
int max = nums[0];
25+
int min = nums[0];
26+
int res = nums[0];
27+
28+
for(int i = 1; i < n; i++) {
29+
int cur = nums[i];
30+
//ํ˜„์žฌ๊ฐ’์ด ์Œ์ˆ˜์ด๋ฉด min, max swap
31+
if(cur < 0) {
32+
int tmp = min;
33+
min = max;
34+
max = tmp;
35+
}
36+
max = Math.max(cur, max * cur);
37+
min = Math.min(cur, min * cur);
38+
39+
res = Math.max(max, res);
40+
}
41+
return res;
42+
}
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Solution {
2+
public String minWindow(String s, String t) {
3+
/**
4+
1.prob: t๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์ตœ์†Œ ๊ธธ์ด์˜ substring return.
5+
2.constraints
6+
- ์ •๋‹ต์€ ๋‹จ 1๊ฐœ
7+
- t์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ํฌํ•จ
8+
- m,n length min = 1, max = 100000
9+
- s,t ๋Š” uppercase or lowercase
10+
- ์ •๋‹ต ์—†์œผ๋ฉด emptystring "" return
11+
3.solution
12+
- brutforce, ๋ชจ๋“  substring ๊ตฌํ•ด์„œ t๋ฌธ์ž ํฌํ•จ ์—ฌ๋ถ€ ์ฒดํฌ, time complexity: O(n^2)
13+
- two pointer: right pointer ์›€์ง์ด๋‹ค๊ฐ€ t๋ฌธ์ž ๋ชจ๋‘ ํฌํ•จํ•˜๋ฉด left pointer ์˜ฎ๊ธฐ๋ฉด์„œ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ substring ๊ตฌํ•˜๊ธฐ, time compliexty: O(m+n), space: O(1)
14+
ํžŒํŠธ ๋ด๋„ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ํ’€์ด๋ณด๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ใ…œใ…œ
15+
*/
16+
17+
int m = s.length();
18+
int n = t.length();
19+
String answer = "";
20+
21+
if(m < n) return "";
22+
23+
int left = 0, right = 0;
24+
int count = n; //ํ•„์š”ํ•œ ๋ฌธ์ž ์ˆ˜ = t.length()
25+
int minLen = Integer.MAX_VALUE;
26+
int start = 0; //์กฐ๊ฑด ๋งŒ์กฑํ•˜๋Š” left pointer ์‹œ์ž‘ ํฌ์ธํŠธ
27+
28+
int[] freq = new int[128];
29+
for(char c : t.toCharArray()) {
30+
freq[c]++;
31+
}
32+
33+
while(right < m) {
34+
char r = s.charAt(right);
35+
//t ๋ฌธ์ž๋ฉด count--
36+
if(freq[r] > 0) {
37+
count --;
38+
}
39+
freq[r]--;
40+
right++;
41+
42+
//์กฐ๊ฑด ๋งŒ์กฑํ•˜๋ฉด left pointer ์ด๋™ํ•˜๋ฉฐ minimum length ์ฐพ์Œ
43+
while(count == 0) {
44+
//minLen update
45+
if(right - left < minLen) {
46+
minLen = right - left;
47+
start = left; //minLen ์—…๋ฐ์ดํŠธ ์‹œ Left pointer index ์ €์žฅ
48+
}
49+
char l = s.charAt(left);
50+
freq[l]++;
51+
52+
//t๋ฌธ์ž(ํ•„์š”ํ•œ ๋ฌธ์ž)์ด๋ฉด count ์ฆ๊ฐ€ (์›๋ž˜ ๊ฐ’์œผ๋กœ ๋ณต๊ตฌ)
53+
if(freq[l] > 0) count++;
54+
left++;
55+
}
56+
57+
}
58+
59+
if(minLen == Integer.MAX_VALUE) {
60+
answer = "";
61+
} else {
62+
answer = s.substring(start, start + minLen);
63+
}
64+
return answer;
65+
}
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};
3+
4+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
5+
/**
6+
1.2๊ฐœ์˜ ocean ๋ชจ๋‘๋กœ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์นธ๋“ค์„ ์ฐพ๋Š” ๋ฌธ์ œ
7+
2.height ๋†’์€ ๊ณณ -> ๋‚ฎ์€ ๊ณณ์œผ๋กœ ์ด๋™
8+
time, space: O(mn)
9+
*/
10+
int m = heights.length;
11+
int n = heights[0].length;
12+
boolean[][] pacific = new boolean[m][n];
13+
boolean[][] atlantic = new boolean[m][n];
14+
15+
List<List<Integer>> answer = new ArrayList<>();
16+
17+
for(int i = 0; i < m; i++) {
18+
dfs(i, 0, m, n, heights, pacific);
19+
dfs(i, n-1, m, n, heights, atlantic);
20+
}
21+
for(int j = 0; j < n; j++) {
22+
dfs(0, j, m, n, heights, pacific);
23+
dfs(m-1, j, m, n, heights, atlantic);
24+
}
25+
26+
for(int i = 0; i < m; i++) {
27+
for(int j = 0; j < n; j++) {
28+
if(pacific[i][j] && atlantic[i][j]) {
29+
answer.add(Arrays.asList(i,j));
30+
}
31+
}
32+
}
33+
return answer;
34+
}
35+
36+
void dfs(int i, int j, int m, int n, int[][] heights, boolean[][] visited) {
37+
if(i < 0 || i >= m || j < 0 || j >= n || visited[i][j]) {
38+
return;
39+
}
40+
41+
visited[i][j] = true;
42+
for(int[] d: dirs) {
43+
int nexti = i + d[0];
44+
int nextj = j + d[1];
45+
46+
//๋ฒ”์œ„ ์•ˆ && ๋ฐฉ๋ฌธ ์•ˆํ–ˆ๊ณ  && ๋†’์ด ์กฐ๊ฑด ๋งŒ์กฑํ•˜๋ฉด ๋‹ค์Œ ๋ฃจํŠธ ํƒ์ƒ‰
47+
if(nexti >= 0 && nexti < m && nextj >= 0 && nextj < n && !visited[nexti][nextj] && heights[nexti][nextj] >= heights[i][j]) {
48+
dfs(nexti, nextj, m, n, heights, visited);
49+
}
50+
51+
}
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int getSum(int a, int b) {
3+
/**
4+
1.operator ์„ ์“ฐ์ง€์•Š๊ณ  two sum ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
5+
2.constraints
6+
- without using operators + and -
7+
- value min = -1000, max = 1000
8+
3.solution
9+
-bit ์—ฐ์‚ฐ
10+
*/
11+
while (b != 0) {
12+
int carry = (a & b) << 1; // ์ž๋ฆฌ ์˜ฌ๋ฆผ
13+
a = a ^ b; // ์ž๋ฆฌ ์˜ฌ๋ฆผ ์—†๋Š” ํ•ฉ
14+
b = carry; // ๋‹ค์Œ์— ๋”ํ•  carry
15+
}
16+
return a;
17+
}
18+
}

0 commit comments

Comments
ย (0)