Skip to content

Commit 8b5b0f6

Browse files
committed
Merge branch 'release/1.25.0'
2 parents 29d7f96 + e579468 commit 8b5b0f6

File tree

90 files changed

+3318
-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.

90 files changed

+3318
-2
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,60 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
5555
| 55 | [Jump Game](/problems/jump-game) | Medium | Array, Greedy |
5656
| 64 | [Minimum Path Sum](/problems/minimum-path-sum) | Medium | Array, Dynamic Programming |
5757
| 70 | [Climbing Stairs](/problems/climbing-stairs) | Easy | Dynamic Programming |
58+
| 72 | [Edit Distance](/problems/edit-distance) | Hard | String, Dynamic Programming |
5859
| 121 | [Best Time to Buy and Sell Stock](/problems/best-time-to-buy-and-sell-stock) | Easy | Array, Dynamic Programming |
5960
| 122 | [Best Time to Buy and Sell Stock II](/problems/best-time-to-buy-and-sell-stock-ii) | Easy | Array, Greedy |
6061
| 124 | [Binary Tree Maximum Path Sum](/problems/binary-tree-maximum-path-sum) | Hard | Tree, Depth-first Search |
6162
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
6263
| 146 | [LRU Cache](/problems/lru-cache) | Medium | Design |
6364
| 155 | [Min Stack](/problems/min-stack) | Easy | Stack, Design |
65+
| 169 | [Majority Element](/problems/majority-element) | Easy | Array, Divide and Conquer, Bit Manipulation |
6466
| 189 | [Rotate Array](/problems/rotate-array) | Easy | Array |
6567
| 198 | [House Robber](/problems/house-robber) | Easy | Dynamic Programming |
6668
| 200 | [Number of Islands](/problems/number-of-islands) | Medium | Depth-first Search, Breadth-first Search, Union Find |
6769
| 201 | [Bitwise AND of Numbers Range](/problems/bitwise-and-of-numbers-range) | Medium | Bit Manipulation |
6870
| 202 | [Happy Number](/problems/happy-number) | Easy | Hash Table, Math |
71+
| 207 | [Course Schedule](/problems/course-schedule) | Medium | Depth-first Search, Breadth-first Search, Graph, Topological Sort |
72+
| 208 | [Implement Trie (Prefix Tree)](/problems/implement-trie-prefix-tree) | Medium | Design, Trie |
6973
| 221 | [Maximal Square](/problems/maximal-square) | Medium | Dynamic Programming |
74+
| 230 | [Kth Smallest Element in a BST](/problems/kth-smallest-element-in-a-bst) | Medium | Binary Search, Tree |
7075
| 238 | [Product of Array Except Self](/problems/product-of-array-except-self) | Medium | Array |
76+
| 278 | [First Bad Version](/problems/first-bad-version) | Easy | Binary Search |
7177
| 283 | [Move Zeroes](/problems/move-zeroes) | Easy | Array, Two Pointers |
7278
| 303 | [Range Sum Query - Immutable](/problems/range-sum-query-immutable) | Easy | Dynamic Programming |
79+
| 328 | [Odd Even Linked List](/problems/odd-even-linked-list) | Medium | Linked List |
80+
| 338 | [Counting Bits](/problems/counting-bits) | Medium | Dynamic Programming, Bit Manipulation |
7381
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash table, two pointers, binary search, sort, set |
7482
| 350 | [Intersection of Two Arrays II](/problems/intersection-of-two-arrays-ii) | Easy | Hash Table, two pointers, binary search, sort |
83+
| 367 | [Valid Perfect Square](/problems/valid-perfect-square) | Easy | Math, Binary Search |
84+
| 383 | [Ransom Note](/problems/ransom-note) | Easy | String |
85+
| 387 | [First Unique Character in a String](/problems/first-unique-character-in-a-string) | Easy | Hash Table, String |
7586
| 392 | [Is Subsequence](/problems/is-subsequence) | Easy | Binary Search, Dynamic Programming, Greedy |
87+
| 402 | [Remove K Digits](/problems/remove-k-digits) | Medium | Stack, Greedy |
88+
| 438 | [Find All Anagrams in a String](/problems/find-all-anagrams-in-a-string) | Medium | Hash Table |
89+
| 451 | [Sort Characters By Frequency](/problems/sort-characters-by-frequency) | Medium | Hash Table, Heap |
90+
| 476 | [Number Complement](/problems/number-complement) | Easy | Bit Manipulation |
7691
| 525 | [Contiguous Array](/problems/contiguous-array) | Medium | Hash Table |
92+
| 540 | [Single Element in a Sorted Array](/problems/single-element-in-a-sorted-array) | Medium | - |
7793
| 543 | [Diameter of Binary Tree](/problems/diameter-of-binary-tree) | Easy | Tree |
7894
| 560 | [Subarray Sum Equals K](/problems/subarray-sum-equals-k) | Medium | Array, Hash Table |
95+
| 567 | [Permutation in String](/problems/permutation-in-string) | Medium | Two Pointers, Sliding Window |
7996
| 678 | [Valid Parenthesis String](/problems/valid-parenthesis-string) | Medium | String |
97+
| 733 | [Flood Fill](/problems/flood-fill) | Easy | Depth-first Search |
8098
| 746 | [Min Cost Climbing Stairs](/problems/min-cost-climbing-stairs) | Easy | Array, Dynamic Programming |
99+
| 771 | [Jewels and Stones](/problems/jewels-and-stones) | Easy | Hash Table |
81100
| 844 | [Backspace String Compare](/problems/backspace-string-compare) | Easy | Two Pointers, Stack |
82101
| 876 | [Middle of the Linked List](/problems/middle-of-the-linked-list) | Easy | Linked List |
102+
| 886 | [Possible Bipartition](/problems/possible-bipartition) | Medium | Depth-first Search |
103+
| 901 | [Online Stock Span](/problems/online-stock-span) | Medium | Stack |
104+
| 918 | [Maximum Sum Circular Subarray](/problems/maximum-sum-circular-subarray) | Medium | Array |
105+
| 973 | [K Closest Points to Origin](/problems/k-closest-points-to-origin) | Medium | Divide and Conquer, Heap, Sort |
106+
| 986 | [Interval List Intersections](/problems/interval-list-intersections) | Medium | Two Pointers |
107+
| 993 | [Cousins in Binary Tree](/problems/cousins-in-binary-tree) | Easy | Tree, Breadth-first Search |
108+
| 997 | [Find the Town Judge](/problems/find-the-town-judge) | Easy | Graph |
83109
| 1008 | [Construct Binary Search Tree from Preorder Traversal](/problems/construct-binary-search-tree-from-preorder-traversal) | Medium | Tree |
84110
| 1025 | [Divisor Game](/problems/divisor-game) | Easy | Math, Dynamic Programming |
111+
| 1035 | [Uncrossed Lines](/problems/uncrossed-lines) | Medium | Array |
85112
| 1046 | [Last Stone Weight](/problems/last-stone-weight) | Easy | Heap, Greedy |
86113
| 1143 | [Longest Common Subsequence](/problems/longest-common-subsequence) | Medium | Dynamic Programming |
87114
| 1144 | [Decrease Elements To Make Array Zigzag](/problems/decrease-elements-to-make-array-zigzag) | Medium | Array |
@@ -104,6 +131,7 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
104131
| 1213 | [Intersection of Three Sorted Arrays](/problems/intersection-of-three-sorted-arrays) | Easy | Hash table, two pointers |
105132
| 1214 | [Two Sum BSTs](/problems/two-sum-bsts) | Medium | Binary search tree |
106133
| 1217 | [Play with Chips](/problems/play-with-chips) | Easy | Array, math, greedy |
134+
| 1232 | [Check If It Is a Straight Line](/problems/check-if-it-is-a-straight-line) | Easy | Array, Math, Geometry |
107135
| 1243 | [Array Transformation](/problems/array-transformation) | Easy | Array |
108136
| 1244 | [Design A Leaderboard](/problems/design-a-leaderboard) | Medium | Hash table, sort, design |
109137
| 1247 | [Minimum Swaps to Make Strings Equal](/problems/minimum-swaps-to-make-strings-equal) | Medium | String, greedy |
@@ -114,6 +142,7 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
114142
| 1272 | [Remove Interval](/problems/remove-interval) | Medium | Math, line sweep |
115143
| 1275 | [Find Winner on a Tic Tac Toe Game](/problems/find-winner-on-a-tic-tac-toe-game) | Easy | Array |
116144
| 1276 | [Number of Burgers with No Waste of Ingredients](/problems/number-of-burgers-with-no-waste-of-ingredients) | Medium | Math, greedy |
145+
| 1277 | [Count Square Submatrices with All Ones](/problems/count-square-submatrices-with-all-ones) | Medium | Array, Dynamic Programming |
117146
| 1313 | [Decompress Run-Length Encoded List](/problems/decompress-run-length-encoded-list) | Easy | Array |
118147
| 1314 | [Matrix Block Sum](/problems/matrix-block-sum) | Medium | Dynamic programming |
119148
| 1317 | [Convert Integer to the Sum of Two No-Zero Integers](/problems/convert-integer-to-the-sum-of-two-no-zero-integers) | Easy | Math |

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.24.0",
3+
"version": "1.25.0",
44
"description": "My solutions for LeetCode problems.",
55
"main": "index.js",
66
"scripts": {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Check If It Is a Straight Line
2+
3+
LeetCode #: [1232](https://leetcode.com/problems/check-if-it-is-a-straight-line/)
4+
5+
Difficulty: Easy
6+
7+
Topic: Array, Math, Geometry.
8+
9+
## Problem
10+
11+
You are given an array `coordinates`, `coordinates[i] = [x, y]`, where `[x, y]` represents the coordinate of a point. Check if these points make a straight line in the XY plane.
12+
13+
Example 1:
14+
15+
![Example 1](https://assets.leetcode.com/uploads/2019/10/15/untitled-diagram-2.jpg)
16+
17+
```text
18+
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
19+
Output: true
20+
```
21+
22+
Example 2:
23+
24+
![Example 2](https://assets.leetcode.com/uploads/2019/10/09/untitled-diagram-1.jpg)
25+
26+
```text
27+
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
28+
Output: false
29+
```
30+
31+
Constraints:
32+
33+
- `2 <= coordinates.length <= 1000`
34+
- `coordinates[i].length == 2`
35+
- `-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4`
36+
- `coordinates` contains no duplicate point.
37+
38+
## Complexity Analysis
39+
40+
Time complexity: O(n)
41+
42+
Space complexity: O(1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const getSlope = (point1, point2) => {
2+
const [x1, y1] = point1
3+
const [x2, y2] = point2
4+
const diffX = x2 - x1
5+
const diffY = y2 - y1
6+
7+
return (diffX === 0)
8+
? null
9+
: (diffY / diffX)
10+
}
11+
12+
/**
13+
* @param {number[][]} coordinates
14+
* @return {boolean}
15+
*/
16+
const checkStraightLine = function (coordinates) {
17+
const slope = getSlope(coordinates[0], coordinates[1])
18+
19+
for (let i = 2; i < coordinates.length; i++) {
20+
const curSlope = getSlope(coordinates[i - 1], coordinates[i])
21+
22+
if (curSlope !== slope) {
23+
return false
24+
}
25+
}
26+
27+
return true
28+
}
29+
30+
module.exports = checkStraightLine
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const checkStraightLine = require('./checkStraightLine')
2+
3+
test('Example 1', () => {
4+
const coordinates = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
5+
6+
const result = checkStraightLine(coordinates)
7+
8+
expect(result).toBe(true)
9+
})
10+
11+
test('Example 2', () => {
12+
const coordinates = [[1, 1], [2, 2], [3, 4], [4, 5], [5, 6], [7, 7]]
13+
14+
const result = checkStraightLine(coordinates)
15+
16+
expect(result).toBe(false)
17+
})
18+
19+
test('horizontal straight line', () => {
20+
const coordinates = [[-3, -2], [-1, -2], [2, -2], [-2, -2], [0, -2]]
21+
22+
const result = checkStraightLine(coordinates)
23+
24+
expect(result).toBe(true)
25+
})
26+
27+
test('vertical straight line', () => {
28+
const coordinates = [[1, 3], [1, 4], [1, 5]]
29+
30+
const result = checkStraightLine(coordinates)
31+
32+
expect(result).toBe(true)
33+
})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Count Square Submatrices with All Ones
2+
3+
LeetCode #: [1277](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)
4+
5+
Difficulty: Medium
6+
7+
Topic: Array, Dynamic Programming.
8+
9+
## Problem
10+
11+
Given a `m * n` matrix of ones and zeros, return how many square submatrices have all ones.
12+
13+
Example 1:
14+
15+
```text
16+
Input: matrix =
17+
[
18+
[0,1,1,1],
19+
[1,1,1,1],
20+
[0,1,1,1]
21+
]
22+
Output: 15
23+
Explanation:
24+
There are 10 squares of side 1.
25+
There are 4 squares of side 2.
26+
There is 1 square of side 3.
27+
Total number of squares = 10 + 4 + 1 = 15.
28+
```
29+
30+
Example 2:
31+
32+
```text
33+
Input: matrix =
34+
[
35+
[1,0,1],
36+
[1,1,0],
37+
[1,1,0]
38+
]
39+
Output: 7
40+
Explanation:
41+
There are 6 squares of side 1.
42+
There is 1 square of side 2.
43+
Total number of squares = 6 + 1 = 7.
44+
```
45+
46+
Constraints:
47+
48+
- `1 <= arr.length <= 300`
49+
- `1 <= arr[0].length <= 300`
50+
- `0 <= arr[i][j] <= 1`
51+
52+
## Complexity Analysis
53+
54+
Time complexity: O(m*n)
55+
56+
Space complexity: O(1)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number}
4+
*/
5+
const countSquares = function (matrix) {
6+
let count = 0
7+
8+
for (let i = 0; i < matrix.length; i++) {
9+
for (let j = 0; j < matrix[i].length; j++) {
10+
if (matrix[i][j] === 1) {
11+
const up = (matrix[i - 1] && matrix[i - 1][j]) || 0
12+
const upleft = (matrix[i - 1] && matrix[i - 1][j - 1]) || 0
13+
const left = (matrix[i][j - 1]) || 0
14+
matrix[i][j] += Math.min(up, upleft, left)
15+
count += matrix[i][j]
16+
}
17+
}
18+
}
19+
20+
return count
21+
}
22+
23+
module.exports = countSquares
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const countSquares = require('./countSquares')
2+
3+
test('Example 1', () => {
4+
const matrix = [
5+
[0, 1, 1, 1],
6+
[1, 1, 1, 1],
7+
[0, 1, 1, 1]
8+
]
9+
10+
const result = countSquares(matrix)
11+
12+
expect(result).toBe(15)
13+
})
14+
15+
test('Example 2', () => {
16+
const matrix = [
17+
[1, 0, 1],
18+
[1, 1, 0],
19+
[1, 1, 0]
20+
]
21+
22+
const result = countSquares(matrix)
23+
24+
expect(result).toBe(7)
25+
})

problems/counting-bits/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Counting Bits
2+
3+
LeetCode #: [338](https://leetcode.com/problems/counting-bits/)
4+
5+
Difficulty: Medium
6+
7+
Topics: Dynamic Programming, Bit Manipulation.
8+
9+
## Problem
10+
11+
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
12+
13+
Example 1:
14+
15+
```text
16+
Input: 2
17+
Output: [0,1,1]
18+
```
19+
20+
Example 2:
21+
22+
```text
23+
Input: 5
24+
Output: [0,1,1,2,1,2]
25+
```
26+
27+
Follow up:
28+
29+
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
30+
- Space complexity should be O(n).
31+
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
32+
33+
## Solution Explanation
34+
35+
Reference: [Three-Line Java Solution](https://leetcode.com/problems/counting-bits/discuss/79539/Three-Line-Java-Solution) by [lixx2100
36+
](https://leetcode.com/lixx2100)

0 commit comments

Comments
 (0)