Skip to content

Commit 0e1f588

Browse files
authored
Added tasks 148-160
1 parent c1f7e88 commit 0e1f588

File tree

15 files changed

+469
-0
lines changed

15 files changed

+469
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
148\. Sort List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
10+
11+
**Input:** head = [4,2,1,3]
12+
13+
**Output:** [1,2,3,4]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
18+
19+
**Input:** head = [-1,5,3,4,0]
20+
21+
**Output:** [-1,0,3,4,5]
22+
23+
**Example 3:**
24+
25+
**Input:** head = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
32+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
33+
34+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List
2+
// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Big_O_Time_O(log(N))_Space_O(log(N))
3+
// #2023_10_08_Time_141_ms_(97.14%)_Space_71.9_MB_(47.35%)
4+
5+
import { ListNode } from '../../com_github_leetcode/listnode'
6+
7+
/*
8+
* Definition for singly-linked list.
9+
* class ListNode {
10+
* val: number
11+
* next: ListNode | null
12+
* constructor(val?: number, next?: ListNode | null) {
13+
* this.val = (val===undefined ? 0 : val)
14+
* this.next = (next===undefined ? null : next)
15+
* }
16+
* }
17+
*/
18+
19+
function sortList(head: ListNode | null): ListNode | null {
20+
if (!head) return null
21+
let array = []
22+
while (head) {
23+
array.push([head, head.val])
24+
head = head.next
25+
}
26+
array.sort((a, b) => {
27+
return a[1] - b[1]
28+
})
29+
for (let iter = 0; iter < array.length; iter++) {
30+
if (iter + 1 >= array.length) array[iter][0].next = null
31+
else array[iter][0].next = array[iter + 1][0]
32+
}
33+
return array[0][0]
34+
}
35+
36+
export { sortList }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
152\. Maximum Product Subarray
2+
3+
Medium
4+
5+
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
6+
7+
It is **guaranteed** that the answer will fit in a **32-bit** integer.
8+
9+
A **subarray** is a contiguous subsequence of the array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,3,-2,4]
14+
15+
**Output:** 6
16+
17+
**Explanation:** [2,3] has the largest product 6.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [-2,0,-1]
22+
23+
**Output:** 0
24+
25+
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
30+
* `-10 <= nums[i] <= 10`
31+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2+
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
3+
// #Big_O_Time_O(N)_Space_O(1) #2023_10_08_Time_53_ms_(87.50%)_Space_44.6_MB_(63.86%)
4+
5+
function maxProduct(nums: number[]): number {
6+
let cMin = 1
7+
let cMax = 1
8+
let max = nums[0]
9+
for (const num of nums) {
10+
let tempMin = cMin
11+
cMin = Math.min(cMax * num, cMin * num, num)
12+
cMax = Math.max(cMax * num, tempMin * num, num)
13+
max = Math.max(max, cMax)
14+
}
15+
return max
16+
}
17+
18+
export { maxProduct }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
153\. Find Minimum in Rotated Sorted Array
2+
3+
Medium
4+
5+
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
6+
7+
* `[4,5,6,7,0,1,2]` if it was rotated `4` times.
8+
* `[0,1,2,4,5,6,7]` if it was rotated `7` times.
9+
10+
Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.
11+
12+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
13+
14+
You must write an algorithm that runs in `O(log n) time.`
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [3,4,5,1,2]
19+
20+
**Output:** 1
21+
22+
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [4,5,6,7,0,1,2]
27+
28+
**Output:** 0
29+
30+
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [11,13,15,17]
35+
36+
**Output:** 11
37+
38+
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.
39+
40+
**Constraints:**
41+
42+
* `n == nums.length`
43+
* `1 <= n <= 5000`
44+
* `-5000 <= nums[i] <= 5000`
45+
* All the integers of `nums` are **unique**.
46+
* `nums` is sorted and rotated between `1` and `n` times.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search
2+
// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N)
3+
// #2023_10_08_Time_42_ms_(98.87%)_Space_42.9_MB_(84.75%)
4+
5+
function findMin(nums: number[]): number {
6+
return Math.min(...nums)
7+
}
8+
9+
export { findMin }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
155\. Min Stack
2+
3+
Easy
4+
5+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
6+
7+
Implement the `MinStack` class:
8+
9+
* `MinStack()` initializes the stack object.
10+
* `void push(int val)` pushes the element `val` onto the stack.
11+
* `void pop()` removes the element on the top of the stack.
12+
* `int top()` gets the top element of the stack.
13+
* `int getMin()` retrieves the minimum element in the stack.
14+
15+
**Example 1:**
16+
17+
**Input**
18+
19+
["MinStack","push","push","push","getMin","pop","top","getMin"]
20+
[[],[-2],[0],[-3],[],[],[],[]]
21+
22+
**Output:** [null,null,null,null,-3,null,0,-2]
23+
24+
**Explanation:**
25+
26+
MinStack minStack = new MinStack();
27+
minStack.push(-2);
28+
minStack.push(0);
29+
minStack.push(-3);
30+
minStack.getMin(); // return -3
31+
minStack.pop();
32+
minStack.top(); // return 0
33+
minStack.getMin(); // return -2
34+
35+
**Constraints:**
36+
37+
* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code>
38+
* Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks.
39+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `push`, `pop`, `top`, and `getMin`.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design
2+
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
3+
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2023_10_08_Time_84_ms_(92.72%)_Space_51.8_MB_(30.46%)
4+
5+
class MinStack {
6+
stack: number[]
7+
minStack: number[]
8+
9+
constructor() {
10+
this.stack = []
11+
this.minStack = []
12+
}
13+
14+
push(val: number): void {
15+
this.stack.push(val)
16+
if (this.minStack.length === 0 || this.minStack[this.minStack.length - 1] >= val) {
17+
this.minStack.push(val)
18+
}
19+
}
20+
21+
pop(): void {
22+
const popValue = this.stack.pop()
23+
if (popValue === this.minStack[this.minStack.length - 1]) {
24+
this.minStack.pop()
25+
}
26+
}
27+
28+
top(): number {
29+
return this.stack[this.stack.length - 1]
30+
}
31+
32+
getMin(): number {
33+
return this.minStack[this.minStack.length - 1]
34+
}
35+
}
36+
37+
/*
38+
* Your MinStack object will be instantiated and called as such:
39+
* var obj = new MinStack()
40+
* obj.push(val)
41+
* obj.pop()
42+
* var param_3 = obj.top()
43+
* var param_4 = obj.getMin()
44+
*/
45+
46+
export { MinStack }
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
160\. Intersection of Two Linked Lists
2+
3+
Easy
4+
5+
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`.
6+
7+
For example, the following two linked lists begin to intersect at node `c1`:
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/05/160_statement.png)
10+
11+
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
12+
13+
**Note** that the linked lists must **retain their original structure** after the function returns.
14+
15+
**Custom Judge:**
16+
17+
The inputs to the **judge** are given as follows (your program is **not** given these inputs):
18+
19+
* `intersectVal` - The value of the node where the intersection occurs. This is `0` if there is no intersected node.
20+
* `listA` - The first linked list.
21+
* `listB` - The second linked list.
22+
* `skipA` - The number of nodes to skip ahead in `listA` (starting from the head) to get to the intersected node.
23+
* `skipB` - The number of nodes to skip ahead in `listB` (starting from the head) to get to the intersected node.
24+
25+
The judge will then create the linked structure based on these inputs and pass the two heads, `headA` and `headB` to your program. If you correctly return the intersected node, then your solution will be **accepted**.
26+
27+
**Example 1:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png)
30+
31+
**Input:** intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
32+
33+
**Output:** Intersected at '8'
34+
35+
**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png)
40+
41+
**Input:** intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
42+
43+
**Output:** Intersected at '2'
44+
45+
**Explanation:** The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
46+
47+
**Example 3:**
48+
49+
![](https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png)
50+
51+
**Input:** intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
52+
53+
**Output:** No intersection
54+
55+
**Explanation:** From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. Explanation: The two lists do not intersect, so return null.
56+
57+
**Constraints:**
58+
59+
* The number of nodes of `listA` is in the `m`.
60+
* The number of nodes of `listB` is in the `n`.
61+
* <code>0 <= m, n <= 3 * 10<sup>4</sup></code>
62+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
63+
* `0 <= skipA <= m`
64+
* `0 <= skipB <= n`
65+
* `intersectVal` is `0` if `listA` and `listB` do not intersect.
66+
* `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect.
67+
68+
**Follow up:** Could you write a solution that runs in `O(n)` time and use only `O(1)` memory?

0 commit comments

Comments
 (0)