You are given a non-empty list of 2D points coordinates, where each point is [x, y].
Return four integers describing the smallest axis-aligned bounding box that covers every point:
minX: the minimum x-valueminY: the minimum y-valuewidth:maxX - minXheight:maxY - minYFunction Description Complete the functionboundingBoxFromCoordinatesin the editor below.boundingBoxFromCoordinateshas the following parameter:int[][] coordinates: the list of points Returnsint[]:[minX, minY, width, height].
Constraints
1 ≤ coordinates.length ≤ 2 * 10⁵- Each point contains exactly two integers.
-10⁹ ≤ x, y ≤ 10⁹
Example 1
Input:
coordinates = [[2, 3], [5, 7], [1, 4]]
Output:
[1, 3, 4, 4]
Explanation:
The covered x-range is [1, 5] and the y-range is [3, 7], so the width and height are both 4.
Example 2
Input:
coordinates = [[0, 0]]
Output:
[0, 0, 0, 0]
Explanation: A single point produces a zero-width, zero-height bounding box rooted at that point.
解法
遍历一次找 minX/maxX/minY/maxY。返回 [minX, minY, maxX-minX, maxY-minY]。时间复杂度 O(n)。
from typing import List
def boundingBoxFromCoordinates(coordinates: List[List[int]]) -> List[int]:
xs = [p[0] for p in coordinates]
ys = [p[1] for p in coordinates]
return [min(xs), min(ys), max(xs) - min(xs), max(ys) - min(ys)]class Solution {
int[] boundingBoxFromCoordinates(int[][] coordinates) {
int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE, maxY = Integer.MIN_VALUE;
for (int[] p : coordinates) {
minX = Math.min(minX, p[0]); maxX = Math.max(maxX, p[0]);
minY = Math.min(minY, p[1]); maxY = Math.max(maxY, p[1]);
}
return new int[]{minX, minY, maxX - minX, maxY - minY};
}
}class Solution {
public:
vector<int> boundingBoxFromCoordinates(vector<vector<int>>& coordinates) {
int minX = INT_MAX, minY = INT_MAX, maxX = INT_MIN, maxY = INT_MIN;
for (auto& p : coordinates) {
minX = min(minX, p[0]); maxX = max(maxX, p[0]);
minY = min(minY, p[1]); maxY = max(maxY, p[1]);
}
return {minX, minY, maxX - minX, maxY - minY};
}
};You are given an integer array nums.
<p cl
Constraints
1 ≤ nums.length ≤ 2 * 10⁵-10⁹ ≤ nums[i] ≤ 10⁹
Example 1
Input:
nums = [1, 2, 3, 4, 6]
Output:
[6, 4, 2]
Explanation:
After removing odd values, the remaining array is [2, 4, 6]. Reversing it gives [6, 4, 2].
Example 2
Input:
nums = [7, 9, 11]
Output:
[]
Explanation: Every value is odd, so the result is empty.
解法
过滤偶数后反转。时间复杂度 O(n)。
from typing import List
def filterOddsAndReverseEvens(nums: List[int]) -> List[int]:
return [x for x in nums if x % 2 == 0][::-1]class Solution {
int[] filterOddsAndReverseEvens(int[] nums) {
List<Integer> even = new ArrayList<>();
for (int x : nums) if (x % 2 == 0) even.add(x);
Collections.reverse(even);
int[] out = new int[even.size()];
for (int i = 0; i < even.size(); i++) out[i] = even.get(i);
return out;
}
}class Solution {
public:
vector<int> filterOddsAndReverseEvens(vector<int>& nums) {
vector<int> even;
for (int x : nums) if (x % 2 == 0) even.push_back(x);
reverse(even.begin(), even.end());
return even;
}
};Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem.
Given records records = [(name, score), ...] and an integer threshold:
Filter out all records with score > threshold (keep score ≤ threshold).
Among the remaining records, find the record with the maximum score and output its name.
Constraints
At least one record satisfies score ≤ threshold.
name is a no-space string, score is an integer.
Input:
Line 1: integer n
Line 2: integer threshold
Next n lines: name score
Output:
One line: the name with the maximum score among those ≤ threshold.
Example: Input:
4
80
alice 50
bob 90
cindy 80
dave 70
Output:
cindy
Example
Input
4
80
alice 50
bob 90
cindy 80
dave 70
Output
cindy
Function Description
Complete solveFilterThresholdMaxScore. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters.
Constraints
Use the limits and requirements stated in the prompt.
解法
解析 stdin:第一行 n,第二行 threshold,接下来 n 行 name score;过滤 score ≤ threshold,找最大 score 的 name。时间复杂度 O(n)。
from typing import List
def solveFilterThresholdMaxScore(input: str) -> List[str]:
lines = input.strip().split('\n')
n = int(lines[0])
threshold = int(lines[1])
best_name = None
best_score = -float('inf')
for i in range(n):
parts = lines[2 + i].split()
name, score = parts[0], int(parts[1])
if score <= threshold and score > best_score:
best_score = score; best_name = name
return [best_name]class Solution {
String[] solveFilterThresholdMaxScore(String input) {
String[] lines = input.trim().split("\n");
int n = Integer.parseInt(lines[0].trim());
int threshold = Integer.parseInt(lines[1].trim());
String bestName = null;
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
String[] parts = lines[2 + i].trim().split("\\s+");
int sc = Integer.parseInt(parts[1]);
if (sc <= threshold && sc > bestScore) { bestScore = sc; bestName = parts[0]; }
}
return new String[]{bestName};
}
}class Solution {
public:
vector<string> solveFilterThresholdMaxScore(string input) {
stringstream ss(input);
int n, threshold; ss >> n >> threshold;
string bestName; int bestScore = INT_MIN;
for (int i = 0; i < n; i++) {
string name; int sc;
ss >> name >> sc;
if (sc <= threshold && sc > bestScore) { bestScore = sc; bestName = name; }
}
return {bestName};
}
};