Skip to content

Commit e782e30

Browse files
committed
Merge branch 'release/1.28.0'
2 parents 8583800 + b711ff1 commit e782e30

File tree

92 files changed

+2920
-3
lines changed

Some content is hidden

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

92 files changed

+2920
-3
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,20 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
5353
| 49 | [Group Anagrams](/problems/group-anagrams) | Medium | Hash Table, String |
5454
| 53 | [Maximum Subarray](/problems/maximum-subarray) | Easy | Array, Divide and Conquer, Dynamic Programming |
5555
| 55 | [Jump Game](/problems/jump-game) | Medium | Array, Greedy |
56+
| 60 | [Permutation Sequence](/problems/permutation-sequence) | Medium | Math, Backtracking |
57+
| 62 | [Unique Paths](/problems/unique-paths) | Medium | Array, Dynamic Programming |
5658
| 64 | [Minimum Path Sum](/problems/minimum-path-sum) | Medium | Array, Dynamic Programming |
5759
| 70 | [Climbing Stairs](/problems/climbing-stairs) | Easy | Dynamic Programming |
5860
| 72 | [Edit Distance](/problems/edit-distance) | Hard | String, Dynamic Programming |
61+
| 75 | [Sort Colors](/problems/sort-colors) | Medium | Array, Two Pointers, Sort |
62+
| 96 | [Unique Binary Search Trees](/problems/unique-binary-search-trees) | Medium | Dynamic Programming, Tree |
5963
| 121 | [Best Time to Buy and Sell Stock](/problems/best-time-to-buy-and-sell-stock) | Easy | Array, Dynamic Programming |
6064
| 122 | [Best Time to Buy and Sell Stock II](/problems/best-time-to-buy-and-sell-stock-ii) | Easy | Array, Greedy |
6165
| 124 | [Binary Tree Maximum Path Sum](/problems/binary-tree-maximum-path-sum) | Hard | Tree, Depth-first Search |
66+
| 129 | [Sum Root to Leaf Numbers](/problems/sum-root-to-leaf-numbers) | Medium | Tree, Depth-first Search |
67+
| 130 | [Surrounded Regions](/problems/surrounded-regions) | Medium | Depth-first Search, Breadth-first Search, Union Find |
6268
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
69+
| 137 | [Single Number II](/problems/single-number-ii) | Medium | Bit Manipulation |
6370
| 146 | [LRU Cache](/problems/lru-cache) | Medium | Design |
6471
| 155 | [Min Stack](/problems/min-stack) | Easy | Stack, Design |
6572
| 169 | [Majority Element](/problems/majority-element) | Easy | Array, Divide and Conquer, Bit Manipulation |
@@ -70,33 +77,52 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
7077
| 202 | [Happy Number](/problems/happy-number) | Easy | Hash Table, Math |
7178
| 207 | [Course Schedule](/problems/course-schedule) | Medium | Depth-first Search, Breadth-first Search, Graph, Topological Sort |
7279
| 208 | [Implement Trie (Prefix Tree)](/problems/implement-trie-prefix-tree) | Medium | Design, Trie |
80+
| 212 | [Word Search II](/problems/word-search-ii) | Hard | Backtracking, Trie |
7381
| 221 | [Maximal Square](/problems/maximal-square) | Medium | Dynamic Programming |
82+
| 222 | [Count Complete Tree Nodes](/problems/count-complete-tree-nodes) | Medium | Binary Search, Tree |
83+
| 226 | [Invert Binary Tree](/problems/invert-binary-tree) | Easy | Tree |
7484
| 230 | [Kth Smallest Element in a BST](/problems/kth-smallest-element-in-a-bst) | Medium | Binary Search, Tree |
85+
| 231 | [Power of Two](/problems/power-of-two) | Easy | Math, Bit Manipulation |
86+
| 237 | [Delete Node in a Linked List](/problems/delete-node-in-a-linked-list) | Easy | Linked List |
7587
| 238 | [Product of Array Except Self](/problems/product-of-array-except-self) | Medium | Array |
88+
| 275 | [H-Index II](/problems/h-index-ii) | Medium | Binary Search |
7689
| 278 | [First Bad Version](/problems/first-bad-version) | Easy | Binary Search |
90+
| 279 | [Perfect Squares](/problems/perfect-squares) | Medium | Math, Dynamic Programming, Breadth-first Search |
7791
| 283 | [Move Zeroes](/problems/move-zeroes) | Easy | Array, Two Pointers |
92+
| 287 | [Find the Duplicate Number](/problems/find-the-duplicate-number) | Medium | Array, Two Pointers, Binary Search |
7893
| 303 | [Range Sum Query - Immutable](/problems/range-sum-query-immutable) | Easy | Dynamic Programming |
7994
| 328 | [Odd Even Linked List](/problems/odd-even-linked-list) | Medium | Linked List |
95+
| 332 | [Reconstruct Itinerary](/problems/reconstruct-itinerary) | Medium | Depth-first Search, Graph |
8096
| 338 | [Counting Bits](/problems/counting-bits) | Medium | Dynamic Programming, Bit Manipulation |
97+
| 344 | [Reverse String](/problems/reverse-string) | Easy | Two Pointers, String |
8198
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash table, two pointers, binary search, sort, set |
8299
| 350 | [Intersection of Two Arrays II](/problems/intersection-of-two-arrays-ii) | Easy | Hash Table, two pointers, binary search, sort |
83100
| 367 | [Valid Perfect Square](/problems/valid-perfect-square) | Easy | Math, Binary Search |
101+
| 368 | [Largest Divisible Subset](/problems/largest-divisible-subset) | Medium | Math, Dynamic Programming |
102+
| 380 | [Insert Delete GetRandom O(1)](/problems/insert-delete-getrandom-o1) | Medium | Array, Hash Table, Design |
84103
| 383 | [Ransom Note](/problems/ransom-note) | Easy | String |
85104
| 387 | [First Unique Character in a String](/problems/first-unique-character-in-a-string) | Easy | Hash Table, String |
86105
| 392 | [Is Subsequence](/problems/is-subsequence) | Easy | Binary Search, Dynamic Programming, Greedy |
87106
| 402 | [Remove K Digits](/problems/remove-k-digits) | Medium | Stack, Greedy |
107+
| 406 | [Queue Reconstruction by Height](/problems/queue-reconstruction-by-height) | Medium | Greedy |
88108
| 438 | [Find All Anagrams in a String](/problems/find-all-anagrams-in-a-string) | Medium | Hash Table |
89109
| 451 | [Sort Characters By Frequency](/problems/sort-characters-by-frequency) | Medium | Hash Table, Heap |
110+
| 468 | [Validate IP Address](/problems/validate-ip-address) | Medium | String |
90111
| 476 | [Number Complement](/problems/number-complement) | Easy | Bit Manipulation |
112+
| 518 | [Coin Change 2](/problems/coin-change-2) | Medium | - |
91113
| 525 | [Contiguous Array](/problems/contiguous-array) | Medium | Hash Table |
114+
| 528 | [Random Pick with Weight](/problems/random-pick-with-weight) | Medium | Binary Search, Random |
92115
| 540 | [Single Element in a Sorted Array](/problems/single-element-in-a-sorted-array) | Medium | - |
93116
| 543 | [Diameter of Binary Tree](/problems/diameter-of-binary-tree) | Easy | Tree |
94117
| 560 | [Subarray Sum Equals K](/problems/subarray-sum-equals-k) | Medium | Array, Hash Table |
95118
| 567 | [Permutation in String](/problems/permutation-in-string) | Medium | Two Pointers, Sliding Window |
96119
| 678 | [Valid Parenthesis String](/problems/valid-parenthesis-string) | Medium | String |
120+
| 700 | [Search in a Binary Search Tree](/problems/search-in-a-binary-search-tree) | Easy | Tree |
121+
| 714 | [Dungeon Game](/problems/dungeon-game) | Hard | Binary Search, Dynamic Programming |
97122
| 733 | [Flood Fill](/problems/flood-fill) | Easy | Depth-first Search |
98123
| 746 | [Min Cost Climbing Stairs](/problems/min-cost-climbing-stairs) | Easy | Array, Dynamic Programming |
99124
| 771 | [Jewels and Stones](/problems/jewels-and-stones) | Easy | Hash Table |
125+
| 787 | [Cheapest Flights Within K Stops](/problems/cheapest-flights-within-k-stops) | Medium | Dynamic Programming, Heap, Breadth-first Search |
100126
| 844 | [Backspace String Compare](/problems/backspace-string-compare) | Easy | Two Pointers, Stack |
101127
| 876 | [Middle of the Linked List](/problems/middle-of-the-linked-list) | Easy | Linked List |
102128
| 886 | [Possible Bipartition](/problems/possible-bipartition) | Medium | Depth-first Search |
@@ -108,7 +134,9 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
108134
| 997 | [Find the Town Judge](/problems/find-the-town-judge) | Easy | Graph |
109135
| 1008 | [Construct Binary Search Tree from Preorder Traversal](/problems/construct-binary-search-tree-from-preorder-traversal) | Medium | Tree |
110136
| 1025 | [Divisor Game](/problems/divisor-game) | Easy | Math, Dynamic Programming |
137+
| 1029 | [Two City Scheduling](/problems/two-city-scheduling) | Easy | Greedy |
111138
| 1035 | [Uncrossed Lines](/problems/uncrossed-lines) | Medium | Array |
139+
| 1044 | [Longest Duplicate Substring](/problems/longest-duplicate-substring) | Hard | Hash Table, Binary Search |
112140
| 1046 | [Last Stone Weight](/problems/last-stone-weight) | Easy | Heap, Greedy |
113141
| 1143 | [Longest Common Subsequence](/problems/longest-common-subsequence) | Medium | Dynamic Programming |
114142
| 1144 | [Decrease Elements To Make Array Zigzag](/problems/decrease-elements-to-make-array-zigzag) | Medium | Array |
@@ -180,7 +208,7 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
180208
| 1476 | [Subrectangle Queries](/problems/subrectangle-queries) | Medium | Array |
181209
| 1480 | [Running Sum of 1d Array](/problems/running-sum-of-1d-array) | Easy | Array |
182210
| 1481 | [Least Number of Unique Integers after K Removals](/problems/least-number-of-unique-integers-after-k-removals) | Medium | Array, Sort |
183-
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](/problems/average-salary-excluding-the-minimum-and-maximum-salary) | Easy | Array, Sort. |
211+
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](/problems/average-salary-excluding-the-minimum-and-maximum-salary) | Easy | Array, Sort |
184212
| 1492 | [The kth Factor of n](/problems/the-kth-factor-of-n) | Medium | Math |
185213
| 1493 | [Longest Subarray of 1's After Deleting One Element](/problems/longest-subarray-of-1s-after-deleting-one-element) | Medium | Array |
186214

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.27.0",
3+
"version": "1.28.0",
44
"description": "My solutions for LeetCode problems.",
55
"main": "index.js",
66
"scripts": {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Cheapest Flights Within K Stops
2+
3+
LeetCode #: [787](https://leetcode.com/problems/cheapest-flights-within-k-stops/)
4+
5+
Difficulty: Medium
6+
7+
Topics: Dynamic Programming, Heap, Breadth-first Search.
8+
9+
## Problem
10+
11+
There are `n` cities connected by `m` flights. Each flight starts from city `u` and arrives at `v` with a price `w`.
12+
13+
Now given all the cities and flights, together with starting city `src` and the destination `dst`, your task is to find the cheapest price from `src` to `dst` with up to `k` stops. If there is no such route, output `-1`.
14+
15+
Example 1:
16+
17+
![image](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png)
18+
19+
```text
20+
Input:
21+
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
22+
src = 0, dst = 2, k = 1
23+
Output: 200
24+
Explanation:
25+
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
26+
```
27+
28+
Example 2:
29+
30+
![image](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png)
31+
32+
```text
33+
Input:
34+
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
35+
src = 0, dst = 2, k = 0
36+
Output: 500
37+
Explanation:
38+
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
39+
```
40+
41+
Constraints:
42+
43+
- The number of nodes `n` will be in range `[1, 100]`, with nodes labeled from `0` to `n - 1`.
44+
- The size of flights will be in range `[0, n * (n - 1) / 2]`.
45+
- The format of each flight will be `(src, dst, price)`.
46+
- The price of each flight will be in the range `[1, 10000]`.
47+
- `k` is in the range of `[0, n - 1]`.
48+
- There will not be any duplicated flights or self cycles.
49+
50+
## Solution Explanation
51+
52+
The idea is based on [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm).
53+
54+
Reference: [[Java/Python] Priority Queue Solution](https://leetcode.com/problems/cheapest-flights-within-k-stops/discuss/115541/JavaPython-Priority-Queue-Solution) by [lee215](https://leetcode.com/lee215)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const sortedIndexBy = require('lodash/sortedIndexBy')
2+
3+
// get a src->dst->price map object based on flights.
4+
const getSrcDstPriceMap = (flights) => {
5+
const srcDstPriceMap = new Map()
6+
7+
for (let i = 0; i < flights.length; i++) {
8+
const [src, dst, price] = flights[i]
9+
srcDstPriceMap.set(src, (srcDstPriceMap.get(src) || new Map()).set(dst, price))
10+
}
11+
12+
return srcDstPriceMap
13+
}
14+
15+
/**
16+
* @param {number} n
17+
* @param {number[][]} flights
18+
* @param {number} src
19+
* @param {number} dst
20+
* @param {number} K
21+
* @return {number}
22+
*/
23+
const findCheapestPrice = function (n, flights, src, dst, k) {
24+
const srcDstPriceMap = getSrcDstPriceMap(flights)
25+
26+
// a priority queue that sorts based on accumulated price.
27+
// each item is in the format [accumulated price, current city, remaining flights].
28+
// note that remaining flights is equal to k+1.
29+
const queue = []
30+
queue.push([0, src, k + 1])
31+
32+
while (queue.length > 0) {
33+
const [accPrice, city, remainingFlights] = queue.shift()
34+
35+
if (city === dst) {
36+
return accPrice
37+
}
38+
39+
if (remainingFlights > 0) {
40+
const adjDstPriceMap = srcDstPriceMap.get(city) || new Map()
41+
42+
for (const [dst, price] of adjDstPriceMap.entries()) {
43+
const next = [accPrice + price, dst, remainingFlights - 1]
44+
const idx = sortedIndexBy(queue, next, (o) => o[0])
45+
queue.splice(idx, 0, next)
46+
}
47+
}
48+
}
49+
50+
return -1
51+
}
52+
53+
module.exports = findCheapestPrice
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const findCheapestPrice = require('./findCheapestPrice')
2+
3+
test('Example 1', () => {
4+
const n = 3
5+
const flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
6+
const src = 0
7+
const dst = 2
8+
const k = 1
9+
10+
const result = findCheapestPrice(n, flights, src, dst, k)
11+
12+
expect(result).toBe(200)
13+
})
14+
15+
test('Example 2', () => {
16+
const n = 3
17+
const flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500]]
18+
const src = 0
19+
const dst = 2
20+
const k = 0
21+
22+
const result = findCheapestPrice(n, flights, src, dst, k)
23+
24+
expect(result).toBe(500)
25+
})
26+
27+
test('no such route should return -1', () => {
28+
const n = 4
29+
const flights = [[0, 1, 100], [1, 2, 100], [0, 2, 500], [2, 3, 200]]
30+
const src = 0
31+
const dst = 3
32+
const k = 0
33+
34+
const result = findCheapestPrice(n, flights, src, dst, k)
35+
36+
expect(result).toBe(-1)
37+
})
38+
39+
test('intermediate city 4 has no flight out', () => {
40+
const n = 4
41+
const flights = [[0, 1, 100], [1, 4, 120], [0, 2, 500], [2, 3, 200]]
42+
const src = 0
43+
const dst = 3
44+
const k = 5
45+
46+
const result = findCheapestPrice(n, flights, src, dst, k)
47+
48+
expect(result).toBe(700)
49+
})

problems/coin-change-2/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Coin Change 2
2+
3+
LeetCode #: [518](https://leetcode.com/problems/coin-change-2/)
4+
5+
Difficulty: Medium
6+
7+
Topics: -
8+
9+
## Problem
10+
11+
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
12+
13+
Example 1:
14+
15+
```text
16+
Input: amount = 5, coins = [1, 2, 5]
17+
Output: 4
18+
Explanation: there are four ways to make up the amount:
19+
5=5
20+
5=2+2+1
21+
5=2+1+1+1
22+
5=1+1+1+1+1
23+
```
24+
25+
Example 2:
26+
27+
```text
28+
Input: amount = 3, coins = [2]
29+
Output: 0
30+
Explanation: the amount of 3 cannot be made up just with coins of 2.
31+
```
32+
33+
Example 3:
34+
35+
```text
36+
Input: amount = 10, coins = [10]
37+
Output: 1
38+
```
39+
40+
Note:
41+
42+
You can assume that
43+
44+
- `0 <= amount <= 5000`
45+
- `1 <= coin <= 5000`
46+
- the number of coins is less than 500
47+
- the answer is guaranteed to fit into signed 32-bit integer
48+
49+
## Solution Explanation
50+
51+
This is a [knapsack problem](https://en.wikipedia.org/wiki/Knapsack_problem) which can be solved by using dynamic programming.
52+
53+
Reference: [Knapsack problem - Java solution with thinking process O(nm) Time and O(m) Space](https://leetcode.com/problems/coin-change-2/discuss/99212/Knapsack-problem-Java-solution-with-thinking-process-O(nm)-Time-and-O(m)-Space) by [tankztc](https://leetcode.com/tankztc)
54+
55+
## Complexity Analysis
56+
57+
Assume m is the amount and n is the number of coins.
58+
59+
Time complexity: O(mn)
60+
61+
Space complexity: O(m)

problems/coin-change-2/change.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} amount
3+
* @param {number[]} coins
4+
* @return {number}
5+
*/
6+
const change = function (amount, coins) {
7+
const dp = new Array(amount + 1).fill(0)
8+
dp[0] = 1
9+
for (const coin of coins) {
10+
for (let i = coin; i <= amount; i++) {
11+
dp[i] += dp[i - coin]
12+
}
13+
}
14+
15+
return dp[amount]
16+
}
17+
18+
module.exports = change
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const change = require('./change')
2+
3+
test('Example 1', () => {
4+
const amount = 5
5+
const coins = [1, 2, 5]
6+
7+
const result = change(amount, coins)
8+
9+
expect(result).toBe(4)
10+
})
11+
12+
test('Example 2', () => {
13+
const amount = 3
14+
const coins = [2]
15+
16+
const result = change(amount, coins)
17+
18+
expect(result).toBe(0)
19+
})
20+
21+
test('Example 3', () => {
22+
const amount = 10
23+
const coins = [10]
24+
25+
const result = change(amount, coins)
26+
27+
expect(result).toBe(1)
28+
})
29+
30+
test('amount 0 should return 1 (empty combination)', () => {
31+
const amount = 0
32+
const coins = [1, 2, 5]
33+
34+
const result = change(amount, coins)
35+
36+
expect(result).toBe(1)
37+
})

0 commit comments

Comments
 (0)