Skip to content

Commit 44da4a8

Browse files
authored
Added tasks 121-136
1 parent a92c5af commit 44da4a8

File tree

15 files changed

+332
-0
lines changed

15 files changed

+332
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 `ith` 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. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
18+
19+
**Example 2:**
20+
21+
**Input:** prices = [7,6,4,3,1]
22+
23+
**Output:** 0
24+
25+
**Explanation:** In this case, no transactions are done and the max profit = 0.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
30+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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) #2023_10_05_Time_56_ms_(99.56%)_Space_52.3_MB_(13.22%)
4+
5+
function maxProfit(prices: number[]): number {
6+
let buyPrice = prices[0]
7+
let maxProfit = 0
8+
for (let i = 1; i < prices.length; i++) {
9+
if (prices[i] < buyPrice) {
10+
buyPrice = prices[i]
11+
}
12+
if (prices[i] - buyPrice > maxProfit) {
13+
maxProfit = prices[i] - buyPrice
14+
}
15+
}
16+
return maxProfit
17+
}
18+
19+
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// #2023_10_05_Time_61_ms_(96.73%)_Space_51.2_MB_(97.45%)
4+
5+
import { TreeNode } from '../../com_github_leetcode/treenode'
6+
7+
/*
8+
* Definition for a binary tree node.
9+
* class TreeNode {
10+
* val: number
11+
* left: TreeNode | null
12+
* right: TreeNode | null
13+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.left = (left===undefined ? null : left)
16+
* this.right = (right===undefined ? null : right)
17+
* }
18+
* }
19+
*/
20+
function maxPathSum(root: TreeNode | null): number {
21+
let max = { sum: Number.NEGATIVE_INFINITY }
22+
dfs(root, max)
23+
return max.sum
24+
}
25+
26+
function dfs(root: TreeNode | null, max) {
27+
if (root == null) {
28+
return 0
29+
}
30+
let l = Math.max(0, dfs(root.left, max))
31+
let r = Math.max(0, dfs(root.right, max))
32+
max.sum = Math.max(max.sum, l + r + root.val)
33+
return root.val + Math.max(l, r)
34+
}
35+
36+
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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) #2023_10_05_Time_92_ms_(93.69%)_Space_64_MB_(30.13%)
3+
4+
function longestConsecutive(nums: number[]): number {
5+
const set = new Set(nums)
6+
let max = 0
7+
for (const num of nums) {
8+
if (set.has(num + 1)) continue
9+
let counter = 1,
10+
current = num
11+
while (set.has(--current)) counter++
12+
max = Math.max(counter, max)
13+
}
14+
return max
15+
}
16+
17+
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+
// #2023_10_05_Time_203_ms_(99.22%)_Space_81.9_MB_(42.19%)
4+
5+
function partition(s: string): string[][] {
6+
const ans: string[][] = []
7+
const memo = Array.from({ length: s.length }).map(() => new Array(s.length))
8+
const track = (cur, start) => {
9+
if (start === s.length) {
10+
ans.push(cur.slice())
11+
return
12+
}
13+
for (let i = start; i < s.length; i++) {
14+
if (memo[start][i]) continue
15+
16+
if (isPalindrome(s, start, i, memo)) {
17+
cur.push(s.substring(start, i + 1))
18+
track(cur, i + 1)
19+
cur.pop()
20+
}
21+
}
22+
}
23+
track([], 0)
24+
return ans
25+
}
26+
27+
function isPalindrome(str: string, l: number, r: number, memo: boolean[][]) {
28+
while (l < r) {
29+
if (str[l] !== str[r]) {
30+
memo[l][r] = true
31+
return false
32+
}
33+
l++
34+
r--
35+
}
36+
return true
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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) #2023_10_05_Time_56_ms_(85.48%)_Space_45.6_MB_(63.32%)
4+
5+
function singleNumber(nums: number[]): number {
6+
let ans = 0
7+
for (let num of nums) {
8+
ans ^= num
9+
}
10+
return ans
11+
}
12+
13+
export { singleNumber }

0 commit comments

Comments
 (0)