• 675. Cut Off Trees for Golf Event


    // Potential improvements:
    // 1. we can use vector<int> { h, x, y } to replace Element, sorting vector is to compare elements one by one.
    // 2. use 2-d bool vector<vector<bool>> to replace unordered_set.
    
    class Element {
    public:
        int x, y, h;
        Element(int x, int y, int h) {
            this->x = x;
            this->y = y;
            this->h = h;
        }
    };
    class Solution {
    public:
        int m;
        int n;
        int cutOffTree(vector<vector<int>>& forest) {
            m = forest.size();  if (m == 0) return 0;
            n = forest[0].size();   if (n == 0) return 0;
            vector<Element> v;
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    if (forest[i][j] > 1)
                        v.emplace_back(i, j, forest[i][j]);
            auto comp = [](const Element& a, const Element& b) { return a.h < b.h; };
            v.emplace_back(0, 0, 0);
            sort(v.begin(), v.end(), comp);
            
            int res = 0;
            for (int i = 0; i < v.size()  - 1; i++) {
                int t = helper(forest, v[i], v[i+1]);
                if (t < 0)  return t;
                res += t;
            }
            return res;
        }
        int helper(vector<vector<int>>& forest, const Element& a, const Element& b) {
            const int dirs[] = { -1, 0, 1, 0, -1 };
            // (x,y) is small enough, so x*n+y won't overflow. otherwise we have to use long,
            // and be careful x*n+y will overflow, we may use (long)x*n+y instead.
            unordered_set<int> s;   
            queue<pair<int,int>> q;
            q.push({a.x, a.y});
            s.insert(a.x * n + a.y);
            int lv = 0;
            while (!q.empty()) {
                int qsz = q.size();
                for (int i = 0; i < qsz; i++) {
                    auto cur = q.front();
                    q.pop();
                    if (cur.first == b.x && cur.second == b.y)
                        return lv;
                    for (int i = 0; i < 4; i++) {
                        int nx = cur.first + dirs[i];
                        int ny = cur.second + dirs[i+1];
                        pair<int,int> np = {nx,ny};
                        if (nx >= 0 && nx < m && ny >= 0 && ny < n && 
                            forest[nx][ny] > 0 &&
                            s.find(nx * n + ny) == s.end()) {
                            q.push(np);
                            s.insert(nx * n + ny);
                        }
                    }
                }
                lv++;
            }
            return -1;
        }
    };

     huge perf improve from 1000+ ms to 300 ms: 

    use 2-d bool vector<vector<bool>> to replace unordered_set
    class Element {
    public:
        int x, y, h;
        Element(int x, int y, int h) {
            this->x = x;
            this->y = y;
            this->h = h;
        }
    };
    class Solution {
    public:
        int m;
        int n;
        int cutOffTree(vector<vector<int>>& forest) {
            m = forest.size();  if (m == 0) return 0;
            n = forest[0].size();   if (n == 0) return 0;
            vector<Element> v;
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    if (forest[i][j] > 1)
                        v.emplace_back(i, j, forest[i][j]);
            auto comp = [](const Element& a, const Element& b) { return a.h < b.h; };
            v.emplace_back(0, 0, 0);
            sort(v.begin(), v.end(), comp);
            
            int res = 0;
            for (int i = 0; i < v.size()  - 1; i++) {
                int t = helper(forest, v[i], v[i+1]);
                if (t < 0)  return t;
                res += t;
            }
            return res;
        }
        int helper(vector<vector<int>>& forest, const Element& a, const Element& b) {
            const int dirs[] = { -1, 0, 1, 0, -1 };
            // (x,y) is small enough, so x*n+y won't overflow. otherwise we have to use long,
            // and be careful x*n+y will overflow, we may use (long)x*n+y instead.
            vector<vector<bool>> s(m, vector<bool>(n));
            queue<pair<int,int>> q;
            q.emplace(a.x, a.y);
            s[a.x][a.y] = true;
            int lv = 0;
            while (!q.empty()) {
                int qsz = q.size();
                for (int i = 0; i < qsz; i++) {
                    auto cur = q.front();
                    q.pop();
                    if (cur.first == b.x && cur.second == b.y)
                        return lv;
                    for (int i = 0; i < 4; i++) {
                        int nx = cur.first + dirs[i];
                        int ny = cur.second + dirs[i+1];
                        pair<int,int> np = {nx,ny};
                        if (nx >= 0 && nx < m && ny >= 0 && ny < n && 
                            forest[nx][ny] > 0 &&
                            !s[nx][ny]) {
                            q.push(np);
                            s[nx][ny] = true;
                        }
                    }
                }
                lv++;
            }
            return -1;
        }
    };
  • 相关阅读:
    [ZZ] Valse 2017 | 生成对抗网络(GAN)研究年度进展评述
    [ZZ] 多领域视觉数据的转换、关联与自适应学习
    [ZZ] 深度学习三巨头之一来清华演讲了,你只需要知道这7点
    [ZZ] 如何在多版本anaconda python环境下转换spyder
    支持向量机(Support Vector Machine,SVM)
    Wavelet Ridgelet Curvelet Contourlet Ripplet
    新技术革命思潮
    [ZZ] 边缘检测 梯度与Roberts、Prewitt、Sobel、Lapacian算子
    [ZZ] matlab中小波变换函数dwt2和wavedec2 系数提取函数appcoef2和detcoef2
    [综] 卷积的物理意义
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/10041759.html
Copyright © 2020-2023  润新知