Skip to content

[Sehwan]Week2 solutions with JavaScript #58

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

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions invert-binary-tree/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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) {
// Check root is null
if (!root) return null;
// Create left and right variable to make recurrsive
let left = root.left;
let right = root.right;
// Execute invertTree functino
root.left = invertTree(right);
root.right = invertTree(left);
return root;
};

// TC: O(n)
// SC: O(n)
27 changes: 27 additions & 0 deletions linked-list-cycle/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
// Make fast pointer
// Fast pointer will move two steps further inside of list
let fast = head;
// Iterate until fast pointer and head is equal
while (fast && fast.next) {
head = head.next;
fast = fast.next.next;
if (head == fast) return true;
}
return false;
};

// TC: O(n)
// SC: O(1)
44 changes: 44 additions & 0 deletions merge-two-sorted-lists/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function (list1, list2) {
// Make sure both or one of the list is null
if (!list1 && !list2) return null;
if (!list1) return list2;
if (!list2) return list1;

// Create dummy listNode
let dummy = new ListNode(0);
let head = dummy;
// Make head of dummy list by using smaller node from list1 and list2
while (list1 && list2) {
if (list1.val <= list2.val) {
dummy.next = list1;
list1 = list1.next;
} else {
dummy.next = list2;
list2 = list2.next;
}
// After choosing with dummy list, head should be moved next
dummy = dummy.next;
}
// Iterate until both of list head is equal to null
if (list1 !== null) {
dummy.next = list1;
} else {
dummy.next = list2;
}
return head.next;
};

// TC: O(m+n)
// SC: O(1)
63 changes: 63 additions & 0 deletions reverse-linked-list/nhistory.js
Copy link
Contributor

Choose a reason for hiding this comment

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

였우... ν…ŒμŠ€νŠΈκΉŒμ§€ ν•΄λ³΄μ…¨κ΅°μš” πŸ‘

Copy link
Contributor Author

Choose a reason for hiding this comment

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

λ„€λ„€ μ΄λ ‡κ²Œ λ§Œλ“€μ–΄λ³΄λ‹ˆκΉŒ λ§ν¬λ“œ λ¦¬μŠ€νŠΈμ— λŒ€ν•œ 이해도가 쑰금 더 μ˜¬λΌκ°€λŠ” 것 κ°™μ•„μš” γ…Žγ…Ž

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/

// Declare linked list
function ListNode(val) {
this.val = val;
this.next = null;
}

var reverseList = function (head) {
let prev = null;
let curr = head;
let next;

while (curr !== null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
};

// Create liked list nodes
const node1 = new ListNode(1);
const node2 = new ListNode(2);
const node3 = new ListNode(3);
const node4 = new ListNode(4);
const node5 = new ListNode(5);

node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;

// Print out linked list
let current = node1;
while (current !== null) {
console.log(current.val);
current = current.next;
}

// Reversed linked list
const reversedHead = reverseList(node1);

// Print out reversed linked list
current = reversedHead;
while (current !== null) {
console.log(current.val);
current = current.next;
}

// TC: O(n)
// SC: O(1)
26 changes: 26 additions & 0 deletions valid-parentheses/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
// Create stack array
let stack = [];
// Iterate string to find there is an open bracket
for (char of s) {
// If it is an open bracket, push the close bracket into stack array
if (char === "(") stack.push(")");
else if (char === "{") stack.push("}");
else if (char === "[") stack.push("]");
// If it is not an open bracket, compare last element of stack array
else if (char !== stack.pop()) return false;
Comment on lines +10 to +15
Copy link
Member

Choose a reason for hiding this comment

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

보톡 μ—¬λŠ” κ΄„ν˜Έλ₯Ό μŠ€νƒμ— λ„£κ³ , λ‹«λŠ” κ΄„ν˜Έκ°€ λ‚˜μ˜¬ λ•Œ, μ—¬λŠ” κ΄„ν˜Έλ₯Ό μŠ€νƒμ—μ„œ κΊΌλ‚΄μ„œ λ‘˜μ΄ 쌍이 λ§žλŠ”μ§€λ₯Ό ν™•μΈν•˜λŠ”λ°μš”. μ΄λ ‡κ²Œ μ²˜μŒλΆ€ν„° 쌍이 λ§žλŠ” λ‹«λŠ” κ΄„ν˜Έλ₯Ό λ„£μ–΄λ‘λŠ” 것도 μ‹ μ„ ν•œ μ ‘κ·Όμ΄λ„€μš”! μ•„μ£Ό μ’‹μŠ΅λ‹ˆλ‹€. πŸ‘

Copy link
Contributor Author

Choose a reason for hiding this comment

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

κ°μ‚¬ν•©λ‹ˆλ‹€. πŸ™

}
// Check stack array still has any element after iteration
return stack.length === 0;
};

// TC: O(n)
// MC: O(n)

console.log(isValid("()")); //true
console.log(isValid("()[]{}")); //true
console.log(isValid("([)]")); //false