Skip to content

Commit 7029592

Browse files
committed
feat: add solutions to lc problem: No.2113
No.2113.Elements in Array After Removing and Replacing Elements
1 parent 0982c5d commit 7029592

File tree

6 files changed

+211
-4
lines changed

6 files changed

+211
-4
lines changed

solution/2100-2199/2113.Elements in Array After Removing and Replacing Elements/README.md

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,106 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
**方法一:直接计算**
78+
79+
我们先初始化一个数组 $ans$,长度为 $m$,用于存储答案,初始化所有元素为 $-1$。
80+
81+
接下来遍历数组 $queries$,对于每个查询,我们先获取当前查询的时间 $t$ 和索引 $i$,先将 $t$ 对 $2n$ 取模,然后判断 $t$ 和 $n$ 的关系:
82+
83+
- 如果 $t \lt n$,那么 $t$ 时刻的数组元素个数为 $n - t$,并且数组元素是原数组元素整体向左移动 $t$ 个位置后的结果,因此如果 $i \lt n - t$,答案为 $nums[i + t]$;
84+
- 如果 $t \gt n$,那么 $t$ 时刻的数组元素个数为 $t - n$,并且数组元素是原数组元素的前 $t - n$ 个元素,因此如果 $i \lt t - n$,答案为 $nums[i]$。
85+
86+
最后返回数组 $ans$ 即可。
87+
88+
时间复杂度 $O(m)$,其中 $m$ 为数组 $queries$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
89+
7790
<!-- tabs:start -->
7891

7992
### **Python3**
8093

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

8396
```python
84-
97+
class Solution:
98+
def elementInNums(self, nums: List[int], queries: List[List[int]]) -> List[int]:
99+
n, m = len(nums), len(queries)
100+
ans = [-1] * m
101+
for j, (t, i) in enumerate(queries):
102+
t %= (2 * n)
103+
if t < n and i < n - t:
104+
ans[j] = nums[i + t]
105+
elif t > n and i < t - n:
106+
ans[j] = nums[i]
107+
return ans
85108
```
86109

87110
### **Java**
88111

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

91114
```java
115+
class Solution {
116+
public int[] elementInNums(int[] nums, int[][] queries) {
117+
int n = nums.length, m = queries.length;
118+
int[] ans = new int[m];
119+
for (int j = 0; j < m; ++j) {
120+
ans[j] = -1;
121+
int t = queries[j][0], i = queries[j][1];
122+
t %= (2 * n);
123+
if (t < n && i < n - t) {
124+
ans[j] = nums[i + t];
125+
} else if (t > n && i < t - n) {
126+
ans[j] = nums[i];
127+
}
128+
}
129+
return ans;
130+
}
131+
}
132+
```
92133

134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
vector<int> elementInNums(vector<int>& nums, vector<vector<int>>& queries) {
140+
int n = nums.size(), m = queries.size();
141+
vector<int> ans(m, -1);
142+
for (int j = 0; j < m; ++j) {
143+
int t = queries[j][0], i = queries[j][1];
144+
t %= (n * 2);
145+
if (t < n && i < n - t) {
146+
ans[j] = nums[i + t];
147+
} else if (t > n && i < t - n) {
148+
ans[j] = nums[i];
149+
}
150+
}
151+
return ans;
152+
}
153+
};
93154
```
94155
95-
### **TypeScript**
156+
### **Go**
157+
158+
```go
159+
func elementInNums(nums []int, queries [][]int) []int {
160+
n, m := len(nums), len(queries)
161+
ans := make([]int, m)
162+
for j, q := range queries {
163+
t, i := q[0], q[1]
164+
t %= (n * 2)
165+
ans[j] = -1
166+
if t < n && i < n-t {
167+
ans[j] = nums[i+t]
168+
} else if t > n && i < t-n {
169+
ans[j] = nums[i]
170+
}
171+
}
172+
return ans
173+
}
174+
```
96175

97-
<!-- 这里可写当前语言的特殊实现逻辑 -->
176+
### **TypeScript**
98177

99178
```ts
100179

solution/2100-2199/2113.Elements in Array After Removing and Replacing Elements/README_EN.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,81 @@ At minute 3, nums[0] does not exist.
7575
### **Python3**
7676

7777
```python
78-
78+
class Solution:
79+
def elementInNums(self, nums: List[int], queries: List[List[int]]) -> List[int]:
80+
n, m = len(nums), len(queries)
81+
ans = [-1] * m
82+
for j, (t, i) in enumerate(queries):
83+
t %= (2 * n)
84+
if t < n and i < n - t:
85+
ans[j] = nums[i + t]
86+
elif t > n and i < t - n:
87+
ans[j] = nums[i]
88+
return ans
7989
```
8090

8191
### **Java**
8292

8393
```java
94+
class Solution {
95+
public int[] elementInNums(int[] nums, int[][] queries) {
96+
int n = nums.length, m = queries.length;
97+
int[] ans = new int[m];
98+
for (int j = 0; j < m; ++j) {
99+
ans[j] = -1;
100+
int t = queries[j][0], i = queries[j][1];
101+
t %= (2 * n);
102+
if (t < n && i < n - t) {
103+
ans[j] = nums[i + t];
104+
} else if (t > n && i < t - n) {
105+
ans[j] = nums[i];
106+
}
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
vector<int> elementInNums(vector<int>& nums, vector<vector<int>>& queries) {
119+
int n = nums.size(), m = queries.size();
120+
vector<int> ans(m, -1);
121+
for (int j = 0; j < m; ++j) {
122+
int t = queries[j][0], i = queries[j][1];
123+
t %= (n * 2);
124+
if (t < n && i < n - t) {
125+
ans[j] = nums[i + t];
126+
} else if (t > n && i < t - n) {
127+
ans[j] = nums[i];
128+
}
129+
}
130+
return ans;
131+
}
132+
};
133+
```
84134
135+
### **Go**
136+
137+
```go
138+
func elementInNums(nums []int, queries [][]int) []int {
139+
n, m := len(nums), len(queries)
140+
ans := make([]int, m)
141+
for j, q := range queries {
142+
t, i := q[0], q[1]
143+
t %= (n * 2)
144+
ans[j] = -1
145+
if t < n && i < n-t {
146+
ans[j] = nums[i+t]
147+
} else if t > n && i < t-n {
148+
ans[j] = nums[i]
149+
}
150+
}
151+
return ans
152+
}
85153
```
86154

87155
### **TypeScript**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> elementInNums(vector<int>& nums, vector<vector<int>>& queries) {
4+
int n = nums.size(), m = queries.size();
5+
vector<int> ans(m, -1);
6+
for (int j = 0; j < m; ++j) {
7+
int t = queries[j][0], i = queries[j][1];
8+
t %= (n * 2);
9+
if (t < n && i < n - t) {
10+
ans[j] = nums[i + t];
11+
} else if (t > n && i < t - n) {
12+
ans[j] = nums[i];
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func elementInNums(nums []int, queries [][]int) []int {
2+
n, m := len(nums), len(queries)
3+
ans := make([]int, m)
4+
for j, q := range queries {
5+
t, i := q[0], q[1]
6+
t %= (n * 2)
7+
ans[j] = -1
8+
if t < n && i < n-t {
9+
ans[j] = nums[i+t]
10+
} else if t > n && i < t-n {
11+
ans[j] = nums[i]
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int[] elementInNums(int[] nums, int[][] queries) {
3+
int n = nums.length, m = queries.length;
4+
int[] ans = new int[m];
5+
for (int j = 0; j < m; ++j) {
6+
ans[j] = -1;
7+
int t = queries[j][0], i = queries[j][1];
8+
t %= (2 * n);
9+
if (t < n && i < n - t) {
10+
ans[j] = nums[i + t];
11+
} else if (t > n && i < t - n) {
12+
ans[j] = nums[i];
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def elementInNums(self, nums: List[int], queries: List[List[int]]) -> List[int]:
3+
n, m = len(nums), len(queries)
4+
ans = [-1] * m
5+
for j, (t, i) in enumerate(queries):
6+
t %= (2 * n)
7+
if t < n and i < n - t:
8+
ans[j] = nums[i + t]
9+
elif t > n and i < t - n:
10+
ans[j] = nums[i]
11+
return ans

0 commit comments

Comments
 (0)