Skip to content

Commit 9d4c488

Browse files
committed
Added js tasks
1 parent c2446d8 commit 9d4c488

File tree

35 files changed

+2783
-0
lines changed

35 files changed

+2783
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 121\. Best Time to Buy and Sell Stock
5+
6+
Easy
7+
8+
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.
9+
10+
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.
11+
12+
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
13+
14+
**Example 1:**
15+
16+
**Input:** prices = [7,1,5,3,6,4]
17+
18+
**Output:** 5
19+
20+
**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
21+
22+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
23+
24+
**Example 2:**
25+
26+
**Input:** prices = [7,6,4,3,1]
27+
28+
**Output:** 0
29+
30+
**Explanation:** In this case, no transactions are done and the max profit = 0.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
35+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
36+
37+
## Solution
38+
39+
```javascript
40+
/**
41+
* @param {number[]} prices
42+
* @return {number}
43+
*/
44+
var maxProfit = function(prices) {
45+
let maxProfit = 0
46+
let min = prices[0]
47+
48+
for (let i = 1; i < prices.length; i++) {
49+
if (prices[i] > min) {
50+
maxProfit = Math.max(maxProfit, prices[i] - min)
51+
} else {
52+
min = prices[i]
53+
}
54+
}
55+
56+
return maxProfit
57+
};
58+
59+
export { maxProfit }
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 124\. Binary Tree Maximum Path Sum
5+
6+
Hard
7+
8+
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.
9+
10+
The **path sum** of a path is the sum of the node's values in the path.
11+
12+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
17+
18+
**Input:** root = [1,2,3]
19+
20+
**Output:** 6
21+
22+
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
27+
28+
**Input:** root = [-10,9,20,null,null,15,7]
29+
30+
**Output:** 42
31+
32+
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
33+
34+
**Constraints:**
35+
36+
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.
37+
* `-1000 <= Node.val <= 1000`
38+
39+
## Solution
40+
41+
```javascript
42+
/**
43+
* Definition for a binary tree node.
44+
* function TreeNode(val, left, right) {
45+
* this.val = (val===undefined ? 0 : val)
46+
* this.left = (left===undefined ? null : left)
47+
* this.right = (right===undefined ? null : right)
48+
* }
49+
*/
50+
/**
51+
* @param {TreeNode} root
52+
* @return {number}
53+
*/
54+
var maxPathSum = function(root) {
55+
let max = Number.MIN_SAFE_INTEGER
56+
57+
const helper = (node) => {
58+
if (node === null) {
59+
return 0
60+
}
61+
62+
// Calculate max sum on the left and right subtrees, avoiding negatives
63+
const left = Math.max(0, helper(node.left))
64+
const right = Math.max(0, helper(node.right))
65+
66+
// Current path sum including the node
67+
const current = node.val + left + right
68+
69+
// Update the global max if the current path sum is greater
70+
max = Math.max(max, current)
71+
72+
// Return the max sum of the path passing through the current node
73+
return node.val + Math.max(left, right)
74+
}
75+
76+
helper(root)
77+
return max
78+
};
79+
80+
export { maxPathSum }
81+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 128\. Longest Consecutive Sequence
5+
6+
Medium
7+
8+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._
9+
10+
You must write an algorithm that runs in `O(n)` time.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [100,4,200,1,3,2]
15+
16+
**Output:** 4
17+
18+
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [0,3,7,2,5,8,4,6,0,1]
23+
24+
**Output:** 9
25+
26+
**Constraints:**
27+
28+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
29+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
30+
31+
## Solution
32+
33+
```javascript
34+
/**
35+
* @param {number[]} nums
36+
* @return {number}
37+
*/
38+
var longestConsecutive = function(nums) {
39+
if (nums.length === 0) {
40+
return 0
41+
}
42+
nums.sort((a, b) => a - b)
43+
let max = Number.MIN_SAFE_INTEGER
44+
let thsMax = 1
45+
for (let i = 0; i < nums.length - 1; i++) {
46+
if (nums[i + 1] === nums[i] + 1) {
47+
thsMax += 1
48+
continue
49+
}
50+
if (nums[i + 1] === nums[i]) {
51+
continue
52+
}
53+
max = Math.max(max, thsMax)
54+
thsMax = 1 // NOSONAR
55+
}
56+
return Math.max(max, thsMax)
57+
};
58+
59+
export { longestConsecutive }
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 131\. Palindrome Partitioning
5+
6+
Medium
7+
8+
Given a string `s`, partition `s` such that every substring of the partition is a **palindrome**. Return all possible palindrome partitioning of `s`.
9+
10+
A **palindrome** string is a string that reads the same backward as forward.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "aab"
15+
16+
**Output:** [["a","a","b"],["aa","b"]]
17+
18+
**Example 2:**
19+
20+
**Input:** s = "a"
21+
22+
**Output:** [["a"]]
23+
24+
**Constraints:**
25+
26+
* `1 <= s.length <= 16`
27+
* `s` contains only lowercase English letters.
28+
29+
## Solution
30+
31+
```javascript
32+
/**
33+
* @param {string} s
34+
* @return {string[][]}
35+
*/
36+
var partition = function(s) {
37+
const res = []
38+
backtracking(res, [], s, 0)
39+
return res
40+
};
41+
42+
const backtracking = (res, currArr, s, start) => {
43+
if (start === s.length) {
44+
res.push([...currArr]) // Add a copy of the current array to the result
45+
return
46+
}
47+
48+
for (let end = start; end < s.length; end++) {
49+
if (!isPalindrome(s, start, end)) {
50+
continue
51+
}
52+
currArr.push(s.substring(start, end + 1)) // Add the current substring
53+
backtracking(res, currArr, s, end + 1) // Recurse to the next part
54+
currArr.pop() // Remove the last element to backtrack
55+
}
56+
};
57+
58+
const isPalindrome = (s, start, end) => {
59+
while (start < end && s[start] === s[end]) {
60+
start++
61+
end--
62+
}
63+
return start >= end
64+
};
65+
66+
export { partition }
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/fork)
3+
4+
## 136\. Single Number
5+
6+
Easy
7+
8+
Given a **non-empty** array of integers `nums`, every element appears _twice_ except for one. Find that single one.
9+
10+
You must implement a solution with a linear runtime complexity and use only constant extra space.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [2,2,1]
15+
16+
**Output:** 1
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [4,1,2,1,2]
21+
22+
**Output:** 4
23+
24+
**Example 3:**
25+
26+
**Input:** nums = [1]
27+
28+
**Output:** 1
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
33+
* <code>-3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup></code>
34+
* Each element in the array appears twice except for one element which appears only once.
35+
36+
## Solution
37+
38+
```javascript
39+
/**
40+
* @param {number[]} nums
41+
* @return {number}
42+
*/
43+
var singleNumber = function(nums) {
44+
let res = 0
45+
for (const num of nums) {
46+
res ^= num
47+
}
48+
return res
49+
};
50+
51+
export { singleNumber }
52+
```

0 commit comments

Comments
 (0)