{"id":9209,"date":"2021-12-23T13:15:32","date_gmt":"2021-12-23T21:15:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9209"},"modified":"2021-12-23T13:21:33","modified_gmt":"2021-12-23T21:21:33","slug":"leetcode-1232-check-if-it-is-a-straight-line","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-1232-check-if-it-is-a-straight-line\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1232. Check If It Is a Straight Line"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>coordinates<\/code>,&nbsp;<code>coordinates[i] = [x, y]<\/code>, where&nbsp;<code>[x, y]<\/code>&nbsp;represents the coordinate of a point. Check if these points&nbsp;make a straight line in the XY plane.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/10\/15\/untitled-diagram-2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/10\/09\/untitled-diagram-1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;=&nbsp;coordinates.length &lt;= 1000<\/code><\/li><li><code>coordinates[i].length == 2<\/code><\/li><li><code>-10^4 &lt;=&nbsp;coordinates[i][0],&nbsp;coordinates[i][1] &lt;= 10^4<\/code><\/li><li><code>coordinates<\/code>&nbsp;contains no duplicate point.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Slope and Hashset<\/strong><\/h2>\n\n\n\n<p>This is not a easy problem, a few corner cases:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>dx == 0<\/li><li>dy == 0<\/li><li>dx &lt; 0<\/li><\/ul>\n\n\n\n<p>Basically we are counting (dx \/ gcd(dx, dy), dy \/ gcd(dx, dy)). We will have only ONE entry if all the points are on the same line.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1) w\/ early exit.<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool checkStraightLine(vector<vector<int>>& coordinates) {\n    set<pair<int, int>> s;\n    for (int i = 1; i < coordinates.size(); ++i) {      \n      int dx = coordinates[i][0] - coordinates[0][0];\n      int dy = coordinates[i][1] - coordinates[0][1];      \n      if (dy == 0)\n        s.emplace(1, 0);\n      else if (dx == 0)\n        s.emplace(0, 1);\n      else {\n        if (dx < 0) dx *= -1, dy *= -1;\n        const int d = gcd(dx, dy);\n        s.emplace(dx \/ d, dy \/ d);\n      }\n      if (s.size() > 1) return false;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;coordinates,&nbsp;coordinates[i] = [x, y], where&nbsp;[x, y]&nbsp;represents the coordinate of a point. Check if these points&nbsp;make a straight line in the XY&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[748,284,82],"class_list":["post-9209","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-eary","tag-geometry","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9209"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9209\/revisions"}],"predecessor-version":[{"id":9212,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9209\/revisions\/9212"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}