LeetCode 582. Kill Process 解题报告(队列)

【例子1】  Kill Process

Problem statement:

Given n processes, each process has a unique PID (process id) and its PPID (parent process id).

Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.

We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.

Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.

Example 1:

Input: 
pid =  [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation: 
           3
         /   \
        1     5
             /
            10
Kill 5 will also kill 10.

Note:

  1. The given kill id is guaranteed to be one of the given PIDs.
  2. n >= 1.

思路:利用BFS+HashMap,首先将父进程和子进程存入HashMap。然后利用BFS,这里需要用到队列。当然,这里用DFS也行。

代码:

 vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill){
     unordered_map<int,vector<int>> map;
     for(int i = 0;i<ppid.size();i++)map[ppid[i].push_back(pid[i]);
     queue<int> q;
     q.push(kill);
     vector<int> res;
     while(!q.empty()){
        int cur = q.front()
        res.push(cur);
        q.pop();
         for(int i=0;i<map[cur].size();i++){
                q.push(map[cur][i]);
         }
     }
     return res;
 }
View Code 

 

【例子2】 打印杨辉三角形

  杨辉三角形:(a+b)的n次方的系数。

 思路:由于杨辉三角形中,下一行的系数能够依据前一行得到,所以,将前一行的系数放入队列中,在逐个出队列的时候,打印,同时将下一行的系数放在队列末尾。当前一行的系数已经全部出队列(打印完毕)之后,这时候下一行的所有系数刚好完全在队列当中。需要注意的是,为了方便计算,需要在每一层队列的最后都加上0,即.....1+0=1,得到下一层的最后一个数字1。打印时,将0忽略即可。

这里的思路其实就是BFS。同样的还有层序遍历二叉树。

   代码:

#include <queue>
#include<iostream>
using namespace std;
void printYangHui(int n){
    queue<int> q;
    q.push(1);
    int cnt = 0;
    while(cnt<n){
        int last = 0,cur;
        q.push(0);
        int len = q.size();
        for(int i=0;i<len;i++){
            cur = q.front();
            if(cur)cout << cur << " ";

            q.pop();
            q.push(last + cur);
            last = cur;
        }
        cout << endl;
        cnt ++;
    }
}

int main(){
    printYangHui(5);
    return 0;
}
View Code

 

【例子3】363. Max Sum of Rectangle No Larger Than K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.

Example:

Given matrix = [
  [1,  0, 1],
  [0, -2, 3]
]
k = 2

The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).

思路:首先降维,例如,我们对在选取左边列和右边列之后,就可以对每一行进行求和,这样就得到了一个一维数组。这里面的时间复杂度是o(n*n*m),然后,再解决一个数组中,连续子数组中不超过k的最大和是多少。这里就需要用到有序set,将连续子数组的和存下来。那么,怎么得到比k小的连续子数组的和呢,只要让每个sum减去set中比k - sum稍大的一个数,这不就行了吗。利用set中的lower_bound正好可以满足需要。

不过,这个题目好像和queue没啥关系。

代码:

 int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
        if (matrix.empty()) return 0;
        int M = max(matrix.size(),matrix[0].size());
        int N = min(matrix.size(),matrix[0].size());
        int res = INT_MIN;
        for (int l = 0; l < N; ++l) {
            vector<int> sums(M, 0);
            for (int r = l; r <N; ++r) {
                for (int i = 0; i < M; ++i) {
                    sums[i] +=  matrix.size()>matrix[0].size() ? matrix[i][r] : matrix[r][i]; //M是行,还是列
                }
                set<int> s;
                s.insert(0);
                int cur_sum =0;
                for(int t:sums){
                    cur_sum += t;
                    set<int>::iterator it  = s.lower_bound(cur_sum - k);
                    //it迭代器,指向set中第一个不小于cur_sum - k的值,如果set中没有,则指向end
                    if(it!=s.end())res = max(res,cur_sum - *it);
                    s.insert(cur_sum);//将当前计算得到的矩形面积入栈
                }
            }
        }     
        return res;
    }
View Code

 

posted on 2017-08-08 11:43  1592653  阅读(529)  评论(0编辑  收藏  举报