Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fdb410a

Browse files
feat: add No.219,228,1971
1 parent d9f34b2 commit fdb410a

File tree

3 files changed

+259
-0
lines changed

3 files changed

+259
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 1971. Find if Path Exists in Graph
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Depth-First Search, Breadth-First Search, Union Find, Graph.
5+
- Similar Questions: Valid Arrangement of Pairs, Paths in Maze That Lead to Same Room.
6+
7+
## Problem
8+
9+
There is a **bi-directional** graph with `n` vertices, where each vertex is labeled from `0` to `n - 1` (**inclusive**). The edges in the graph are represented as a 2D integer array `edges`, where each `edges[i] = [ui, vi]` denotes a bi-directional edge between vertex `ui` and vertex `vi`. Every vertex pair is connected by **at most one** edge, and no vertex has an edge to itself.
10+
11+
You want to determine if there is a **valid path** that exists from vertex `source` to vertex `destination`.
12+
13+
Given `edges` and the integers `n`, `source`, and `destination`, return `true`** if there is a **valid path** from **`source`** to **`destination`**, or **`false`** otherwise****.**
14+
15+
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png)
19+
20+
```
21+
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
22+
Output: true
23+
Explanation: There are two paths from vertex 0 to vertex 2:
24+
- 0 → 1 → 2
25+
- 0 → 2
26+
```
27+
28+
Example 2:
29+
30+
![](https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png)
31+
32+
```
33+
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
34+
Output: false
35+
Explanation: There is no path from vertex 0 to vertex 5.
36+
```
37+
38+
39+
**Constraints:**
40+
41+
42+
43+
- `1 <= n <= 2 * 105`
44+
45+
- `0 <= edges.length <= 2 * 105`
46+
47+
- `edges[i].length == 2`
48+
49+
- `0 <= ui, vi <= n - 1`
50+
51+
- `ui != vi`
52+
53+
- `0 <= source, destination <= n - 1`
54+
55+
- There are no duplicate edges.
56+
57+
- There are no self edges.
58+
59+
60+
61+
## Solution
62+
63+
```javascript
64+
/**
65+
* @param {number} n
66+
* @param {number[][]} edges
67+
* @param {number} source
68+
* @param {number} destination
69+
* @return {boolean}
70+
*/
71+
var validPath = function(n, edges, source, destination) {
72+
var dp = Array(n);
73+
var map = Array(n).fill(0).map(() => []);
74+
for (var i = 0; i < edges.length; i++) {
75+
map[edges[i][0]].push(edges[i][1]);
76+
map[edges[i][1]].push(edges[i][0]);
77+
}
78+
var dfs = function(i) {
79+
if (i === destination) return true;
80+
if (dp[i] !== undefined) return dp[i];
81+
dp[i] = false;
82+
for (var j = 0; j < map[i].length; j++) {
83+
if (dfs(map[i][j])) {
84+
dp[i] = true;
85+
break;
86+
}
87+
}
88+
return dp[i];
89+
};
90+
return dfs(source);
91+
};
92+
```
93+
94+
**Explain:**
95+
96+
nope.
97+
98+
**Complexity:**
99+
100+
* Time complexity : O(n).
101+
* Space complexity : O(n).
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 219. Contains Duplicate II
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, Sliding Window.
5+
- Similar Questions: Contains Duplicate, Contains Duplicate III.
6+
7+
## Problem
8+
9+
Given an integer array `nums` and an integer `k`, return `true` **if there are two **distinct indices** **`i`** and **`j`** in the array such that **`nums[i] == nums[j]`** and **`abs(i - j) <= k`.
10+
11+
12+
Example 1:
13+
14+
```
15+
Input: nums = [1,2,3,1], k = 3
16+
Output: true
17+
```
18+
19+
Example 2:
20+
21+
```
22+
Input: nums = [1,0,1,1], k = 1
23+
Output: true
24+
```
25+
26+
Example 3:
27+
28+
```
29+
Input: nums = [1,2,3,1,2,3], k = 2
30+
Output: false
31+
```
32+
33+
34+
**Constraints:**
35+
36+
37+
38+
- `1 <= nums.length <= 105`
39+
40+
- `-109 <= nums[i] <= 109`
41+
42+
- `0 <= k <= 105`
43+
44+
45+
46+
## Solution
47+
48+
```javascript
49+
/**
50+
* @param {number[]} nums
51+
* @param {number} k
52+
* @return {boolean}
53+
*/
54+
var containsNearbyDuplicate = function(nums, k) {
55+
var map = {};
56+
for (var i = 0; i < nums.length; i++) {
57+
if (map[nums[i]] !== undefined && i - map[nums[i]] <= k) return true;
58+
map[nums[i]] = i;
59+
}
60+
return false;
61+
};
62+
```
63+
64+
**Explain:**
65+
66+
nope.
67+
68+
**Complexity:**
69+
70+
* Time complexity : O(n).
71+
* Space complexity : O(n).

‎201-300/228. Summary Ranges.md‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 228. Summary Ranges
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array.
5+
- Similar Questions: Missing Ranges, Data Stream as Disjoint Intervals, Find Maximal Uncovered Ranges.
6+
7+
## Problem
8+
9+
You are given a **sorted unique** integer array `nums`.
10+
11+
A **range** `[a,b]` is the set of all integers from `a` to `b` (inclusive).
12+
13+
Return **the **smallest sorted** list of ranges that **cover all the numbers in the array exactly****. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`.
14+
15+
Each range `[a,b]` in the list should be output as:
16+
17+
18+
19+
- `"a->b"` if `a != b`
20+
21+
- `"a"` if `a == b`
22+
23+
24+
25+
Example 1:
26+
27+
```
28+
Input: nums = [0,1,2,4,5,7]
29+
Output: ["0->2","4->5","7"]
30+
Explanation: The ranges are:
31+
[0,2] --> "0->2"
32+
[4,5] --> "4->5"
33+
[7,7] --> "7"
34+
```
35+
36+
Example 2:
37+
38+
```
39+
Input: nums = [0,2,3,4,6,8,9]
40+
Output: ["0","2->4","6","8->9"]
41+
Explanation: The ranges are:
42+
[0,0] --> "0"
43+
[2,4] --> "2->4"
44+
[6,6] --> "6"
45+
[8,9] --> "8->9"
46+
```
47+
48+
49+
**Constraints:**
50+
51+
52+
53+
- `0 <= nums.length <= 20`
54+
55+
- `-231 <= nums[i] <= 231 - 1`
56+
57+
- All the values of `nums` are **unique**.
58+
59+
- `nums` is sorted in ascending order.
60+
61+
62+
63+
## Solution
64+
65+
```javascript
66+
/**
67+
* @param {number[]} nums
68+
* @return {string[]}
69+
*/
70+
var summaryRanges = function(nums) {
71+
var res = [];
72+
for (var i = 0; i < nums.length; i++) {
73+
if (i === 0 || nums[i] !== nums[i - 1] + 1) res.push(`${nums[i]}`);
74+
else if (i === nums.length - 1 || nums[i] !== nums[i + 1] - 1) res[res.length - 1] += `->${nums[i]}`;
75+
}
76+
return res;
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
nope.
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n).
87+
* Space complexity : O(n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /