Skip to content

Commit 229146b

Browse files
committed
Added tasks 148-1143
1 parent 835caaf commit 229146b

41 files changed

Lines changed: 3115 additions & 6 deletions

File tree

Some content is hidden

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

README.md

Lines changed: 114 additions & 0 deletions
Large diffs are not rendered by default.

src/main/js/g0101_0200/s0128_longest_consecutive_sequence/readme.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ var longestConsecutive = function(nums) {
3939
if (nums.length === 0) {
4040
return 0
4141
}
42-
43-
nums.sort((a, b) => a - b) // Sort the array in ascending order
42+
nums.sort((a, b) => a - b)
4443
let max = Number.MIN_SAFE_INTEGER
4544
let thsMax = 1
46-
4745
for (let i = 0; i < nums.length - 1; i++) {
4846
if (nums[i + 1] === nums[i] + 1) {
4947
thsMax += 1
@@ -52,11 +50,9 @@ var longestConsecutive = function(nums) {
5250
if (nums[i + 1] === nums[i]) {
5351
continue
5452
}
55-
// Start of a new sequence
5653
max = Math.max(max, thsMax)
57-
thsMax = 1
54+
thsMax = 1 // NOSONAR
5855
}
59-
6056
return Math.max(max, thsMax)
6157
};
6258

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
## 148\. Sort List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
13+
14+
**Input:** head = [4,2,1,3]
15+
16+
**Output:** [1,2,3,4]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
21+
22+
**Input:** head = [-1,5,3,4,0]
23+
24+
**Output:** [-1,0,3,4,5]
25+
26+
**Example 3:**
27+
28+
**Input:** head = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
35+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
36+
37+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
38+
39+
## Solution
40+
41+
```javascript
42+
/**
43+
* Definition for singly-linked list.
44+
* function ListNode(val, next) {
45+
* this.val = (val===undefined ? 0 : val)
46+
* this.next = (next===undefined ? null : next)
47+
* }
48+
*/
49+
/**
50+
* @param {ListNode} head
51+
* @return {ListNode}
52+
*/
53+
var sortList = function(head) {
54+
let arr = []
55+
let current = head
56+
while (current) {
57+
arr.push(current)
58+
current = current.next
59+
}
60+
61+
arr = arr.sort((a, b) => {
62+
if (a.val > b.val) {
63+
return 1
64+
} else {
65+
return -1
66+
}
67+
})
68+
69+
const result = arr.reduce((acc, curr) => {
70+
acc.next = curr
71+
return acc.next
72+
}, arr[0])
73+
74+
if (!result) {
75+
return null
76+
}
77+
result.next = null
78+
return arr[0]
79+
};
80+
81+
export { sortList }
82+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
## 152\. Maximum Product Subarray
5+
6+
Medium
7+
8+
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
9+
10+
The test cases are generated so that the answer will fit in a **32-bit** integer.
11+
12+
A **subarray** is a contiguous subsequence of the array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,3,-2,4]
17+
18+
**Output:** 6
19+
20+
**Explanation:** [2,3] has the largest product 6.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [-2,0,-1]
25+
26+
**Output:** 0
27+
28+
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
33+
* `-10 <= nums[i] <= 10`
34+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
35+
36+
## Solution
37+
38+
```javascript
39+
/**
40+
* @param {number[]} nums
41+
* @return {number}
42+
*/
43+
var maxProduct = function(nums) {
44+
let overAllMaxProd = Number.MIN_SAFE_INTEGER
45+
let n = nums.length
46+
let start = 1
47+
let end = 1
48+
49+
for (let i = 0; i < n; i++) {
50+
// Reset `start` and `end` to 1 if they become 0
51+
if (start === 0) {
52+
start = 1
53+
}
54+
if (end === 0) {
55+
end = 1
56+
}
57+
58+
start *= nums[i]
59+
end *= nums[n - i - 1]
60+
61+
overAllMaxProd = Math.max(overAllMaxProd, Math.max(start, end))
62+
}
63+
64+
return overAllMaxProd
65+
};
66+
67+
export { maxProduct }
68+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
## 153\. Find Minimum in Rotated Sorted Array
5+
6+
Medium
7+
8+
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:
9+
10+
* `[4,5,6,7,0,1,2]` if it was rotated `4` times.
11+
* `[0,1,2,4,5,6,7]` if it was rotated `7` times.
12+
13+
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]]`.
14+
15+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
16+
17+
You must write an algorithm that runs in `O(log n) time.`
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [3,4,5,1,2]
22+
23+
**Output:** 1
24+
25+
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [4,5,6,7,0,1,2]
30+
31+
**Output:** 0
32+
33+
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [11,13,15,17]
38+
39+
**Output:** 11
40+
41+
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.
42+
43+
**Constraints:**
44+
45+
* `n == nums.length`
46+
* `1 <= n <= 5000`
47+
* `-5000 <= nums[i] <= 5000`
48+
* All the integers of `nums` are **unique**.
49+
* `nums` is sorted and rotated between `1` and `n` times.
50+
51+
## Solution
52+
53+
```javascript
54+
/**
55+
* @param {number[]} nums
56+
* @return {number}
57+
*/
58+
var findMin = function(nums) {
59+
function findMinUtil(nums, l, r) {
60+
if (l === r) {
61+
return nums[l]
62+
}
63+
let mid = Math.floor((l + r) / 2)
64+
65+
if (mid === l && nums[mid] < nums[r]) {
66+
return nums[l]
67+
}
68+
if (mid - 1 >= 0 && nums[mid - 1] > nums[mid]) {
69+
return nums[mid]
70+
}
71+
if (nums[mid] < nums[l]) {
72+
return findMinUtil(nums, l, mid - 1)
73+
} else if (nums[mid] > nums[r]) {
74+
return findMinUtil(nums, mid + 1, r)
75+
}
76+
return findMinUtil(nums, l, mid - 1)
77+
}
78+
79+
let l = 0
80+
let r = nums.length - 1
81+
return findMinUtil(nums, l, r)
82+
};
83+
84+
export { findMin }
85+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
## 155\. Min Stack
5+
6+
Easy
7+
8+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
9+
10+
Implement the `MinStack` class:
11+
12+
* `MinStack()` initializes the stack object.
13+
* `void push(int val)` pushes the element `val` onto the stack.
14+
* `void pop()` removes the element on the top of the stack.
15+
* `int top()` gets the top element of the stack.
16+
* `int getMin()` retrieves the minimum element in the stack.
17+
18+
**Example 1:**
19+
20+
**Input**
21+
22+
["MinStack","push","push","push","getMin","pop","top","getMin"]
23+
[[],[-2],[0],[-3],[],[],[],[]]
24+
25+
**Output:** [null,null,null,null,-3,null,0,-2]
26+
27+
**Explanation:**
28+
29+
MinStack minStack = new MinStack();
30+
minStack.push(-2);
31+
minStack.push(0);
32+
minStack.push(-3);
33+
minStack.getMin(); // return -3
34+
minStack.pop();
35+
minStack.top(); // return 0
36+
minStack.getMin(); // return -2
37+
38+
**Constraints:**
39+
40+
* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code>
41+
* Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks.
42+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `push`, `pop`, `top`, and `getMin`.
43+
44+
## Solution
45+
46+
```javascript
47+
var MinStack = function () {
48+
this.stack = []
49+
this.minStack = []
50+
};
51+
52+
/**
53+
* @param {number} val
54+
* @return {void}
55+
*/
56+
MinStack.prototype.push = function (val) {
57+
this.stack.push(val)
58+
59+
60+
if (this.minStack.length == 0) {
61+
this.minStack.push(val)
62+
} else {
63+
let min = Math.min(val, this.minStack[this.minStack.length - 1]);
64+
this.minStack.push(min)
65+
}
66+
67+
};
68+
69+
/**
70+
* @return {void}
71+
*/
72+
MinStack.prototype.pop = function () {
73+
this.stack.pop()
74+
this.minStack.pop()
75+
};
76+
77+
/**
78+
* @return {number}
79+
*/
80+
MinStack.prototype.top = function () {
81+
return this.stack[this.stack.length - 1]
82+
};
83+
84+
/**
85+
* @return {number}
86+
*/
87+
MinStack.prototype.getMin = function () {
88+
return this.minStack[this.minStack.length - 1]
89+
};
90+
91+
/**
92+
* Your MinStack object will be instantiated and called as such:
93+
* var obj = new MinStack()
94+
* obj.push(val)
95+
* obj.pop()
96+
* var param_3 = obj.top()
97+
* var param_4 = obj.getMin()
98+
*/
99+
100+
export { MinStack }
101+
```

0 commit comments

Comments
 (0)