-
-
Notifications
You must be signed in to change notification settings - Fork 195
[YeomChaeeun] Week 6 #890
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
[YeomChaeeun] Week 6 #890
Changes from all commits
Commits
Show all changes
3 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,33 @@ | ||
/** | ||
* 배열의 두개의 높이를 가지고 최대 용량을 구하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(1) | ||
* @param height | ||
*/ | ||
function maxArea(height: number[]): number { | ||
// end - start * min(height[start], height[end])가 큰 것 | ||
|
||
// 8 - 0 * min(1, 7) = 8 | ||
// 8 - 1 * min(8, 7) = 49 | ||
// 7 - 1 * min(8, 3) = 18 | ||
// 6 - 1 * min(8, 8) = 40 | ||
// ... | ||
|
||
let s = 0 | ||
let e = height.length - 1 | ||
let curr = 0 | ||
let max = 0 | ||
|
||
while(s < e) { | ||
curr = (e - s) * Math.min(height[s], height[e]) | ||
max = Math.max(curr, max) | ||
if(height[s] < height[e]){ | ||
s += 1 | ||
} else { | ||
e -= 1 | ||
} | ||
} | ||
|
||
return max | ||
} |
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,26 @@ | ||
/** | ||
* 주어진 배열에서 가장 긴 부분 수열의 길이 구하기 | ||
* 달고알레 풀이를 참고하여 동적 프로그래밍 적용했습니다 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n2) | ||
* - 공간 복잡도: O(n) | ||
* @param nums | ||
*/ | ||
function lengthOfLIS(nums: number[]): number { | ||
// dp 배열을 1로 초기화 - 각 숫자 단독의 기본 길이는 1임 | ||
const dp: number[] = new Array(nums.length).fill(1) | ||
let maxLength = 1 | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
// 현재 위치(i) 이전의 모든 원소들을 확인 | ||
for (let j = 0; j < i; j++) { | ||
// 현재 숫자가 이전 숫자보다 큰 경우 - 부분 수열이 가능하다는 것 | ||
if (nums[i] > nums[j]) { | ||
dp[i] = Math.max(dp[i], dp[j] + 1) | ||
} | ||
} | ||
maxLength = Math.max(maxLength, dp[i]) | ||
} | ||
|
||
return maxLength | ||
} |
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,43 @@ | ||
/** | ||
* 달팽이 알고리즘 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) - 모든 행렬의 원소의 수 (rows * columns) | ||
* - 공간 복잡도: O(n) - 결과 저장을 위한 배열 | ||
* @param matrix | ||
*/ | ||
function spiralOrder(matrix: number[][]): number[] { | ||
// 정처기 단골 문제였던 기억이.. | ||
const result: number[] = []; | ||
let top = 0 | ||
let bottom = matrix.length - 1; | ||
let left = 0 | ||
let right = matrix[0].length - 1; | ||
|
||
while(top <= bottom && left <= right) { // 순환 조건 | ||
for(let i = left; i <= right; i++) { | ||
result.push(matrix[top][i]) | ||
} | ||
top++ | ||
|
||
for(let i = top; i <= bottom; i++) { | ||
result.push(matrix[i][right]) | ||
} | ||
right-- | ||
|
||
if(top <= bottom) { | ||
for(let i = right; i >= left; i--) { | ||
result.push(matrix[bottom][i]) | ||
} | ||
bottom-- | ||
} | ||
|
||
if(left <= right) { | ||
for(let i = bottom; i >= top; i--) { | ||
result.push(matrix[i][left]) | ||
} | ||
left++ | ||
} | ||
} | ||
|
||
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,42 @@ | ||
/** | ||
* valid-parentheses | ||
* 괄호의 열고 닫히는 짝을 확인하는 알고리즘 | ||
* Stack(LIFO) 데이터 구조 사용 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param s | ||
*/ | ||
function isValid(s: string): boolean { | ||
|
||
// 접근 1 - {}, (), [] 가 포함되는지 보고 replace문으로 단순하게 풀어봄.. | ||
// while (s.includes("{}") || s.includes("()") || s.includes("[]")) { | ||
// s = s.replace("{}", ""); | ||
// s = s.replace("()", ""); | ||
// s = s.replace("[]", ""); | ||
// } | ||
// return s === ''; | ||
|
||
// 접근 2 - leetCode의 hint를 보고 stack 을 적용 | ||
const stack: string[] = [] | ||
const pairs: {[key: string]: string} = { | ||
'}': '{', | ||
')': '(', | ||
']': '[' | ||
} | ||
|
||
for (const char of s) { | ||
if (!pairs[char]) { | ||
// 여는 괄호 저장 | ||
stack.push(char) | ||
} else { | ||
// 닫는 괄호와 매칭 확인 | ||
if (stack.pop() !== pairs[char]) { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return stack.length === 0 | ||
} | ||
|
Oops, something went wrong.
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.
👍