-
-
Notifications
You must be signed in to change notification settings - Fork 195
[krokerdile] Week 11 Solutions #1580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @param {string} s | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var characterReplacement = function(s, k) { | ||
let left = 0; | ||
let maxCount = 0; | ||
const freq = new Array(26).fill(0); // 알파벳 빈도수 저장 | ||
|
||
let result = 0; | ||
|
||
for (let right = 0; right < s.length; right++) { | ||
const idx = s.charCodeAt(right) - 65; // A ~ Z -> 0 ~ 25 | ||
freq[idx]++; | ||
maxCount = Math.max(maxCount, freq[idx]); | ||
|
||
// 현재 윈도우 크기 - 가장 많은 문자 수 > k 면 왼쪽 포인터 이동 | ||
while ((right - left + 1) - maxCount > k) { | ||
freq[s.charCodeAt(left) - 65]--; | ||
left++; | ||
} | ||
|
||
result = Math.max(result, right - left + 1); | ||
} | ||
|
||
return result; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {string} | ||
*/ | ||
var minWindow = function(s, t) { | ||
if (s.length < t.length) return ""; | ||
|
||
// 1. t의 문자 개수를 세기 | ||
const targetMap = new Map(); | ||
for (let char of t) { | ||
targetMap.set(char, (targetMap.get(char) || 0) + 1); | ||
} | ||
|
||
let left = 0; | ||
let right = 0; | ||
let minLen = Infinity; | ||
let minStart = 0; | ||
|
||
let required = targetMap.size; // 만족시켜야 할 문자 종류 수 | ||
let formed = 0; | ||
const windowMap = new Map(); | ||
|
||
while (right < s.length) { | ||
const char = s[right]; | ||
windowMap.set(char, (windowMap.get(char) || 0) + 1); | ||
|
||
// 문자 개수가 딱 맞는 경우 | ||
if (targetMap.has(char) && windowMap.get(char) === targetMap.get(char)) { | ||
formed++; | ||
} | ||
|
||
// 모든 문자가 충족될 경우 -> 왼쪽 포인터 당겨보기 | ||
while (left <= right && formed === required) { | ||
if (right - left + 1 < minLen) { | ||
minLen = right - left + 1; | ||
minStart = left; | ||
} | ||
|
||
const leftChar = s[left]; | ||
windowMap.set(leftChar, windowMap.get(leftChar) - 1); | ||
if (targetMap.has(leftChar) && windowMap.get(leftChar) < targetMap.get(leftChar)) { | ||
formed--; | ||
} | ||
left++; | ||
} | ||
|
||
right++; | ||
} | ||
|
||
return minLen === Infinity ? "" : s.slice(minStart, minStart + minLen); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @param {number[][]} heights | ||
* @return {number[][]} | ||
*/ | ||
const pacificAtlantic = (heights) => { | ||
const m = heights.length; | ||
const n = heights[0].length; | ||
const pacific = Array.from({ length: m }, () => Array(n).fill(false)); | ||
const atlantic = Array.from({ length: m }, () => Array(n).fill(false)); | ||
|
||
const dfs = (r, c, visited, prevHeight) => { | ||
if ( | ||
r < 0 || c < 0 || r >= m || c >= n || | ||
visited[r][c] || heights[r][c] < prevHeight | ||
) return; | ||
|
||
visited[r][c] = true; | ||
|
||
dfs(r + 1, c, visited, heights[r][c]); | ||
dfs(r - 1, c, visited, heights[r][c]); | ||
dfs(r, c + 1, visited, heights[r][c]); | ||
dfs(r, c - 1, visited, heights[r][c]); | ||
}; | ||
|
||
for (let i = 0; i < m; i++) { | ||
dfs(i, 0, pacific, heights[i][0]); | ||
dfs(i, n - 1, atlantic, heights[i][n - 1]); | ||
} | ||
|
||
for (let j = 0; j < n; j++) { | ||
dfs(0, j, pacific, heights[0][j]); | ||
dfs(m - 1, j, atlantic, heights[m - 1][j]); | ||
} | ||
|
||
const result = []; | ||
for (let i = 0; i < m; i++) { | ||
for (let j = 0; j < n; j++) { | ||
if (pacific[i][j] && atlantic[i][j]) { | ||
result.push([i, j]); | ||
} | ||
} | ||
} | ||
return result; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dfs로 잘 풀이해 주셨네요. BFS로 풀이 방법도 도전해보시면 좋을것 같습니다!