-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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.2389 (#4027)
No.2389.Longest Subsequence With Limited Sum
- Loading branch information
Showing
8 changed files
with
126 additions
and
196 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
8 changes: 4 additions & 4 deletions
8
solution/2300-2399/2389.Longest Subsequence With Limited Sum/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,14 +1,14 @@ | ||
class Solution { | ||
public: | ||
vector<int> answerQueries(vector<int>& nums, vector<int>& queries) { | ||
sort(nums.begin(), nums.end()); | ||
ranges::sort(nums); | ||
for (int i = 1; i < nums.size(); i++) { | ||
nums[i] += nums[i - 1]; | ||
} | ||
vector<int> ans; | ||
for (auto& q : queries) { | ||
ans.push_back(upper_bound(nums.begin(), nums.end(), q) - nums.begin()); | ||
for (const auto& q : queries) { | ||
ans.emplace_back(upper_bound(nums.begin(), nums.end(), q) - nums.begin()); | ||
} | ||
return ans; | ||
} | ||
}; | ||
}; |
23 changes: 8 additions & 15 deletions
23
solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.cs
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,22 +1,15 @@ | ||
public class Solution { | ||
public int[] AnswerQueries(int[] nums, int[] queries) { | ||
int[] result = new int[queries.Length]; | ||
Array.Sort(nums); | ||
for (int i = 0; i < queries.Length; i++) { | ||
result[i] = getSubsequent(nums, queries[i]); | ||
for (int i = 1; i < nums.Length; ++i) { | ||
nums[i] += nums[i - 1]; | ||
} | ||
return result; | ||
|
||
} | ||
|
||
public int getSubsequent(int[] nums,int query) { | ||
int sum = 0; | ||
for (int i = 0; i < nums.Length; i++) { | ||
sum += nums[i]; | ||
if (sum > query) { | ||
return i; | ||
} | ||
int m = queries.Length; | ||
int[] ans = new int[m]; | ||
for (int i = 0; i < m; ++i) { | ||
int j = Array.BinarySearch(nums, queries[i] + 1); | ||
ans[i] = j < 0 ? -j - 1 : j; | ||
} | ||
return nums.Length; | ||
return ans; | ||
} | ||
} |
Oops, something went wrong.