Skip to content

Commit 64b62be

Browse files
committed
Added js tasks
1 parent 1788627 commit 64b62be

File tree

51 files changed

+1927
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1927
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Node {
2+
constructor(val, next, random) {
3+
this.val = val ?? 0
4+
this.next = next ?? null
5+
this.random = random ?? null
6+
}
7+
8+
toString() {
9+
const result = []
10+
let curr = this
11+
while (curr !== null) {
12+
const result2 = []
13+
result2.push(String(curr.val))
14+
if (curr.random === null) {
15+
result2.push('null')
16+
} else {
17+
let randomIndex = 0
18+
let curr2 = this
19+
while (curr2?.next !== null && curr2 !== curr.random) {
20+
randomIndex++
21+
curr2 = curr2.next
22+
}
23+
result2.push(String(randomIndex))
24+
}
25+
result.push(`[${result2.join(',')}]`)
26+
curr = curr.next
27+
}
28+
return `[${result.join(',')}]`
29+
}
30+
};
31+
32+
export { Node }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
121\. Best Time to Buy and Sell Stock
2+
3+
Easy
4+
5+
You are given an array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day.
6+
7+
You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.
8+
9+
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
10+
11+
**Example 1:**
12+
13+
**Input:** prices = [7,1,5,3,6,4]
14+
15+
**Output:** 5
16+
17+
**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
18+
19+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
20+
21+
**Example 2:**
22+
23+
**Input:** prices = [7,6,4,3,1]
24+
25+
**Output:** 0
26+
27+
**Explanation:** In this case, no transactions are done and the max profit = 0.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
32+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2+
// #Data_Structure_I_Day_3_Array #Dynamic_Programming_I_Day_7 #Level_1_Day_5_Greedy #Udemy_Arrays
3+
// #Big_O_Time_O(N)_Space_O(1) #2024_12_15_Time_1_ms_(97.34%)_Space_59.1_MB_(51.64%)
4+
5+
/**
6+
* @param {number[]} prices
7+
* @return {number}
8+
*/
9+
var maxProfit = function(prices) {
10+
let maxProfit = 0
11+
let min = prices[0]
12+
13+
for (let i = 1; i < prices.length; i++) {
14+
if (prices[i] > min) {
15+
maxProfit = Math.max(maxProfit, prices[i] - min)
16+
} else {
17+
min = prices[i]
18+
}
19+
}
20+
21+
return maxProfit
22+
};
23+
24+
export { maxProfit }
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
124\. Binary Tree Maximum Path Sum
2+
3+
Hard
4+
5+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
6+
7+
The **path sum** of a path is the sum of the node's values in the path.
8+
9+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
14+
15+
**Input:** root = [1,2,3]
16+
17+
**Output:** 6
18+
19+
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
24+
25+
**Input:** root = [-10,9,20,null,null,15,7]
26+
27+
**Output:** 42
28+
29+
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.
34+
* `-1000 <= Node.val <= 1000`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Depth_First_Search
2+
// #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(N)
3+
// #2024_12_15_Time_1_ms_(98.34%)_Space_59.8_MB_(12.47%)
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+
* @return {number}
16+
*/
17+
var maxPathSum = function(root) {
18+
let max = Number.MIN_SAFE_INTEGER
19+
20+
const helper = (node) => {
21+
if (node === null) {
22+
return 0
23+
}
24+
25+
// Calculate max sum on the left and right subtrees, avoiding negatives
26+
const left = Math.max(0, helper(node.left))
27+
const right = Math.max(0, helper(node.right))
28+
29+
// Current path sum including the node
30+
const current = node.val + left + right
31+
32+
// Update the global max if the current path sum is greater
33+
max = Math.max(max, current)
34+
35+
// Return the max sum of the path passing through the current node
36+
return node.val + Math.max(left, right)
37+
}
38+
39+
helper(root)
40+
return max
41+
};
42+
43+
export { maxPathSum }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
128\. Longest Consecutive Sequence
2+
3+
Medium
4+
5+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._
6+
7+
You must write an algorithm that runs in `O(n)` time.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [100,4,200,1,3,2]
12+
13+
**Output:** 4
14+
15+
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [0,3,7,2,5,8,4,6,0,1]
20+
21+
**Output:** 9
22+
23+
**Constraints:**
24+
25+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
26+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Union_Find
2+
// #Big_O_Time_O(N_log_N)_Space_O(1) #2024_12_15_Time_31_ms_(93.87%)_Space_59_MB_(96.32%)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var longestConsecutive = function(nums) {
9+
if (nums.length === 0) {
10+
return 0
11+
}
12+
nums.sort((a, b) => a - b)
13+
let max = Number.MIN_SAFE_INTEGER
14+
let thsMax = 1
15+
for (let i = 0; i < nums.length - 1; i++) {
16+
if (nums[i + 1] === nums[i] + 1) {
17+
thsMax += 1
18+
continue
19+
}
20+
if (nums[i + 1] === nums[i]) {
21+
continue
22+
}
23+
max = Math.max(max, thsMax)
24+
thsMax = 1 // NOSONAR
25+
}
26+
return Math.max(max, thsMax)
27+
};
28+
29+
export { longestConsecutive }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
131\. Palindrome Partitioning
2+
3+
Medium
4+
5+
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**. Return all possible palindrome partitioning of `s`.
6+
7+
A **palindrome** string is a string that reads the same backward as forward.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "aab"
12+
13+
**Output:** [["a","a","b"],["aa","b"]]
14+
15+
**Example 2:**
16+
17+
**Input:** s = "a"
18+
19+
**Output:** [["a"]]
20+
21+
**Constraints:**
22+
23+
* `1 <= s.length <= 16`
24+
* `s` contains only lowercase English letters.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
2+
// #Backtracking #Big_O_Time_O(N*2^N)_Space_O(2^N*N)
3+
// #2024_12_15_Time_21_ms_(89.90%)_Space_71.7_MB_(95.05%)
4+
5+
/**
6+
* @param {string} s
7+
* @return {string[][]}
8+
*/
9+
var partition = function(s) {
10+
const res = []
11+
backtracking(res, [], s, 0)
12+
return res
13+
};
14+
15+
const backtracking = (res, currArr, s, start) => {
16+
if (start === s.length) {
17+
res.push([...currArr]) // Add a copy of the current array to the result
18+
return
19+
}
20+
21+
for (let end = start; end < s.length; end++) {
22+
if (!isPalindrome(s, start, end)) {
23+
continue
24+
}
25+
currArr.push(s.substring(start, end + 1)) // Add the current substring
26+
backtracking(res, currArr, s, end + 1) // Recurse to the next part
27+
currArr.pop() // Remove the last element to backtrack
28+
}
29+
};
30+
31+
const isPalindrome = (s, start, end) => {
32+
while (start < end && s[start] === s[end]) {
33+
start++
34+
end--
35+
}
36+
return start >= end
37+
};
38+
39+
export { partition }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
136\. Single Number
2+
3+
Easy
4+
5+
Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one.
6+
7+
You must implement a solution with a linear runtime complexity and use only constant extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,2,1]
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [4,1,2,1,2]
18+
19+
**Output:** 4
20+
21+
**Example 3:**
22+
23+
**Input:** nums = [1]
24+
25+
**Output:** 1
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
30+
* <code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code>
31+
* Each element in the array appears twice except for one element which appears only once.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Bit_Manipulation
2+
// #Data_Structure_II_Day_1_Array #Algorithm_I_Day_14_Bit_Manipulation #Udemy_Integers
3+
// #Big_O_Time_O(N)_Space_O(1) #2024_12_15_Time_0_ms_(100.00%)_Space_52.3_MB_(38.50%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
var singleNumber = function(nums) {
10+
let res = 0
11+
for (const num of nums) {
12+
res ^= num
13+
}
14+
return res
15+
};
16+
17+
export { singleNumber }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
138\. Copy List with Random Pointer
2+
3+
Medium
4+
5+
A linked list of length `n` is given such that each node contains an additional random pointer, which could point to any node in the list, or `null`.
6+
7+
Construct a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) of the list. The deep copy should consist of exactly `n` **brand new** nodes, where each new node has its value set to the value of its corresponding original node. Both the `next` and `random` pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. **None of the pointers in the new list should point to nodes in the original list**.
8+
9+
For example, if there are two nodes `X` and `Y` in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`.
10+
11+
Return _the head of the copied linked list_.
12+
13+
The linked list is represented in the input/output as a list of `n` nodes. Each node is represented as a pair of `[val, random_index]` where:
14+
15+
* `val`: an integer representing `Node.val`
16+
* `random_index`: the index of the node (range from `0` to `n-1`) that the `random` pointer points to, or `null` if it does not point to any node.
17+
18+
Your code will **only** be given the `head` of the original linked list.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2019/12/18/e1.png)
23+
24+
**Input:** head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
25+
26+
**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]]
27+
28+
**Example 2:**
29+
30+
![](https://assets.leetcode.com/uploads/2019/12/18/e2.png)
31+
32+
**Input:** head = [[1,1],[2,1]]
33+
34+
**Output:** [[1,1],[2,1]]
35+
36+
**Example 3:**
37+
38+
**![](https://assets.leetcode.com/uploads/2019/12/18/e3.png)**
39+
40+
**Input:** head = [[3,null],[3,0],[3,null]]
41+
42+
**Output:** [[3,null],[3,0],[3,null]]
43+
44+
**Constraints:**
45+
46+
* `0 <= n <= 1000`
47+
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
48+
* `Node.random` is `null` or is pointing to some node in the linked list.

0 commit comments

Comments
 (0)