Skip to content

Commit 537d0ab

Browse files
committed
Added js tasks
1 parent 64b62be commit 537d0ab

File tree

20 files changed

+751
-0
lines changed

20 files changed

+751
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
230\. Kth Smallest Element in a BST
2+
3+
Medium
4+
5+
Given the `root` of a binary search tree, and an integer `k`, return _the_ <code>k<sup>th</sup></code> _smallest value (**1-indexed**) of all the values of the nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg)
10+
11+
**Input:** root = [3,1,4,null,2], k = 1
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg)
18+
19+
**Input:** root = [5,3,6,2,4,null,null,1], k = 3
20+
21+
**Output:** 3
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is `n`.
26+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
27+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
28+
29+
**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
2+
// #Data_Structure_II_Day_17_Tree #Level_2_Day_9_Binary_Search_Tree #Big_O_Time_O(n)_Space_O(n)
3+
// #2024_12_21_Time_0_ms_(100.00%)_Space_55.4_MB_(48.16%)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val, left, right) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
*/
13+
/**
14+
* @param {TreeNode} root
15+
* @param {number} k
16+
* @return {number}
17+
*/
18+
var kthSmallest = function(root, k) {
19+
let count = 0
20+
let result = null
21+
22+
const calculate = (node) => {
23+
if (!node) return
24+
if (node.left) calculate(node.left)
25+
26+
count++
27+
if (count === k) {
28+
result = node.val
29+
return
30+
}
31+
32+
if (node.right) calculate(node.right)
33+
}
34+
35+
calculate(root)
36+
return result
37+
};
38+
39+
export { kthSmallest }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
234\. Palindrome Linked List
2+
3+
Easy
4+
5+
Given the `head` of a singly linked list, return `true` _if it is a palindrome or_ `false` _otherwise_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg)
10+
11+
**Input:** head = [1,2,2,1]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg)
18+
19+
**Input:** head = [1,2]
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
26+
* `0 <= Node.val <= 9`
27+
28+
**Follow up:** Could you do it in `O(n)` time and `O(1)` space?
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// #Easy #Top_100_Liked_Questions #Two_Pointers #Stack #Linked_List #Recursion
2+
// #Level_2_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
3+
// #2024_12_21_Time_3_ms_(93.71%)_Space_69.8_MB_(76.50%)
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* function ListNode(val, next) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.next = (next===undefined ? null : next)
10+
* }
11+
*/
12+
/**
13+
* @param {ListNode} head
14+
* @return {boolean}
15+
*/
16+
var isPalindrome = function(head) {
17+
let len = 0
18+
let right = head
19+
20+
// Calculate the length of the linked list
21+
while (right !== null) {
22+
right = right.next
23+
len++
24+
}
25+
26+
// Move to the start of the second half of the list
27+
len = Math.floor(len / 2)
28+
right = head
29+
for (let i = 0; i < len; i++) {
30+
right = right.next
31+
}
32+
33+
// Reverse the right half of the list
34+
let prev = null
35+
while (right !== null) {
36+
let next = right.next
37+
right.next = prev
38+
prev = right
39+
right = next
40+
}
41+
42+
// Compare the left half and the reversed right half
43+
for (let i = 0; i < len; i++) {
44+
if (prev !== null && head.val === prev.val) {
45+
head = head.next
46+
prev = prev.next
47+
} else {
48+
return false
49+
}
50+
}
51+
return true
52+
};
53+
54+
export { isPalindrome }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
236\. Lowest Common Ancestor of a Binary Tree
2+
3+
Medium
4+
5+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
6+
7+
According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).”
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png)
12+
13+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
14+
15+
**Output:** 3
16+
17+
**Explanation:** The LCA of nodes 5 and 1 is 3.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png)
22+
23+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
24+
25+
**Output:** 5
26+
27+
**Explanation:** The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
28+
29+
**Example 3:**
30+
31+
**Input:** root = [1,2], p = 1, q = 2
32+
33+
**Output:** 1
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.
38+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
39+
* All `Node.val` are **unique**.
40+
* `p != q`
41+
* `p` and `q` will exist in the tree.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree
2+
// #Data_Structure_II_Day_18_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
3+
// #2024_12_21_Time_53_ms_(98.59%)_Space_58.7_MB_(88.33%)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val) {
8+
* this.val = val;
9+
* this.left = this.right = null;
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @param {TreeNode} p
15+
* @param {TreeNode} q
16+
* @return {TreeNode}
17+
*/
18+
var lowestCommonAncestor = function(root, p, q) {
19+
if(!root) {
20+
return null
21+
}
22+
if(p.val === root.val || q.val === root.val) {
23+
return root
24+
}
25+
const leftNode = lowestCommonAncestor(root.left, p, q)
26+
const rightNode = lowestCommonAncestor(root.right, p, q)
27+
if(leftNode && rightNode) {
28+
return root
29+
}
30+
return leftNode || rightNode
31+
};
32+
33+
export { lowestCommonAncestor }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
238\. Product of Array Except Self
2+
3+
Medium
4+
5+
Given an integer array `nums`, return _an array_ `answer` _such that_ `answer[i]` _is equal to the product of all the elements of_ `nums` _except_ `nums[i]`.
6+
7+
The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
8+
9+
You must write an algorithm that runs in `O(n)` time and without using the division operation.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3,4]
14+
15+
**Output:** [24,12,8,6]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [-1,1,0,-3,3]
20+
21+
**Output:** [0,0,9,0,0]
22+
23+
**Constraints:**
24+
25+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
26+
* `-30 <= nums[i] <= 30`
27+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
28+
29+
**Follow up:** Can you solve the problem in `O(1) `extra space complexity? (The output array **does not** count as extra space for space complexity analysis.)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Prefix_Sum #Data_Structure_II_Day_5_Array #Udemy_Arrays
2+
// #Big_O_Time_O(n^2)_Space_O(n) #2024_12_21_Time_3_ms_(93.60%)_Space_64.9_MB_(45.67%)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number[]}
7+
*/
8+
var productExceptSelf = function(nums) {
9+
const res = new Array(nums.length).fill(1)
10+
11+
// Compute prefix product
12+
let prefixProduct = 1
13+
for (let i = 0; i < nums.length; i++) {
14+
res[i] = prefixProduct
15+
prefixProduct *= nums[i]
16+
}
17+
18+
// Compute suffix product and multiply with prefix product
19+
let suffixProduct = 1
20+
for (let i = nums.length - 1; i >= 0; i--) {
21+
res[i] *= suffixProduct
22+
suffixProduct *= nums[i]
23+
}
24+
25+
return res
26+
};
27+
28+
export { productExceptSelf }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
239\. Sliding Window Maximum
2+
3+
Hard
4+
5+
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.
6+
7+
Return _the max sliding window_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3
12+
13+
**Output:** [3,3,5,5,6,7]
14+
15+
**Explanation:**
16+
17+
Window position Max
18+
19+
--------------- -----
20+
21+
[1 3 -1] -3 5 3 6 7 **3**
22+
23+
1 [3 -1 -3] 5 3 6 7 **3**
24+
25+
1 3 [-1 -3 5] 3 6 7 **5**
26+
27+
1 3 -1 [-3 5 3] 6 7 **5**
28+
29+
1 3 -1 -3 [5 3 6] 7 **6**
30+
31+
1 3 -1 -3 5 [3 6 7] **7**
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [1], k = 1
36+
37+
**Output:** [1]
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
43+
* `1 <= k <= nums.length`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
2+
// #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
3+
// #2024_12_21_Time_28_ms_(98.27%)_Space_83.4_MB_(17.86%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @param {number} k
8+
* @return {number[]}
9+
*/
10+
var maxSlidingWindow = function (nums, k) {
11+
// initialize an empty deqeue
12+
let dq = []
13+
// initailize two pointers with 0
14+
let i = 0, j = 0
15+
16+
// intiailize an empty result variable
17+
let res = []
18+
19+
// loop till j reaches to the end of loop
20+
while (j < nums.length) {
21+
22+
// if dq is empty, push j
23+
if (dq.length === 0) {
24+
dq.push(j)
25+
} else {
26+
// if dq is non empty & back element in deqeue is less or equals to nums[j],
27+
// pop back element from deqeue
28+
while (dq.length !== 0 && nums[dq[dq.length - 1]] <= nums[j]) {
29+
dq.pop()
30+
}
31+
32+
// push j in deqeue
33+
dq.push(j)
34+
}
35+
36+
// if current window size is equals to k,
37+
// push front deqeue element in res
38+
if ((j - i + 1) === k) {
39+
res.push(nums[dq[0]])
40+
41+
// if front deqeue element is equals to i,
42+
// remove front deqeue element
43+
if (dq[0] === i) {
44+
dq.shift()
45+
}
46+
47+
// increment i by 1
48+
i++
49+
}
50+
51+
// increment j by 1
52+
j++
53+
}
54+
55+
// return result
56+
return res
57+
};
58+
59+
export { maxSlidingWindow }

0 commit comments

Comments
 (0)