Skip to content

Commit 57f661e

Browse files
authored
feat: add solutions to lc problem: No.2576 (#3490)
No.2576.Find the Maximum Number of Marked Indices
1 parent 9e6ca66 commit 57f661e

File tree

8 files changed

+137
-141
lines changed

8 files changed

+137
-141
lines changed

solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ tags:
7878

7979
### 方法一:贪心 + 双指针
8080

81-
为了将下标尽可能多地标记,我们可以将数组 `nums` 排序,然后从左到右遍历数组,对于每个下标 $i$,我们在数组的右半部分找到第一个满足 $2 \times nums[i] \leq nums[j]$ 的下标 $j$,然后标记下标 $i$ 和 $j$。继续遍历下一个下标 $i$。当我们遍历完数组的右半部分时,说明标记已经完成,此时标记的下标数目即为答案
81+
根据题目描述,题目最多产生 $n / 2$ 组标记,其中 $n$ 为数组 $\textit{nums}$ 的长度
8282

83-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。
83+
为了将下标尽可能多地标记,我们可以将数组 $\textit{nums}$ 排序,接下来,我们遍历右半部分的每个元素 $\textit{nums}[j]$,用一个指针 $\textit{i}$ 指向左半部分的最小元素,如果 $\textit{nums}[i] \times 2 \leq \textit{nums}[j]$,则可以标记下标 $\textit{i}$ 和 $\textit{j}$,我们将 $\textit{i}$ 向右移动一个位置。继续遍历右半部分的元素,直到到达数组的末尾。此时,我们可以标记的下标数目为 $\textit{i} \times 2$。
84+
85+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8486

8587
<!-- tabs:start -->
8688

@@ -90,16 +92,11 @@ tags:
9092
class Solution:
9193
def maxNumOfMarkedIndices(self, nums: List[int]) -> int:
9294
nums.sort()
93-
n = len(nums)
94-
i, j = 0, (n + 1) // 2
95-
ans = 0
96-
while j < n:
97-
while j < n and nums[i] * 2 > nums[j]:
98-
j += 1
99-
if j < n:
100-
ans += 2
101-
i, j = i + 1, j + 1
102-
return ans
95+
i, n = 0, len(nums)
96+
for x in nums[(n + 1) // 2 :]:
97+
if nums[i] * 2 <= x:
98+
i += 1
99+
return i * 2
103100
```
104101

105102
#### Java
@@ -108,17 +105,13 @@ class Solution:
108105
class Solution {
109106
public int maxNumOfMarkedIndices(int[] nums) {
110107
Arrays.sort(nums);
111-
int n = nums.length;
112-
int ans = 0;
113-
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) {
114-
while (j < n && nums[i] * 2 > nums[j]) {
115-
++j;
116-
}
117-
if (j < n) {
118-
ans += 2;
108+
int i = 0, n = nums.length;
109+
for (int j = (n + 1) / 2; j < n; ++j) {
110+
if (nums[i] * 2 <= nums[j]) {
111+
++i;
119112
}
120113
}
121-
return ans;
114+
return i * 2;
122115
}
123116
}
124117
```
@@ -129,18 +122,14 @@ class Solution {
129122
class Solution {
130123
public:
131124
int maxNumOfMarkedIndices(vector<int>& nums) {
132-
sort(nums.begin(), nums.end());
133-
int n = nums.size();
134-
int ans = 0;
135-
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) {
136-
while (j < n && nums[i] * 2 > nums[j]) {
137-
++j;
138-
}
139-
if (j < n) {
140-
ans += 2;
125+
ranges::sort(nums);
126+
int i = 0, n = nums.size();
127+
for (int j = (n + 1) / 2; j < n; ++j) {
128+
if (nums[i] * 2 <= nums[j]) {
129+
++i;
141130
}
142131
}
143-
return ans;
132+
return i * 2;
144133
}
145134
};
146135
```
@@ -150,16 +139,13 @@ public:
150139
```go
151140
func maxNumOfMarkedIndices(nums []int) (ans int) {
152141
sort.Ints(nums)
153-
n := len(nums)
154-
for i, j := 0, (n+1)/2; j < n; i, j = i+1, j+1 {
155-
for j < n && nums[i]*2 > nums[j] {
156-
j++
157-
}
158-
if j < n {
159-
ans += 2
142+
i, n := 0, len(nums)
143+
for _, x := range nums[(n+1)/2:] {
144+
if nums[i]*2 <= x {
145+
i++
160146
}
161147
}
162-
return
148+
return i * 2
163149
}
164150
```
165151

@@ -169,16 +155,31 @@ func maxNumOfMarkedIndices(nums []int) (ans int) {
169155
function maxNumOfMarkedIndices(nums: number[]): number {
170156
nums.sort((a, b) => a - b);
171157
const n = nums.length;
172-
let ans = 0;
173-
for (let i = 0, j = Math.floor((n + 1) / 2); j < n; ++i, ++j) {
174-
while (j < n && nums[i] * 2 > nums[j]) {
175-
++j;
158+
let i = 0;
159+
for (let j = (n + 1) >> 1; j < n; ++j) {
160+
if (nums[i] * 2 <= nums[j]) {
161+
++i;
176162
}
177-
if (j < n) {
178-
ans += 2;
163+
}
164+
return i * 2;
165+
}
166+
```
167+
168+
#### Rust
169+
170+
```rust
171+
impl Solution {
172+
pub fn max_num_of_marked_indices(mut nums: Vec<i32>) -> i32 {
173+
nums.sort();
174+
let mut i = 0;
175+
let n = nums.len();
176+
for j in (n + 1) / 2..n {
177+
if nums[i] * 2 <= nums[j] {
178+
i += 1;
179+
}
179180
}
181+
(i * 2) as i32
180182
}
181-
return ans;
182183
}
183184
```
184185

solution/2500-2599/2576.Find the Maximum Number of Marked Indices/README_EN.md

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Since there is no other operation, the answer is 4.
7070
</ul>
7171

7272
<p>&nbsp;</p>
73-
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
73+
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
7474
}
7575
.spoiler {overflow:hidden;}
7676
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
@@ -86,9 +86,11 @@ Since there is no other operation, the answer is 4.
8686

8787
### Solution 1: Greedy + Two Pointers
8888

89-
In order to mark as many indices as possible, we can sort the array `nums`, and then traverse the array from left to right. For each index $i$, we find the first index $j$ in the right half of the array that satisfies $2 \times nums[i] \leq nums[j]$, and then mark indices $i$ and $j$. Continue to traverse the next index $i$. When we have traversed the right half of the array, it means that the marking is complete, and the number of marked indices is the answer.
89+
According to the problem description, the problem can generate at most $n / 2$ pairs of indices, where $n$ is the length of the array $\textit{nums}$.
9090

91-
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array `nums`.
91+
To mark as many indices as possible, we can sort the array $\textit{nums}$. Next, we traverse each element $\textit{nums}[j]$ in the right half of the array, using a pointer $\textit{i}$ to point to the smallest element in the left half. If $\textit{nums}[i] \times 2 \leq \textit{nums}[j]$, we can mark the indices $\textit{i}$ and $\textit{j}$, and move $\textit{i}$ one position to the right. Continue traversing the elements in the right half until reaching the end of the array. At this point, the number of indices we can mark is $\textit{i} \times 2$.
92+
93+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $\textit{nums}$.
9294

9395
<!-- tabs:start -->
9496

@@ -98,16 +100,11 @@ The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log
98100
class Solution:
99101
def maxNumOfMarkedIndices(self, nums: List[int]) -> int:
100102
nums.sort()
101-
n = len(nums)
102-
i, j = 0, (n + 1) // 2
103-
ans = 0
104-
while j < n:
105-
while j < n and nums[i] * 2 > nums[j]:
106-
j += 1
107-
if j < n:
108-
ans += 2
109-
i, j = i + 1, j + 1
110-
return ans
103+
i, n = 0, len(nums)
104+
for x in nums[(n + 1) // 2 :]:
105+
if nums[i] * 2 <= x:
106+
i += 1
107+
return i * 2
111108
```
112109

113110
#### Java
@@ -116,17 +113,13 @@ class Solution:
116113
class Solution {
117114
public int maxNumOfMarkedIndices(int[] nums) {
118115
Arrays.sort(nums);
119-
int n = nums.length;
120-
int ans = 0;
121-
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) {
122-
while (j < n && nums[i] * 2 > nums[j]) {
123-
++j;
124-
}
125-
if (j < n) {
126-
ans += 2;
116+
int i = 0, n = nums.length;
117+
for (int j = (n + 1) / 2; j < n; ++j) {
118+
if (nums[i] * 2 <= nums[j]) {
119+
++i;
127120
}
128121
}
129-
return ans;
122+
return i * 2;
130123
}
131124
}
132125
```
@@ -137,18 +130,14 @@ class Solution {
137130
class Solution {
138131
public:
139132
int maxNumOfMarkedIndices(vector<int>& nums) {
140-
sort(nums.begin(), nums.end());
141-
int n = nums.size();
142-
int ans = 0;
143-
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) {
144-
while (j < n && nums[i] * 2 > nums[j]) {
145-
++j;
146-
}
147-
if (j < n) {
148-
ans += 2;
133+
ranges::sort(nums);
134+
int i = 0, n = nums.size();
135+
for (int j = (n + 1) / 2; j < n; ++j) {
136+
if (nums[i] * 2 <= nums[j]) {
137+
++i;
149138
}
150139
}
151-
return ans;
140+
return i * 2;
152141
}
153142
};
154143
```
@@ -158,16 +147,13 @@ public:
158147
```go
159148
func maxNumOfMarkedIndices(nums []int) (ans int) {
160149
sort.Ints(nums)
161-
n := len(nums)
162-
for i, j := 0, (n+1)/2; j < n; i, j = i+1, j+1 {
163-
for j < n && nums[i]*2 > nums[j] {
164-
j++
165-
}
166-
if j < n {
167-
ans += 2
150+
i, n := 0, len(nums)
151+
for _, x := range nums[(n+1)/2:] {
152+
if nums[i]*2 <= x {
153+
i++
168154
}
169155
}
170-
return
156+
return i * 2
171157
}
172158
```
173159

@@ -177,16 +163,31 @@ func maxNumOfMarkedIndices(nums []int) (ans int) {
177163
function maxNumOfMarkedIndices(nums: number[]): number {
178164
nums.sort((a, b) => a - b);
179165
const n = nums.length;
180-
let ans = 0;
181-
for (let i = 0, j = Math.floor((n + 1) / 2); j < n; ++i, ++j) {
182-
while (j < n && nums[i] * 2 > nums[j]) {
183-
++j;
166+
let i = 0;
167+
for (let j = (n + 1) >> 1; j < n; ++j) {
168+
if (nums[i] * 2 <= nums[j]) {
169+
++i;
184170
}
185-
if (j < n) {
186-
ans += 2;
171+
}
172+
return i * 2;
173+
}
174+
```
175+
176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn max_num_of_marked_indices(mut nums: Vec<i32>) -> i32 {
181+
nums.sort();
182+
let mut i = 0;
183+
let n = nums.len();
184+
for j in (n + 1) / 2..n {
185+
if nums[i] * 2 <= nums[j] {
186+
i += 1;
187+
}
187188
}
189+
(i * 2) as i32
188190
}
189-
return ans;
190191
}
191192
```
192193

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
class Solution {
22
public:
33
int maxNumOfMarkedIndices(vector<int>& nums) {
4-
sort(nums.begin(), nums.end());
5-
int n = nums.size();
6-
int ans = 0;
7-
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) {
8-
while (j < n && nums[i] * 2 > nums[j]) {
9-
++j;
10-
}
11-
if (j < n) {
12-
ans += 2;
4+
ranges::sort(nums);
5+
int i = 0, n = nums.size();
6+
for (int j = (n + 1) / 2; j < n; ++j) {
7+
if (nums[i] * 2 <= nums[j]) {
8+
++i;
139
}
1410
}
15-
return ans;
11+
return i * 2;
1612
}
17-
};
13+
};
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
func maxNumOfMarkedIndices(nums []int) (ans int) {
22
sort.Ints(nums)
3-
n := len(nums)
4-
for i, j := 0, (n+1)/2; j < n; i, j = i+1, j+1 {
5-
for j < n && nums[i]*2 > nums[j] {
6-
j++
7-
}
8-
if j < n {
9-
ans += 2
3+
i, n := 0, len(nums)
4+
for _, x := range nums[(n+1)/2:] {
5+
if nums[i]*2 <= x {
6+
i++
107
}
118
}
12-
return
13-
}
9+
return i * 2
10+
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class Solution {
22
public int maxNumOfMarkedIndices(int[] nums) {
33
Arrays.sort(nums);
4-
int n = nums.length;
5-
int ans = 0;
6-
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) {
7-
while (j < n && nums[i] * 2 > nums[j]) {
8-
++j;
9-
}
10-
if (j < n) {
11-
ans += 2;
4+
int i = 0, n = nums.length;
5+
for (int j = (n + 1) / 2; j < n; ++j) {
6+
if (nums[i] * 2 <= nums[j]) {
7+
++i;
128
}
139
}
14-
return ans;
10+
return i * 2;
1511
}
16-
}
12+
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
class Solution:
22
def maxNumOfMarkedIndices(self, nums: List[int]) -> int:
33
nums.sort()
4-
n = len(nums)
5-
i, j = 0, (n + 1) // 2
6-
ans = 0
7-
while j < n:
8-
while j < n and nums[i] * 2 > nums[j]:
9-
j += 1
10-
if j < n:
11-
ans += 2
12-
i, j = i + 1, j + 1
13-
return ans
4+
i, n = 0, len(nums)
5+
for x in nums[(n + 1) // 2 :]:
6+
if nums[i] * 2 <= x:
7+
i += 1
8+
return i * 2

0 commit comments

Comments
 (0)