OAmaster
— / 4已做

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-value
  • minY: the minimum y-value
  • width: maxX - minX
  • height: maxY - minY Function Description Complete the function boundingBoxFromCoordinates in the editor below. boundingBoxFromCoordinates has the following parameter:
  • int[][] coordinates: the list of points Returns int[]: [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};
    }
};

You are given three non-negative integers: num1, num2, and givenSum. Compare the correct result of num1 + num2 with givenSum digit by digit in base 10, starting from the least significant digit. The ones place is index 0, the tens place is index 1, and so on. If one number has fewer digits than the other, treat the missing higher digits as 0 when comparing. Return the index of the first mismatching digit. Return -1 if every digit matches. Function Description Complete the function findWrongDigitIndex in the editor below. findWrongDigitIndex has the following parameters:

  • long num1: the first addend
  • long num2: the second addend
  • long givenSum: the sum to compare against Returns int: the first mismatching digit index, or -1 if the sum is correct.

Constraints

  • 0 ≤ num1, num2, givenSum ≤ 10¹⁸
  • Use base-10 d

Example 1

Input:

num1 = 13
num2 = 7
givenSum = 19

Output:

0

Explanation: The correct sum is 20. The ones digit differs immediately: 0 versus 9.

Example 2

Input:

num1 = 95
num2 = 5
givenSum = 100

Output:

-1

Explanation: The provided sum is correct, so there is no mismatching digit.

Pro解法 · 三语代码 · 复杂度分析
边界讨论 + 面试官追问 · $98 / 一年解锁
Pro 会员

解锁全部 1 道题的解法

题面你已经看到了 — 解法 + 三语代码 + 复杂度推导 + 边界讨论, Pro 解锁.

Pro 解锁全部
  • 📚1000+ 道真实北美 OA, Python / Java / C++ 三语题解
  • 📊个人 dashboard + 进度可视化 + 14 天活跃图
  • 📝题目笔记跨设备同步 + 个人复盘库
  • 🔓随时取消下次续费, Stripe Customer Portal 自助管理
$12/月($98/年, 一次付清省 32%)

≈ 北美 SWE 工资 10 分钟 · LeetCode Premium $35/月 的 23%