Skip to content

Commit aeecc6b

Browse files
committed
add solutions
1 parent 14f23a5 commit aeecc6b

File tree

10 files changed

+224
-1
lines changed

10 files changed

+224
-1
lines changed

problems/1002.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/find-common-characters", "name": "Find Common Characters", "difficulty": "Easy", "statement": "<div><p>Given an array&nbsp;<code>A</code> of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list <strong>(including duplicates)</strong>.&nbsp;&nbsp;For example, if a character occurs 3 times&nbsp;in all strings but not 4 times, you need to include that character three times&nbsp;in the final answer.</p>\n\n<p>You may return the answer in any order.</p>\n\n<p>&nbsp;</p>\n\n<div>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[\"bella\",\"label\",\"roller\"]</span>\n<strong>Output: </strong><span id=\"example-output-1\">[\"e\",\"l\",\"l\"]</span>\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">[\"cool\",\"lock\",\"cook\"]</span>\n<strong>Output: </strong><span id=\"example-output-2\">[\"c\",\"o\"]</span>\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong><span>Note:</span></strong></p>\n\n<ol>\n\t<li><code>1 &lt;= A.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= A[i].length &lt;= 100</code></li>\n\t<li><code>A[i][j]</code> is a lowercase letter</li>\n</ol>\n</div>\n</div></div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n vector<string> commonChars(vector<string> &A)\n {\n\n int len = A.size();\n vector<vector<int>> m;\n for (int i = 0; i < 26; i++)\n {\n vector<int> t;\n for (string temp : A)\n t.push_back(0);\n m.push_back(t);\n }\n\n for (int i = 0; i < len; ++i)\n {\n for (char ch : A[i])\n {\n ++m[(int)(ch)-97][i];\n }\n }\n\n vector<string> out;\n\n for (int i = 0; i < 26; i++)\n {\n vector<int> counts = m[i];\n int min_count = *min_element(counts.begin(), counts.end());\n stringstream s;\n s << (char)(i + 97);\n for (int j = 0; j < min_count; ++j)\n {\n out.push_back(s.str());\n }\n }\n\n return out;\n }\n};"}

problems/1122.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/relative-sort-array", "name": "Relative Sort Array", "difficulty": "Easy", "statement": "<div><p>Given two arrays <code>arr1</code> and <code>arr2</code>, the elements of <code>arr2</code> are distinct, and all elements in <code>arr2</code> are also in <code>arr1</code>.</p>\n\n<p>Sort the elements of <code>arr1</code> such that the relative ordering of items in <code>arr1</code> are the same as in <code>arr2</code>.&nbsp; Elements that don't appear in <code>arr2</code> should be placed at the end of <code>arr1</code> in <strong>ascending</strong> order.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n<pre><strong>Input:</strong> arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]\n<strong>Output:</strong> [2,2,2,1,4,3,3,9,6,7,19]\n</pre>\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>arr1.length, arr2.length &lt;= 1000</code></li>\n\t<li><code>0 &lt;= arr1[i], arr2[i] &lt;= 1000</code></li>\n\t<li>Each&nbsp;<code>arr2[i]</code>&nbsp;is&nbsp;distinct.</li>\n\t<li>Each&nbsp;<code>arr2[i]</code> is in <code>arr1</code>.</li>\n</ul>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n vector<int> relativeSortArray(vector<int> &arr1, vector<int> &arr2)\n {\n\n int *m1 = (int *)malloc(sizeof(int) * 1001);\n fill_n(m1, 1001, 0);\n\n for (int i : arr1)\n ++m1[i];\n\n int *m2 = (int *)malloc(sizeof(int) * 1001);\n fill_n(m2, 1001, 0);\n\n for (int i : arr2)\n ++m2[i];\n\n vector<int> out;\n vector<int> exc;\n\n for (int i : arr2)\n {\n if (m2[i])\n {\n int count = m1[i];\n for (int j = 0; j < count; ++j)\n {\n out.push_back(i);\n }\n }\n }\n\n for (int i = 0; i <= 1000; ++i)\n {\n if (!m2[i])\n {\n int count = m1[i];\n for (int j = 0; j < count; ++j)\n {\n exc.push_back(i);\n }\n }\n }\n\n sort(exc.begin(), exc.end());\n for (int i : exc)\n out.push_back(i);\n\n return out;\n }\n};"}

problems/883.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/projection-area-of-3d-shapes", "name": "Projection Area of 3D Shapes", "difficulty": "Easy", "statement": "<div><p>On a&nbsp;<code>N&nbsp;*&nbsp;N</code> grid, we place some&nbsp;<code>1 * 1 * 1&nbsp;</code>cubes that are axis-aligned with the x, y, and z axes.</p>\n\n<p>Each value&nbsp;<code>v = grid[i][j]</code>&nbsp;represents a tower of&nbsp;<code>v</code>&nbsp;cubes placed on top of grid cell <code>(i, j)</code>.</p>\n\n<p>Now we view the&nbsp;<em>projection</em>&nbsp;of these cubes&nbsp;onto the xy, yz, and zx planes.</p>\n\n<p>A projection is like a shadow, that&nbsp;maps our 3 dimensional figure to a 2 dimensional plane.&nbsp;</p>\n\n<p>Here, we are viewing the \"shadow\" when looking at the cubes from the top, the front, and the side.</p>\n\n<p>Return the total area of all three projections.</p>\n\n<p>&nbsp;</p>\n\n<div>\n<ul>\n</ul>\n</div>\n\n<div>\n<div>\n<ul>\n</ul>\n</div>\n</div>\n\n<div>\n<div>\n<div>\n<div>\n<ul>\n</ul>\n</div>\n</div>\n</div>\n</div>\n\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<ul>\n</ul>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n\n<div>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[[2]]</span>\n<strong>Output: </strong><span id=\"example-output-1\">5</span>\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">[[1,2],[3,4]]</span>\n<strong>Output: </strong><span id=\"example-output-2\">17</span>\n<strong>Explanation: </strong>\nHere are the three projections (\"shadows\") of the shape made with each axis-aligned plane.\n<img alt=\"\" src=\"https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/02/shadow.png\" style=\"width: 749px; height: 200px;\">\n</pre>\n\n<div>\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-3-1\">[[1,0],[0,2]]</span>\n<strong>Output: </strong><span id=\"example-output-3\">8</span>\n</pre>\n\n<div>\n<p><strong>Example 4:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-4-1\">[[1,1,1],[1,0,1],[1,1,1]]</span>\n<strong>Output: </strong><span id=\"example-output-4\">14</span>\n</pre>\n\n<div>\n<p><strong>Example 5:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-5-1\">[[2,2,2],[2,1,2],[2,2,2]]</span>\n<strong>Output: </strong><span id=\"example-output-5\">21</span>\n</pre>\n\n<p>&nbsp;</p>\n\n<div>\n<div>\n<div>\n<p><span><strong>Note:</strong></span></p>\n\n<ul>\n\t<li><code>1 &lt;= grid.length = grid[0].length&nbsp;&lt;= 50</code></li>\n\t<li><code>0 &lt;= grid[i][j] &lt;= 50</code></li>\n</ul>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n int projectionArea(vector<vector<int>> &grid)\n {\n int n = grid.size();\n\n int top = 0;\n for (vector<int> temp : grid)\n {\n for (int i : temp)\n {\n if (i)\n ++top;\n }\n }\n\n vector<int> front;\n vector<int> right;\n for (int i = 0; i < n; i++)\n {\n front.push_back(0);\n right.push_back(0);\n for (int j = 0; j < n; j++)\n {\n front[i] = max(front[i], grid[i][j]);\n right[i] = max(right[i], grid[j][i]);\n }\n }\n\n return top + accumulate(front.begin(), front.end(), 0) + accumulate(right.begin(), right.end(), 0);\n }\n};"}

problems/893.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/groups-of-special-equivalent-strings", "name": "Groups of Special-Equivalent Strings", "difficulty": "Easy", "statement": "<div><p>You are given an array <code>A</code> of strings.</p>\n\n<p>A <em>move&nbsp;onto <code>S</code></em> consists of swapping any two even indexed characters of <code>S</code>, or any two odd indexed characters of <code>S</code>.</p>\n\n<p>Two strings <code>S</code> and <code>T</code> are&nbsp;<em>special-equivalent</em>&nbsp;if after any number of <em>moves onto <code>S</code></em>, <code>S == T</code>.</p>\n\n<p>For example, <code>S = \"zzxy\"</code> and <code>T = \"xyzz\"</code> are special-equivalent because we may make the moves <code>\"zzxy\" -&gt; \"xzzy\" -&gt; \"xyzz\"</code>&nbsp;that swap <code>S[0]</code> and <code>S[2]</code>, then <code>S[1]</code> and <code>S[3]</code>.</p>\n\n<p>Now, a <em>group of special-equivalent strings from <code>A</code></em>&nbsp;is a non-empty subset of&nbsp;A such that:</p>\n\n<ol>\n\t<li>Every pair of strings in the group are special equivalent, and;</li>\n\t<li>The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group)</li>\n</ol>\n\n<p>Return the number of groups of special-equivalent strings from <code>A</code>.</p>\n\n<div>&nbsp;</div>\n\n<div>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[\"abcd\",\"cdab\",\"cbad\",\"xyzz\",\"zzxy\",\"zzyx\"]</span>\n<strong>Output: </strong><span id=\"example-output-1\">3</span>\n<strong>Explanation: </strong>\nOne group is [\"abcd\", \"cdab\", \"cbad\"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these.\n\nThe other two groups are [\"xyzz\", \"zzxy\"] and [\"zzyx\"]. Note that in particular, \"zzxy\" is not special equivalent to \"zzyx\".\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">[\"abc\",\"acb\",\"bac\",\"bca\",\"cab\",\"cba\"]</span>\n<strong>Output: </strong><span id=\"example-output-2\">3</span></pre>\n\n<p>&nbsp;</p>\n</div>\n</div>\n\n<div>\n<div>\n<div>\n<div>\n<p><strong>Note:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= A.length &lt;= 1000</code></li>\n\t<li><code>1 &lt;= A[i].length &lt;= 20</code></li>\n\t<li>All <code>A[i]</code> have the same length.</li>\n\t<li>All <code>A[i]</code> consist of only lowercase letters.</li>\n</ul>\n</div>\n</div>\n</div>\n</div>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nlong int calc_even(string a)\n{\n long int product = 1;\n int arr[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};\n\n int n = a.length();\n for(int i = 0; i < n; i+=2) {\n product *= arr[(int)(a[i]) - 97];\n }\n\n return product;\n}\n\nlong int calc_odd(string a)\n{\n long int product = 1;\n int arr[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};\n\n int n = a.length();\n for(int i = 1; i < n; i+=2) {\n product *= arr[(int)(a[i]) - 97];\n }\n\n return product;\n}\n\nclass Solution\n{\npublic:\n int numSpecialEquivGroups(vector<string> &A)\n {\n vector<int> matches;\n vector<int> even_scores;\n vector<int> odd_scores;\n\n for(string temp: A) {\n even_scores.push_back(calc_even(temp));\n odd_scores.push_back(calc_odd(temp));\n }\n\n int len = A.size();\n\n for (int i = 0; i < len; i++)\n {\n matches.push_back(-1);\n for (int j = i + 1; j < len; j++)\n {\n if (even_scores[i] == even_scores[j] && odd_scores[i] == odd_scores[j])\n {\n matches[i] = j;\n j = len;\n }\n }\n }\n\n int *group_id = (int *)malloc(sizeof(int) * len);\n fill_n(group_id, len, 0);\n\n int group_number = 0;\n\n for (int i = 0; i < len; i++)\n {\n if (!group_id[i])\n group_id[i] = ++group_number;\n if (matches[i] != -1)\n {\n group_id[matches[i]] = group_id[i];\n }\n }\n\n return group_number;\n }\n};"}

solutions/easy/1002.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
vector<string> commonChars(vector<string> &A)
10+
{
11+
12+
int len = A.size();
13+
vector<vector<int>> m;
14+
for (int i = 0; i < 26; i++)
15+
{
16+
vector<int> t;
17+
for (string temp : A)
18+
t.push_back(0);
19+
m.push_back(t);
20+
}
21+
22+
for (int i = 0; i < len; ++i)
23+
{
24+
for (char ch : A[i])
25+
{
26+
++m[(int)(ch)-97][i];
27+
}
28+
}
29+
30+
vector<string> out;
31+
32+
for (int i = 0; i < 26; i++)
33+
{
34+
vector<int> counts = m[i];
35+
int min_count = *min_element(counts.begin(), counts.end());
36+
stringstream s;
37+
s << (char)(i + 97);
38+
for (int j = 0; j < min_count; ++j)
39+
{
40+
out.push_back(s.str());
41+
}
42+
}
43+
44+
return out;
45+
}
46+
};

solutions/easy/1122.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
vector<int> relativeSortArray(vector<int> &arr1, vector<int> &arr2)
10+
{
11+
12+
int *m1 = (int *)malloc(sizeof(int) * 1001);
13+
fill_n(m1, 1001, 0);
14+
15+
for (int i : arr1)
16+
++m1[i];
17+
18+
int *m2 = (int *)malloc(sizeof(int) * 1001);
19+
fill_n(m2, 1001, 0);
20+
21+
for (int i : arr2)
22+
++m2[i];
23+
24+
vector<int> out;
25+
vector<int> exc;
26+
27+
for (int i : arr2)
28+
{
29+
if (m2[i])
30+
{
31+
int count = m1[i];
32+
for (int j = 0; j < count; ++j)
33+
{
34+
out.push_back(i);
35+
}
36+
}
37+
}
38+
39+
for (int i = 0; i <= 1000; ++i)
40+
{
41+
if (!m2[i])
42+
{
43+
int count = m1[i];
44+
for (int j = 0; j < count; ++j)
45+
{
46+
exc.push_back(i);
47+
}
48+
}
49+
}
50+
51+
sort(exc.begin(), exc.end());
52+
for (int i : exc)
53+
out.push_back(i);
54+
55+
return out;
56+
}
57+
};
File renamed without changes.

solutions/easy/883.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
int projectionArea(vector<vector<int>> &grid)
10+
{
11+
int n = grid.size();
12+
13+
int top = 0;
14+
for (vector<int> temp : grid)
15+
{
16+
for (int i : temp)
17+
{
18+
if (i)
19+
++top;
20+
}
21+
}
22+
23+
vector<int> front;
24+
vector<int> right;
25+
for (int i = 0; i < n; i++)
26+
{
27+
front.push_back(0);
28+
right.push_back(0);
29+
for (int j = 0; j < n; j++)
30+
{
31+
front[i] = max(front[i], grid[i][j]);
32+
right[i] = max(right[i], grid[j][i]);
33+
}
34+
}
35+
36+
return top + accumulate(front.begin(), front.end(), 0) + accumulate(right.begin(), right.end(), 0);
37+
}
38+
};

solutions/easy/893.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
long int calc_even(string a)
7+
{
8+
long int product = 1;
9+
int arr[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
10+
11+
int n = a.length();
12+
for(int i = 0; i < n; i+=2) {
13+
product *= arr[(int)(a[i]) - 97];
14+
}
15+
16+
return product;
17+
}
18+
19+
long int calc_odd(string a)
20+
{
21+
long int product = 1;
22+
int arr[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
23+
24+
int n = a.length();
25+
for(int i = 1; i < n; i+=2) {
26+
product *= arr[(int)(a[i]) - 97];
27+
}
28+
29+
return product;
30+
}
31+
32+
class Solution
33+
{
34+
public:
35+
int numSpecialEquivGroups(vector<string> &A)
36+
{
37+
vector<int> matches;
38+
vector<int> even_scores;
39+
vector<int> odd_scores;
40+
41+
for(string temp: A) {
42+
even_scores.push_back(calc_even(temp));
43+
odd_scores.push_back(calc_odd(temp));
44+
}
45+
46+
int len = A.size();
47+
48+
for (int i = 0; i < len; i++)
49+
{
50+
matches.push_back(-1);
51+
for (int j = i + 1; j < len; j++)
52+
{
53+
if (even_scores[i] == even_scores[j] && odd_scores[i] == odd_scores[j])
54+
{
55+
matches[i] = j;
56+
j = len;
57+
}
58+
}
59+
}
60+
61+
int *group_id = (int *)malloc(sizeof(int) * len);
62+
fill_n(group_id, len, 0);
63+
64+
int group_number = 0;
65+
66+
for (int i = 0; i < len; i++)
67+
{
68+
if (!group_id[i])
69+
group_id[i] = ++group_number;
70+
if (matches[i] != -1)
71+
{
72+
group_id[matches[i]] = group_id[i];
73+
}
74+
}
75+
76+
return group_number;
77+
}
78+
};

src/solutions.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)