Skip to content
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

[YeomChaeeun] Week 9 #988

Merged
merged 4 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions find-minimum-in-rotated-sorted-array/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 정렬된 배열에서 최소값 찾기
* 알고리즘 복잡도
* - 시간 복잡도: O(logn)
* - 공간 복잡도: O(1)
* @param nums
*/
function findMin(nums: number[]): number {
// 풀이 1 - sort() 사용
// 시간 복잡도: O(nlogn) / 공간 복잡도: O(1)
// nums.sort((a, b) => a - b)
// return nums[0]

// 풀이 2 - 배열이 정렬되어 있음을 활용한 풀이
// 시간 복잡도: O(n) / 공간 복잡도: O(1)
// let min = nums[0];
// for(let i = 1; i < nums.length; i++) {
// console.log(nums[i])
// min = Math.min(nums[i], min)
// }
// return min

// 이분 탐색법 활용
// 절반씩 잘라서 nums[n-1] > nums[n] 의 지점을 찾는다
let low = 1;
let high = nums.length - 1;
while(low <= high) {
let mid = Math.floor((low + high) / 2);
if(nums[mid - 1] > nums[mid]) {
return nums[mid];
}
if(nums[0] < nums[mid]) {
low = mid + 1;
} else {
high = mid -1;
}
YeomChaeeun marked this conversation as resolved.
Show resolved Hide resolved
}
return nums[0]
}
28 changes: 28 additions & 0 deletions linked-list-cycle/YeomChaeeun.ts
YeomChaeeun marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
/**
* 순환되는 링크드 리스트 찾기
* 알고리즘 복잡도
* - 시간 복잡도: O(n)
* - 공간 복잡도: O(n)
* @param head
*/
function hasCycle(head: ListNode | null): boolean {
let set = new Set();
while(head !== null) {
// set에 이미 존재하는지 확인
if(set.has(head)) return true
set.add(head)
head = head.next
}
return false
}