Skip to content

Commit 3013bb6

Browse files
committed
feat: add solutions to lc problems: No.3264,3265
* No.3264.Final Array State After K Multiplication Operations I * No.3265.Count Almost Equal Pairs I
1 parent b0946c3 commit 3013bb6

File tree

14 files changed

+719
-16
lines changed

14 files changed

+719
-16
lines changed

solution/3200-3299/3264.Final Array State After K Multiplication Operations I/README.md

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3264.Fi
114114

115115
<!-- solution:start -->
116116

117-
### 方法一
117+
### 方法一:优先队列(小根堆)+ 模拟
118+
119+
我们可以用一个小根堆来维护数组 $\textit{nums}$ 中的元素,每次从小根堆中取出最小值,将其乘以 $\textit{multiplier}$ 后再放回小根堆中。在实现过程中,我们往小根堆插入的是元素的下标,然后自定义比较函数,使得小根堆按照 $\textit{nums}$ 中元素的大小作为第一关键字,下标作为第二关键字进行排序。
120+
121+
最后,我们返回数组 $\textit{nums}$ 即可。
122+
123+
时间复杂度 $O((n + k) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
118124

119125
<!-- tabs:start -->
120126

121127
#### Python3
122128

123129
```python
124-
130+
class Solution:
131+
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
132+
pq = [(x, i) for i, x in enumerate(nums)]
133+
heapify(pq)
134+
for _ in range(k):
135+
_, i = heappop(pq)
136+
nums[i] *= multiplier
137+
heappush(pq, (nums[i], i))
138+
return nums
125139
```
126140

127141
#### Java
128142

129143
```java
130-
144+
class Solution {
145+
public int[] getFinalState(int[] nums, int k, int multiplier) {
146+
PriorityQueue<Integer> pq
147+
= new PriorityQueue<>((i, j) -> nums[i] - nums[j] == 0 ? i - j : nums[i] - nums[j]);
148+
for (int i = 0; i < nums.length; i++) {
149+
pq.offer(i);
150+
}
151+
while (k-- > 0) {
152+
int i = pq.poll();
153+
nums[i] *= multiplier;
154+
pq.offer(i);
155+
}
156+
return nums;
157+
}
158+
}
131159
```
132160

133161
#### C++
134162

135163
```cpp
136-
164+
class Solution {
165+
public:
166+
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
167+
auto cmp = [&nums](int i, int j) {
168+
return nums[i] == nums[j] ? i > j : nums[i] > nums[j];
169+
};
170+
priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
171+
172+
for (int i = 0; i < nums.size(); ++i) {
173+
pq.push(i);
174+
}
175+
176+
while (k--) {
177+
int i = pq.top();
178+
pq.pop();
179+
nums[i] *= multiplier;
180+
pq.push(i);
181+
}
182+
183+
return nums;
184+
}
185+
};
137186
```
138187

139188
#### Go
140189

141190
```go
191+
func getFinalState(nums []int, k int, multiplier int) []int {
192+
h := &hp{nums: nums}
193+
for i := range nums {
194+
heap.Push(h, i)
195+
}
196+
197+
for k > 0 {
198+
i := heap.Pop(h).(int)
199+
nums[i] *= multiplier
200+
heap.Push(h, i)
201+
k--
202+
}
203+
204+
return nums
205+
}
206+
207+
type hp struct {
208+
sort.IntSlice
209+
nums []int
210+
}
211+
212+
func (h *hp) Less(i, j int) bool {
213+
if h.nums[h.IntSlice[i]] == h.nums[h.IntSlice[j]] {
214+
return h.IntSlice[i] < h.IntSlice[j]
215+
}
216+
return h.nums[h.IntSlice[i]] < h.nums[h.IntSlice[j]]
217+
}
218+
219+
func (h *hp) Pop() any {
220+
old := h.IntSlice
221+
n := len(old)
222+
x := old[n-1]
223+
h.IntSlice = old[:n-1]
224+
return x
225+
}
226+
227+
func (h *hp) Push(x any) {
228+
h.IntSlice = append(h.IntSlice, x.(int))
229+
}
230+
```
142231

232+
#### TypeScript
233+
234+
```ts
235+
function getFinalState(nums: number[], k: number, multiplier: number): number[] {
236+
const pq = new PriorityQueue({
237+
compare: (i, j) => (nums[i] === nums[j] ? i - j : nums[i] - nums[j]),
238+
});
239+
for (let i = 0; i < nums.length; ++i) {
240+
pq.enqueue(i);
241+
}
242+
while (k--) {
243+
const i = pq.dequeue()!;
244+
nums[i] *= multiplier;
245+
pq.enqueue(i);
246+
}
247+
return nums;
248+
}
143249
```
144250

145251
<!-- tabs:end -->

solution/3200-3299/3264.Final Array State After K Multiplication Operations I/README_EN.md

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3264.Fi
112112

113113
<!-- solution:start -->
114114

115-
### Solution 1
115+
### Solution 1: Priority Queue (Min-Heap) + Simulation
116+
117+
We can use a min-heap to maintain the elements in the array $\textit{nums}$. Each time, we extract the minimum value from the min-heap, multiply it by $\textit{multiplier}$, and then put it back into the min-heap. During the implementation, we insert the indices of the elements into the min-heap and define a custom comparator function to sort the min-heap based on the values of the elements in $\textit{nums}$ as the primary key and the indices as the secondary key.
118+
119+
Finally, we return the array $\textit{nums}$.
120+
121+
The time complexity is $O((n + k) \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
116122

117123
<!-- tabs:start -->
118124

119125
#### Python3
120126

121127
```python
122-
128+
class Solution:
129+
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
130+
pq = [(x, i) for i, x in enumerate(nums)]
131+
heapify(pq)
132+
for _ in range(k):
133+
_, i = heappop(pq)
134+
nums[i] *= multiplier
135+
heappush(pq, (nums[i], i))
136+
return nums
123137
```
124138

125139
#### Java
126140

127141
```java
128-
142+
class Solution {
143+
public int[] getFinalState(int[] nums, int k, int multiplier) {
144+
PriorityQueue<Integer> pq
145+
= new PriorityQueue<>((i, j) -> nums[i] - nums[j] == 0 ? i - j : nums[i] - nums[j]);
146+
for (int i = 0; i < nums.length; i++) {
147+
pq.offer(i);
148+
}
149+
while (k-- > 0) {
150+
int i = pq.poll();
151+
nums[i] *= multiplier;
152+
pq.offer(i);
153+
}
154+
return nums;
155+
}
156+
}
129157
```
130158

131159
#### C++
132160

133161
```cpp
134-
162+
class Solution {
163+
public:
164+
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
165+
auto cmp = [&nums](int i, int j) {
166+
return nums[i] == nums[j] ? i > j : nums[i] > nums[j];
167+
};
168+
priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
169+
170+
for (int i = 0; i < nums.size(); ++i) {
171+
pq.push(i);
172+
}
173+
174+
while (k--) {
175+
int i = pq.top();
176+
pq.pop();
177+
nums[i] *= multiplier;
178+
pq.push(i);
179+
}
180+
181+
return nums;
182+
}
183+
};
135184
```
136185

137186
#### Go
138187

139188
```go
189+
func getFinalState(nums []int, k int, multiplier int) []int {
190+
h := &hp{nums: nums}
191+
for i := range nums {
192+
heap.Push(h, i)
193+
}
194+
195+
for k > 0 {
196+
i := heap.Pop(h).(int)
197+
nums[i] *= multiplier
198+
heap.Push(h, i)
199+
k--
200+
}
201+
202+
return nums
203+
}
204+
205+
type hp struct {
206+
sort.IntSlice
207+
nums []int
208+
}
209+
210+
func (h *hp) Less(i, j int) bool {
211+
if h.nums[h.IntSlice[i]] == h.nums[h.IntSlice[j]] {
212+
return h.IntSlice[i] < h.IntSlice[j]
213+
}
214+
return h.nums[h.IntSlice[i]] < h.nums[h.IntSlice[j]]
215+
}
216+
217+
func (h *hp) Pop() any {
218+
old := h.IntSlice
219+
n := len(old)
220+
x := old[n-1]
221+
h.IntSlice = old[:n-1]
222+
return x
223+
}
224+
225+
func (h *hp) Push(x any) {
226+
h.IntSlice = append(h.IntSlice, x.(int))
227+
}
228+
```
140229

230+
#### TypeScript
231+
232+
```ts
233+
function getFinalState(nums: number[], k: number, multiplier: number): number[] {
234+
const pq = new PriorityQueue({
235+
compare: (i, j) => (nums[i] === nums[j] ? i - j : nums[i] - nums[j]),
236+
});
237+
for (let i = 0; i < nums.length; ++i) {
238+
pq.enqueue(i);
239+
}
240+
while (k--) {
241+
const i = pq.dequeue()!;
242+
nums[i] *= multiplier;
243+
pq.enqueue(i);
244+
}
245+
return nums;
246+
}
141247
```
142248

143249
<!-- tabs:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
4+
auto cmp = [&nums](int i, int j) {
5+
return nums[i] == nums[j] ? i > j : nums[i] > nums[j];
6+
};
7+
priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);
8+
9+
for (int i = 0; i < nums.size(); ++i) {
10+
pq.push(i);
11+
}
12+
13+
while (k--) {
14+
int i = pq.top();
15+
pq.pop();
16+
nums[i] *= multiplier;
17+
pq.push(i);
18+
}
19+
20+
return nums;
21+
}
22+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
func getFinalState(nums []int, k int, multiplier int) []int {
2+
h := &hp{nums: nums}
3+
for i := range nums {
4+
heap.Push(h, i)
5+
}
6+
7+
for k > 0 {
8+
i := heap.Pop(h).(int)
9+
nums[i] *= multiplier
10+
heap.Push(h, i)
11+
k--
12+
}
13+
14+
return nums
15+
}
16+
17+
type hp struct {
18+
sort.IntSlice
19+
nums []int
20+
}
21+
22+
func (h *hp) Less(i, j int) bool {
23+
if h.nums[h.IntSlice[i]] == h.nums[h.IntSlice[j]] {
24+
return h.IntSlice[i] < h.IntSlice[j]
25+
}
26+
return h.nums[h.IntSlice[i]] < h.nums[h.IntSlice[j]]
27+
}
28+
29+
func (h *hp) Pop() any {
30+
old := h.IntSlice
31+
n := len(old)
32+
x := old[n-1]
33+
h.IntSlice = old[:n-1]
34+
return x
35+
}
36+
37+
func (h *hp) Push(x any) {
38+
h.IntSlice = append(h.IntSlice, x.(int))
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] getFinalState(int[] nums, int k, int multiplier) {
3+
PriorityQueue<Integer> pq
4+
= new PriorityQueue<>((i, j) -> nums[i] - nums[j] == 0 ? i - j : nums[i] - nums[j]);
5+
for (int i = 0; i < nums.length; i++) {
6+
pq.offer(i);
7+
}
8+
while (k-- > 0) {
9+
int i = pq.poll();
10+
nums[i] *= multiplier;
11+
pq.offer(i);
12+
}
13+
return nums;
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
3+
pq = [(x, i) for i, x in enumerate(nums)]
4+
heapify(pq)
5+
for _ in range(k):
6+
_, i = heappop(pq)
7+
nums[i] *= multiplier
8+
heappush(pq, (nums[i], i))
9+
return nums
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getFinalState(nums: number[], k: number, multiplier: number): number[] {
2+
const pq = new PriorityQueue({
3+
compare: (i, j) => (nums[i] === nums[j] ? i - j : nums[i] - nums[j]),
4+
});
5+
for (let i = 0; i < nums.length; ++i) {
6+
pq.enqueue(i);
7+
}
8+
while (k--) {
9+
const i = pq.dequeue()!;
10+
nums[i] *= multiplier;
11+
pq.enqueue(i);
12+
}
13+
return nums;
14+
}

0 commit comments

Comments
 (0)