Skip to content

Commit 29d7f96

Browse files
committed
Merge branch 'release/1.24.0'
2 parents 5e3b5b5 + 9b7d5a9 commit 29d7f96

File tree

96 files changed

+3677
-2
lines changed

Some content is hidden

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

96 files changed

+3677
-2
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,42 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
4848
| LeetCode # | Title | Difficulty | Topics |
4949
|-----------:|:------|:-----------|:-------|
5050
| 1 | [Two Sum](/problems/two-sum) | Easy | Array, hash table |
51+
| 33 | [Search in Rotated Sorted Array](/problems/search-in-rotated-sorted-array) | Medium | Array, Binary Search |
5152
| 35 | [Search Insert Position](/problems/search-insert-position) | Easy | Array, binary search |
53+
| 49 | [Group Anagrams](/problems/group-anagrams) | Medium | Hash Table, String |
5254
| 53 | [Maximum Subarray](/problems/maximum-subarray) | Easy | Array, Divide and Conquer, Dynamic Programming |
55+
| 55 | [Jump Game](/problems/jump-game) | Medium | Array, Greedy |
56+
| 64 | [Minimum Path Sum](/problems/minimum-path-sum) | Medium | Array, Dynamic Programming |
5357
| 70 | [Climbing Stairs](/problems/climbing-stairs) | Easy | Dynamic Programming |
5458
| 121 | [Best Time to Buy and Sell Stock](/problems/best-time-to-buy-and-sell-stock) | Easy | Array, Dynamic Programming |
59+
| 122 | [Best Time to Buy and Sell Stock II](/problems/best-time-to-buy-and-sell-stock-ii) | Easy | Array, Greedy |
60+
| 124 | [Binary Tree Maximum Path Sum](/problems/binary-tree-maximum-path-sum) | Hard | Tree, Depth-first Search |
5561
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
62+
| 146 | [LRU Cache](/problems/lru-cache) | Medium | Design |
63+
| 155 | [Min Stack](/problems/min-stack) | Easy | Stack, Design |
5664
| 189 | [Rotate Array](/problems/rotate-array) | Easy | Array |
5765
| 198 | [House Robber](/problems/house-robber) | Easy | Dynamic Programming |
66+
| 200 | [Number of Islands](/problems/number-of-islands) | Medium | Depth-first Search, Breadth-first Search, Union Find |
67+
| 201 | [Bitwise AND of Numbers Range](/problems/bitwise-and-of-numbers-range) | Medium | Bit Manipulation |
68+
| 202 | [Happy Number](/problems/happy-number) | Easy | Hash Table, Math |
69+
| 221 | [Maximal Square](/problems/maximal-square) | Medium | Dynamic Programming |
70+
| 238 | [Product of Array Except Self](/problems/product-of-array-except-self) | Medium | Array |
71+
| 283 | [Move Zeroes](/problems/move-zeroes) | Easy | Array, Two Pointers |
5872
| 303 | [Range Sum Query - Immutable](/problems/range-sum-query-immutable) | Easy | Dynamic Programming |
5973
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash table, two pointers, binary search, sort, set |
6074
| 350 | [Intersection of Two Arrays II](/problems/intersection-of-two-arrays-ii) | Easy | Hash Table, two pointers, binary search, sort |
6175
| 392 | [Is Subsequence](/problems/is-subsequence) | Easy | Binary Search, Dynamic Programming, Greedy |
76+
| 525 | [Contiguous Array](/problems/contiguous-array) | Medium | Hash Table |
77+
| 543 | [Diameter of Binary Tree](/problems/diameter-of-binary-tree) | Easy | Tree |
78+
| 560 | [Subarray Sum Equals K](/problems/subarray-sum-equals-k) | Medium | Array, Hash Table |
79+
| 678 | [Valid Parenthesis String](/problems/valid-parenthesis-string) | Medium | String |
6280
| 746 | [Min Cost Climbing Stairs](/problems/min-cost-climbing-stairs) | Easy | Array, Dynamic Programming |
81+
| 844 | [Backspace String Compare](/problems/backspace-string-compare) | Easy | Two Pointers, Stack |
82+
| 876 | [Middle of the Linked List](/problems/middle-of-the-linked-list) | Easy | Linked List |
83+
| 1008 | [Construct Binary Search Tree from Preorder Traversal](/problems/construct-binary-search-tree-from-preorder-traversal) | Medium | Tree |
6384
| 1025 | [Divisor Game](/problems/divisor-game) | Easy | Math, Dynamic Programming |
85+
| 1046 | [Last Stone Weight](/problems/last-stone-weight) | Easy | Heap, Greedy |
86+
| 1143 | [Longest Common Subsequence](/problems/longest-common-subsequence) | Medium | Dynamic Programming |
6487
| 1144 | [Decrease Elements To Make Array Zigzag](/problems/decrease-elements-to-make-array-zigzag) | Medium | Array |
6588
| 1145 | [Binary Tree Coloring Game](/problems/binary-tree-coloring-game/) | Medium | Tree, depth-first search |
6689
| 1150 | [Check If a Number Is Majority Element in a Sorted Array](/problems/is-a-a-majority-element) | Easy | Array, binary search |
@@ -119,6 +142,11 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
119142
| 1417 | [Reformat The String](/problems/reformat-the-string) | Easy | String |
120143
| 1418 | [Display Table of Food Orders in a Restaurant](/problems/display-table-of-food-orders-in-a-restaurant) | Medium | Hash Table |
121144
| 1419 | [Minimum Number of Frogs Croaking](/problems/minimum-number-of-frogs-croaking) | Medium | String |
145+
| 1426 | [Counting Elements](/problem/counting-elements) | - | - |
146+
| 1427 | [Perform String Shifts](/problems/perform-string-shifts) | - | - |
147+
| 1428 | [Leftmost Column with at Least a One](/problems/leftmost-column-with-at-least-a-one) | - | - |
148+
| 1429 | [First Unique Number](/problems/first-unique-number) | - | - |
149+
| 1430 | [Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree](/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) | - | - |
122150

123151
## Questions / Issues
124152

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leetcode",
3-
"version": "1.23.0",
3+
"version": "1.24.0",
44
"description": "My solutions for LeetCode problems.",
55
"main": "index.js",
66
"scripts": {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Backspace String Compare
2+
3+
LeetCode #: [844](https://leetcode.com/problems/backspace-string-compare/)
4+
5+
Difficulty: Easy
6+
7+
Topics: Two Pointers, Stack.
8+
9+
## Problem
10+
11+
Given two strings `S` and `T`, return if they are equal when both are typed into empty text editors. `#` means a backspace character.
12+
13+
Example 1:
14+
15+
```text
16+
Input: S = "ab#c", T = "ad#c"
17+
Output: true
18+
Explanation: Both S and T become "ac".
19+
```
20+
21+
Example 2:
22+
23+
```text
24+
Input: S = "ab##", T = "c#d#"
25+
Output: true
26+
Explanation: Both S and T become "".
27+
```
28+
29+
Example 3:
30+
31+
```text
32+
Input: S = "a##c", T = "#a#c"
33+
Output: true
34+
Explanation: Both S and T become "c".
35+
```
36+
37+
Example 4:
38+
39+
```text
40+
Input: S = "a#c", T = "b"
41+
Output: false
42+
Explanation: S becomes "c" while T becomes "b".
43+
```
44+
45+
Note:
46+
47+
* `1 <= S.length <= 200`
48+
* `1 <= T.length <= 200`
49+
* `S` and `T` only contain lowercase letters and `'#'` characters.
50+
51+
Follow up:
52+
53+
* Can you solve it in `O(N)` time and `O(1)` space?
54+
55+
## Solution Explanation
56+
57+
The solution uses a two-pointer approach and performs the character comparison from the back of the strings.
58+
59+
If there is a `#` character, it will loop and skip towards the start of the string accordingly.
60+
61+
As soon as the string comparison fail, it will return `false` and exit immediately.
62+
63+
## Complexity Analysis
64+
65+
Time complexity: O(length of S + length of T)
66+
67+
Space complexity: O(1)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @param {string} S
3+
* @param {string} T
4+
* @return {boolean}
5+
*/
6+
const backspaceCompare = function (S, T) {
7+
let i = S.length - 1
8+
let j = T.length - 1
9+
let backspaceS = 0
10+
let backspaceT = 0
11+
12+
while (i >= 0 || j >= 0) {
13+
while (i >= 0) {
14+
const s = S[i]
15+
16+
if (s === '#') {
17+
backspaceS++
18+
i--
19+
continue
20+
}
21+
22+
if (backspaceS > 0) {
23+
backspaceS--
24+
i--
25+
continue
26+
}
27+
28+
break
29+
}
30+
31+
while (j >= 0) {
32+
if (T[j] === '#') {
33+
backspaceT++
34+
j--
35+
continue
36+
}
37+
38+
if (backspaceT > 0) {
39+
backspaceT--
40+
j--
41+
continue
42+
}
43+
44+
break
45+
}
46+
47+
if (S[i] !== T[j]) {
48+
return false
49+
}
50+
51+
i--
52+
j--
53+
}
54+
55+
return true
56+
}
57+
58+
module.exports = backspaceCompare
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const backspaceCompare = require('./backspaceCompare')
2+
3+
test('Example 1', () => {
4+
const S = 'ab#c'
5+
const T = 'ad#c'
6+
7+
const result = backspaceCompare(S, T)
8+
9+
expect(result).toBe(true)
10+
})
11+
12+
test('Example 2', () => {
13+
const S = 'ab##'
14+
const T = 'c#d#'
15+
16+
const result = backspaceCompare(S, T)
17+
18+
expect(result).toBe(true)
19+
})
20+
21+
test('Example 3', () => {
22+
const S = 'a##c'
23+
const T = '#a#c'
24+
25+
const result = backspaceCompare(S, T)
26+
27+
expect(result).toBe(true)
28+
})
29+
30+
test('Example 4', () => {
31+
const S = 'a#c'
32+
const T = 'b'
33+
34+
const result = backspaceCompare(S, T)
35+
36+
expect(result).toBe(false)
37+
})
38+
39+
test('test', () => {
40+
const S = '###'
41+
const T = 'bbb'
42+
43+
const result = backspaceCompare(S, T)
44+
45+
expect(result).toBe(false)
46+
})
47+
48+
test('test 2', () => {
49+
const S = '###bbb'
50+
const T = 'bbb'
51+
52+
const result = backspaceCompare(S, T)
53+
54+
expect(result).toBe(true)
55+
})
56+
57+
test('test 3', () => {
58+
const S = 'bbbextm'
59+
const T = 'bbb#extm'
60+
61+
const result = backspaceCompare(S, T)
62+
63+
expect(result).toBe(false)
64+
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Best Time to Buy and Sell Stock II
2+
3+
LeetCode #: [122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)
4+
5+
Difficulty: Easy
6+
7+
Topics: Array, Greedy.
8+
9+
## Problem
10+
11+
Say you have an array for which the `i`th element is the price of a given stock on day `i`.
12+
13+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
14+
15+
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
16+
17+
Example 1:
18+
19+
```text
20+
Input: [7,1,5,3,6,4]
21+
Output: 7
22+
Explanation:
23+
Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
24+
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
25+
```
26+
27+
Example 2:
28+
29+
```text
30+
Input: [1,2,3,4,5]
31+
Output: 4
32+
Explanation:
33+
Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
34+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
35+
```
36+
37+
Example 3:
38+
39+
```text
40+
Input: [7,6,4,3,1]
41+
Output: 0
42+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
43+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
const maxProfit = function (prices) {
6+
let sum = 0
7+
8+
for (let i = 1; i < prices.length; i++) {
9+
const profit = prices[i] - prices[i - 1]
10+
if (profit > 0) {
11+
sum += profit
12+
}
13+
}
14+
15+
return sum
16+
}
17+
18+
module.exports = maxProfit
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const maxProfit = require('./maxProfit')
2+
3+
test('Example 1', () => {
4+
const prices = [7, 1, 5, 3, 6, 4]
5+
6+
const result = maxProfit(prices)
7+
8+
expect(result).toBe(7)
9+
})
10+
11+
test('Example 2', () => {
12+
const prices = [1, 2, 3, 4, 5]
13+
14+
const result = maxProfit(prices)
15+
16+
expect(result).toBe(4)
17+
})
18+
19+
test('Example 3', () => {
20+
const prices = [7, 6, 4, 3, 1]
21+
22+
const result = maxProfit(prices)
23+
24+
expect(result).toBe(0)
25+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Binary Tree Maximum Path Sum
2+
3+
LeetCode #: [124](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
4+
5+
Difficulty: Hard.
6+
7+
Topics: Tree, Depth-first Search.
8+
9+
## Problem
10+
11+
Given a non-empty binary tree, find the maximum path sum.
12+
13+
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
14+
15+
Example 1:
16+
17+
```text
18+
Input: [1,2,3]
19+
20+
1
21+
/ \
22+
2 3
23+
24+
Output: 6
25+
```
26+
27+
Example 2:
28+
29+
```text
30+
Input: [-10,9,20,null,null,15,7]
31+
32+
-10
33+
/ \
34+
9 20
35+
/ \
36+
15 7
37+
38+
Output: 42
39+
```

0 commit comments

Comments
 (0)