There is a process sequence that contains the start and end of each process. There is a query sequence asking how many processes are running at a certain point in time. Please return the query result of the query sequence.

Notice
  • 1 < = logs[i].start,logs[i].end,queries[i] < = 2^32 - 1
  • 1 < = |logs|,|queries| < = 10^5

Have you met this question in a real interview?

Yes

Example

Given logs =[[1, 1234], [2, 1234]], queries =[2], return[2].

Explanation:
There are 2 processes running at time 2.

Given logs =[[1, 1234], [2, 1234]], queries =[1, 1235], return[1, 0].

Explanation:
There is a process running at time 1, and 0 processes running at time 1235.
  1. 时间点拆分并排序,然后用哈希记录查询的状态
#please note the query is not in order
    def numberOfProcesses(self, logs, queries):
        # Write your code here
        if not logs or not queries:
            return []

        ans = []
        times = [(log.start, 0) for log in logs]
        times += [(log.end, 1) for log in logs]
        times += [(query, 2) for query in queries]

        times.sort()
        hash = {}
        count = 0
        print times
        for time, status in times:
            if status == 0:
                count += 1
            elif status == 1:
                count -= 1
            hash[time] = count

        for query in queries:
            ans.append(hash[query])

        return ans

results matching ""

    No results matching ""