Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions invert-binary-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
const dfs = (current) => {
if (!current) {
return;
}

const temp = current.left;
current.left = current.right;
current.right = temp;

dfs(current.left);
dfs(current.right);
};

dfs(root);

return root;
};
Comment on lines +12 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

DFS와 재귀를 활용하면 더 깔끔한 코드로 구현할 수 있겠어요. 참고가 되었습니다. 감사합니다!

28 changes: 28 additions & 0 deletions jump-game/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time complexity: O(n)
// Space complexity: O(1)
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 거의 동일한 방식으로 풀었습니다! 표현하는 방식 등 여러가지로 파이썬과 다른 부분이 있어서 비교해서 보는 재미가 있었어요. 시간/공간 복잡도에 대한 설명을 추가해주시면 더 좋을 것 같습니다. 이번 주도 고생하셨습니다!


/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function (nums) {
let maxValue = nums[0];

for (let i = 1; i < nums.length; i++) {
if (i > maxValue) {
return false;
}

const newNum = i + nums[i];

if (newNum > maxValue) {
maxValue = newNum;
}

if (maxValue >= nums.length - 1) {
break;
}
}

return true;
};
40 changes: 40 additions & 0 deletions search-in-rotated-sorted-array/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time complexity: O(logn)
// Space complexity: O(1)

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

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

if (nums.at(mid) === target) {
return mid;
}

// rotate 된 구간이 있을 때
if (nums.at(mid + 1) > nums.at(right)) {
if (nums.at(right) >= target || nums.at(mid + 1) <= target) {
left = mid + 1;
continue;
}

right = mid - 1;
continue;
}

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

right = mid - 1;
}

return -1;
};