OAmaster
— / 2已做

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. Problem: Task Management System (Level 1) Implement a simple task management system that supports adding tasks and retrieving the list of “current tasks”. Requirements Add task: create a task and store it in the system. Get current tasks: print the tasks currently stored in the system (in the required output format). Task model (suggested) Each task contains at least: taskId: unique identifier (int or string) taskName: string startTime: integer endTime: integer I/O (abstract) The input is a sequence of commands (e.g., ADD_TASK ..., GET_TASKS). For each GET_TASKS, print the task list in the required format. Constraints Number of tasks: up to 1e4. Sample test ideas Add 1 task then list; output contains that task. Add multiple tasks then list; output contains all tasks. List with no tasks; output is empty. Task names with spaces/special chars; formatting is correct. Overlapping task times; Level 1 does not require conflict validation. Function Description Complete solveTaskManagementSystem. 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.

Example 1

Input:

input = "ADD_TASK 1 \"Email\" 0 5\nGET_TASKS"

Output:

["Task(id=1,name=Email,start=0,end=5)"]

Explanation: The returned string must match the expected standard output for the sample input.

解法

按行解析命令:ADD_TASK id "name" start end 用正则提取,存入按插入顺序的列表;GET_TASKS 时按格式打印每条记录。时间复杂度 O(N) 命令数。

import re
from typing import List

def solveTaskManagementSystem(input: str) -> List[str]:
    tasks = []
    out = []
    for line in input.split('\n'):
        if line.startswith('ADD_TASK'):
            m = re.match(r'ADD_TASK\s+(\S+)\s+"([^"]*)"\s+(\d+)\s+(\d+)', line)
            if m:
                tasks.append((m.group(1), m.group(2), int(m.group(3)), int(m.group(4))))
        elif line.strip() == 'GET_TASKS':
            for tid, name, s, e in tasks:
                out.append(f"Task(id={tid},name={name},start={s},end={e})")
    return out
class Solution {
    String[] solveTaskManagementSystem(String input) {
        List<String[]> tasks = new ArrayList<>();
        List<String> out = new ArrayList<>();
        Pattern p = Pattern.compile("ADD_TASK\\s+(\\S+)\\s+\"([^\"]*)\"\\s+(\\d+)\\s+(\\d+)");
        for (String line : input.split("\n")) {
            if (line.startsWith("ADD_TASK")) {
                Matcher m = p.matcher(line);
                if (m.find()) tasks.add(new String[]{m.group(1), m.group(2), m.group(3), m.group(4)});
            } else if (line.trim().equals("GET_TASKS")) {
                for (String[] t : tasks) {
                    out.add("Task(id=" + t[0] + ",name=" + t[1] + ",start=" + t[2] + ",end=" + t[3] + ")");
                }
            }
        }
        return out.toArray(new String[0]);
    }
}
class Solution {
public:
    vector<string> solveTaskManagementSystem(string input) {
        vector<array<string, 4>> tasks;
        vector<string> out;
        regex pat("ADD_TASK\\s+(\\S+)\\s+\"([^\"]*)\"\\s+(\\d+)\\s+(\\d+)");
        stringstream ss(input);
        string line;
        while (getline(ss, line)) {
            smatch m;
            if (regex_search(line, m, pat) && line.find("ADD_TASK") == 0) {
                tasks.push_back({m[1].str(), m[2].str(), m[3].str(), m[4].str()});
            } else if (line.find("GET_TASKS") != string::npos) {
                for (auto& t : tasks) {
                    out.push_back("Task(id=" + t[0] + ",name=" + t[1] + ",start=" + t[2] + ",end=" + t[3] + ")");
                }
            }
        }
        return out;
    }
};

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. Problem: User Quota Scheduling with Expiring Assignments (Level 3) Implement a user/task scheduling system. Requirements Create user: each user has a quota meaning the maximum number of concurrent assignments allowed for that user. Assign a task: assign to a user a time interval [startTime, endTime) with a taskId. Rules Each assignment is active on the half-open interval [startTime, endTime). Once endTime is reached, the assignment expires and no longer counts toward the user’s concurrent quota. Example: existing assignment [4,10) no longer counts for any t ≥ 10. If adding a new assignment would make the number of concurrent active assignments exceed quota at any time, the assignment must be rejected. Output For each assignment attempt, output whether it succeeds (e.g., true/false or OK/FAIL per spec). Constraints Potentially large, but OA tests may be small; a straightforward approach may pass. Test ideas quota=1: add [4,10) ok; add [10,12) ok. quota=1: add [4,10) ok; add [9,12) reject. quota=2: adding a third overlapping interval should reject. Edge: zero-length intervals (per spec). Multiple users are independent. Function Description Complete solveUserQuotaScheduling. 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.

Example 1

Input:

input = "CREATE_USER u1 1\nASSIGN u1 1 4 10\nASSIGN u1 2 10 12"

Output:

["OK", "OK"]

Explanation: The returned string must match the expected standard output for the sample input.

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%