-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from JEONGHWANMIN/main
[ํ๋ฏธ๋๋] Week9 Solutions
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |