Skip to content

Commit a37ad35

Browse files
authored
Merge pull request #1580 from krokerdile/main
[krokerdile] Week 11 Solutions
2 parents 2d21160 + cb416e3 commit a37ad35

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var characterReplacement = function(s, k) {
7+
let left = 0;
8+
let maxCount = 0;
9+
const freq = new Array(26).fill(0); // 알파벳 빈도수 저장
10+
11+
let result = 0;
12+
13+
for (let right = 0; right < s.length; right++) {
14+
const idx = s.charCodeAt(right) - 65; // A ~ Z -> 0 ~ 25
15+
freq[idx]++;
16+
maxCount = Math.max(maxCount, freq[idx]);
17+
18+
// 현재 윈도우 크기 - 가장 많은 문자 수 > k 면 왼쪽 포인터 이동
19+
while ((right - left + 1) - maxCount > k) {
20+
freq[s.charCodeAt(left) - 65]--;
21+
left++;
22+
}
23+
24+
result = Math.max(result, right - left + 1);
25+
}
26+
27+
return result;
28+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {string}
5+
*/
6+
var minWindow = function(s, t) {
7+
if (s.length < t.length) return "";
8+
9+
// 1. t의 문자 개수를 세기
10+
const targetMap = new Map();
11+
for (let char of t) {
12+
targetMap.set(char, (targetMap.get(char) || 0) + 1);
13+
}
14+
15+
let left = 0;
16+
let right = 0;
17+
let minLen = Infinity;
18+
let minStart = 0;
19+
20+
let required = targetMap.size; // 만족시켜야 할 문자 종류 수
21+
let formed = 0;
22+
const windowMap = new Map();
23+
24+
while (right < s.length) {
25+
const char = s[right];
26+
windowMap.set(char, (windowMap.get(char) || 0) + 1);
27+
28+
// 문자 개수가 딱 맞는 경우
29+
if (targetMap.has(char) && windowMap.get(char) === targetMap.get(char)) {
30+
formed++;
31+
}
32+
33+
// 모든 문자가 충족될 경우 -> 왼쪽 포인터 당겨보기
34+
while (left <= right && formed === required) {
35+
if (right - left + 1 < minLen) {
36+
minLen = right - left + 1;
37+
minStart = left;
38+
}
39+
40+
const leftChar = s[left];
41+
windowMap.set(leftChar, windowMap.get(leftChar) - 1);
42+
if (targetMap.has(leftChar) && windowMap.get(leftChar) < targetMap.get(leftChar)) {
43+
formed--;
44+
}
45+
left++;
46+
}
47+
48+
right++;
49+
}
50+
51+
return minLen === Infinity ? "" : s.slice(minStart, minStart + minLen);
52+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {number[][]} heights
3+
* @return {number[][]}
4+
*/
5+
const pacificAtlantic = (heights) => {
6+
const m = heights.length;
7+
const n = heights[0].length;
8+
const pacific = Array.from({ length: m }, () => Array(n).fill(false));
9+
const atlantic = Array.from({ length: m }, () => Array(n).fill(false));
10+
11+
const dfs = (r, c, visited, prevHeight) => {
12+
if (
13+
r < 0 || c < 0 || r >= m || c >= n ||
14+
visited[r][c] || heights[r][c] < prevHeight
15+
) return;
16+
17+
visited[r][c] = true;
18+
19+
dfs(r + 1, c, visited, heights[r][c]);
20+
dfs(r - 1, c, visited, heights[r][c]);
21+
dfs(r, c + 1, visited, heights[r][c]);
22+
dfs(r, c - 1, visited, heights[r][c]);
23+
};
24+
25+
for (let i = 0; i < m; i++) {
26+
dfs(i, 0, pacific, heights[i][0]);
27+
dfs(i, n - 1, atlantic, heights[i][n - 1]);
28+
}
29+
30+
for (let j = 0; j < n; j++) {
31+
dfs(0, j, pacific, heights[0][j]);
32+
dfs(m - 1, j, atlantic, heights[m - 1][j]);
33+
}
34+
35+
const result = [];
36+
for (let i = 0; i < m; i++) {
37+
for (let j = 0; j < n; j++) {
38+
if (pacific[i][j] && atlantic[i][j]) {
39+
result.push([i, j]);
40+
}
41+
}
42+
}
43+
return result;
44+
};

0 commit comments

Comments
 (0)