Skip to content

Commit 65cefcc

Browse files
committed
feat: linked-list-cycle, find-minimum-in-rotated-sorted-array solution
1 parent f2b9477 commit 65cefcc

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ์ตœ์†Œ๊ฐ’ ์ฐพ๊ธฐ
3+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
4+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(logn)
5+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
6+
* @param nums
7+
*/
8+
function findMin(nums: number[]): number {
9+
// ํ’€์ด 1 - sort() ์‚ฌ์šฉ
10+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(nlogn) / ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
11+
// nums.sort((a, b) => a - b)
12+
// return nums[0]
13+
14+
// ํ’€์ด 2 - ๋ฐฐ์—ด์ด ์ •๋ ฌ๋˜์–ด ์žˆ์Œ์„ ํ™œ์šฉํ•œ ํ’€์ด
15+
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) / ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
16+
// let min = nums[0];
17+
// for(let i = 1; i < nums.length; i++) {
18+
// console.log(nums[i])
19+
// min = Math.min(nums[i], min)
20+
// }
21+
// return min
22+
23+
// ์ด๋ถ„ ํƒ์ƒ‰๋ฒ• ํ™œ์šฉ
24+
// ์ ˆ๋ฐ˜์”ฉ ์ž˜๋ผ์„œ nums[n-1] > nums[n] ์˜ ์ง€์ ์„ ์ฐพ๋Š”๋‹ค
25+
let low = 1;
26+
let high = nums.length - 1;
27+
while(low <= high) {
28+
let mid = Math.floor((low + high) / 2);
29+
if(nums[mid - 1] > nums[mid]) {
30+
return nums[mid];
31+
}
32+
if(nums[0] < nums[mid]) {
33+
low = mid + 1;
34+
} else {
35+
high = mid -1;
36+
}
37+
}
38+
return nums[0]
39+
}

โ€Žlinked-list-cycle/YeomChaeeun.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
/**
13+
* ์ˆœํ™˜๋˜๋Š” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ์ฐพ๊ธฐ
14+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
15+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
16+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
17+
* @param head
18+
*/
19+
function hasCycle(head: ListNode | null): boolean {
20+
let set = new Set();
21+
while(head !== null) {
22+
// set์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
23+
if(set.has(head)) return true
24+
set.add(head)
25+
head = head.next
26+
}
27+
return false
28+
}

0 commit comments

Comments
ย (0)