Skip to content

Commit f180f5c

Browse files
committedJun 3, 2022
feat: add solutions to lc problems: No.2200~2203
* No.2200.Find All K-Distant Indices in an Array * No.2201.Count Artifacts That Can Be Extracted * No.2202.Maximize the Topmost Element After K Moves * No.2203.Minimum Weighted Subgraph With the Required Paths
1 parent 790bcbc commit f180f5c

File tree

22 files changed

+1037
-12
lines changed

22 files changed

+1037
-12
lines changed
 

‎solution/2200-2299/2200.Find All K-Distant Indices in an Array/README.md

+74-2
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,38 @@
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
62+
class Solution:
63+
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
64+
ans = []
65+
n = len(nums)
66+
for i in range(n):
67+
for j in range(n):
68+
if abs(i - j) <= k and nums[j] == key:
69+
ans.append(i)
70+
break
71+
return ans
6372
```
6473

6574
### **Java**
6675

6776
<!-- 这里可写当前语言的特殊实现逻辑 -->
6877

6978
```java
70-
79+
class Solution {
80+
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
81+
int n = nums.length;
82+
List<Integer> ans = new ArrayList<>();
83+
for (int i = 0; i < n; ++i) {
84+
for (int j = 0; j < n; ++j) {
85+
if (Math.abs(i - j) <= k && nums[j] == key) {
86+
ans.add(i);
87+
break;
88+
}
89+
}
90+
}
91+
return ans;
92+
}
93+
}
7194
```
7295

7396
### **TypeScript**
@@ -89,6 +112,55 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
89112
}
90113
```
91114

115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
121+
int n = nums.size();
122+
vector<int> ans;
123+
for (int i = 0; i < n; ++i)
124+
{
125+
for (int j = 0; j < n; ++j)
126+
{
127+
if (abs(i - j) <= k && nums[j] == key)
128+
{
129+
ans.push_back(i);
130+
break;
131+
}
132+
}
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func findKDistantIndices(nums []int, key int, k int) []int {
143+
n := len(nums)
144+
var ans []int
145+
for i := 0; i < n; i++ {
146+
for j, v := range nums {
147+
if abs(i-j) <= k && v == key {
148+
ans = append(ans, i)
149+
break
150+
}
151+
}
152+
}
153+
return ans
154+
}
155+
156+
func abs(x int) int {
157+
if x < 0 {
158+
return -x
159+
}
160+
return x
161+
}
162+
```
163+
92164
### **...**
93165

94166
```

‎solution/2200-2299/2200.Find All K-Distant Indices in an Array/README_EN.md

+74-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,36 @@ Hence, we return [0,1,2,3,4].
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
56+
ans = []
57+
n = len(nums)
58+
for i in range(n):
59+
for j in range(n):
60+
if abs(i - j) <= k and nums[j] == key:
61+
ans.append(i)
62+
break
63+
return ans
5564
```
5665

5766
### **Java**
5867

5968
```java
60-
69+
class Solution {
70+
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
71+
int n = nums.length;
72+
List<Integer> ans = new ArrayList<>();
73+
for (int i = 0; i < n; ++i) {
74+
for (int j = 0; j < n; ++j) {
75+
if (Math.abs(i - j) <= k && nums[j] == key) {
76+
ans.add(i);
77+
break;
78+
}
79+
}
80+
}
81+
return ans;
82+
}
83+
}
6184
```
6285

6386
### **TypeScript**
@@ -79,6 +102,55 @@ function findKDistantIndices(nums: number[], key: number, k: number): number[] {
79102
}
80103
```
81104

105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
111+
int n = nums.size();
112+
vector<int> ans;
113+
for (int i = 0; i < n; ++i)
114+
{
115+
for (int j = 0; j < n; ++j)
116+
{
117+
if (abs(i - j) <= k && nums[j] == key)
118+
{
119+
ans.push_back(i);
120+
break;
121+
}
122+
}
123+
}
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func findKDistantIndices(nums []int, key int, k int) []int {
133+
n := len(nums)
134+
var ans []int
135+
for i := 0; i < n; i++ {
136+
for j, v := range nums {
137+
if abs(i-j) <= k && v == key {
138+
ans = append(ans, i)
139+
break
140+
}
141+
}
142+
}
143+
return ans
144+
}
145+
146+
func abs(x int) int {
147+
if x < 0 {
148+
return -x
149+
}
150+
return x
151+
}
152+
```
153+
82154
### **...**
83155

84156
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
4+
int n = nums.size();
5+
vector<int> ans;
6+
for (int i = 0; i < n; ++i)
7+
{
8+
for (int j = 0; j < n; ++j)
9+
{
10+
if (abs(i - j) <= k && nums[j] == key)
11+
{
12+
ans.push_back(i);
13+
break;
14+
}
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func findKDistantIndices(nums []int, key int, k int) []int {
2+
n := len(nums)
3+
var ans []int
4+
for i := 0; i < n; i++ {
5+
for j, v := range nums {
6+
if abs(i-j) <= k && v == key {
7+
ans = append(ans, i)
8+
break
9+
}
10+
}
11+
}
12+
return ans
13+
}
14+
15+
func abs(x int) int {
16+
if x < 0 {
17+
return -x
18+
}
19+
return x
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
3+
int n = nums.length;
4+
List<Integer> ans = new ArrayList<>();
5+
for (int i = 0; i < n; ++i) {
6+
for (int j = 0; j < n; ++j) {
7+
if (Math.abs(i - j) <= k && nums[j] == key) {
8+
ans.add(i);
9+
break;
10+
}
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
3+
ans = []
4+
n = len(nums)
5+
for i in range(n):
6+
for j in range(n):
7+
if abs(i - j) <= k and nums[j] == key:
8+
ans.append(i)
9+
break
10+
return ans

‎solution/2200-2299/2201.Count Artifacts That Can Be Extracted/README.md

+97-1
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,52 @@
7373
<!-- 这里可写当前语言的特殊实现逻辑 -->
7474

7575
```python
76-
76+
class Solution:
77+
def digArtifacts(self, n: int, artifacts: List[List[int]], dig: List[List[int]]) -> int:
78+
def check(artifact):
79+
r1, c1, r2, c2 = artifact
80+
for x in range(r1, r2 + 1):
81+
for y in range(c1, c2 + 1):
82+
if (x, y) not in s:
83+
return False
84+
return True
85+
86+
s = {(i, j) for i, j in dig}
87+
return sum(check(v) for v in artifacts)
7788
```
7889

7990
### **Java**
8091

8192
<!-- 这里可写当前语言的特殊实现逻辑 -->
8293

8394
```java
95+
class Solution {
96+
public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
97+
Set<Integer> s = new HashSet<>();
98+
for (int[] d : dig) {
99+
s.add(d[0] * n + d[1]);
100+
}
101+
int ans = 0;
102+
for (int[] a : artifacts) {
103+
if (check(a, s, n)) {
104+
++ans;
105+
}
106+
}
107+
return ans;
108+
}
84109

110+
private boolean check(int[] a, Set<Integer> s, int n) {
111+
int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3];
112+
for (int i = r1; i <= r2; ++i) {
113+
for (int j = c1; j <= c2; ++j) {
114+
if (!s.contains(i * n + j)) {
115+
return false;
116+
}
117+
}
118+
}
119+
return true;
120+
}
121+
}
85122
```
86123

87124
### **TypeScript**
@@ -112,6 +149,65 @@ function digArtifacts(
112149
}
113150
```
114151

152+
### **C++**
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) {
158+
unordered_set<int> s;
159+
for (auto& d : dig) s.insert(d[0] * n + d[1]);
160+
int ans = 0;
161+
for (auto& a : artifacts) ans += check(a, s, n);
162+
return ans;
163+
}
164+
165+
bool check(vector<int>& a, unordered_set<int>& s, int n) {
166+
int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3];
167+
for (int i = r1; i <= r2; ++i)
168+
{
169+
for (int j = c1; j <= c2; ++j)
170+
{
171+
if (!s.count(i * n + j))
172+
{
173+
return false;
174+
}
175+
}
176+
}
177+
return true;
178+
}
179+
};
180+
```
181+
182+
### **Go**
183+
184+
```go
185+
func digArtifacts(n int, artifacts [][]int, dig [][]int) int {
186+
s := map[int]bool{}
187+
for _, d := range dig {
188+
s[d[0]*n+d[1]] = true
189+
}
190+
check := func(a []int) bool {
191+
r1, c1, r2, c2 := a[0], a[1], a[2], a[3]
192+
for i := r1; i <= r2; i++ {
193+
for j := c1; j <= c2; j++ {
194+
if !s[i*n+j] {
195+
return false
196+
}
197+
}
198+
}
199+
return true
200+
}
201+
ans := 0
202+
for _, a := range artifacts {
203+
if check(a) {
204+
ans++
205+
}
206+
}
207+
return ans
208+
}
209+
```
210+
115211
### **...**
116212

117213
```

0 commit comments

Comments
 (0)