Skip to content

Commit 835caaf

Browse files
committed
Added tasks 121-146
1 parent c469096 commit 835caaf

File tree

11 files changed

+833
-0
lines changed

11 files changed

+833
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494

9595
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9696
|-|-|-|-|-|-
97+
| 0136 |[Single Number](src/main/js/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
9798

9899
### Algorithm II
99100

@@ -185,6 +186,7 @@
185186

186187
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
187188
|-|-|-|-|-|-
189+
| 0139 |[Word Break](src/main/js/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
188190

189191
#### Day 16 Dynamic Programming
190192

@@ -425,6 +427,7 @@
425427

426428
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
427429
|-|-|-|-|-|-
430+
| 0121 |[Best Time to Buy and Sell Stock](src/main/js/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 97.34
428431

429432
#### Day 8
430433

@@ -435,6 +438,7 @@
435438

436439
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
437440
|-|-|-|-|-|-
441+
| 0139 |[Word Break](src/main/js/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
438442
| 0042 |[Trapping Rain Water](src/main/js/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
439443

440444
#### Day 10
@@ -638,6 +642,7 @@
638642

639643
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
640644
|-|-|-|-|-|-
645+
| 0138 |[Copy List with Random Pointer](src/main/js/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 43 | 97.07
641646

642647
#### Day 15
643648

@@ -816,11 +821,13 @@
816821

817822
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
818823
|-|-|-|-|-|-
824+
| 0142 |[Linked List Cycle II](src/main/js/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 58 | 92.76
819825

820826
#### Day 5 Greedy
821827

822828
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
823829
|-|-|-|-|-|-
830+
| 0121 |[Best Time to Buy and Sell Stock](src/main/js/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 97.34
824831

825832
#### Day 6 Tree
826833

@@ -994,6 +1001,7 @@
9941001

9951002
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9961003
|-|-|-|-|-|-
1004+
| 0136 |[Single Number](src/main/js/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
9971005
| 0007 |[Reverse Integer](src/main/js/g0001_0100/s0007_reverse_integer)| Medium | Top_Interview_Questions, Math | 67 | 85.38
9981006
| 0009 |[Palindrome Number](src/main/js/g0001_0100/s0009_palindrome_number)| Easy | Math | 4 | 97.14
9991007

@@ -1016,6 +1024,7 @@
10161024

10171025
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10181026
|-|-|-|-|-|-
1027+
| 0121 |[Best Time to Buy and Sell Stock](src/main/js/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 97.34
10191028
| 0001 |[Two Sum](src/main/js/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.15
10201029
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10211030
| 0075 |[Sort Colors](src/main/js/g0001_0100/s0075_sort_colors)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
@@ -1054,8 +1063,12 @@
10541063
|-|-|-|-|-|-
10551064
| 0114 |[Flatten Binary Tree to Linked List](src/main/js/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
10561065
| 0024 |[Swap Nodes in Pairs](src/main/js/g0001_0100/s0024_swap_nodes_in_pairs)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 1 | 100.00
1066+
| 0142 |[Linked List Cycle II](src/main/js/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 58 | 92.76
1067+
| 0141 |[Linked List Cycle](src/main/js/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 49 | 99.06
10571068
| 0021 |[Merge Two Sorted Lists](src/main/js/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
1069+
| 0138 |[Copy List with Random Pointer](src/main/js/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 43 | 97.07
10581070
| 0025 |[Reverse Nodes in k-Group](src/main/js/g0001_0100/s0025_reverse_nodes_in_k_group)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
1071+
| 0146 |[LRU Cache](src/main/js/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 91 | 96.82
10591072

10601073
#### Udemy Tree Stack Queue
10611074

@@ -1064,6 +1077,7 @@
10641077
| 0094 |[Binary Tree Inorder Traversal](src/main/js/g0001_0100/s0094_binary_tree_inorder_traversal)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
10651078
| 0102 |[Binary Tree Level Order Traversal](src/main/js/g0101_0200/s0102_binary_tree_level_order_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 87.97
10661079
| 0104 |[Maximum Depth of Binary Tree](src/main/js/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
1080+
| 0124 |[Binary Tree Maximum Path Sum](src/main/js/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 98.34
10671081
| 0098 |[Validate Binary Search Tree](src/main/js/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
10681082

10691083
#### Udemy Trie and Heap
@@ -1080,6 +1094,7 @@
10801094

10811095
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10821096
|-|-|-|-|-|-
1097+
| 0139 |[Word Break](src/main/js/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
10831098
| 0070 |[Climbing Stairs](src/main/js/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
10841099
| 0064 |[Minimum Path Sum](src/main/js/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 3 | 83.07
10851100
| 0072 |[Edit Distance](src/main/js/g0001_0100/s0072_edit_distance)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 5 | 99.01
@@ -1123,6 +1138,7 @@
11231138

11241139
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11251140
|-|-|-|-|-|-
1141+
| 0121 |[Best Time to Buy and Sell Stock](src/main/js/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 97.34
11261142

11271143
#### Day 4 Array
11281144

@@ -1144,6 +1160,7 @@
11441160

11451161
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11461162
|-|-|-|-|-|-
1163+
| 0141 |[Linked List Cycle](src/main/js/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 49 | 99.06
11471164
| 0021 |[Merge Two Sorted Lists](src/main/js/g0001_0100/s0021_merge_two_sorted_lists)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
11481165

11491166
#### Day 8 Linked List
@@ -1193,6 +1210,7 @@
11931210

11941211
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11951212
|-|-|-|-|-|-
1213+
| 0136 |[Single Number](src/main/js/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
11961214
| 0015 |[3Sum](src/main/js/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 31 | 91.92
11971215

11981216
#### Day 2 Array
@@ -1245,6 +1263,7 @@
12451263
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12461264
|-|-|-|-|-|-
12471265
| 0002 |[Add Two Numbers](src/main/js/g0001_0100/s0002_add_two_numbers)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion, Big_O_Time_O(max(N,M))_Space_O(max(N,M)), AI_can_be_used_to_solve_the_task | 3 | 81.61
1266+
| 0142 |[Linked List Cycle II](src/main/js/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 58 | 92.76
12481267

12491268
#### Day 11 Linked List
12501269

@@ -1308,6 +1327,16 @@
13081327

13091328
| # | Title | Difficulty | Tag | Time, ms | Time, %
13101329
|------|----------------|-------------|-------------|----------|--------
1330+
| 0146 |[LRU Cache](src/main/js/g0101_0200/s0146_lru_cache)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 91 | 96.82
1331+
| 0142 |[Linked List Cycle II](src/main/js/g0101_0200/s0142_linked_list_cycle_ii)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 58 | 92.76
1332+
| 0141 |[Linked List Cycle](src/main/js/g0101_0200/s0141_linked_list_cycle)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_I_Day_7_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 49 | 99.06
1333+
| 0139 |[Word Break](src/main/js/g0101_0200/s0139_word_break)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_9, Udemy_Dynamic_Programming, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
1334+
| 0138 |[Copy List with Random Pointer](src/main/js/g0101_0200/s0138_copy_list_with_random_pointer)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Programming_Skills_II_Day_14, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 43 | 97.07
1335+
| 0136 |[Single Number](src/main/js/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1336+
| 0131 |[Palindrome Partitioning](src/main/js/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 21 | 89.90
1337+
| 0128 |[Longest Consecutive Sequence](src/main/js/g0101_0200/s0128_longest_consecutive_sequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 31 | 93.87
1338+
| 0124 |[Binary Tree Maximum Path Sum](src/main/js/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 1 | 98.34
1339+
| 0121 |[Best Time to Buy and Sell Stock](src/main/js/g0101_0200/s0121_best_time_to_buy_and_sell_stock)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Big_O_Time_O(N)_Space_O(1) | 1 | 97.34
13111340
| 0114 |[Flatten Binary Tree to Linked List](src/main/js/g0101_0200/s0114_flatten_binary_tree_to_linked_list)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
13121341
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/js/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 1 | 98.60
13131342
| 0104 |[Maximum Depth of Binary Tree](src/main/js/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(H) | 0 | 100.00
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript/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+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript/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+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-JavaScript/LeetCode-in-JavaScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-JavaScript/LeetCode-in-JavaScript/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+
43+
nums.sort((a, b) => a - b) // Sort the array in ascending order
44+
let max = Number.MIN_SAFE_INTEGER
45+
let thsMax = 1
46+
47+
for (let i = 0; i < nums.length - 1; i++) {
48+
if (nums[i + 1] === nums[i] + 1) {
49+
thsMax += 1
50+
continue
51+
}
52+
if (nums[i + 1] === nums[i]) {
53+
continue
54+
}
55+
// Start of a new sequence
56+
max = Math.max(max, thsMax)
57+
thsMax = 1
58+
}
59+
60+
return Math.max(max, thsMax)
61+
};
62+
63+
export { longestConsecutive }
64+
```

0 commit comments

Comments
 (0)