Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions container-with-most-water/sadie100.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 양 끝에서 시작하여 포인터를 이동시키며 최대 면적을 찾는 방식으로, Two Pointers 패턴에 해당합니다. 효율적인 탐색을 위해 포인터를 좁혀가며 비교하는 구조입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
시작인덱스 start와 끝인덱스 end를 지정하고 result 변수를 갱신, start와 end를 좁혀가며 최대 result를 찾는다
- start와 end 중 더 작은 값을 옮겨가며 값을 비교.
- 둘이 겹쳐지면 리턴한다.

result를 리턴

시간복잡도 : O(N), 공간복잡도 : O(1)
*/

function maxArea(height: number[]): number {
let start = 0
let end = height.length - 1
let result = 0

function getAmount(startIdx: number, endIdx: number): number {
return (endIdx - startIdx) * Math.min(height[endIdx], height[startIdx])
}

while (start < end) {
const area = getAmount(start, end)
result = Math.max(result, area)

if (height[start] < height[end]) {
start += 1
} else {
end -= 1
}
}

return result
}
28 changes: 28 additions & 0 deletions find-minimum-in-rotated-sorted-array/sadie100.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search
  • 설명: 이 코드는 회전된 정렬 배열에서 최소값을 찾기 위해 이분탐색을 활용하여 범위를 좁혀가는 방식으로 동작한다. 중간값과 끝값을 비교하며 탐색 범위를 조정하는 특징이 있다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
이분탐색으로 정렬 탐색. 최소값이 들어있는 범위를 바탕으로 계속 좁혀 간다
중간값을 기준값으로 잡고, 마지막 값보다 큰지 작은지 확인한다
기준값이 마지막 값보다 작을 경우, 최소값은 왼쪽 반쪽에 있거나 기준값임. ex) 5 1 2 3 4
-> range를 왼쪽 반쪽으로 줄이고(기준값 포함) 해당 중간값을 기준값으로 잡는다
기준값이 마지막 값보다 클 경우, 최소값은 오른쪽 반쪽에 있음. ex) 3 4 5 1 2
-> range를 오른쪽 반쪽으로 줄이고 해당 중간값을 기준값으로 잡는다

최종적으로 start, end가 같아지는 값이 최소값이 된다.

시간복잡도 : O(LogN) -> N은 nums의 개수. 이분탐색
*/

function findMin(nums: number[]): number {
let start = 0
let end = nums.length - 1

while (start < end) {
let target = Math.floor((start + end) / 2)
if (nums[target] < nums[end]) {
end = target
} else {
start = target + 1
}
}

return nums[start]
}
33 changes: 33 additions & 0 deletions valid-parentheses/sadie100.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Stack / Priority Queue
  • 설명: 이 코드는 괄호의 짝을 맞추기 위해 스택을 사용하며, 열린 괄호를 스택에 넣고 닫는 괄호를 만날 때 짝을 확인하는 방식입니다. 스택 구조를 활용한 대표적인 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
스택을 만들어 s를 탐색하며 집어넣고, 허용되지 않은 괄호가 오면 false를 리턴한다
- 허용하는 괄호 : 열려있는 괄호 (, {, [ 혹은 마지막 원소의 짝이 되는 닫는괄호

시간복잡도 : O(N) - N은 s의 length
공간복잡도 : O(N) (괄호 배열 및 괄호 쌍)
*/

function isValid(s: string): boolean {
const stack = []
const openBrackets = ['(', '{', '[']
const bracketMap = {
'(': ')',
'{': '}',
'[': ']',
}

for (const char of s) {
if (openBrackets.includes(char)) {
stack.push(char)
continue
}
const last = stack.at(-1)
if (char === bracketMap[last]) {
stack.pop()
continue
}

return false
}

return stack.length === 0
}
Loading