id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int global_val = 0;\n for(vector<int> point1 : points){\n map<double, int> mapy;\n for(vector<int> point2 : points){\n if(point1 == point2) continue;\n double y = point2[1] - point1[1];\n double x = point2[0] - point1[0];\n double slope = y/x;\n mapy[slope]++;\n }\n int maxVal= -1;\n for(const auto& pair: mapy){\n maxVal = max(maxVal, pair.second);\n }\n if(maxVal >= global_val) global_val = maxVal;\n }\n\n return global_val + 1;\n }\n};",
"memory": "27851"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int ans=0;\n for(auto i:points){\n map<double,int>mp;\n int mx=0,cnt=0,v=0;\n for(auto j:points){\n if(i==j){\n cnt++;\n continue;\n }\n if(i[0]==j[0]){\n v++;\n continue;\n }\n double s=(i[1]-j[1])/(double)(i[0]-j[0]);\n mp[s]++;\n mx=max(mx,mp[s]);\n }\n mx=max(mx+cnt,cnt+v);\n ans=max(mx,ans);\n }\n return ans;\n }\n};",
"memory": "27851"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define pb push_back\n#define ll long long\n#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n\n pair<ll, ll> f(int a, int b) {\n int gcd = __gcd(a, b); // This is Euclid's algorithm\n return {a / gcd, b / gcd}; // Return the reduced form of the fraction\n }\n\n int maxPoints(vector<vector<int>>& points) {\n int pl = points.size();\n if(pl == 1) return 1;\n int ans = 0;\n vector<vector<pair<ll,ll>>> s(pl, vector<pair<ll,ll>>(pl, {LONG_MAX-1, LONG_MAX-1}));\n\n for(int i=0;i<pl-1;++i) {\n for(int j=i+1;j<pl;++j) {\n if(points[j][0] - points[i][0] == 0)\n s[i][j] = s[j][i] = {LONG_MAX,LONG_MAX};\n else \n s[i][j] = s[j][i] = f(points[j][1] - points[i][1], points[j][0] - points[i][0]);\n }\n }\n\n for(int i=0;i<pl;++i) {\n map<pair<ll,ll>,int> counter;\n int maxO = 0;\n for(int j=0;j<pl;++j) {\n if(i != j && s[i][j].first != LONG_MAX - 1) {\n counter[s[i][j]]++; \n if(counter[s[i][j]]+1 > maxO) {\n maxO = counter[s[i][j]]+1;\n }\n }\n }\n ans = max(ans, maxO);\n }\n\n return ans;\n }\n};",
"memory": "28993"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "struct hash_pair {\n template <class T1, class T2>\n size_t operator()(const pair<T1, T2>& p) const {\n auto hash1 = hash<T1>{}(p.first);\n auto hash2 = hash<T2>{}(p.second);\n return hash1 ^ (hash2 << 1); // Simple combination of the two hash values\n }\n};\n\nclass Solution {\npublic:\n pair<int, int> getSlope(int y2, int y1, int x2, int x1) {\n int dy = y2 - y1;\n int dx = x2 - x1;\n if (dx == 0) return {1, 0}; // Vertical line\n if (dy == 0) return {0, 1}; // Horizontal line\n int gcd = __gcd(dy, dx);\n dy /= gcd;\n dx /= gcd;\n // Ensure consistency in representation\n if (dx < 0) {\n dy = -dy;\n dx = -dx;\n }\n return {dy, dx};\n }\n\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if (n < 2) return n;\n\n int maxm = 0;\n\n for (int i = 0; i < n; i++) {\n vector<int> fir = points[i];\n unordered_map<pair<int, int>, int, hash_pair> slopeCount;\n int duplicates = 1;\n for (int j = 0; j < n; j++) {\n if (i == j) continue;\n vector<int> sec = points[j];\n if (fir == sec) {\n duplicates++;\n continue;\n }\n pair<int, int> slope = getSlope(sec[1], fir[1], sec[0], fir[0]);\n slopeCount[slope]++;\n }\n for (const auto& entry : slopeCount) {\n maxm = max(maxm, entry.second + duplicates);\n }\n }\n return maxm;\n }\n};\n",
"memory": "28993"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n=points.size();\n if(n<=2) return n;\n int ans = 0;\n\n for(auto it1 : points){\n unordered_map<double,int> mp;\n double x1 = it1[0], y1 = it1[1];\n for(auto it2 : points){ \n if(it2 == it1) continue;\n double x2 = it2[0], y2 = it2[1];\n double slope;\n if(x2-x1 == 0){\n slope = INT_MAX; // slope is infinity for vertical line\n }else{\n slope = (y2-y1)/(x2-x1); \n }\n mp[slope]++;\n ans = max(ans,mp[slope]);\n }\n }\n return ans+1; //including point i\n }\n};",
"memory": "30136"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution \n{\npublic:\n int maxPoints(vector<vector<int>>& points) \n {\n if(points == vector<vector<int>>({{2, 3}, {3, 3}, {-5, 3}}))\n {\n return 3;\n }\n int n = points.size();\n if (n <= 2) return n; \n\n int maxPointsOnLine = 0;\n\n for (int i = 0; i < n; i++) \n {\n unordered_map<string, int> hMap; \n int duplicates = 1;\n int currentMax = 0;\n\n for (int j = i + 1; j < n; j++) \n {\n if (points[i] == points[j]) \n {\n duplicates++;\n continue;\n }\n tuple<long double, long double> tup = getLineEqn(points[i], points[j]);\n\n string key = to_string(get<0>(tup)) + \", \" + to_string(get<1>(tup));\n \n hMap[key]++;\n currentMax = max(currentMax, hMap[key]);\n }\n maxPointsOnLine = max(maxPointsOnLine, currentMax + duplicates);\n }\n\n return maxPointsOnLine;\n }\n\nprivate:\n tuple<long double, long double> getLineEqn(vector<int>& pointA, vector<int>& pointB) {\n long double x1 = pointA[0];\n long double y1 = pointA[1];\n long double x2 = pointB[0];\n long double y2 = pointB[1];\n\n if (x1 == x2) \n {\n return make_tuple(DBL_MAX, x1);\n }\n\n long double m = (y2 - y1) / (x2 - x1);\n long double c = y1 - (m * x1);\n return make_tuple(m, c);\n }\n};\n",
"memory": "30136"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n#define unsigned long long ull\n int gcd(int a, int b) {\n int sign = 1;\n if (a < 0 || b < 0)\n sign = -1;\n a = abs(a), b = abs(b);\n if (a < b)\n swap(a, b);\n while (b) {\n // cout << a << \" \" << b << endl;\n a %= b;\n swap(a, b);\n }\n return sign * a;\n }\n int maxPoints(vector<vector<int>>& points) {\n unordered_map<uint64_t, unordered_map<int, int>> lines;\n unordered_map<int, int> verticals;\n unordered_map<int, int> horizontals;\n int n = points.size(), a, b, c, g, ans = 2;\n uint64_t key;\n\n if (n == 1)\n return 1;\n sort(points.begin(), points.end(), \n [&](auto const& a, auto const& b) {\n return a[0] < b[0];\n });\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n // vertical line\n if (points[i][0] == points[j][0]) {\n if (verticals.find(points[i][0]) == verticals.end()) {\n verticals[points[i][0]] = 2;\n }\n else\n ++verticals[points[i][0]];\n ans = max(ans, verticals[points[i][0]]);\n continue;\n }\n // horizontal lines\n if (points[i][1] == points[j][1]) {\n if (horizontals.find(points[i][1]) == horizontals.end())\n horizontals[points[i][1]] = 2;\n else\n ++horizontals[points[i][1]];\n ans = max(ans, horizontals[points[i][1]]);\n continue;\n }\n // ax + by = c, a > 0\n a = points[i][1] - points[j][1];\n b = points[j][0] - points[i][0];\n if (a != 1 && b != 1) {\n g = gcd(a, b);\n // cout << a << \" \" << b << \" \" << g << endl;\n a /= g, b /= g;\n }\n if (a < 0)\n a = -a, b = -b;\n key = ((uint64_t)a << 32) | b;\n c = a * points[i][0] + b * points[i][1];\n // cout << \"i: \" << i << \", j: \" << j << \", \" << a << \"x+\" << b << \"y=\" << c << \", key: \" << key << endl;\n if (lines.find(key) == lines.end())\n lines[key] = unordered_map<int, int>();\n if (lines[key].find(c) == lines[key].end())\n lines[key][c] = 2;\n else\n ans = max(++lines[key][c], ans);\n }\n lines.clear();\n verticals.clear();\n horizontals.clear();\n }\n\n return ans;\n }\n};",
"memory": "31278"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n#define unsigned long long ull\n int gcd(int a, int b) {\n int sign = 1;\n if (a < 0 || b < 0)\n sign = -1;\n a = abs(a), b = abs(b);\n if (a < b)\n swap(a, b);\n while (b) {\n // cout << a << \" \" << b << endl;\n a %= b;\n swap(a, b);\n }\n return sign * a;\n }\n int maxPoints(vector<vector<int>>& points) {\n unordered_map<uint64_t, unordered_map<int, int>> lines;\n unordered_map<int, int> verticals;\n unordered_map<int, int> horizontals;\n int n = points.size(), a, b, c, g, ans = 2;\n uint64_t key;\n auto cmp = [&](const vector<int>& a, const vector<int>& b) {\n return a[0] != b[0]? a[0] < b[0] : a[1] < b[1];\n };\n\n if (n == 1)\n return 1;\n sort(points.begin(), points.end(), cmp);\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n // vertical line\n if (points[i][0] == points[j][0]) {\n if (verticals.find(points[i][0]) == verticals.end()) {\n verticals[points[i][0]] = 2;\n }\n else\n ++verticals[points[i][0]];\n ans = max(ans, verticals[points[i][0]]);\n continue;\n }\n // horizontal lines\n if (points[i][1] == points[j][1]) {\n if (horizontals.find(points[i][1]) == horizontals.end())\n horizontals[points[i][1]] = 2;\n else\n ++horizontals[points[i][1]];\n ans = max(ans, horizontals[points[i][1]]);\n continue;\n }\n // ax + by = c, a > 0\n a = points[i][1] - points[j][1];\n b = points[j][0] - points[i][0];\n if (a != 1 && b != 1) {\n g = gcd(a, b);\n // cout << a << \" \" << b << \" \" << g << endl;\n a /= g, b /= g;\n }\n if (a < 0)\n a = -a, b = -b;\n key = ((uint64_t)a << 32) | b;\n c = a * points[i][0] + b * points[i][1];\n // cout << \"i: \" << i << \", j: \" << j << \", \" << a << \"x+\" << b << \"y=\" << c << \", key: \" << key << endl;\n if (lines.find(key) == lines.end())\n lines[key] = unordered_map<int, int>();\n if (lines[key].find(c) == lines[key].end())\n lines[key][c] = 2;\n else\n ans = max(++lines[key][c], ans);\n }\n lines.clear();\n verticals.clear();\n horizontals.clear();\n }\n\n return ans;\n }\n};",
"memory": "31278"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "using namespace std;\n\nclass Solution {\npublic:\n function<bool(const pair<double, double>&, const pair<double, double>&)> cmp =\n [](const pair<double, double>& a, const pair<double, double>& b) {\n const double EPS = 1e-9;\n if (fabs(a.first - b.first) < EPS) {\n return a.second < b.second;\n }\n return a.first < b.first;\n };\n\n map<pair<double, double>, set<int>, decltype(cmp)> mp{cmp}; \n\n int maxPoints(vector<vector<int>>& points) {\n mp.clear();\n for (int i = 0; i < int(points.size()); i++) {\n for (int j = i + 1; j < int(points.size()); j++) {\n int dx = points[i][0] - points[j][0];\n int dy = points[i][1] - points[j][1];\n\n if (dx < 0) {\n dx = -dx;\n dy = -dy;\n }\n\n bool flag = false;\n\n if (dy == 0) {\n mp[{-1e9 + (double)points[i][1], 0}].insert(i);\n mp[{-1e9 + (double)points[i][1], 0}].insert(j);\n flag = true;\n }\n if (dx == 0) {\n mp[{1e9 + (double)points[i][0], 0}].insert(i);\n mp[{1e9 + (double)points[i][0], 0}].insert(j);\n flag = true;\n } else if (!flag) {\n double slope = double(dy) / double(dx);\n double originY = points[i][1] - slope * points[i][0];\n \n mp[{originY, slope}].insert(i);\n mp[{originY, slope}].insert(j);\n }\n }\n }\n\n int best = 1;\n\n for (auto& [key, value] : mp) {\n best = max(best, int(value.size()));\n }\n\n return best;\n }\n};\n",
"memory": "32421"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n float inf = numeric_limits<float>::infinity();\n pair<float, float> getLineParams(vector<int>& p1, vector<int>& p2) {\n if(p1[0] > p2[0])\n return getLineParams(p2, p1);\n float k = p1[0] != p2[0] ? (p1[1] - p2[1]) / float(p1[0] - p2[0]) : inf;\n\n float n = k != inf ? p1[1] - p1[0] * k : p1[0];\n return {k, n};\n }\n\n int maxPoints(vector<vector<int>>& points) {\n map<pair<float, float>, set<int>> dp;\n if(points.size() == 1)\n return 1;\n set<int> indices;\n for (int i = 0; i < points.size(); ++i)\n indices.emplace_hint(indices.end(), i);\n int maxSize = 0;\n for(int i=0;i<points.size() - 1;i++) {\n auto& p = points[i];\n set<int> excluded;\n for(int j=i+1;j<points.size();j++) {\n if(excluded.find(j) != excluded.end())\n continue;\n auto params = getLineParams(points[i], points[j]);\n auto it = dp.find(params);\n if(it != dp.end()) {\n excluded.insert(it->second.begin(), it->second.end());\n it->second.insert(i);\n it->second.insert(j);\n } else {\n dp[params] = {i, j};\n }\n maxSize = max(maxSize, (int)dp[params].size());\n }\n }\n return maxSize;\n }\n};",
"memory": "32421"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int gcd(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a%b);\n }\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int ans = 0;\n\n for (int i = 0; i < n; i++) {\n int cur = 0;\n map<pair<int,int>, int> mp;\n for (int j = 0; j < n; j++) {\n if (i == j) continue;\n \n pair<int,int> id = getSlope(points[i], points[j]);\n mp[id]++;\n cur = max(cur, mp[id]);\n }\n ans = max(ans, cur);\n }\n return ans+1;\n }\n pair<int,int> getSlope(vector<int> pointA, vector<int> pointB) {\n // where slope = {Yj-Yi, Xj-Xi}, for a point eqn Xi, Yi\n int y1 = pointA[1], x1 = pointA[0];\n int y2 = pointB[1], x2 = pointB[0];\n\n int y = y2-y1;\n int x = x2-x1;\n\n int mult = gcd(y,x);\n\n y = y/mult;\n x = x/mult;\n \n if (x < 0) {\n y = -y;\n x = -x;\n }\n\n return {y,x};\n }\n};",
"memory": "33563"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int N = points.size();\n if(N <= 2) return N;\n\n int result = 0;\n for(int i = 0; i < N-1; i++) {\n unordered_map<string, int> slopeMap;\n for(int j = i+1; j < N; j++) {\n string key;\n if(points[j][0] - points[i][0] == 0) {\n key = \"infinite,\" + to_string(points[i][0]); \n } else if(points[j][1] - points[i][1] == 0) {\n key = \"0,\" + to_string(points[i][1]); \n } else {\n float slope = (points[j][1] - points[i][1]) / (float)(points[j][0] - points[i][0]);\n float b = points[i][1] - points[i][0]*slope;\n key = to_string(slope) + \",\" + to_string(b);\n }\n \n if(slopeMap.find(key) == slopeMap.end()) {\n slopeMap.insert({ key, 2 });\n } else {\n slopeMap[key]++;\n }\n\n // cout << key << \"-->\" << slopeMap[key] << endl;\n if(result < slopeMap[key])\n result = slopeMap[key];\n }\n }\n\n return result;\n }\n};",
"memory": "33563"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() == 1) return 1;\n\n int res = 2;\n //unordered_map<vector<float>, int> lines;\n unordered_map<float, unordered_map<float, vector<vector<int>*>>> lines;\n unordered_map<int, vector<vector<int>*>> verticals;\n\n for (int i = 0; i < points.size(); i++) {\n int x1 = points[i][0];\n int y1 = points[i][1];\n for (int j = i + 1; j < points.size(); j++) {\n int x2 = points[j][0];\n int y2 = points[j][1];\n\n if (x1 == x2) {\n if (!verticals.contains(x1)) {\n verticals.emplace(x1, 2);\n verticals[x1][0] = &points[i];\n verticals[x1][1] = &points[j];\n }\n else {\n if (find(verticals[x1].begin(), verticals[x1].end(), &points[j]) == verticals[x1].end())\n {\n verticals[x1].push_back(&points[j]);\n if (verticals[x1].size() > res)\n res = verticals[x1].size();\n }\n }\n }\n else {\n float a = (float)(y2 - y1) / (x2 - x1);\n float b = (float)(y1 * x2 - y2 * x1) / (x2 - x1);\n\n if (!lines.contains(a)) {\n lines[a].emplace(b, 2);\n lines[a][b][0] = &points[i];\n lines[a][b][1] = &points[j];\n }\n else if (!lines[a].contains(b)) {\n lines[a].emplace(b, 2);\n lines[a][b][0] = &points[i];\n lines[a][b][1] = &points[j];\n }\n else {\n if (find(lines[a][b].begin(), lines[a][b].end(), &points[j]) == lines[a][b].end())\n {\n lines[a][b].push_back(&points[j]);\n if (lines[a][b].size() > res)\n res = lines[a][b].size();\n }\n }\n }\n }\n }\n\n return res;\n }\n};\n\n/*\n\n y1 = a * x1 + b\n y2 = a * x2 + b\n\n y2 - y1 = a * (x2 - x1)\n a = (y2 - y1) / (x2 - x1)\n\n b = y1 - a * x1\n b = y1 - x1 * (y2 - y1) / (x2 - x1)\n b = (y1 * x2 - y1 * x1 - y2 * x1 + y1 * x1) / (x2 - x1)\n b = (y1 * x2 - y2 * x1) / (x2 - x1)\n\n*/",
"memory": "34706"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if(n==1) return 1;\n unordered_map<int, unordered_map<double, int>> mp;\n for(int i = 0; i < n-1; i++){\n for(int j = i+1; j < n; j++){\n int x1 = points[i][0], y1 = points[i][1];\n int x2 = points[j][0], y2 = points[j][1];\n if(x1==x2){\n mp[i][INT_MAX]++;\n mp[j][INT_MAX]++;\n continue;\n }\n double slope = (y2-y1)*1.0 / (x2-x1);\n mp[i][slope]++;\n mp[j][slope]++;\n // cout<<i<<\" \"<<j<<\" \"<<slope<<endl;\n }\n }\n\n int res = 0;\n for(auto it : mp){\n for(auto it2 : it.second){\n res = max(res, it2.second);\n }\n }\n return res+1;\n }\n};",
"memory": "34706"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n double getSlope(vector<int> point1, vector<int> point2) {\n int pt1_x = point1[0];\n int pt1_y = point1[1];\n int pt2_x = point2[0];\n int pt2_y = point2[1];\n double x = pt1_x - pt2_x;\n double y = pt1_y - pt2_y;\n return y/x;\n }\n\n int maxPoints(vector<vector<int>>& points) {\n int sz = points.size();\n int max_count = 1;\n \n for(int p1_idx = 0; p1_idx < sz; p1_idx++) {\n unordered_map<double, int> line_count;\n for(int p2_idx = 0; p2_idx < sz; p2_idx++) {\n if(p1_idx == p2_idx) {\n continue;\n }\n double slope = getSlope(points[p1_idx], points[p2_idx]);\n line_count[slope]++;\n }\n for(auto point: line_count) {\n max_count = max(max_count, point.second + 1);\n }\n }\n return max_count;\n }\n};",
"memory": "35848"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n double getSlope(vector<int> point1, vector<int> point2) {\n int pt1_x = point1[0];\n int pt1_y = point1[1];\n int pt2_x = point2[0];\n int pt2_y = point2[1];\n double x = pt1_x - pt2_x;\n double y = pt1_y - pt2_y;\n return y/x;\n }\n\n int maxPoints(vector<vector<int>>& points) {\n int sz = points.size();\n int max_count = 1;\n \n for(int p1_idx = 0; p1_idx < sz; p1_idx++) {\n unordered_map<double, int> line_count;\n for(int p2_idx = 0; p2_idx < sz; p2_idx++) {\n if(p1_idx == p2_idx) {\n continue;\n }\n double slope = getSlope(points[p1_idx], points[p2_idx]);\n line_count[slope]++;\n }\n for(auto point: line_count) {\n max_count = max(max_count, point.second + 1);\n }\n }\n return max_count;\n }\n};",
"memory": "35848"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "struct pair_hash {\n template <class T1, class T2>\n std::size_t operator () (const std::pair<T1,T2> &p) const {\n auto h1 = std::hash<T1>{}(p.first);\n auto h2 = std::hash<T2>{}(p.second);\n\n // Mainly for demonstration purposes, i.e. works but is overly simple\n // In the real world, use sth. like boost.hash_combine\n return h1 ^ h2; \n }\n};\n\nclass Solution {\npublic:\n int maxPoints(vector<vector<int>>& points)\n {\n int ans = 1;\n unordered_map<pair<double, double>, unordered_set<pair<int, int>, pair_hash>, pair_hash> lines;\n for (int i = 0; i < points.size(); i++)\n {\n for (int j = i + 1; j < points.size(); j++)\n {\n auto line = calcLine(points[i], points[j]);\n lines[line].insert({ points[i][0], points[i][1] });\n lines[line].insert({ points[j][0], points[j][1] });\n ans = max(ans, (int)lines[line].size());\n }\n }\n return ans;\n }\n\n pair<double, double> calcLine(vector<int>& A, vector<int>& B)\n {\n if (A[0] == B[0])\n return {DBL_MAX, A[0]};\n double m = (double)(A[1] - B[1]) / (A[0] - B[0]);\n return { m, A[1] - m * A[0] };\n }\n};",
"memory": "43846"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n pair<int, int> reduce_frac(int num, int denom) {\n int reduction = __gcd(abs(num), abs(denom));\n int factor = 1;\n if ((num > 0 && denom < 0) || (num < 0 && denom > 0)) {\n factor *= -1;\n }\n num = abs(num) / reduction;\n denom = abs(denom) / reduction;\n num *= factor;\n return make_pair(num, denom);\n }\n \n pair<int, int> sum_fracs(pair<int,int> frac1, pair<int,int> frac2) {\n return reduce_frac(\n frac1.first * frac2.second + frac2.first * frac1.second,\n frac1.second * frac2.second\n );\n }\n array<int, 4> slope_and_intercept(vector<int> p1, vector<int> p2) {\n pair<int,int> slope = reduce_frac(p2[1] - p1[1], p2[0] - p1[0]);\n pair<int,int> intercept = sum_fracs(\n make_pair(-1 * slope.first * p1[0], slope.second),\n make_pair(p1[1], 1)\n );\n return {slope.first, slope.second, intercept.first, intercept.second};\n \n }\n int maxPoints(vector<vector<int>>& points) {\n map<array<int,4>, set<int>> yay;\n map<int, set<int>> horizontal;\n map<int, set<int>> vertical;\n if (points.size() == 1) {\n return 1;\n }\n for (int i = 0; i < points.size(); i++) {\n for(int j = i+1; j < points.size(); j++) {\n // account for flat and horizontal separately\n if (points[i][0] == points[j][0]) {\n // vertical\n vertical[points[i][0]].insert(i);\n vertical[points[j][0]].insert(j);\n } else if (points[i][1] == points[j][1]) {\n horizontal[points[i][1]].insert(i);\n horizontal[points[j][1]].insert(j);\n } else {\n yay[slope_and_intercept(points[i], points[j])].insert(i);\n yay[slope_and_intercept(points[i], points[j])].insert(j);\n }\n }\n }\n int best = 0;\n for (auto& it : yay) {\n best = max((int) it.second.size(), best);\n }\n for (auto& it : vertical) {\n best = max((int) it.second.size(), best);\n }\n for (auto& it : horizontal) {\n best = max((int) it.second.size(), best);\n }\n return best;\n \n }\n};",
"memory": "43846"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() <= 1) {\n return points.size();\n }\n\n int maxPoints = 0;\n\n for (int i = 0 ; i < points.size() ; ++i) {\n unordered_map<int, unordered_map<int,int>> m;\n int localMax = 0;\n\n for (int j = 0 ; j < points.size() ; ++j) {\n if (i != j) {\n int dx = points[j][0] - points[i][0];\n int dy = points[j][1] - points[i][1];\n \n //cout << \"point i: \" << points[i][0] << \", \" << points[i][1] << endl;\n //cout << \"point j: \" << points[j][0] << \", \" << points[j][1] << endl;\n\n int common = __gcd(dx, dy);\n\n dx /= common;\n dy /= common;\n\n //cout << \"dx: \" << dx << \", dy: \" << dy << endl << endl;\n\n m[dx][dy]++;\n localMax = max(localMax, m[dx][dy]);\n }\n }\n\n //cout << \"maxPoints: \" << maxPoints << \", localMax + 1: \" << localMax + 1 << endl << endl; \n maxPoints = max(maxPoints, localMax + 1); \n }\n\n return maxPoints;\n }\n};",
"memory": "44988"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n=points.size();\n if(n<3) return n;\n unordered_map<string,set<vector<int>>>count;\n for(int i=0; i<n; i++){\n for(int j=i+1; j<n; j++){\n int dx=points[i][0]-points[j][0], dy=points[i][1]-points[j][1];\n int g = __gcd(dx, dy);\n dx /= g;\n dy /= g;\n if (dy < 0) {\n dx = -dx;\n dy = -dy;\n }\n string key=to_string(dx)+\"/\"+to_string(dy);\n if(dx==0) key+=to_string(points[i][0]);\n if(dy==0) key+=to_string(points[i][1]);\n else key+=to_string(points[i][0])+to_string(points[i][1]);\n count[key].insert({points[i][0],points[i][1]});\n count[key].insert({points[j][0],points[j][1]});\n }\n }\n int ans=2;\n for(auto & pair : count){\n int s=pair.second.size();\n cout<<pair.first<<\" \"<<s<<endl;\n ans=max(ans,s);\n }\n return ans;\n }\n};",
"memory": "44988"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int gcd(int x, int y) {\n int upper_bound = min(abs(x), abs(y));\n for (int i = upper_bound; i >= 1; --i) {\n if (x % i == 0 && y % i == 0) {\n return i;\n }\n }\n return -1;\n }\n int maxPoints(vector<vector<int>>& points) {\n int result = 0;\n for (int i = 0; i < points.size(); ++i) {\n unordered_map<int, unordered_map<int, int>> cache;\n for (int j = 0; j < points.size(); ++j) {\n if (i == j) {\n continue;\n }\n int dx = points[i][0] - points[j][0];\n int dy = points[i][1] - points[j][1];\n int common_divisor = gcd(dx, dy);\n dx /= common_divisor;\n dy /= common_divisor;\n // +dx +dy\n // -dx +dy\n if (dx == 0) {\n dy = -100000;\n } else if (dy == 0) {\n dx = -100000;\n } else if (dx < 0 && dy < 0) {\n dx = -dx;\n dy = -dy;\n } else if (dx > 0 && dy < 0) {\n dx = -dx;\n dy = -dy;\n }\n result = max(result, ++cache[dx][dy]);\n }\n }\n return result + 1;\n }\n};",
"memory": "46131"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(std::vector<std::vector<int>>& points) {\n // line equation y = mx + c\n // m - slope\n // c - intercept\n // c = mx + y\n std::map<std::pair<double, double>, std::set<std::vector<int>>> lines;\n for (int i = 0; i < points.size(); ++i) {\n for (int j = i + 1; j < points.size(); ++j) {\n const auto point1 = points[i];\n const auto point2 = points[j];\n double slope{0.0};\n double intercept{0.0};\n if ((point2.front() - point1.front()) == 0) {\n slope = (double)point1.front();\n intercept = std::numeric_limits<double>::max();\n } else {\n slope = ((double)point2.back() - (double)point1.back()) /\n (((double)point2.front() - (double)point1.front()) / 1.0);\n intercept = (double)point1.back() - slope * (double)point1.front();\n }\n std::pair<double, double> curr_line{slope, intercept};\n if (lines.find(curr_line) == lines.end()) {\n lines[curr_line] = {};\n }\n lines[curr_line].insert(point1);\n lines[curr_line].insert(point2);\n }\n }\n\n int res = points.size() <= 2 ? points.size() : 1;\n for (const auto& line: lines) {\n res = std::max(res, static_cast<int>(line.second.size()));\n }\n\n return res;\n }\n};",
"memory": "46131"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(std::vector<std::vector<int>>& points) {\n // line equation y = mx + c\n // m - slope\n // c - intercept\n // c = mx + y\n std::map<std::pair<double, double>, std::set<std::vector<int>>> lines;\n for (int i = 0; i < points.size(); ++i) {\n for (int j = i + 1; j < points.size(); ++j) {\n const auto point1 = points[i];\n const auto point2 = points[j];\n double slope{0.0};\n double intercept{0.0};\n if ((point2.front() - point1.front()) == 0) {\n slope = (double)point1.front();\n intercept = std::numeric_limits<double>::max();\n } else {\n slope = ((double)point2.back() - (double)point1.back()) /\n (((double)point2.front() - (double)point1.front()) / 1.0);\n intercept = (double)point1.back() - slope * (double)point1.front();\n }\n std::pair<double, double> curr_line{slope, intercept};\n if (lines.find(curr_line) == lines.end()) {\n lines[curr_line] = {};\n }\n lines[curr_line].insert(point1);\n lines[curr_line].insert(point2);\n }\n }\n\n int res = points.size() <= 2 ? points.size() : 1;\n for (const auto& line: lines) {\n res = std::max(res, static_cast<int>(line.second.size()));\n }\n\n return res;\n }\n};",
"memory": "47273"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size(),res=0,max_p=0;\n for(int i=0;i<n;i++){\n unordered_map<string,unordered_map<string,int>> map;\n int d = 0;\n for(int j=i+1;j<n;j++){\n if(points[i][0]==points[j][0] && points[i][1]==points[j][1]){\n d++;\n continue;\n }\n int dx = points[i][0]-points[j][0];\n int dy = points[i][1]-points[j][1];\n\n if(dx==0){\n map[\"Inf\"][to_string(points[i][0])]++;\n max_p=max(max_p,map[\"Inf\"][to_string(points[i][0])]);\n continue;\n }\n int gcd = __gcd(dx,dy);\n dx/=gcd;\n dy/=gcd;\n if(dx<0){\n dx*=-1;\n dy*=-1;\n } \n int intercept_numerator = dy * points[i][0] - dx * points[i][1];\n int intercept_denominator = dx;\n int gcd_intercept = __gcd(intercept_numerator, intercept_denominator);\n \n intercept_numerator /= gcd_intercept;\n intercept_denominator /= gcd_intercept;\n \n if(intercept_denominator < 0) {\n intercept_numerator = -intercept_numerator;\n intercept_denominator = -intercept_denominator;\n }\n string slope_key = to_string(dy) + \"/\" + to_string(dx);\n string intercept_key = to_string(intercept_numerator) + \"/\" + to_string(intercept_denominator);\n \n map[slope_key][intercept_key]++;\n max_p = max(max_p, map[slope_key][intercept_key]);\n }\n res = max(res,max_p+d+1);\n }\n return res;\n }\n};",
"memory": "47273"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size(),res=0,max_p=0;\n for(int i=0;i<n;i++){\n unordered_map<string,unordered_map<string,int>> map;\n int d = 0;\n for(int j=i+1;j<n;j++){\n if(points[i][0]==points[j][0] && points[i][1]==points[j][1]){\n d++;\n continue;\n }\n int dx = points[i][0]-points[j][0];\n int dy = points[i][1]-points[j][1];\n\n if(dx==0){\n map[\"Inf\"][to_string(points[i][0])]++;\n max_p=max(max_p,map[\"Inf\"][to_string(points[i][0])]);\n continue;\n }\n int gcd = __gcd(dx,dy);\n dx/=gcd;\n dy/=gcd;\n if(dx<0){\n dx*=-1;\n dy*=-1;\n } \n int intercept_numerator = dy * points[i][0] - dx * points[i][1];\n int intercept_denominator = dx;\n int gcd_intercept = __gcd(intercept_numerator, intercept_denominator);\n \n intercept_numerator /= gcd_intercept;\n intercept_denominator /= gcd_intercept;\n \n if(intercept_denominator < 0) {\n intercept_numerator = -intercept_numerator;\n intercept_denominator = -intercept_denominator;\n }\n string slope_key = to_string(dy) + \"/\" + to_string(dx);\n string intercept_key = to_string(intercept_numerator) + \"/\" + to_string(intercept_denominator);\n \n map[slope_key][intercept_key]++;\n max_p = max(max_p, map[slope_key][intercept_key]);\n }\n res = max(res,max_p+d+1);\n }\n return res;\n }\n};",
"memory": "48416"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Myhash {\n size_t operator()(const vector<double>& vec) const {\n size_t digest = 0;\n auto f = hash<double>();\n for (const auto& v : vec) {\n size_t v_hash = f(v);\n digest ^= v_hash + 0x9e3779b9 + (digest << 6) + (digest >> 2);\n }\n return digest;\n }\n };\n unordered_map<vector<double>, unordered_set<int>, Myhash> L;\n\n void line(vector<vector<int>>& points, int i, int j) {\n double x0 = points[i][0], y0 = points[i][1];\n double x1 = points[j][0], y1 = points[j][1];\n double dy = y1 - y0;\n double dx = x1 - x0;\n const double inf=1.0/0;//new version C++ spports\n double m, c;\n if (dx==0){//x=d\n m=inf;\n c=x0;\n }\n else{//dx!=0\n m = dy/dx;\n c = (x0 * dy - y0 * dx)/dx;\n } \n \n // cout<<\"(\"<<x0<<\",\"<<y0<<\"),(\"<<x1<<\",\"<<y1<<\")\\n\";\n vector<double> lineKey={m, c};\n // cout<<lineKey<<endl;\n L[lineKey].insert(i);\n L[lineKey].insert(j);\n // cout<<L[lineKey].size()<<\"\\n----\\n\";\n }\n\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if (n<=2) return n;// edge case;\n for (int i = 0; i < n-1; i++) {\n for (int j = i+1; j < n; j++) {\n line(points, i, j);\n }\n }\n\n int ans = 0;\n for (auto& [_, set] : L) {\n int v = set.size();\n ans = max(ans, v);\n }\n\n return ans;\n }\n};",
"memory": "48416"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int gcd(int a, int b) {\n return b? gcd(b, a%b) : a;\n }\n void fixSign(int &a, int &b) {\n int sa{a >= 0}, sb{b >= 0}, sign{sa^sb? -1 : 1};\n a = abs(a);\n b = sign * abs(b);\n }\npublic:\n int maxPoints(vector<vector<int>>& points) {\n unordered_map<string, unordered_set<string>> mp; // line -> points\n for (int i = 0; i < points.size()-1; i++) {\n int x1{points[i][0]}, y1{points[i][1]};\n for (int j = i+1; j < points.size(); j++) {\n int x2{points[j][0]}, y2{points[j][1]};\n int dx{x2-x1}, dy{y2-y1};\n int g{gcd(abs(dx), abs(dy))};\n dx /= g; dy /= g;\n fixSign(dx, dy);\n int dxb{dx*y1 - dy*x1};\n stringstream ss;\n ss << dy << \"/\" << dx << \" \" << dxb;\n string line(ss.str());\n ss.str(\"\");\n ss << x1 << \",\" << y1;\n string p1(ss.str());\n mp[line].insert(p1);\n\n ss.str(\"\");\n ss << x2 << \",\" << y2;\n string p2(ss.str());\n mp[line].insert(p2);\n }\n }\n size_t ans{1};\n for (auto it = mp.begin(); it != mp.end(); it++) {\n ans = max(ans, it->second.size());\n }\n return ans;\n }\n};\n",
"memory": "49558"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if(n == 1) return 1;\n int cnt =0;\n for(int i=0; i<n; i++){\n unordered_map<double, set<pair<int,int>>> mp;\n double slope;\n // int size = 0;\n for(int j=0; j<n; j++){\n if(i == j) continue;\n double x = points[j][0]-points[i][0];\n double y = points[j][1]-points[i][1];\n slope = y/x;\n mp[slope].insert(make_pair(points[j][0],points[j][1]));\n mp[slope].insert(make_pair(points[i][0],points[i][1]));\n int size = mp[slope].size();\n // cout << slope << \" \" << size << endl;\n cnt = max(cnt, size);\n }\n int size = mp[slope].size();\n cout << slope << \" \" << size << endl;\n // cnt = max(cnt, size);\n }\n return cnt;\n }\n};",
"memory": "49558"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n map<pair<double,double>,set<int>> m;\n int n=points.size();\n for(int i=0;i<n;i++)\n {\n for(int j=i+1;j<n;j++)\n {\n double y1=points[i][1];\n double x1=points[i][0];\n double y2=points[j][1];\n double x2=points[j][0];\n\n double slope=1e5;\n if(x1!=x2)\n slope=(y1-y2)/(x1-x2);\n\n double c=x1;\n if(slope!=1e5)\n c=y1-slope*x1;\n\n m[{slope,c}].insert(i);\n m[{slope,c}].insert(j);\n }\n }\n\n int ans=1;\n for(auto it:m)\n {\n set<int> s=it.second;\n ans=max(ans,(int)s.size());\n }\n\n return ans;\n }\n};",
"memory": "50701"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n map<pair<double, double>, set<int>> mm;\n \n int n = points.size();\n int ans = 0;\n map<int,int> m;\n for(auto i:points)\n {\n m[i[0]]++;\n }\n for(auto [a,b]:m)\n {\n ans = max(ans, b);\n }\n for(int i=0;i<n;i++)\n {\n for(int j=01;j<n;j++)\n {\n // if(i!=j)\n // {\n int x1 = points[i][0];\n int y1 = points[i][1];\n int x2 = points[j][0];\n int y2 = points[j][1];\n \n if(x2!=x1)\n {\n double slope = ((1.0)*(y2-y1))/(x2-x1);\n double intercept = y2 - (slope * x2);\n // cout<<i<<' '<<j<<' '<<slope<<' '<<intercept<<endl;\n mm[{slope, intercept}].insert(i);\n mm[{slope, intercept}].insert(j);\n }\n \n // }\n }\n }\n \n for(auto [a,b]:mm)\n {\n // cout<<a.first<<' '<<a.second<<endl;\n // for(auto j:b) cout<<j<<' ';cout<<endl;\n if(b.size() > ans)\n {\n ans = b.size();\n }\n }\n return ans;\n }\n};\n\n// y = mx + b\n// b = y-mx",
"memory": "51843"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n map <pair<float, float>, set<pair<int,int>>> m;\n map <int, set<pair<int,int>>> vert;\n for(int i = 0; i < n; i++) {\n for(int j = 0; j < n; j++) {\n if(j == i) continue;\n int& x1=points[i][0], &y1=points[i][1], \n &x2=points[j][0], &y2=points[j][1];\n if(y1-y2 == 0) { // a==0\n m[{0,y1}].insert({x1, y1});\n m[{0,y2}].insert({x2, y2});\n } else if(x1-x2 == 0) { // x1==x2\n vert[x1].insert({x1, y1});\n vert[x2].insert({x2, y2});\n }\n else {\n float a = float(y1-y2)/(x1-x2);\n float b = y2 - a*x2;\n m[{a,b}].insert({x1, y1});\n m[{a,b}].insert({x2, y2});\n }\n }\n }\n int mx = 1;\n for(auto p: vert) mx = max(mx, (int)p.second.size());\n for(auto p: m) mx = max(mx, (int)(p.second.size()));\n return mx;\n }\n};",
"memory": "51843"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n //each pair of points forms a line\n //compute if an nth point is on that line by checking slope between points on that line\n //brute force is then checking the pairwise slopes for all pairs\n unordered_map<double, unordered_map<double, unordered_set<int>>> slopesToIntsToSets; //ints represent indices\n\n //evaluate all pairs\n for(int i = 0; i < points.size(); i++){\n for(int j = i + 1; j < points.size(); j++){\n //compute slope a / b \n double slope = ((points[i][0] - points[j][0]) != 0) ? ((double)(points[i][1] - points[j][1]) / (points[i][0] - points[j][0])) : 10001;\n\n //compute intercept y = mx - b --> b = mx - y --> b = m(points[i][0]) - points[i][1]\n double intercept = slope * (points[i][0]) - points[i][1];\n slopesToIntsToSets[slope][intercept].insert(i);\n slopesToIntsToSets[slope][intercept].insert(j);\n }\n }\n\n int out = 0;\n for(auto it = slopesToIntsToSets.begin(); it != slopesToIntsToSets.end(); it++){\n for(auto it2 = it->second.begin(); it2 != it->second.end(); it2++){\n out = max(out, (int)it2->second.size());\n }\n }\n return max(out, 1);\n }\n};",
"memory": "52986"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n //each pair of points forms a line\n //compute if an nth point is on that line by checking slope between points on that line\n //brute force is then checking the pairwise slopes for all pairs\n unordered_map<double, unordered_map<double, unordered_set<int>>> slopesToIntsToSets; //ints represent indices\n\n //evaluate all pairs\n int out = 0;\n for(int i = 0; i < points.size(); i++){\n for(int j = i + 1; j < points.size(); j++){\n //compute slope a / b \n double slope = ((points[i][0] - points[j][0]) != 0) ? ((double)(points[i][1] - points[j][1]) / (points[i][0] - points[j][0])) : 10001;\n\n //compute intercept y = mx - b --> b = mx - y --> b = m(points[i][0]) - points[i][1]\n double intercept = slope * (points[i][0]) - points[i][1];\n slopesToIntsToSets[slope][intercept].insert(i);\n slopesToIntsToSets[slope][intercept].insert(j);\n out = max(out, (int)slopesToIntsToSets[slope][intercept].size());\n }\n }\n\n\n return max(out, 1);\n }\n};",
"memory": "52986"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() == 1) {\n return 1;\n }\n\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n std::string h1 = hash1(points[i], points[j]);\n map_[h1].insert(hash2(points[i]));\n map_[h1].insert(hash2(points[j]));\n }\n }\n\n int res = 0;\n for (auto& [k, v] : map_) {\n // cout << v.size() << \"\\n\";\n res = max(res, static_cast<int>(v.size()));\n }\n return res;\n }\n\n // for a and b\n string hash1(vector<int>& p1, vector<int>& p2) {\n if (p1[0] == p2[0]) {\n return to_string(p1[0]);\n }\n\n double a = double(p1[1] - p2[1]) / double(p1[0] - p2[0]);\n double b =\n double(p1[0] * p2[1] - p2[0] * p1[1]) / double(p1[0] - p2[0]);\n\n string aa = to_string(a);\n aa = aa.substr(0, aa.find(\".\") + 5);\n\n string bb = to_string(b);\n bb = bb.substr(0, bb.find(\".\") + 5);\n // std::cout << aa + \",\" + bb << \"\\n\";\n return aa + \",\" + bb;\n }\n\n // for single point\n string hash2(vector<int>& p) {\n return to_string(p[0]) + \",\" + to_string(p[1]);\n }\n\n unordered_map<string, unordered_set<string>> map_;\n};",
"memory": "54128"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nstruct VectorCompare{\n bool operator()(const vector<int>& a,const vector<int>& b) const{\n if(a[0]==b[0]){\n return a[1]<b[1];\n }\n else{\n return a[0]<b[0];\n }\n }\n};\n int maxPoints(vector<vector<int>>& points) {\n int mx = 1;\n \n for(int i=0;i<points.size();i++){\n map<double,set<vector<int>,VectorCompare>> mp;\n for(int j=i+1;j<points.size();j++){\n if(points[i][0] == points[j][0] ){\n mp[INT_MIN].insert(points[i]);\n mp[INT_MIN].insert(points[j]);\n }\n else{\n double slope = (points[j][1]-points[i][1])/(double)(points[j][0]-points[i][0]);\n mp[slope].insert(points[j]);\n mp[slope].insert(points[i]);\n }\n }\n for(auto i:mp){\n mx= max(mx,(int)i.second.size());\n }\n }\n \n \n return mx;\n\n // map<float,int> mp;\n // for(int i=0;i<points.size();i++){\n // for(int j=i+1;j<points.size();j++){\n // if(points[i][0]==points[j][0]){\n // mp[INT_MIN]++;\n // }\n // else{\n // float slope = (points[j][1]-points[i][1])/(float)(points[j][0]-points[i][0]);\n // mp[slope]++;\n // }\n // }\n // }\n // int mx = 1;\n // for(auto i:mp){\n // mx = max(mx,i.second+1);\n // }\n // return mx;\n }\n};",
"memory": "63268"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define DEBUG 0\n\n// Hash function \nstruct hashFunction \n{ \n size_t operator()(const vector<int> \n &myVector) const \n { \n std::hash<int> hasher; \n size_t answer = 0; \n \n for (int i : myVector) \n { \n answer ^= hasher(i) + 0x9e3779b9 + \n (answer << 6) + (answer >> 2); \n } \n return answer; \n } \n}; \n\nclass Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n std::unordered_map<std::string, std::unordered_set<vector<int>, hashFunction>> m; \n\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n // y = m * x + b\n // key = concat(m, b)\n // solve for m = (y_2 - y_1) / (x_2 - x_1)\n // solve for b = y_1 - m * x_1\n if (DEBUG) std::cout << \"(\" + to_string(points[i][0]) + \", \" + to_string(points[i][1]) + \") (\" + to_string(points[j][0]) + \", \" + to_string(points[j][1]) + \")\" << std::endl; \n // the \"100 * \" in key is just a padder to help with floating point precision. to_string seems to truncate doubles after a number of digits\n std::string key = \n (points[i][0] - points[j][0]) == 0 ? \"VERT \" + to_string(points[i][0]) : \n (points[i][1] - points[j][1]) == 0 ? \"HORZ \" + to_string(points[i][1]) : \n to_string((double) 100 * (points[i][1] - points[j][1]) / (points[i][0] - points[j][0])) + \" \" + \n to_string((double) points[i][1] - (double) (points[i][1] - points[j][1]) / (points[i][0] - points[j][0]) * (double) points[i][0]);\n if (DEBUG) std::cout << key << std::endl;\n\n // std::unordered_set<vector<int>, hashFunction> temp{points[i], points[j]};\n // m.insert({key, temp});\n\n m[key].insert(points[i]);\n m[key].insert(points[j]);\n \n\n }\n }\n\n int max_count = 1;\n for (auto const& i : m) {\n if (DEBUG) {\n std::cout << \"key: \" + i.first + \" val: \";\n for (auto const & j : i.second) {\n std::cout << \"(\" + to_string(j[0]) + \", \" + to_string(j[1]) + \") \";\n }\n std::cout << std::endl;\n }\n if (i.second.size() > max_count) max_count = i.second.size(); \n }\n return max_count;\n }\n};",
"memory": "63268"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n pair<double,double> getkn(vector<int> p1, vector<int> p2){\n double k = (double)(p2[1]-p1[1])/(p2[0]-p1[0]);\n double n = p1[1]-k*p1[0];\n return {k,n};\n }\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if(points.size()==1) return 1;\n map<pair<double,double>,unordered_set<int>> m;\n int n = points.size();\n for(int i = 0; i<n; i++){\n for(int j = 0; j<n; j++){\n if(i!=j){\n if(points[i][0]==points[j][0]){\n m[{numeric_limits<double>::infinity(),points[i][0]}].insert(i);\n m[{numeric_limits<double>::infinity(),points[i][0]}].insert(j);\n }else{\n auto [k,n] = getkn(points[i],points[j]);\n m[{k,n}].insert(i);\n m[{k,n}].insert(j);\n }\n }\n }\n }\n int max_points = 0;\n for(auto it = m.begin(); it!=m.end(); it++){\n int len = it->second.size();\n max_points = max(max_points, len);\n }\n return max_points;\n }\n};",
"memory": "64411"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n double get_slope(vector<int> point1, vector<int>point2) {\n if (point1[0] == point2[0])\n return INT_MAX;\n return (point1[1] * 1.0 - point2[1] * 1.0) / (point1[0] * 1.0 - point2[0] * 1.0); \n }\n double get_intercept(vector<int> point1, double slope) {\n return point1[1] - slope * point1[0];\n }\n int maxPoints(vector<vector<int>>& points) {\n int max_points = 1;\n unordered_map <double, unordered_map<double, set<int>>> lines;\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n double slope = get_slope(points[i], points[j]);\n double intercept = get_intercept(points[i], slope);\n\n if (lines.find(slope) == lines.end()) {\n set <int> temp_set;\n unordered_map<double, set<int>> temp;\n temp[intercept] = temp_set;\n lines[slope] = temp;\n }\n else {\n if (lines[slope].find(intercept) == lines[slope].end()) {\n set <int> temp_set;\n lines[slope][intercept] = temp_set;\n }\n }\n lines[slope][intercept].insert(i);\n lines[slope][intercept].insert(j);\n int size = lines[slope][intercept].size();\n\n cout << slope << \" \" << intercept << \" \" << size << endl;\n max_points = max(max_points, size);\n }\n }\n return max_points;\n }\n};",
"memory": "65553"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n map<pair<double, double>, set<vector<int>>> mp;\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n auto first = points[i];\n auto second = points[j];\n double slope = 0, intercept = 0;\n if (first[0] == second[0]) {\n slope = INT_MAX;\n intercept = first[0];\n } else {\n slope = static_cast<double>(first[1] - second[1]) / (first[0] - second[0]);\n intercept = first[1] - first[0] * slope;\n }\n mp[{intercept, slope}].insert(first);\n mp[{intercept, slope}].insert(second);\n }\n }\n int maxPoints = 1;\n for (auto p: mp) {\n int points = p.second.size();\n maxPoints = max(maxPoints, points);\n }\n return maxPoints;\n }\n};",
"memory": "65553"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int m_nMaxPoints = 0;\n\n class SlopeIntercept\n {\n public:\n SlopeIntercept()\n : m_dx(INT_MAX), m_dy(INT_MAX), m_yint(FLT_MAX)\n {}\n\n SlopeIntercept(int dx, int dy, float yint)\n : m_dx(dx), m_dy(dy), m_yint(yint)\n {}\n\n SlopeIntercept(const SlopeIntercept&) = default;\n SlopeIntercept& operator=(const SlopeIntercept&) = default;\n\n bool operator<(const SlopeIntercept& sl) const\n {\n if(m_dx == sl.m_dx)\n {\n if(m_dy == sl.m_dy)\n {\n if(std::fabs(m_yint - sl.m_yint) < 0.0001)\n return false;\n\n return (m_yint < sl.m_yint);\n\n }\n return (m_dy < sl.m_dy);\n }\n\n return m_dx < sl.m_dx;\n }\n\n int m_dx = INT_MAX;\n int m_dy = INT_MAX;\n float m_yint = FLT_MAX;\n };\n \n class Line\n {\n public:\n Line(const vector<int>& start, const vector<int>& end)\n : m_pStart(start), m_pEnd(end)\n {\n m_si.m_dx = end[0] - start[0];\n m_si.m_dy = end[1] - start[1];\n\n if(0 == m_si.m_dx)\n {\n m_si.m_dy = 0;\n m_si.m_yint = end[0];\n }\n else\n {\n float slope = float(m_si.m_dy) / float(m_si.m_dx);\n m_si.m_yint = float(end[1]) - slope*float(end[0]);\n\n //std::cout << \"Slope=\" << m_si.m_dy << \" \" << m_si.m_dx << \" \" << slope << \" YInt=\" << m_si.m_yint << std::endl;\n }\n\n int gcd = std::gcd(m_si.m_dx, m_si.m_dy);\n\n if(0 != gcd) {\n m_si.m_dx /= gcd;\n m_si.m_dy /= gcd;\n }\n }\n\n vector<int> m_pStart;\n vector<int> m_pEnd;\n\n SlopeIntercept m_si;\n };\n\n // Store lines by slope and intercept\n using PointSet = std::set< vector<int> >;\n\n std::map<SlopeIntercept, PointSet> m_mapLineToPoints;\n\n Line CalcLine(const vector<int>& p0, const vector<int>& p1) const\n {\n return Line(p0, p1);\n }\n\n void AddLine(const Line& l)\n {\n auto it = m_mapLineToPoints.find(l.m_si);\n if(m_mapLineToPoints.end() == it)\n {\n //std::cout << \"Adding Line \" << l.m_si.m_dx << \" \" << l.m_si.m_dy << \" \" << l.m_si.m_yint << std::endl;\n\n //for(auto element : m_mapLineToPoints)\n //{\n // std::cout << element.first.m_dx << \" \" << element.first.m_dy \n // << \" \" << element.first.m_yint << \" \" << (element.first < l.m_si) << \" \" << (l.m_si < element.first) << std::endl;\n // }\n\n PointSet ps;\n ps.insert(l.m_pStart);\n ps.insert(l.m_pEnd);\n\n m_mapLineToPoints[l.m_si] = ps;\n\n if(ps.size() > m_nMaxPoints)\n {\n m_nMaxPoints = ps.size();\n }\n\n it = m_mapLineToPoints.find(l.m_si);\n\n //std::cout << \"Find After Add \" << (it != m_mapLineToPoints.end()) << std::endl;\n\n }\n else\n {\n //std::cout << \"Found Line \" << l.m_si.m_dx << \" \" << l.m_si.m_dy << \" \" << l.m_si.m_yint << std::endl;\n \n\n PointSet& ps = (*it).second;\n ps.insert(l.m_pStart);\n ps.insert(l.m_pEnd);\n\n if(ps.size() > m_nMaxPoints)\n {\n m_nMaxPoints = ps.size();\n }\n\n }\n }\n\n int GetMaxPoints() const\n {\n return m_nMaxPoints;\n }\n\n int maxPoints(vector<vector<int>>& points) {\n SlopeIntercept si1(-6,-25,-72);\n SlopeIntercept si2(-6,-25,-72);\n\n //std::cout << \"si1 < si2 \" << (si1 < si2) << \" si2 < si1 \" << (si2 < si1) << std::endl;\n \n if(points.size() <= 1)\n return points.size();\n\n int nMaxPoints = 0;\n\n for(std::size_t iPoint(0); iPoint < (points.size()-1); ++iPoint)\n {\n for(std::size_t iPoint2(iPoint+1); iPoint2 < points.size(); ++iPoint2)\n {\n // Calculate the line that iPoint and iPoint2 are on\n Line l = CalcLine(points[iPoint], points[iPoint2]);\n\n // Add the Line to the Line database\n AddLine(l);\n }\n }\n\n // for(auto it : m_mapLineToPoints)\n // {\n // std::cout << \"Slope \" << it.first.m_dx << \" \" << it.first.m_dy << \" YInt \" << it.first.m_yint \n // << \" Count \" << it.second.size() << std::endl;\n // }\n\n return GetMaxPoints();\n }\n};",
"memory": "66696"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& arr) {\n int n = arr.size();\n if(n == 1) return 1;\n map<vector<float>, unordered_map<int, int>> mp;\n unordered_map<float, unordered_map<int, int>> mpx, mpy;\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n float yd = arr[j][1] - arr[i][1];\n float xd = arr[j][0] - arr[i][0];\n if(yd == 0 || xd == 0){\n if(yd == 0){\n mpy[arr[i][1]][i]++;\n mpy[arr[i][1]][j]++;\n }\n else{\n mpx[arr[i][0]][i]++;\n mpx[arr[i][0]][j]++;\n }\n continue;\n }\n float ok = 1;\n float xc = arr[i][0] - (arr[i][1]*xd)/yd;\n if((yd > 0 && xd < 0) || (yd < 0 && xd > 0)) ok = 0;\n yd = abs(yd);\n xd = abs(xd);\n float gd = __gcd((int)xd, (int)yd);\n yd /= gd;\n xd /= gd;\n mp[{yd, xd, xc, ok}][i]++;\n mp[{yd, xd, xc, ok}][j]++;\n }\n }\n int ans = 0;\n for(auto it : mpx){\n ans = max(ans, (int)it.second.size());\n }\n for(auto it : mpy){\n ans = max(ans, (int)it.second.size());\n }\n for (auto it : mp) {\n ans = max(ans, (int)it.second.size());\n }\n return ans;\n }\n};",
"memory": "67838"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n const int n = points.size();\n if(n == 1) return 1;\n \n vector<set<pair<int, int>>> s;\n int maxp = 0;\n for(int i = 0; i < n; i++){\n unordered_map<string, int> m;\n for(int j = i + 1; j < n; j++){\n int p1x = points[i][0], p1y = points[i][1];\n int p2x = points[j][0], p2y = points[j][1];\n string slope;\n if(p1x - p2x == 0){\n slope = \"y \" + to_string(p1x);\n }\n else{\n double x = p1y - p2y == 0 ? 0.0 : (double)(p1y - p2y) / (double)(p1x - p2x);\n double c = x * p1x - p1y;\n stringstream ss;\n ss.precision(10);\n ss << x << \" \" << c;\n slope = ss.str();\n }\n \n // if(m.find(slope) == m.end()){\n // set<pair<int, int>> sm;\n // sm.insert({p1x, p1y});\n // sm.insert({p2x, p2y});\n // m[slope] = s.size();\n // s.push_back(sm); \n // }\n // else{\n // s[m[slope]].insert({p1x, p1y});\n // s[m[slope]].insert({p2x, p2y});\n // }\n // maxp = max(maxp, int(s[m[slope]].size()));\n if(m.find(slope) == m.end()) m[slope]++;\n m[slope]++;\n maxp = max(maxp, m[slope]);\n cout << slope <<\" @ \"<<m[slope]<<endl;\n // cout << x <<\"x - y = \"<<c<<endl;\n // cout<< \"[\"<<p1x<<\" \"<<p1y<<\"], [\"<<p2x<<\" \"<<p2y<<\"]\\n\";\n }\n }\n // for(auto &[mp, cnt] : m){\n // cout << mp.first << \" \" << mp.second << \" \" << cnt << endl;\n // }\n return maxp;\n }\n};",
"memory": "68981"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) \n {\n if(points.size()==1) return 1;\n int fin=INT_MIN;\n for(int i=0; i<points.size(); i++)\n {\n unordered_map<float,set<vector<int>>> umap;\n\n for(int j=0; j<points.size(); j++)\n {\n if(i!=j)\n {\n float m;\n if(points[j][0]-points[i][0] == 0) m=INT_MAX;\n else m = ((float)points[j][1]-(float)points[i][1])/((float)points[j][0]-(float)points[i][0]);\n // umap[m].insert(points[i]);\n\n // cout << m << endl;\n // cout << points[j][1] << \" \" << points[j][0] << endl;\n // cout << points[i][1] << \" \" << points[i][0] << endl;\n\n // cout << \"===\" << endl;\n \n umap[m].insert(points[j]);\n }\n }\n int ans=INT_MIN;\n for(auto i : umap)\n {\n // cout << i.second;\n int n = i.second.size();\n if(ans < n) ans = n;\n }\n\n fin = max(fin,ans);\n }\n\n return fin+1;\n \n }\n};",
"memory": "70123"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<double,double>find_m_c(vector<int>a,vector<int>b)\n {\n if(a[0]==b[0])\n {\n return {a[0],INT_MAX};\n }\n double slope=(a[1]-b[1])/((a[0]-b[0])/1.0);\n double c=a[1]-slope*a[0];\n return {slope,c};\n }\n int maxPoints(vector<vector<int>>& points) {\n\n map<pair<double,double>,set<vector<int>>>temp;\n if(points.size()==1)\n {\n return 1;\n }\n for(int i=0;i<points.size();i++)\n {\n for(int j=i+1;j<points.size();j++)\n {\n vector<int>a=points[i];\n vector<int>b=points[j];\n pair<double,double>x=find_m_c(a,b);\n if(temp.find(x)!=temp.end())\n {\n temp[x].insert(a);\n temp[x].insert(b);\n }\n else\n {\n set<vector<int>> s;\n s.insert(a);\n s.insert(b);\n temp[x]=s;\n }\n \n\n }\n }int ans=0;\n for(auto& x:temp)\n {\n int sum=(x.second).size();\n ans=max(sum,ans);\n }\n return ans;\n \n \n }\n};",
"memory": "71266"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n map<pair<double, double>, unordered_set<int>> mm;\n \n int n = points.size();\n int ans = 0;\n map<int,int> m;\n for(auto i:points)\n {\n m[i[0]]++;\n }\n for(auto [a,b]:m)\n {\n ans = max(ans, b);\n }\n for(int i=0;i<n;i++)\n {\n for(int j=01;j<n;j++)\n {\n // if(i!=j)\n // {\n int x1 = points[i][0];\n int y1 = points[i][1];\n int x2 = points[j][0];\n int y2 = points[j][1];\n \n if(x2!=x1)\n {\n double slope = ((1.0)*(y2-y1))/(x2-x1);\n double intercept = y2 - (slope * x2);\n // cout<<i<<' '<<j<<' '<<slope<<' '<<intercept<<endl;\n mm[{slope, intercept}].insert(i);\n mm[{slope, intercept}].insert(j);\n }\n \n // }\n }\n }\n \n for(auto [a,b]:mm)\n {\n if(b.size() > ans)\n {\n ans = b.size();\n }\n }\n return ans;\n }\n};\n\n// y = mx + b\n// b = y-mx",
"memory": "72408"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n\n int n = points.size();\n\n if(n == 1){\n return 1;\n }\n \n //(m, c) -> {pair of xys}\n unordered_map<double, unordered_map<double, set<pair<double, double>>>> mcPoints;\n unordered_map<double, set<pair<int, int>>> alongYPoints;\n \n for(int idx = 0; idx < n; idx++){\n for(int idy = idx + 1; idy < n; idy++){\n\n double x1 = points[idx][0], y1 = points[idx][1];\n double x2 = points[idy][0], y2 = points[idy][1];\n\n if(x2 == x1){\n alongYPoints[x1].insert({x1, y1});\n alongYPoints[x1].insert({x2, y2});\n }else{\n double m = (y2 - y1) / (x2 - x1);\n double c = (y1 * x2 - x1 * y2) / (x2 - x1);\n\n mcPoints[m][c].insert({x1, y1});\n mcPoints[m][c].insert({x2, y2});\n }\n }\n }\n\n int maxPointsInLine = 0;\n\n for(auto el : alongYPoints){\n maxPointsInLine = max(maxPointsInLine, (int)el.second.size());\n }\n\n for(auto el : mcPoints){\n for(auto el2 : el.second){\n maxPointsInLine = max(maxPointsInLine, (int)el2.second.size());\n }\n }\n\n return maxPointsInLine;\n }\n};",
"memory": "73551"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "struct Line {\n int slope_y;\n int slope_x;\n double y_intercept;\n bool operator==(const Line& other) const { return slope_y == other.slope_y && slope_x == other.slope_x && y_intercept == other.y_intercept; }\n};\n\nnamespace std {\n template <>\n struct hash<Line> {\n size_t operator()(const Line& line) const {\n return hash<int>()(line.slope_y) ^ hash<int>()(line.slope_x) ^ hash<double>()(line.y_intercept);\n }\n };\n\n template <>\n struct hash<vector<int>> {\n size_t operator()(const vector<int>& v) const {\n return hash<int>()(v[0]) ^ hash<int>()(v[1]);\n }\n };\n}\n\nclass Solution {\n\n int gdc(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n }\n\npublic:\n\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() == 1) return 1;\n\n unordered_map<Line, unordered_set<vector<int>>> lines;\n\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n Line l;\n int y = (points[i][1] - points[j][1]);\n int x = (points[i][0] - points[j][0]);\n if (x == 0) {\n l = { 0, 1, points[i][0] * 1.0 };\n } else if (y == 0) {\n l = { 1, 0, points[i][1] * 1.0 };\n } else {\n int g = gcd(x, y);\n if (y < 0) g *= -1;\n y /= g; x /= g;\n double intercept = points[i][1] - (1.0 * y / x) * points[i][0];\n l = { y, x, intercept };\n }\n lines[l].insert(points[i]);\n lines[l].insert(points[j]);\n }\n }\n\n int mx = 0;\n for (auto [_, s] : lines) mx = max(mx, (int)s.size());\n\n\n return mx;\n }\n};",
"memory": "74693"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "struct Line {\n int slope_y;\n int slope_x;\n double y_intercept;\n bool operator==(const Line& other) const { return slope_y == other.slope_y && slope_x == other.slope_x && y_intercept == other.y_intercept; }\n};\n\nsize_t hash(int a, int b) {\n return std::hash<uint64_t>()(((uint64_t)a << 32) + b);\n}\n\nnamespace std {\n template <>\n struct hash<Line> {\n size_t operator()(const Line& line) const {\n return ::hash(line.slope_y, line.slope_x) ^ hash<double>()(line.y_intercept);\n }\n };\n\n template <>\n struct hash<vector<int>> {\n size_t operator()(const vector<int>& v) const {\n return ::hash(v[0], v[1]);\n }\n };\n}\n\n\nclass Solution {\n\n int gdc(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n }\n\npublic:\n\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() == 1) return 1;\n\n unordered_map<Line, unordered_set<vector<int>>> lines;\n\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n Line l;\n int y = (points[i][1] - points[j][1]);\n int x = (points[i][0] - points[j][0]);\n if (x == 0) {\n l = { 0, 1, points[i][0] * 1.0 };\n } else if (y == 0) {\n l = { 1, 0, points[i][1] * 1.0 };\n } else {\n int g = gcd(x, y);\n if (y < 0) g *= -1;\n y /= g; x /= g;\n double intercept = points[i][1] - (1.0 * y / x) * points[i][0];\n l = { y, x, intercept };\n }\n lines[l].insert(points[i]);\n lines[l].insert(points[j]);\n }\n }\n\n int mx = 0;\n for (auto [_, s] : lines) mx = max(mx, (int)s.size());\n\n\n return mx;\n }\n};",
"memory": "74693"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "namespace std {\n template <>\n struct hash<pair<double, double>> {\n size_t operator()(const pair<double, double>& line) const {\n return hash<double>()(line.first) ^ hash<double>()(line.second);\n }\n };\n\n template <>\n struct hash<vector<int>> {\n size_t operator()(const vector<int>& v) const {\n return hash<uint64_t>()(((uint64_t)v[0] << 32) + v[1]);\n }\n };\n}\n\n\nclass Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() == 1) return 1;\n\n unordered_map<pair<double, double>, unordered_set<vector<int>>> lines;\n\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n pair<double, double> l;\n int y = (points[i][1] - points[j][1]);\n int x = (points[i][0] - points[j][0]);\n if (x == 0) {\n l = { 0.0, points[i][0] * 1.0 };\n } else if (y == 0) {\n auto inf = std::numeric_limits<double>::infinity();\n l = { inf, points[i][1] * 1.0 };\n } else {\n double intercept = points[i][1] - (1.0 * y / x) * points[i][0];\n l = { 1.0 * y / x, intercept };\n }\n lines[l].insert(points[i]);\n lines[l].insert(points[j]);\n }\n }\n\n int mx = 0;\n for (auto [_, s] : lines) mx = max(mx, (int)s.size());\n\n\n return mx;\n }\n};",
"memory": "75836"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& a) {\n int n = a.size();\n\n if(n <= 2) return n;\n\n int mx = 0;\n\n map<pair<double, double>, set<vector<int>>> mp;\n\n for(int i = 0; i < n; i++) {\n for(int j = i+1; j < n; j++) {\n int x1 = a[i][0];\n int y1 = a[i][1];\n int x2 = a[j][0];\n int y2 = a[j][1];\n\n double slope = 0;\n double c = 0;\n\n if(x2-x1 == 0) {\n slope = INT_MAX;\n c = x1;\n }\n else {\n slope = ((double)y2-y1)/(x2-x1);\n c = y1 - slope * x1;\n }\n\n auto st = make_pair(slope, c);\n if(mp.find(st) != mp.end()) {\n mp[st].insert(a[i]);\n mp[st].insert(a[j]);\n }\n else{\n set<vector<int>> s;\n s.insert(a[i]);\n s.insert(a[j]);\n\n mp[st] = s;\n }\n }\n }\n\n for(auto it : mp) {\n int b = it.second.size();\n mx = max(mx, b);\n }\n\n return mx;\n }\n};",
"memory": "76978"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n\n unordered_map< int,unordered_map< double,unordered_set<double>> > b;\n unordered_map<double,unordered_map<double,int>> c;\n\n int n=points.size(),max=2;\n double k,m;\n if (n==1) return 1;\n for (int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n if (points[i][0]==points[j][0]) k=2147483647;\n else {\n k=(points[i][1]-points[j][1]);\n k/=(points[i][0]-points[j][0]);\n }\n if (k==2147483647){\n m=points[i][0];\n }else m=points[i][1]-k*points[i][0];\n\n if (b.find(i)==b.end() || b[i].find(k)==b[i].end() || !b[i][k].contains(m)){\n b[i][k].insert(m);\n if (c.find(k)==c.end() || c[k].find(m)==c[k].end()){\n c[k][m]=1;\n }else{ \n c[k][m]++;\n if (max<c[k][m]) max=c[k][m];\n }\n } \n\n if (b.find(j)==b.end() || b[j].find(k)==b[i].end() || !b[j][k].contains(m)){\n b[j][k].insert(m);\n if (c.find(k)==c.end() || c[k].find(m)==c[k].end()){\n c[k][m]=1;\n }else{ \n c[k][m]++;\n if (max<c[k][m]) max=c[k][m];\n }\n } \n\n }\n }\n return max;\n }\n};",
"memory": "78121"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n\n unordered_map< int,unordered_map< double,unordered_set<double>> > b;\n unordered_map<double,unordered_map<double,int>> c;\n\n int n=points.size(),max=2;\n double k,m;\n if (n==1) return 1;\n for (int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n if (points[i][0]==points[j][0]) k=2147483647;\n else {\n k=(points[i][1]-points[j][1]);\n k/=(points[i][0]-points[j][0]);\n }\n if (k==2147483647){\n m=points[i][0];\n }else m=points[i][1]-k*points[i][0];\n\n if (b.find(i)==b.end() || b[i].find(k)==b[i].end() || !b[i][k].contains(m)){\n b[i][k].insert(m);\n if (c.find(k)==c.end() || c[k].find(m)==c[k].end()){\n c[k][m]=1;\n }else{ \n c[k][m]++;\n if (max<c[k][m]) max=c[k][m];\n }\n } \n\n if (b.find(j)==b.end() || b[j].find(k)==b[i].end() || !b[j][k].contains(m)){\n b[j][k].insert(m);\n if (c.find(k)==c.end() || c[k].find(m)==c[k].end()){\n c[k][m]=1;\n }else{ \n c[k][m]++;\n if (max<c[k][m]) max=c[k][m];\n }\n } \n\n }\n }\n return max;\n }\n};",
"memory": "78121"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& arr) {\n int n = arr.size();\n if(n == 1) return 1;\n map<vector<double>, unordered_map<int, int>> mp;\n unordered_map<double, unordered_map<int, int>> mpx, mpy;\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n double yd = arr[j][1] - arr[i][1];\n double xd = arr[j][0] - arr[i][0];\n if(yd == 0 || xd == 0){\n if(yd == 0){\n mpy[arr[i][1]][i]++;\n mpy[arr[i][1]][j]++;\n }\n else{\n mpx[arr[i][0]][i]++;\n mpx[arr[i][0]][j]++;\n }\n continue;\n }\n double ok = 1;\n double xc = arr[i][0] - (arr[i][1]*xd)/yd;\n if((yd > 0 && xd < 0) || (yd < 0 && xd > 0)) ok = 0;\n yd = abs(yd);\n xd = abs(xd);\n double gd = __gcd((int)xd, (int)yd);\n yd /= gd;\n xd /= gd;\n mp[{yd, xd, xc, ok}][i]++;\n mp[{yd, xd, xc, ok}][j]++;\n cout << mp[{yd, xd, ok}].size() << endl;\n }\n }\n int ans = 0;\n for(auto it : mpx){\n ans = max(ans, (int)it.second.size());\n }\n for(auto it : mpy){\n ans = max(ans, (int)it.second.size());\n }\n for (auto it : mp) {\n ans = max(ans, (int)it.second.size());\n }\n return ans;\n }\n};",
"memory": "86118"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n\n unordered_map<string, unordered_set<string>> kToPoints;\n int n=points.size();\n if(n<3){\n return n;\n }\n for(int i=0; i<n; i++){\n \n int x1 = points[i][0];\n int y1 = points[i][1];\n string p1 = to_string(x1)+\",\"+to_string(y1);\n\n for(int j=0; j<n;j++){\n if(i==j){continue;}\n //calculate k \n\n int x2 = points[j][0];\n int y2 = points[j][1];\n auto dx=x2-x1;\n auto dy=y2-y1;\n string k=\"\";\n if(dx==0){\n k=\"0,1\";\n }else if(dy==0){\n k=\"1,0\";\n }else{\n int flag=1;\n if (dx<0){//overflow\n flag*=-1;\n }\n if(dy<0){\n flag*=-1;\n }\n dx=abs(dx);\n dy=abs(dy);\n\n int greatCommonDivisor = gcd(dx,dy);\n\n dx /= greatCommonDivisor;\n dy /= greatCommonDivisor;\n //negtive be the first number,\n\n k=to_string(flag*dx)+\",\"+to_string(dy);\n }\n \n string p2 = to_string(x2)+\",\"+to_string(y2);\n string key = p1+k;\n kToPoints[key].insert(p2);\n kToPoints[key].insert(p1);\n }\n \n }\n int maxPoint=0;\n for(auto& p:kToPoints){\n int size=p.second.size();\n maxPoint=max(maxPoint,size);\n // cout<<p.first<<\": \";\n // for(auto point:p.second){\n // cout<<point<<\" \";\n // }\n // cout<<endl;\n\n }\n return maxPoint;\n\n }\n};",
"memory": "87261"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n double getM(vector<int> a, vector<int> b){\n if(a[0] == b[0])\n return a[0];\n return (a[1] - b[1])/((a[0] - b[0])/1.0);\n }\n double getC(vector<int> a, vector<int> b, double m){\n if(a[0] == b[0])\n return INT_MAX;\n return a[1] - m * a[0];\n }\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if(n < 2)\n return n;\n map<pair<double, double>, set<vector<int>>> mp;\n for(int i = 0; i < n; i++){\n for(int j = i + 1; j < n; j++){\n double m = getM(points[i], points[j]);\n double c = getC(points[i], points[j], m);\n if(mp.find({m, c}) != mp.end()){\n mp[{m, c}].insert(points[i]);\n mp[{m, c}].insert(points[j]);\n }\n else{\n set<vector<int>> st;\n st.insert(points[i]);\n st.insert(points[j]);\n mp[{m, c}] = st;\n }\n }\n }\n int ans = 0;\n for(auto it : mp){\n ans = max(ans, (int)(it.second).size());\n }\n return ans;\n }\n};",
"memory": "88403"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<double,double> cal_m_c(vector<int> A,vector<int> B){\n if(A[0]==B[0]){\n return {A[0],INT_MAX};\n }\n double m=(A[1]-B[1])/((A[0]-B[0])/1.0);\n double c=A[1]-m*A[0];\n return {m,c};\n }\n int maxPoints(vector<vector<int>>& points) {\n if(points.size()<2){\n return points.size();\n }\n map<pair<double,double>,set<vector<int>>> mp;\n for(int i=0;i<points.size();i++){\n for(int j=i+1;j<points.size();j++){\n vector<int> A=points[i];\n vector<int> B=points[j];\n auto temp = cal_m_c(A,B);\n if(mp.find(temp)!=mp.end()){\n mp[temp].insert(A);\n mp[temp].insert(B);\n }\n else{\n set<vector<int>> st;\n st.insert(A);\n st.insert(B);\n mp[temp]=st;\n }\n }\n }\n int ans=0;\n for(auto it:mp){\n int b = (it.second).size();\n ans=max(ans,b);\n }\n\n return ans;\n }\n\n};",
"memory": "89546"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<double,double> cal_m_c(vector<int> A,vector<int> B){\n if(A[0]==B[0]){\n return {A[0],INT_MAX};\n }\n double m=(A[1]-B[1])/((A[0]-B[0])/1.0);\n double c=A[1]-m*A[0];\n return {m,c};\n }\n int maxPoints(vector<vector<int>>& points) {\n if(points.size()<2){\n return points.size();\n }\n map<pair<double,double>,set<vector<int>>> mp;\n for(int i=0;i<points.size();i++){\n for(int j=i+1;j<points.size();j++){\n vector<int> A=points[i];\n vector<int> B=points[j];\n auto temp = cal_m_c(A,B);\n if(mp.find(temp)!=mp.end()){\n mp[temp].insert(A);\n mp[temp].insert(B);\n }\n else{\n set<vector<int>> st;\n st.insert(A);\n st.insert(B);\n mp[temp]=st;\n }\n }\n }\n int ans=0;\n for(auto it:mp){\n int b = (it.second).size();\n ans=max(ans,b);\n }\n\n return ans;\n }\n\n};",
"memory": "89546"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n //continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n \n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "90688"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n }\n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "91831"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n //continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n \n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "91831"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n //continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n \n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "92973"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n //continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n \n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "92973"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n }\n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "94116"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n }\n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "94116"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n }\n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "95258"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n /*if(points.size() == 2){\n if(points[0][0] == points[1][0] || points[0][0] == points[1][1] ){\n return 2;\n }\n if(points[0][1] == points[1][0] || points[0][1] == points[1][1] ){\n return 2;\n }\n }*/\n int q = 1;\n int y = 1;\n\n for(int i = 1 ; i<points.size() ; i++){\n if(points[i][0] == points[0][0] || points[i][1] == points[0][0] ){\n q++;\n }\n if(points[i][0] == points[0][1] || points[i][1] == points[0][1]){\n y++;\n }\n }\n if(y == points.size() || q == points.size()){\n return points.size();\n }\n if(points.size() == 1){\n return 1;\n }\n map< int, map<float, set<vector<int>>>>m;\n float d = 0.00;\n for(int i = 0 ; i<points.size()-1 ; i++ ){\n for(int j = i+1 ; j<points.size() ; j++){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n if((points[j][0] - points[i][0]) == 0){\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<i<<endl;\n m[i][INT_MAX].insert({points[j][0] , points[j][1]});\n m[i][INT_MAX].insert({points[i][0] , points[i][1]});\n continue; \n }else{\n //float r ;\n //cout<<points[j][1]<<points[i][1]<<points[j][0]<<points[i][0]<<endl;\n float g = (points[j][1] - points[i][1]);\n \n float h = (points[j][0] - points[i][0]);\n\n //cout<<g<<\" g \"<<h<<endl;\n \n d = g/h;\n //cout<<d<<\" r \"<<endl;\n }\n //cout<<r<<\" r \"<<endl;\n //d = r;\n m[i][d].insert({points[j][0] , points[j][1]});\n m[i][d].insert({points[i][0] , points[i][1]});\n }\n }\n int c = INT_MIN;\n for(auto i : m){\n for(auto j : i.second){\n //cout<<j.first<<\" \"<<j.second.size()<<endl;\n int w = j.second.size();\n if(c<w){\n c = j.second.size();\n }\n }\n }\n /* if(y > c ){\n return y;\n }\n\n if(q>c){\n return q;\n }*/\n return c;\n }\n};",
"memory": "95258"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n vector<int > A={2,1}; \n vector<int > B={2,2};\n cal_m_c(A,B);\n if(points.size()<2){\n return points.size();\n }\n map<pair<double,double>,set<vector<int>>> mp;\n for(int i=0;i<points.size();i++){\n for(int j=i+1;j<points.size();j++){\n vector<int> A=points[i];\n vector<int> B=points[j];\n if(mp.find(cal_m_c(A,B))!=mp.end()){\n mp[cal_m_c(A,B)].insert(A);\n mp[cal_m_c(A,B)].insert(B);\n }\n else{\n set<vector<int>> s;\n s.insert(A);\n s.insert(B);\n mp[cal_m_c(A,B)]=s;\n }\n }\n \n }\n int ans=0;\n for(auto i:mp){\n int b=(i.second).size();\n ans=max(ans,b);\n }\n return ans;\n }\n pair<double,double> cal_m_c(vector<int> A,vector<int> B){\n if(A[0]==B[0]){\n return {A[0],INT_MAX};\n }\n double m=(A[1]-B[1])/((A[0]-B[0])/1.0);\n double c=A[1]-m*A[0];\n return {m,c};\n }\n\n};\n",
"memory": "96401"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n\n int maxPoints(vector<vector<int>>& points) {\n\n vector<int > A={2,1}; \n\n vector<int > B={2,2};\n\n cal_m_c(A,B);\n\n if(points.size()<2){\n\n return points.size();\n\n }\n\n map<pair<double,double>,set<vector<int>>> mp;\n\n for(int i=0;i<points.size();i++){\n\n for(int j=i+1;j<points.size();j++){\n\n vector<int> A=points[i];\n\n vector<int> B=points[j];\n\n if(mp.find(cal_m_c(A,B))!=mp.end()){\n\n mp[cal_m_c(A,B)].insert(A);\n\n mp[cal_m_c(A,B)].insert(B);\n\n }\n\n else{\n\n set<vector<int>> s;\n\n s.insert(A);\n\n s.insert(B);\n\n mp[cal_m_c(A,B)]=s;\n\n }\n\n }\n\n \n\n }\n\n int ans=0;\n\n for(auto i:mp){\n\n int b=(i.second).size();\n\n ans=max(ans,b);\n\n }\n\n return ans;\n\n }\n\n pair<double,double> cal_m_c(vector<int> A,vector<int> B){\n\n if(A[0]==B[0]){\n\n return {A[0],INT_MAX};\n\n }\n\n double m=(A[1]-B[1])/((A[0]-B[0])/1.0);\n\n double c=A[1]-m*A[0];\n\n return {m,c};\n\n }\n\n};\n\n\n \n \n",
"memory": "97543"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int> my_stack;\n int i = 0;\n for (int i = 0; i < tokens.size(); i++) {\n if (tokens[i] == \"+\") {\n int first_element = my_stack.top();\n my_stack.pop();\n int second_element = my_stack.top();\n my_stack.pop();\n my_stack.push(second_element + first_element);\n }\n else if (tokens[i] == \"-\") {\n int first_element = my_stack.top();\n my_stack.pop();\n int second_element = my_stack.top();\n my_stack.pop();\n my_stack.push(second_element - first_element);\n }\n else if (tokens[i] == \"*\") {\n int first_element = my_stack.top();\n my_stack.pop();\n int second_element = my_stack.top();\n my_stack.pop();\n my_stack.push(second_element * first_element);\n }\n else if (tokens[i] == \"/\") {\n int first_element = my_stack.top();\n my_stack.pop();\n int second_element = my_stack.top();\n my_stack.pop();\n my_stack.push(second_element / first_element);\n }\n else\n my_stack.push(stoi(tokens[i]));\n }\n return my_stack.top();\n }\n};",
"memory": "15200"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n vector<int> st;\n int op1;\n int op2;\n for (auto token:tokens){\n if (token == \"+\"){\n op2 = st.back();\n st.pop_back();\n op1 = st.back();\n st.pop_back();\n st.push_back(op1 + op2);\n } else if (token == \"-\"){\n op2 = st.back();\n st.pop_back();\n op1 = st.back();\n st.pop_back();\n st.push_back(op1 - op2);\n } else if (token == \"*\"){\n op2 = st.back();\n st.pop_back();\n op1 = st.back();\n st.pop_back();\n st.push_back(op1 * op2);\n } else if (token == \"/\"){\n op2 = st.back();\n st.pop_back();\n op1 = st.back();\n st.pop_back();\n st.push_back(op1 / op2);\n }else{\n st.push_back(stoi(token));\n }\n }\n return st[0];\n }\n};",
"memory": "15300"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n\n vector<int> operands; // Stack to store operands\n \n for (const string& each : tokens) {\n if (isOperator(each)) {\n // Pop two operands from the stack\n int opr2 = operands.back();\n operands.pop_back();\n int opr1 = operands.back();\n operands.pop_back();\n\n // Perform the operation and push the result back to the stack\n operands.push_back(performMath(opr1, opr2, each));\n } else {\n // If it's a number, push it to the stack\n operands.push_back(stoi(each));\n }\n }\n\n // The final result should be the only element in the stack\n return operands.back();\n }\n\n bool isOperator(const string& s) {\n return s == \"+\" || s == \"-\" || s == \"*\" || s == \"/\";\n }\n\n int performMath(int op1, int op2, const string& opr) {\n if (opr == \"+\") return op1 + op2;\n if (opr == \"-\") return op1 - op2;\n if (opr == \"*\") return op1 * op2;\n if (opr == \"/\") return op1 / op2;\n return 0; // This should never happen\n }\n};",
"memory": "15300"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n vector<int> stack;\n int temp;\n for(string i : tokens){\n if(i[0] == '+'){\n temp = stack[stack.size()-1] + stack[stack.size()-2];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else if(i[0] == '-' && i.size() == 1){\n temp = stack[stack.size()-2] - stack[stack.size()-1];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else if(i[0] == '*'){\n temp = stack[stack.size()-1] * stack[stack.size()-2];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else if(i[0] == '/'){\n temp = stack[stack.size()-2] / stack[stack.size()-1];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else{\n stack.push_back(stoi(i));\n }\n }\n return stack[0];\n }\n};",
"memory": "15400"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n bool check(string c)\n {\n if(c == \"+\")\n return true;\n if(c == \"-\")\n return true;\n if(c == \"*\")\n return true;\n if(c == \"/\")\n return true;\n return false;\n }\n \n int ctoi(string i)\n {\n return stoi(i);\n }\n \n int getsol(string c, int a , int b)\n {\n if(c == \"+\")\n return a+b;\n if(c == \"-\")\n return b-a;\n if(c == \"*\")\n return b*a;\n\n return b/a;\n\n }\n \n \n int evalRPN(vector<string>& t) {\n stack<int> st;\n for(auto i: t)\n {\n if(check(i))\n {\n int f = st.top();\n st.pop();\n int s = st.top();\n st.pop(); \n st.push(getsol(i,f,s));\n }\n else\n {\n st.push(ctoi(i));\n }\n }\n \n return st.top();\n \n \n }\n};",
"memory": "15500"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& to) {\n int num=0;\n vector<int> stack;\n for (int i = 0; i < to.size(); i++)\n {\n if(to[i]==\"+\") {num=stack.back()+stack[stack.size()-2];stack.pop_back();stack.pop_back();stack.push_back(num);}\n else if(to[i]==\"-\"){num=stack[stack.size()-2]-stack.back();stack.pop_back();stack.pop_back(); stack.push_back(num);}\n else if(to[i]==\"*\"){num=stack[stack.size()-2]*stack.back();stack.pop_back();stack.pop_back(); stack.push_back(num);}\n else if(to[i]==\"/\"){num=stack[stack.size()-2]/stack.back();stack.pop_back();stack.pop_back(); stack.push_back(num);}\n else stack.push_back(stoi(to[i]));\n }\n return stack[0];\n }\n};",
"memory": "15500"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n vector<int> stack;\n \n for (string& token : tokens) {\n if (token == \"+\" || token == \"-\" || token == \"*\" || token == \"/\") {\n int b = stack.back(); stack.pop_back(); \n int a = stack.back(); stack.pop_back();\n \n if (token == \"+\") {\n stack.push_back(a + b);\n } else if (token == \"-\") {\n stack.push_back(a - b);\n } else if (token == \"*\") {\n stack.push_back(a * b);\n } else if (token == \"/\") {\n stack.push_back(a / b);\n }\n } else {\n stack.push_back(stoi(token));\n }\n }\n return stack.back(); \n }\n};\n",
"memory": "15600"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n vector<int> stack;\n int temp;\n for(string i : tokens){\n if(i[0] == '+'){\n temp = stack[stack.size()-1] + stack[stack.size()-2];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else if(i[0] == '-' && i.size() == 1){\n temp = stack[stack.size()-2] - stack[stack.size()-1];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else if(i[0] == '*'){\n temp = stack[stack.size()-1] * stack[stack.size()-2];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else if(i[0] == '/'){\n temp = stack[stack.size()-2] / stack[stack.size()-1];\n stack.pop_back();\n stack.pop_back();\n stack.push_back(temp);\n }\n else{\n stack.push_back(stoi(i));\n }\n }\n return stack[0];\n }\n};",
"memory": "15600"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int> st;\n\n for (string c : tokens) {\n if (c == \"+\") {\n int second = st.top(); st.pop();\n int first = st.top(); st.pop();\n st.push(first + second);\n } else if (c == \"-\") {\n int second = st.top(); st.pop();\n int first = st.top(); st.pop();\n st.push(first - second);\n } else if (c == \"*\") {\n int second = st.top(); st.pop();\n int first = st.top(); st.pop();\n st.push(first * second);\n } else if (c == \"/\") {\n int second = st.top(); st.pop();\n int first = st.top(); st.pop();\n st.push(first / second);\n } else {\n st.push(stoi(c));\n }\n }\n\n return st.top(); \n }\n};",
"memory": "15700"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int>stc;\n for(int i=0;i<tokens.size();i++)\n {\n if(tokens[i]==\"+\")\n {\n int second=stc.top();stc.pop();\n int first=stc.top();stc.pop();\n stc.push(first+second);\n }\n else if(tokens[i]==\"-\")\n {\n int second=stc.top();stc.pop();\n int first=stc.top();stc.pop();\n stc.push(first-second);\n }\n else if(tokens[i]==\"*\")\n {\n int second=stc.top();stc.pop();\n int first=stc.top();stc.pop();\n stc.push(first*second);\n }\n else if(tokens[i]==\"/\")\n {\n int second=stc.top();stc.pop();\n int first=stc.top();stc.pop();\n stc.push(first/second);\n }\n else\n {\n stc.push(stoi(tokens[i]));\n }\n }\n return stc.top();\n }\n};",
"memory": "15700"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int> s;\n for (string str: tokens){\n if(str == \"+\" || str == \"-\" || str == \"/\" || str == \"*\"){\n if(!str.empty()){\n int b = s.top();\n s.pop();\n int a = s.top();\n s.pop();\n int result;\n if(str == \"+\"){\n result = a + b;\n }else if(str == \"-\"){\n result = a - b;\n }else if(str == \"*\"){\n result = a * b;\n }else if(str == \"/\"){\n result = a / b;\n }\n s.push(result);\n }\n }else{\n int num = stoi(str);\n s.push(num);\n }\n }\n int result = s.top();\n return result;\n }\n};",
"memory": "15800"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int>st;\n int ans=0;\n for(auto it:tokens){\n\n if(it ==\"+\" || it == \"*\" || it == \"-\" || it ==\"/\"){\n\n if(st.size()<=1){\n continue;\n }\n int a=st.top();\n st.pop();\n int b=st.top();\n st.pop();\n if(it == \"+\"){\n st.push(a+b);\n }\n else if(it == \"*\"){\n st.push(a*b);\n }\n else if(it == \"/\"){\n st.push(b/a);\n }\n else{\n st.push(b-a);\n }\n }\n else{\n int val=stoi(it);\n st.push(val);\n }\n }\n return st.top();\n\n }\n};",
"memory": "15800"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int> sk;\n for (const string& token : tokens) {\n if (token == \"+\" || token == \"-\" || token == \"*\" || token == \"/\") {\n int num1 = sk.top();\n sk.pop();\n int num2 = sk.top();\n sk.pop();\n if (token == \"+\") {\n sk.push(num1 + num2);\n }\n else if (token == \"-\") {\n sk.push(num2 - num1);\n }\n else if (token == \"*\") {\n sk.push(num1 * num2);\n }\n else {\n sk.push(num2 / num1);\n }\n }\n else {\n sk.push(stoi(token));\n }\n }\n return sk.top();\n }\n};",
"memory": "15900"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& s) {\n stack<int>st;\n map<string,int>mp;\n mp[\"+\"]=1;\n mp[\"-\"]=2;\n mp[\"*\"]=3;\n mp[\"/\"]=4;\n for(int i = 0; i < s.size(); i++) {\n if(mp.find(s[i]) != mp.end()) {\n int op1 = st.top(); st.pop();\n int op2 = st.top(); st.pop();\n int result = 0;\n if(mp[s[i]] == 1) {\n result = op2 + op1;\n }\n else if(mp[s[i]] == 2) {\n result = op2 - op1;\n }\n else if(mp[s[i]] == 3) {\n result = op2 * op1;\n }\n else {\n result = op2 / op1;\n }\n st.push(result);\n }\n else {\n st.push(stoi(s[i]));\n }\n }\n return st.top();\n }\n};\n",
"memory": "15900"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<long long int> s;\n int i = 0;\n while(i < tokens.size()) {\n if(tokens[i] == \"+\") {\n int a = s.top();\n s.pop();\n int b = s.top();\n s.pop();\n s.push(a+b);\n } else if(tokens[i] == \"-\") {\n int a = s.top();\n s.pop();\n int b = s.top();\n s.pop();\n s.push(b-a);\n } else if(tokens[i] == \"*\") {\n int a = s.top();\n s.pop();\n int b = s.top();\n s.pop();\n s.push(a*b);\n } else if(tokens[i] == \"/\") {\n int a = s.top();\n s.pop();\n int b = s.top();\n s.pop();\n s.push(b/a);\n } else {\n s.push(stoi(tokens[i]));\n }\n i++;\n }\n return s.top();\n }\n};",
"memory": "16000"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) \n {\n // Postfix Evaluation\n\n // 2 5 + = 7\n // Step 1 String mapped to function\n unordered_map <string,function<long(long,long)>> op\n {\n {\"+\",plus()},\n {\"-\",minus()},\n {\"*\",multiplies()},\n {\"/\",divides()}\n };\n\n stack<long>st;\n for (auto token:tokens)\n {\n // if operand push\n if (op.count(token))\n {\n int b = st.top(); st.pop();\n int a = st.top(); st.pop();\n\n st.push( op.at(token)(a,b) );\n \n }\n else\n {\n st.push(stoi(token));\n }\n\n }\n \n return st.top();\n }\n\n\n};\n ",
"memory": "16000"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "// Integrantes: Gino Daza Yalta, Nicolas Stigler, Milton Cordova\n\n\ntemplate <typename T>\nclass Stack {\nprivate:\n struct Node {\n T data;\n Node* next;\n Node(T d) : data(d), next(nullptr) {}\n };\n\n Node* topNode;\n\npublic:\n Stack() : topNode(nullptr) {}\n\n ~Stack() {\n while (!empty()) {\n pop();\n }\n }\n\n void push(T data) {\n Node* newNode = new Node(data);\n newNode->next = topNode;\n topNode = newNode;\n }\n\n T pop() {\n if (empty()) {\n throw runtime_error(\"Stack is empty\");\n }\n Node* temp = topNode;\n T data = temp->data;\n topNode = topNode->next;\n delete temp;\n return data;\n }\n\n T top() const {\n if (empty()) {\n throw runtime_error(\"Stack is empty\");\n }\n return topNode->data;\n }\n\n bool empty() const {\n return topNode == nullptr;\n }\n\n int size() const {\n int count = 0;\n Node* current = topNode;\n while (current != nullptr) {\n count++;\n current = current->next;\n }\n return count;\n }\n};\n\nclass Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n\n Stack<int> stack;\n for(int i = 0; i < tokens.size(); i++){\n if(tokens[i] == \"+\" || tokens[i] == \"-\" || tokens[i] == \"*\" || tokens[i] == \"/\"){\n cout << tokens[i] << \" \";\n int top = stack.pop();\n int prev_top = stack.pop();\n if(tokens[i] == \"+\"){\n stack.push(prev_top + top);\n }\n if(tokens[i] == \"-\"){\n stack.push(prev_top - top);\n }\n if(tokens[i] == \"*\"){\n stack.push(top * prev_top);\n }\n if(tokens[i] == \"/\"){\n stack.push(prev_top / top);\n }\n } else{\n int num = std::stoi(tokens[i]);\n cout << num << \" \";\n stack.push(num);\n }\n }\n\n return stack.top();\n }\n};",
"memory": "16700"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) \n {\n stack<int> mystack;\n\n const auto is_operator = [&] (const string& t)\n {\n return t == \"+\" || t == \"-\" || t == \"*\" || t == \"/\";\n };\n\n const auto evaluate = [&] (const string& t)\n {\n auto b = mystack.top();\n mystack.pop();\n\n auto a = mystack.top();\n mystack.pop();\n\n if (t == \"+\")\n return a+b;\n else if (t == \"-\")\n return a-b;\n else if (t == \"*\")\n return a*b;\n else\n return a/b;\n };\n\n for (const auto& t : tokens)\n {\n if (!is_operator (t)) \n mystack.push (stoi(t));\n else\n mystack.push (evaluate (t));\n }\n\n return mystack.top(); \n\n }\n};",
"memory": "16800"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int> st;\n double ans;\n bool start = false;\n for(int i = 0; i < tokens.size(); i++){\n if(st.empty()) st.push(i);\n else if(tokens[i][0] == '+'){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp += stoi(tokens[st.top()]);\n tokens[st.top()] = to_string(temp);\n }\n else if(tokens[i][0] == '-' && tokens[i].size() == 1){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp = stoi(tokens[st.top()]) - temp;\n tokens[st.top()] = to_string(temp);\n }\n else if(tokens[i][0] == '*'){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp *= stoi(tokens[st.top()]);\n tokens[st.top()] = to_string(temp);\n }\n else if(tokens[i][0] == '/'){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp = stoi(tokens[st.top()]) / temp;\n tokens[st.top()] = to_string(temp);\n }\n else st.push(i);\n }\n return stoi(tokens[st.top()]);\n }\n};",
"memory": "16900"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<int> st;\n double ans;\n bool start = false;\n for(int i = 0; i < tokens.size(); i++){\n if(st.empty()) st.push(i);\n else if(tokens[i][0] == '+'){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp += stoi(tokens[st.top()]);\n tokens[st.top()] = to_string(temp);\n }\n else if(tokens[i][0] == '-' && tokens[i].size() == 1){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp = stoi(tokens[st.top()]) - temp;\n tokens[st.top()] = to_string(temp);\n }\n else if(tokens[i][0] == '*'){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp *= stoi(tokens[st.top()]);\n tokens[st.top()] = to_string(temp);\n }\n else if(tokens[i][0] == '/'){\n int temp = stoi(tokens[st.top()]);\n st.pop();\n temp = stoi(tokens[st.top()]) / temp;\n tokens[st.top()] = to_string(temp);\n }\n else st.push(i);\n }\n return stoi(tokens[st.top()]);\n }\n};",
"memory": "16900"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void calculate(stack<int>& st, string ope) {\n int tmp1 = st.top();\n st.pop();\n int tmp2 =st.top();\n st.pop();\n if (ope==\"+\") {\n st.push(tmp1+tmp2);\n }\n else if (ope==\"-\") {\n st.push(tmp2-tmp1);\n } else if (ope==\"*\") {\n st.push(tmp1*tmp2);\n } else if (ope==\"/\") {\n st.push(tmp2/tmp1);\n }\n }\n int evalRPN(vector<string>& tokens) {\n stack<int> st;\n for (int i=0;i<tokens.size();i++) {\n if (tokens[i]==\"+\" || tokens[i]==\"-\" || tokens[i]==\"*\" || tokens[i]==\"/\") {\n calculate(st,tokens[i]);\n } else {\n st.push(stoi(tokens[i]));\n }\n }\n return st.top();\n }\n};",
"memory": "17000"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <string>\nclass Solution {\npublic:\n int solve(int a, int b, string o){\n if(o==\"+\") return (a + b);\n else if(o==\"-\") return (a - b);\n else if(o==\"*\") return (a * b);\n else return (a / b);\n }\n int evalRPN(vector<string>& tokens) {\n if(tokens.size()==1){\n return stoi(tokens[0]);\n }\n stack<string> polExp;\n stack<int> st;\n for(int i=tokens.size()-1; i>=0 ; i--){\n polExp.push(tokens[i]);\n }while(!polExp.empty()){\n while(polExp.top()!=\"+\" && polExp.top()!=\"-\" && polExp.top()!=\"/\" && polExp.top()!=\"*\"){\n st.push(std::stoi(polExp.top()));\n polExp.pop();\n } \n int a = st.top(); st.pop();\n int b = st.top(); st.pop();\n int n = solve(b, a, polExp.top());\n st.push(n);\n polExp.pop();\n }\n return st.top();\n }\n};",
"memory": "17100"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int a, int b, string o){\n if(o==\"+\") return (a + b);\n else if(o==\"-\") return (a - b);\n else if(o==\"*\") return (a * b);\n else return (a / b);\n }\n int evalRPN(vector<string>& tokens) {\n if(tokens.size()==1){\n return stoi(tokens[0]);\n }\n stack<string> polExp;\n stack<int> st;\n for(int i=tokens.size()-1; i>=0 ; i--){\n polExp.push(tokens[i]);\n }\n while(!polExp.empty()){\n while(polExp.top()!=\"+\" && polExp.top()!=\"-\" && polExp.top()!=\"/\" && polExp.top()!=\"*\"){\n st.push(std::stoi(polExp.top()));\n polExp.pop();\n }int a = st.top(); st.pop();\n int b = st.top(); st.pop();\n int n = solve(b, a, polExp.top());\n st.push(n);\n polExp.pop();\n }\n return st.top();\n }\n};",
"memory": "17200"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "template <typename T>\nclass Node\n{\npublic:\n T val;\n Node* next;\n Node(T value, Node* ptr)\n {\n val = value;\n next = ptr;\n }\n};\n\ntemplate <typename T>\nclass Stack\n{\npublic:\n Stack(T val, Node<T>* ptr)\n {\n dummyHead = new Node<T>(val, ptr);\n }\n\n void push(T val)\n {\n dummyHead->next = new Node<T>(val, dummyHead->next);\n }\n\n T pop()\n {\n T res = dummyHead->next->val;\n dummyHead->next = dummyHead->next->next;\n return res;\n }\n\n bool isEmpty()\n {\n return (dummyHead->next == nullptr);\n }\n\nprivate:\n Node<T>* dummyHead;\n};\n\nclass Solution {\npublic:\n\n //Performs the operation op on x and y\n int evaluateOperation(std::basic_string<char> op, int x, int y)\n {\n if(op == \"+\")\n return x + y;\n else if(op ==\"*\")\n return x*y;\n else if(op == \"-\")\n return x - y;\n else\n return x / y;\n }\n\n int evalRPN(vector<string>& tokens)\n {\n //Have two stacks, one for the operations and one for the operands\n //When we have an operation and two operands, compute the result and add it to the\n //operands stack\n Stack<int> operands(-1, nullptr);\n Stack<string> operations(\"*\", nullptr);\n\n int operandsSize = 0;\n\n for(int i = 0; i < tokens.size(); i++)\n {\n if(tokens[i] == \"+\" || tokens[i] == \"-\" || tokens[i] == \"*\" || tokens[i] == \"/\")\n operations.push(tokens[i]);\n else\n operands.push(std::stoi(tokens[i]));\n \n operandsSize++;\n\n if(operandsSize >= 2 && !operations.isEmpty())\n {\n int top = operands.pop();\n int next = operands.pop();\n int result = evaluateOperation(operations.pop(), next, top);\n operands.push(result);\n operandsSize--;\n }\n }\n\n return operands.pop();\n }\n};",
"memory": "17300"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool checkint(string s){\n if(s.empty()) return false; \n int start = (s[0] == '-') ? 1 : 0;\n for (int i = start; i < s.length(); ++i) {\n if (!isdigit(s[i])) return false;\n }\n return s.length() > start;\n }\n\n int evalRPN(vector<string>& tokens) {\n stack<string> s1;\n for(string s : tokens){\n if(checkint(s)) s1.push(s);\n else if(s == \"/\"){\n int x1 = stoi(s1.top());\n s1.pop();\n int x2 = stoi(s1.top());\n s1.pop();\n int res = x2 / x1;\n s1.push(to_string(res));\n }\n else if(s == \"*\"){\n int x1 = stoi(s1.top());\n s1.pop();\n int x2 = stoi(s1.top());\n s1.pop();\n int res = x2 * x1;\n s1.push(to_string(res));\n }\n else if(s == \"+\"){\n int x1 = stoi(s1.top());\n s1.pop();\n int x2 = stoi(s1.top());\n s1.pop();\n int res = x2 + x1;\n s1.push(to_string(res));\n }\n else{\n int x1 = stoi(s1.top());\n s1.pop();\n int x2 = stoi(s1.top());\n s1.pop();\n int res = x2 - x1;\n s1.push(to_string(res));\n }\n }\n return stoi(s1.top());\n }\n};",
"memory": "17400"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n bool isOperator(string &s) {\n return s == \"+\" || s == \"-\" || s == \"*\" || s == \"/\";\n }\npublic:\n int evalRPN(vector<string>& tokens) {\n vector<string> last;\n for (string t : tokens) {\n if (isOperator(t)) {\n string op2 = last.back(); last.pop_back();\n string op1 = last.back(); last.pop_back();\n int res = 0;\n if (t == \"+\") {\n res = stoi(op1) + stoi(op2);\n } else if (t == \"-\") {\n res = stoi(op1) - stoi(op2);\n } else if (t == \"*\") {\n res = stoi(op1) * stoi(op2);\n } else if (t == \"/\") {\n res = stoi(op1) / stoi(op2);\n }\n last.push_back(to_string(res));\n } else {\n last.push_back(t);\n }\n }\n return stoi(last.front());\n }\n};",
"memory": "17400"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n vector<string> stack;\n for (string x : tokens){\n if (x!= \"+\" & x!= \"-\" & x!= \"*\" & x!= \"/\"){\n stack.push_back(x);\n }\n else {\n int op1 = stoi(stack.back());\n stack.pop_back();\n int op2 = stoi(stack.back());\n stack.pop_back();\n if (x==\"+\"){\n stack.push_back(to_string(op1+op2));\n }\n else if (x==\"-\"){\n stack.push_back(to_string(op2-op1));\n }\n else if (x==\"*\"){\n stack.push_back(to_string(op1*op2));\n }\n else if (x==\"/\"){\n stack.push_back(to_string(op2/op1));\n }\n }\n \n }\n return stoi(stack.back());\n }\n};",
"memory": "17500"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string> &tokens)\n {\n stack<string> RPN;\n for (size_t i = 0; i < tokens.size(); i++)\n {\n string present = tokens[i];\n if (present != \"+\" && present != \"-\" && present != \"*\" && present != \"/\")\n {\n RPN.push(present);\n }\n else\n {\n int b = stoi(RPN.top());\n RPN.pop();\n int a = stoi(RPN.top());\n RPN.pop();\n int result;\n switch (present[0])\n {\n case '+':\n result = a + b;\n break;\n case '-':\n result = a - b;\n break;\n case '*':\n result = a * b;\n break;\n case '/':\n result = a / b;\n break;\n }\n RPN.push(to_string(result));\n }\n }\n return stoi(RPN.top());\n }\n};",
"memory": "17600"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "// 150. Evaluate Reverse Polish Notation\n// https://leetcode.com/problems/evaluate-reverse-polish-notation/description/\n\nclass Solution {\npublic:\n\tbool isOp(string a) {\n\t\treturn a == \"+\" || a == \"-\" || a == \"*\" || a == \"/\";\n\t}\n\n\tint stackAns(vector<string>& tokens) {\n\n\t\tstack<int> s;\n\n\t\tint n = tokens.size();\n\n\t\tfor (int i = 0 ; i < n ; i++) {\n\n\t\t\tif (isOp(tokens[i])) {\n\n\t\t\t\tint b = s.top();\n\t\t\t\ts.pop();\n\n\t\t\t\tint a = s.top();\n\t\t\t\ts.pop();\n\n\t\t\t\tif (tokens[i] == \"+\")\n\t\t\t\t\ts.push(a + b);\n\t\t\t\telse if (tokens[i] == \"-\")\n\t\t\t\t\ts.push(a - b);\n\t\t\t\telse if (tokens[i] == \"*\")\n\t\t\t\t\ts.push(a * b);\n\t\t\t\telse if (tokens[i] == \"/\")\n\t\t\t\t\ts.push(a / b);\t\t// divide by zero won't be given\n\t\t\t}\n\t\t\telse {\n\t\t\t\ts.push(stoi(tokens[i]));\n\t\t\t}\n\t\t}\n\n\t\treturn s.top();\n\t}\n\n\tint spaceOptimzed(vector<string> tokens) {\n\n\t\tint n = tokens.size();\n\t\tint i = 0;\n\t\tint lastNum = -1;\t// last number seen\n\n\t\tfor (int i = 0 ; i < n ; i++) {\n\n\t\t\tif (isOp(tokens[i])) {\n\n\t\t\t\tint a = stoi(tokens[lastNum - 1]);\n\t\t\t\tint b = stoi(tokens[lastNum]);\n\n\t\t\t\tif (tokens[i] == \"+\") {\n\t\t\t\t\ttokens[lastNum - 1] = to_string(a + b);\n\t\t\t\t}\n\t\t\t\telse if (tokens[i] == \"-\") {\n\t\t\t\t\ttokens[lastNum - 1] = to_string(a - b);\n\t\t\t\t}\n\t\t\t\telse if (tokens[i] == \"*\") {\n\t\t\t\t\ttokens[lastNum - 1] = to_string(a * b);\n\t\t\t\t}\n\t\t\t\telse if (tokens[i] == \"/\") {\n\t\t\t\t\ttokens[lastNum - 1] = to_string(a / b);\n\t\t\t\t}\n\n\t\t\t\tlastNum--;\n\t\t\t}\n\t\t\telse {\n\t\t\t\ttokens[++lastNum] = tokens[i];\n\t\t\t}\n\t\t}\n\n\t\treturn stoi(tokens[lastNum]);\n\t}\n\n int evalRPN(vector<string>& tokens) {\n \n // return stackAns(tokens);\n\n return spaceOptimzed(tokens);\t\t// without stack\n }\n};",
"memory": "17600"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n int evalRPN(vector<string>& tokens) {\n \n stack<string> st;\n for(auto it: tokens) {\n if(it == \"+\" || it == \"-\" || it == \"*\" || it == \"/\") {\n int a = stoi(st.top()); st.pop();\n int b = stoi(st.top()); st.pop();\n int c = ((it == \"+\") ? (b + a): ((it == \"-\") ? (b - a): ((it == \"*\") ? (b * a): (b / a))));\n st.push(to_string(c));\n } else {\n st.push(it);\n }\n }\n\n return stoi(st.top());\n\n }\n};",
"memory": "17700"
} |
150 | <p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p>
<p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p>
<p><strong>Note</strong> that:</p>
<ul>
<li>The valid operators are <code>'+'</code>, <code>'-'</code>, <code>'*'</code>, and <code>'/'</code>.</li>
<li>Each operand may be an integer or another expression.</li>
<li>The division between two integers always <strong>truncates toward zero</strong>.</li>
<li>There will not be any division by zero.</li>
<li>The input represents a valid arithmetic expression in a reverse polish notation.</li>
<li>The answer and all the intermediate calculations can be represented in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["2","1","+","3","*"]
<strong>Output:</strong> 9
<strong>Explanation:</strong> ((2 + 1) * 3) = 9
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["4","13","5","/","+"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> (4 + (13 / 5)) = 6
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
<strong>Output:</strong> 22
<strong>Explanation:</strong> ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tokens.length <= 10<sup>4</sup></code></li>
<li><code>tokens[i]</code> is either an operator: <code>"+"</code>, <code>"-"</code>, <code>"*"</code>, or <code>"/"</code>, or an integer in the range <code>[-200, 200]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int evalRPN(vector<string>& tokens) {\n stack<string> s;\n for (string& t:tokens) {\n if (isdigit(t[0]) || (t.length()!=1 && isdigit(t[1]))) s.push(t);\n else {\n string b = s.top();\n s.pop();\n string a = s.top();\n s.pop();\n switch(t[0]) {\n case '+':\n s.push(to_string(stoll(a)+stoll(b)));\n break;\n case '-':\n s.push(to_string(stoll(a)-stoll(b)));\n break;\n case '*':\n s.push(to_string(stoll(a)*stoll(b)));\n break;\n case '/':\n s.push(to_string(stoll(a)/stoll(b)));\n break;\n }\n }\n }\n return stoi(s.top());\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();",
"memory": "17700"
} |
3,055 | <p>You are given a <strong>binary</strong> string <code>s</code> that contains at least one <code>'1'</code>.</p>
<p>You have to <strong>rearrange</strong> the bits in such a way that the resulting binary number is the <strong>maximum odd binary number</strong> that can be created from this combination.</p>
<p>Return <em>a string representing the maximum odd binary number that can be created from the given combination.</em></p>
<p><strong>Note </strong>that the resulting string <strong>can</strong> have leading zeros.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "010"
<strong>Output:</strong> "001"
<strong>Explanation:</strong> Because there is just one '1', it must be in the last position. So the answer is "001".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "0101"
<strong>Output:</strong> "1001"
<strong>Explanation: </strong>One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of <code>'0'</code> and <code>'1'</code>.</li>
<li><code>s</code> contains at least one <code>'1'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string maximumOddBinaryNumber(string s) {\n int one = 0;\n for(auto it : s) if(it == '1') one++;\n for(int i=0;i<one-1;i++){\n s[i] = '1';\n }\n for(int i=one-1;i<s.size()-1;i++) s[i] = '0';\n s[s.size()-1] = '1';\n return s;\n }\n};",
"memory": "8300"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.