-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2576 (#3490)
No.2576.Find the Maximum Number of Marked Indices
- Loading branch information
Showing
8 changed files
with
137 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 7 additions & 11 deletions
18
solution/2500-2599/2576.Find the Maximum Number of Marked Indices/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
class Solution { | ||
public: | ||
int maxNumOfMarkedIndices(vector<int>& nums) { | ||
sort(nums.begin(), nums.end()); | ||
int n = nums.size(); | ||
int ans = 0; | ||
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) { | ||
while (j < n && nums[i] * 2 > nums[j]) { | ||
++j; | ||
} | ||
if (j < n) { | ||
ans += 2; | ||
ranges::sort(nums); | ||
int i = 0, n = nums.size(); | ||
for (int j = (n + 1) / 2; j < n; ++j) { | ||
if (nums[i] * 2 <= nums[j]) { | ||
++i; | ||
} | ||
} | ||
return ans; | ||
return i * 2; | ||
} | ||
}; | ||
}; |
15 changes: 6 additions & 9 deletions
15
solution/2500-2599/2576.Find the Maximum Number of Marked Indices/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
func maxNumOfMarkedIndices(nums []int) (ans int) { | ||
sort.Ints(nums) | ||
n := len(nums) | ||
for i, j := 0, (n+1)/2; j < n; i, j = i+1, j+1 { | ||
for j < n && nums[i]*2 > nums[j] { | ||
j++ | ||
} | ||
if j < n { | ||
ans += 2 | ||
i, n := 0, len(nums) | ||
for _, x := range nums[(n+1)/2:] { | ||
if nums[i]*2 <= x { | ||
i++ | ||
} | ||
} | ||
return | ||
} | ||
return i * 2 | ||
} |
16 changes: 6 additions & 10 deletions
16
solution/2500-2599/2576.Find the Maximum Number of Marked Indices/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
class Solution { | ||
public int maxNumOfMarkedIndices(int[] nums) { | ||
Arrays.sort(nums); | ||
int n = nums.length; | ||
int ans = 0; | ||
for (int i = 0, j = (n + 1) / 2; j < n; ++i, ++j) { | ||
while (j < n && nums[i] * 2 > nums[j]) { | ||
++j; | ||
} | ||
if (j < n) { | ||
ans += 2; | ||
int i = 0, n = nums.length; | ||
for (int j = (n + 1) / 2; j < n; ++j) { | ||
if (nums[i] * 2 <= nums[j]) { | ||
++i; | ||
} | ||
} | ||
return ans; | ||
return i * 2; | ||
} | ||
} | ||
} |
15 changes: 5 additions & 10 deletions
15
solution/2500-2599/2576.Find the Maximum Number of Marked Indices/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
class Solution: | ||
def maxNumOfMarkedIndices(self, nums: List[int]) -> int: | ||
nums.sort() | ||
n = len(nums) | ||
i, j = 0, (n + 1) // 2 | ||
ans = 0 | ||
while j < n: | ||
while j < n and nums[i] * 2 > nums[j]: | ||
j += 1 | ||
if j < n: | ||
ans += 2 | ||
i, j = i + 1, j + 1 | ||
return ans | ||
i, n = 0, len(nums) | ||
for x in nums[(n + 1) // 2 :]: | ||
if nums[i] * 2 <= x: | ||
i += 1 | ||
return i * 2 |
Oops, something went wrong.