Interview problem: Implement convex hull

Graham's scan

  • $\text{arctan2}(y,x)$ can be replaced by \begin{equation}\text{sign}(y)\,\left(1 - \frac{x}{|x|+|y|}\right)\end{equation} since they shared the same monotonicity, as proposed here.
  • Collinearity is tricky compared to monotone chain.
Here is my C++ implementation that is the solution of Leetcode 587:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
struct Point {
    int x;
    int y;
    double angle;
    int dist;
    bool operator < (const Point& other) {
        return angle < other.angle || 
            (angle == other.angle && dist < other.dist);
    }
};

class Solution {
public:
    vector<vector<int>> outerTrees(vector<vector<int>>& points) {
        const auto& min_iter = min_element(points.begin(), points.end(), 
            [](const vector<int>&a, const vector<int>& b){return a[1] < b[1];});
        int x, y, sx = (*min_iter)[0], sy = (*min_iter)[1];
        vector<vector<int>> result{{sx, sy}};
        vector<Point> cache;
        for (const auto& point : points) {
            if (point[0] != sx || point[1] != sy) {
                x = point[0] - sx;
                y = point[1] - sy;
                cache.push_back({x, y, peusdo_angle(x, y), dist(x, y)});
            }
        }
        sort(cache.begin(), cache.end());
        if (!cache.empty() &&cache.begin()->angle != cache.rbegin()->angle) {
            auto iter = cache.rbegin();
            double angle = iter -> angle;
            for (; iter != cache.rend() && iter -> angle == angle; ++iter) {}
            reverse(cache.rbegin(), iter);
        }
        for (const auto &point : cache) {
            x = point.x + sx;
            y = point.y + sy;
            while (clockwise(x, y, result)) {
                result.pop_back();
            }
            result.push_back({x, y});
        }
        return result;
    }
        
    inline double peusdo_angle(int x, int y) {
        return copysign(1.0 - x * 1.0 /(abs(x) + abs(y)) , y);
    }
    
    inline int dist(int x, int y) {
        return x * x + y * y;
    }
    
    bool clockwise(int x, int y, const vector<vector<int>>& result) {
        int n = result.size();
        if (n < 2) {
            return false;
        }
        int x1 = x - result[n-1][0];
        int y1 = y - result[n-1][1];
        int x2 = result[n-1][0] - result[n-2][0];
        int y2 = result[n-1][1] - result[n-2][1];
        return x1 * y2 - x2 * y1 > 0;
    }
};

Andrew's monotone chain


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
    vector<vector<int>> outerTrees(vector<vector<int>>& points) {
        auto compare = [](const vector<int>& a, const vector<int>& b) {
            return a[0] < b[0] || (a[0] == b[0] && a[1] < b[1]);  
        };
        sort(points.begin(), points.end(), compare);
        vector<vector<int>> result;
        for (auto iter = points.begin(); iter != points.end(); ++iter) {
            while (clockwise(*iter, result)) {
                result.pop_back();
            }
            result.push_back(*iter);
        }
        if (result.size() == points.size()) {
            return result;
        }
        for (auto iter=points.rbegin() + 1; iter != points.rend(); ++iter) {
            while (clockwise(*iter, result)) {
                result.pop_back();
            }
            result.push_back(*iter);
        }
        result.pop_back();
        return result;
    }
        
    bool clockwise(const vector<int>& point, const vector<vector<int>>& result) {
        int n = result.size();
        if (n < 2) {
            return false;
        }
        int x1 = point[0] - result[n-1][0];
        int y1 = point[1] - result[n-1][1];
        int x2 = result[n-1][0] - result[n-2][0];
        int y2 = result[n-1][1] - result[n-2][1];
        return x1 * y2 - x2 * y1 > 0;
    }
};

Comments

Popular posts from this blog

529 Plan

How to offset W2 tax

Retirement Accounts