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
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