Skip to content

Commit

Permalink
Merge pull request #528 from JEONGHWANMIN/main
Browse files Browse the repository at this point in the history
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week9 Solutions
  • Loading branch information
JEONGHWANMIN authored Oct 17, 2024
2 parents d7bea6c + a5b768d commit 66cd1dd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
32 changes: 32 additions & 0 deletions find-minimum-in-rotated-sorted-array/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ์‹œ๊ฐ„๋ณต์žก๋„: O(log n)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
let leftIdx = 0;
let rightIdx = nums.length - 1;

if (nums.length === 1) return nums[0]

while (leftIdx <= rightIdx) {
if (nums[leftIdx] < nums[rightIdx]) return nums[leftIdx]

let midIdx = Math.floor((leftIdx + rightIdx) / 2);

if (nums[midIdx] > nums[midIdx+1]) {
return nums[midIdx+1]
}

if (nums[leftIdx] < nums[midIdx] && nums[leftIdx] > nums[rightIdx]) {
leftIdx = midIdx
} else {
rightIdx = midIdx
}
}

return nums[0]
};

29 changes: 29 additions & 0 deletions linked-list-cycle/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
let fastPointer = head;
let slowPointer = head;

while (fastPointer && fastPointer.next) {
slowPointer = slowPointer.next;
fastPointer = fastPointer.next.next

if (fastPointer === slowPointer) return true

}

return false
};
19 changes: 19 additions & 0 deletions maximum-subarray/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
let maxNum = -Infinity;
let prevSum = -Infinity;

for (let i = 0 ; i < nums.length; i++) {
prevSum = Math.max(prevSum + nums[i], nums[i])
maxNum = Math.max(prevSum, maxNum)
}


return maxNum
};

0 comments on commit 66cd1dd

Please sign in to comment.