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

[ganu] Week9 #983

Merged
merged 6 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions find-minimum-in-rotated-sorted-array/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time complexity: O(logn)
// Space complexity: O(1)

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

while (left < right) {
const mid = Math.floor((left + right) / 2);

if (nums.at(mid - 1) > nums.at(mid)) {
return nums[mid];
}

if (nums.at(mid + 1) < nums.at(mid)) {
return nums[mid + 1];
}
Comment on lines +15 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mid 값의 경계에 대한 조건을 없애면 코드를 더 단순화할 수 있을 것 같습니다!


if (nums.at(mid + 1) > nums.at(right)) {
left = mid + 1;
continue;
}

right = mid - 1;
}

return nums[left];
};
22 changes: 22 additions & 0 deletions linked-list-cycle/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity: O(n)
// Space complexity: O(1)

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

while (turtle && rabbit && rabbit.next) {
turtle = turtle.next;
rabbit = rabbit.next.next;

if (turtle === rabbit) {
return true;
}
}

return false;
};
31 changes: 31 additions & 0 deletions maximum-product-subarray/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function (nums) {
let answer = nums[0];
const products = Array.from({ length: nums.length + 1 }, () => null);
products[0] = [1, 1]; // [min, max]

for (let i = 1; i < products.length; i++) {
const cases = [];

// case 1
cases.push(nums[i - 1]);

// case 2
cases.push(nums[i - 1] * products[i - 1][0]);

// case 3
cases.push(nums[i - 1] * products[i - 1][1]);
Comment on lines +16 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석으로 케이스를 구분해주셔서 코드를 읽기 쉬웠습니다! 이번 주도 고생하셨습니다 😊


const product = [Math.min(...cases), Math.max(...cases)];
products[i] = product;
answer = Math.max(answer, product[1]);
}

return answer;
};