Skip to content

Commit 9debd31

Browse files
committed
add problems and solutions
1 parent 841dcde commit 9debd31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+2021
-4
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,4 @@
1818

1919
npm-debug.log*
2020
yarn-debug.log*
21-
yarn-error.log*
22-
23-
problems/
24-
solutions/
21+
yarn-error.log*

problems/1021.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/remove-outermost-parentheses", "name": "Remove Outermost Parentheses", "difficulty": "Easy", "statement": "<div><p>A valid parentheses string is either empty <code>(\"\")</code>, <code>\"(\" + A + \")\"</code>, or <code>A + B</code>, where <code>A</code> and <code>B</code> are valid parentheses strings, and <code>+</code> represents string concatenation.&nbsp; For example, <code>\"\"</code>, <code>\"()\"</code>, <code>\"(())()\"</code>, and <code>\"(()(()))\"</code> are all valid parentheses strings.</p>\n\n<p>A valid parentheses string <code>S</code> is <strong>primitive</strong> if it is nonempty, and there does not exist a way to split it into <code>S = A+B</code>, with <code>A</code> and <code>B</code> nonempty valid parentheses strings.</p>\n\n<p>Given a valid parentheses string <code>S</code>, consider its primitive decomposition: <code>S = P_1 + P_2 + ... + P_k</code>, where <code>P_i</code> are primitive valid parentheses strings.</p>\n\n<p>Return <code>S</code> after removing the outermost parentheses of every primitive string in the primitive decomposition of <code>S</code>.</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">\"(()())(())\"</span>\n<strong>Output: </strong><span id=\"example-output-1\">\"()()()\"</span>\n<strong>Explanation: </strong>\nThe input string is \"(()())(())\", with primitive decomposition \"(()())\" + \"(())\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" = \"()()()\".\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">\"(()())(())(()(()))\"</span>\n<strong>Output: </strong><span id=\"example-output-2\">\"()()()()(())\"</span>\n<strong>Explanation: </strong>\nThe input string is \"(()())(())(()(()))\", with primitive decomposition \"(()())\" + \"(())\" + \"(()(()))\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" + \"()(())\" = \"()()()()(())\".\n</pre>\n\n<div>\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-3-1\">\"()()\"</span>\n<strong>Output: </strong><span id=\"example-output-3\">\"\"</span>\n<strong>Explanation: </strong>\nThe input string is \"()()\", with primitive decomposition \"()\" + \"()\".\nAfter removing outer parentheses of each part, this is \"\" + \"\" = \"\".\n</pre>\n\n<p>&nbsp;</p>\n</div>\n</div>\n\n<p><strong>Note:</strong></p>\n\n<ol>\n\t<li><code>S.length &lt;= 10000</code></li>\n\t<li><code>S[i]</code> is <code>\"(\"</code> or <code>\")\"</code></li>\n\t<li><code>S</code> is a valid parentheses string</li>\n</ol>\n\n<div>\n<div>\n<div>&nbsp;</div>\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 string removeOuterParentheses(string S)\n {\n\n int n = S.length();\n char res[n];\n\n fill_n(res, n, '\\0');\n\n int pos = 0;\n int diff = 0;\n for (char i : S)\n {\n\n if (i == '(')\n {\n if (diff)\n {\n res[pos] = '(';\n ++pos;\n }\n ++diff;\n }\n else\n {\n --diff;\n if (diff)\n {\n res[pos] = ')';\n ++pos;\n }\n }\n }\n\n string r(res);\n\n return r;\n }\n};"}

problems/1022.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers", "name": "Sum of Root To Leaf Binary Numbers", "difficulty": "Easy", "statement": "<div><p>You are given the <code>root</code> of a binary tree where each node has a value <code>0</code>&nbsp;or <code>1</code>.&nbsp; Each root-to-leaf path represents a binary number starting with the most significant bit.&nbsp; For example, if the path is <code>0 -&gt; 1 -&gt; 1 -&gt; 0 -&gt; 1</code>, then this could represent <code>01101</code> in binary, which is <code>13</code>.</p>\n\n<p>For all leaves in the tree, consider the numbers represented by the path&nbsp;from the root to that leaf.</p>\n\n<p>Return <em>the sum of these numbers</em>. The answer is <strong>guaranteed</strong> to fit in a <strong>32-bits</strong> integer.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png\" style=\"width: 450px; height: 296px;\">\n<pre><strong>Input:</strong> root = [1,0,1,0,1,0,1]\n<strong>Output:</strong> 22\n<strong>Explanation: </strong>(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> root = [0]\n<strong>Output:</strong> 0\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> root = [1]\n<strong>Output:</strong> 1\n</pre>\n\n<p><strong>Example 4:</strong></p>\n\n<pre><strong>Input:</strong> root = [1,1]\n<strong>Output:</strong> 3\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>\n\t<li><code>Node.val</code> is <code>0</code> or <code>1</code>.</li>\n</ul>\n</div>", "language": "c", "solution": "#include <stdio.h>\n\nstruct TreeNode\n{\n int val;\n struct TreeNode *left;\n struct TreeNode *right;\n};\n\nint sumRootToLeaf(struct TreeNode *root)\n{\n return findSum(root, 0);\n}\n\nint findSum(struct TreeNode *root, int prevSum) {\n\n int sum = 0;\n\n if (root->left)\n sum += findSum(root->left, 2*prevSum + root->val);\n if (root->right)\n sum += findSum(root->right, 2*prevSum + root->val);\n\n if (!root->left && !root->right)\n return prevSum*2 + root->val;\n return sum;\n\n}"}

problems/1047.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string", "name": "Remove All Adjacent Duplicates In String", "difficulty": "Easy", "statement": "<div><p>Given a string <code>S</code> of lowercase letters, a <em>duplicate removal</em> consists of choosing two adjacent and equal letters, and removing&nbsp;them.</p>\n\n<p>We repeatedly make duplicate removals on S until we no longer can.</p>\n\n<p>Return the final string after all such duplicate removals have been made.&nbsp; It is guaranteed the answer is unique.</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">\"abbaca\"</span>\n<strong>Output: </strong><span id=\"example-output-1\">\"ca\"</span>\n<strong>Explanation: </strong>\nFor example, in \"abbaca\" we could remove \"bb\" since the letters are adjacent and equal, and this is the only possible move.&nbsp; The result of this move is that the string is \"aaca\", of which only \"aa\" is possible, so the final string is \"ca\".\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>Note:</strong></p>\n\n<ol>\n\t<li><code>1 &lt;= S.length &lt;= 20000</code></li>\n\t<li><code>S</code> consists only of English lowercase letters.</li>\n</ol></div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n string removeDuplicates(string S)\n {\n\n stringstream out;\n vector<char> s;\n for (char ch : S)\n s.push_back(ch);\n\n int n = S.length();\n\n for (int i = 0; i < n - 1; i++)\n {\n \n if (s[i] == s[i + 1])\n {\n\n s.erase(s.begin() + i+1);\n s.erase(s.begin() + i);\n\n i -= 2;\n n -= 2;\n\n if (i < -1)\n i = -1;\n\n }\n }\n\n for(char ch: s) out << ch;\n\n return out.str();\n }\n};"}

problems/1051.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/height-checker", "name": "Height Checker", "difficulty": "Easy", "statement": "<div><p>Students are asked to stand in non-decreasing order of heights for an annual photo.</p>\n\n<p>Return the minimum number of students that must move in order for all students to be standing in non-decreasing order of height.</p>\n\n<p>Notice that when a group of students is selected they can reorder in any possible way between themselves and the non selected students&nbsp;remain on their seats.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> heights = [1,1,4,2,1,3]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> \nCurrent array : [1,1,4,2,1,3]\nTarget array : [1,1,1,2,3,4]\nOn index 2 (0-based) we have 4 vs 1 so we have to move this student.\nOn index 4 (0-based) we have 1 vs 3 so we have to move this student.\nOn index 5 (0-based) we have 3 vs 4 so we have to move this student.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> heights = [5,1,2,3,4]\n<strong>Output:</strong> 5\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> heights = [1,2,3,4,5]\n<strong>Output:</strong> 0\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= heights.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= heights[i] &lt;= 100</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 int heightChecker(vector<int> &heights)\n {\n\n vector<int> dup = heights;\n sort(dup.begin(), dup.end());\n int count = 0;\n int n = heights.size();\n for (int i = 0; i < n; i++)\n {\n if (dup[i] != heights[i])\n ++count;\n }\n\n return count;\n }\n};"}

problems/1108.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/defanging-an-ip-address", "name": "Defanging an IP Address", "difficulty": "Easy", "statement": "<div><p>Given a valid (IPv4) IP <code>address</code>, return a defanged version of that IP address.</p>\n\n<p>A <em>defanged&nbsp;IP address</em>&nbsp;replaces every period <code>\".\"</code> with <code>\"[.]\"</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n<pre><strong>Input:</strong> address = \"1.1.1.1\"\n<strong>Output:</strong> \"1[.]1[.]1[.]1\"\n</pre><p><strong>Example 2:</strong></p>\n<pre><strong>Input:</strong> address = \"255.100.50.0\"\n<strong>Output:</strong> \"255[.]100[.]50[.]0\"\n</pre>\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The given <code>address</code> is a valid IPv4 address.</li>\n</ul></div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n string defangIPaddr(string address)\n {\n\n int a, b, c, d;\n\n char arr[address.length() + 1];\n\n strcpy(arr, address.c_str());\n\n sscanf(arr, \"%d.%d.%d.%d\", &a, &b, &c, &d);\n\n char res_arr[address.length() + 1 + 10];\n\n sprintf(res_arr, \"%d[.]%d[.]%d[.]%d\", a, b, c, d);\n\n string res(res_arr);\n\n return res;\n }\n};"}

problems/1207.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/unique-number-of-occurrences", "name": "Unique Number of Occurrences", "difficulty": "Easy", "statement": "<div><p>Given an array of integers <code>arr</code>,&nbsp;write a function that returns <code>true</code> if and only if the number of occurrences of each value in the array is unique.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> arr = [1,2,2,1,1,3]\n<strong>Output:</strong> true\n<strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> arr = [1,2]\n<strong>Output:</strong> false\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]\n<strong>Output:</strong> true\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr.length&nbsp;&lt;= 1000</code></li>\n\t<li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li>\n</ul>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n bool uniqueOccurrences(vector<int>& arr) {\n\n vector<int> count;\n for(int i = 0; i < 2001; i++) {\n count.push_back(0);\n }\n\n for (int i: arr)\n ++count[i+1000];\n\n sort(count.begin(), count.end());\n int n = count.size();\n \n for(int i = 0; i < n - 1; i++) {\n if (count[i] > 0 && count[i] == count[i+1]) return false;\n }\n return true;\n \n }\n};"}

problems/1221.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/split-a-string-in-balanced-strings", "name": "Split a String in Balanced Strings", "difficulty": "Easy", "statement": "<div><p><i data-stringify-type=\"italic\">Balanced</i>&nbsp;strings are those who have equal quantity of 'L' and 'R' characters.</p>\n\n<p>Given a balanced string&nbsp;<code data-stringify-type=\"code\">s</code>&nbsp;split it in the maximum amount of balanced strings.</p>\n\n<p>Return the maximum amount of splitted balanced strings.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> s = \"RLRRLLRLRL\"\n<strong>Output:</strong> 4\n<strong>Explanation: </strong>s can be split into \"RL\", \"RRLL\", \"RL\", \"RL\", each substring contains same number of 'L' and 'R'.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> s = \"RLLLLRRRLR\"\n<strong>Output:</strong> 3\n<strong>Explanation: </strong>s can be split into \"RL\", \"LLLRRR\", \"LR\", each substring contains same number of 'L' and 'R'.\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> s = \"LLLLRRRR\"\n<strong>Output:</strong> 1\n<strong>Explanation: </strong>s can be split into \"LLLLRRRR\".\n</pre>\n\n<p><strong>Example 4:</strong></p>\n\n<pre><strong>Input:</strong> s = \"RLRRRLLRLL\"\n<strong>Output:</strong> 2\n<strong>Explanation: </strong>s can be split into \"RL\", \"RRRLLRLL\", since each substring contains an equal number of 'L' and 'R'\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 1000</code></li>\n\t<li><code>s[i] = 'L' or 'R'</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 int balancedStringSplit(string s)\n {\n\n int count = 0;\n int diff = 0;\n int n = s.length();\n\n for(char i: s) {\n if (i == 'L') diff--;\n else diff++;\n\n if (diff == 0){\n count ++;\n }\n }\n\n return count;\n }\n};"}

problems/1237.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation", "name": "Find Positive Integer Solution for a Given Equation", "difficulty": "Easy", "statement": "<div><p>Given a&nbsp;function&nbsp; <code>f(x, y)</code>&nbsp;and a value <code>z</code>, return all positive integer&nbsp;pairs <code>x</code> and <code>y</code> where <code>f(x,y) == z</code>.</p>\n\n<p>The function is constantly increasing, i.e.:</p>\n\n<ul>\n\t<li><code>f(x, y) &lt; f(x + 1, y)</code></li>\n\t<li><code>f(x, y) &lt; f(x, y + 1)</code></li>\n</ul>\n\n<p>The function interface is defined like this:&nbsp;</p>\n\n<pre>interface CustomFunction {\npublic:\n&nbsp; // Returns positive integer f(x, y) for any given positive integer x and y.\n&nbsp; int f(int x, int y);\n};\n</pre>\n\n<p>For custom testing purposes you're given an integer <code>function_id</code> and a target <code>z</code> as input, where <code>function_id</code> represent one function from an secret internal list, on the examples you'll know only two functions from the list. &nbsp;</p>\n\n<p>You may return the solutions in any order.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> function_id = 1, z = 5\n<strong>Output:</strong> [[1,4],[2,3],[3,2],[4,1]]\n<strong>Explanation:</strong>&nbsp;function_id = 1 means that f(x, y) = x + y</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> function_id = 2, z = 5\n<strong>Output:</strong> [[1,5],[5,1]]\n<strong>Explanation:</strong>&nbsp;function_id = 2 means that f(x, y) = x * y\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= function_id &lt;= 9</code></li>\n\t<li><code>1 &lt;= z &lt;= 100</code></li>\n\t<li>It's guaranteed that the solutions of <code>f(x, y) == z</code> will be on the range <code>1 &lt;= x, y &lt;= 1000</code></li>\n\t<li>It's also guaranteed that <code>f(x, y)</code> will fit in 32 bit signed integer if <code>1 &lt;= x, y &lt;= 1000</code></li>\n</ul>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass CustomFunction\n{\npublic:\n // Returns f(x, y) for any given positive integers x and y.\n // Note that f(x, y) is increasing with respect to both x and y.\n // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)\n int f(int x, int y);\n};\n\nclass Solution\n{\npublic:\n vector<vector<int>> findSolution(CustomFunction &customfunction, int z)\n {\n\n vector<vector<int>> res;\n for (int x = 1; x <= 1000; x++)\n {\n for (int y = 1; y <= 1000; y++)\n {\n int val = customfunction.f(x, y);\n if (val == z)\n {\n vector<int> r = {x, y};\n res.push_back(r);\n break;\n }\n if (val > z)\n {\n break;\n }\n }\n }\n\n return res;\n }\n};"}

0 commit comments

Comments
 (0)