diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 9f6f0d0..0000000 --- a/.gitignore +++ /dev/null @@ -1,21 +0,0 @@ -# dependencies -/node_modules -/.pnp -.pnp.js - -# testing -/coverage - -# production -/build - -# misc -.DS_Store -.env.local -.env.development.local -.env.test.local -.env.production.local - -npm-debug.log* -yarn-debug.log* -yarn-error.log* \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 72147a7..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Leetcode Solutions - -An application to display my leetcode solutions. -Try it out at [temanisparsh.github.io/leetcode](https://temanisparsh.github.io/leetcode) or access the Android app at [this link](https://s3.amazonaws.com/gonativeio/static/5f5a06c6fa107e59c7d20838/app-release.apk). \ No newline at end of file diff --git a/asset-manifest.json b/asset-manifest.json new file mode 100644 index 0000000..a0954d4 --- /dev/null +++ b/asset-manifest.json @@ -0,0 +1,25 @@ +{ + "files": { + "main.css": "/leetcode/static/css/main.bca8e935.chunk.css", + "main.js": "/leetcode/static/js/main.6d289f42.chunk.js", + "main.js.map": "/leetcode/static/js/main.6d289f42.chunk.js.map", + "runtime-main.js": "/leetcode/static/js/runtime-main.bca5a1f5.js", + "runtime-main.js.map": "/leetcode/static/js/runtime-main.bca5a1f5.js.map", + "static/css/2.d9ad5f5c.chunk.css": "/leetcode/static/css/2.d9ad5f5c.chunk.css", + "static/js/2.8c98df72.chunk.js": "/leetcode/static/js/2.8c98df72.chunk.js", + "static/js/2.8c98df72.chunk.js.map": "/leetcode/static/js/2.8c98df72.chunk.js.map", + "index.html": "/leetcode/index.html", + "precache-manifest.c18e282f527e738ff22f58d8288588ee.js": "/leetcode/precache-manifest.c18e282f527e738ff22f58d8288588ee.js", + "service-worker.js": "/leetcode/service-worker.js", + "static/css/2.d9ad5f5c.chunk.css.map": "/leetcode/static/css/2.d9ad5f5c.chunk.css.map", + "static/css/main.bca8e935.chunk.css.map": "/leetcode/static/css/main.bca8e935.chunk.css.map", + "static/js/2.8c98df72.chunk.js.LICENSE.txt": "/leetcode/static/js/2.8c98df72.chunk.js.LICENSE.txt" + }, + "entrypoints": [ + "static/js/runtime-main.bca5a1f5.js", + "static/css/2.d9ad5f5c.chunk.css", + "static/js/2.8c98df72.chunk.js", + "static/css/main.bca8e935.chunk.css", + "static/js/main.6d289f42.chunk.js" + ] +} \ No newline at end of file diff --git a/dump_solutions.py b/dump_solutions.py deleted file mode 100644 index ec4e4be..0000000 --- a/dump_solutions.py +++ /dev/null @@ -1,16 +0,0 @@ -import os -import json - -problem_dict = {} -problem_files = os.listdir('./problems') - -for file in problem_files: - - filename = file.split('.')[0] - - with open('./problems/'+file, 'r') as json_file: - problem_dict[filename] = json.loads(json_file.read()) - problem_dict[filename]['number'] = filename - -with open('./src/solutions.json', 'w') as json_file: - json.dump(problem_dict, json_file) \ No newline at end of file diff --git a/public/favicon.ico b/favicon.ico similarity index 100% rename from public/favicon.ico rename to favicon.ico diff --git a/index.html b/index.html new file mode 100644 index 0000000..685e8c1 --- /dev/null +++ b/index.html @@ -0,0 +1 @@ +
Given an array A
of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
You may return the answer in any order.
\n\n\n\n
Example 1:
\n\nInput: [\"bella\",\"label\",\"roller\"]\nOutput: [\"e\",\"l\",\"l\"]\n\n\n
Example 2:
\n\nInput: [\"cool\",\"lock\",\"cook\"]\nOutput: [\"c\",\"o\"]\n\n\n
\n\n
Note:
\n\n1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j]
is a lowercase letterA valid parentheses string is either empty (\"\")
, \"(\" + A + \")\"
, or A + B
, where A
and B
are valid parentheses strings, and +
represents string concatenation. For example, \"\"
, \"()\"
, \"(())()\"
, and \"(()(()))\"
are all valid parentheses strings.
A valid parentheses string S
is primitive if it is nonempty, and there does not exist a way to split it into S = A+B
, with A
and B
nonempty valid parentheses strings.
Given a valid parentheses string S
, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k
, where P_i
are primitive valid parentheses strings.
Return S
after removing the outermost parentheses of every primitive string in the primitive decomposition of S
.
\n\n
Example 1:
\n\nInput: \"(()())(())\"\nOutput: \"()()()\"\nExplanation: \nThe input string is \"(()())(())\", with primitive decomposition \"(()())\" + \"(())\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" = \"()()()\".\n\n\n
Example 2:
\n\nInput: \"(()())(())(()(()))\"\nOutput: \"()()()()(())\"\nExplanation: \nThe input string is \"(()())(())(()(()))\", with primitive decomposition \"(()())\" + \"(())\" + \"(()(()))\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" + \"()(())\" = \"()()()()(())\".\n\n\n
Example 3:
\n\nInput: \"()()\"\nOutput: \"\"\nExplanation: \nThe input string is \"()()\", with primitive decomposition \"()\" + \"()\".\nAfter removing outer parentheses of each part, this is \"\" + \"\" = \"\".\n\n\n
\n
Note:
\n\nS.length <= 10000
S[i]
is \"(\"
or \")\"
S
is a valid parentheses stringYou are given the root
of a binary tree where each node has a value 0
or 1
. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1
, then this could represent 01101
in binary, which is 13
.
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
\n\nReturn the sum of these numbers. The answer is guaranteed to fit in a 32-bits integer.
\n\n\n
Example 1:
\nInput: root = [1,0,1,0,1,0,1]\nOutput: 22\nExplanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22\n\n\n
Example 2:
\n\nInput: root = [0]\nOutput: 0\n\n\n
Example 3:
\n\nInput: root = [1]\nOutput: 1\n\n\n
Example 4:
\n\nInput: root = [1,1]\nOutput: 3\n\n\n
\n
Constraints:
\n\n[1, 1000]
.Node.val
is 0
or 1
.Given a string S
of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
We repeatedly make duplicate removals on S until we no longer can.
\n\nReturn the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
\n\n\n\n
Example 1:
\n\nInput: \"abbaca\"\nOutput: \"ca\"\nExplanation: \nFor example, in \"abbaca\" we could remove \"bb\" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is \"aaca\", of which only \"aa\" is possible, so the final string is \"ca\".\n\n\n
\n\n
Note:
\n\n1 <= S.length <= 20000
S
consists only of English lowercase letters.Students are asked to stand in non-decreasing order of heights for an annual photo.
\n\nReturn the minimum number of students that must move in order for all students to be standing in non-decreasing order of height.
\n\nNotice that when a group of students is selected they can reorder in any possible way between themselves and the non selected students remain on their seats.
\n\n\n
Example 1:
\n\nInput: heights = [1,1,4,2,1,3]\nOutput: 3\nExplanation: \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\n\n
Example 2:
\n\nInput: heights = [5,1,2,3,4]\nOutput: 5\n\n\n
Example 3:
\n\nInput: heights = [1,2,3,4,5]\nOutput: 0\n\n\n
\n
Constraints:
\n\n1 <= heights.length <= 100
1 <= heights[i] <= 100
Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period \".\"
with \"[.]\"
.
\n
Example 1:
\nInput: address = \"1.1.1.1\"\nOutput: \"1[.]1[.]1[.]1\"\n
Example 2:
\nInput: address = \"255.100.50.0\"\nOutput: \"255[.]100[.]50[.]0\"\n\n
\n
Constraints:
\n\naddress
is a valid IPv4 address.Given two arrays arr1
and arr2
, the elements of arr2
are distinct, and all elements in arr2
are also in arr1
.
Sort the elements of arr1
such that the relative ordering of items in arr1
are the same as in arr2
. Elements that don't appear in arr2
should be placed at the end of arr1
in ascending order.
\n
Example 1:
\nInput: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]\nOutput: [2,2,2,1,4,3,3,9,6,7,19]\n\n
\n
Constraints:
\n\narr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
arr2[i]
is distinct.arr2[i]
is in arr1
.You are given an array of strings words
and a string chars
.
A string is good if it can be formed by characters from chars
(each character can only be used once).
Return the sum of lengths of all good strings in words
.
\n\n
Example 1:
\n\nInput: words = [\"cat\",\"bt\",\"hat\",\"tree\"], chars = \"atach\"\nOutput: 6\nExplanation: \nThe strings that can be formed are \"cat\" and \"hat\" so the answer is 3 + 3 = 6.\n\n\n
Example 2:
\n\nInput: words = [\"hello\",\"world\",\"leetcode\"], chars = \"welldonehoneyr\"\nOutput: 10\nExplanation: \nThe strings that can be formed are \"hello\" and \"world\" so the answer is 5 + 5 = 10.\n\n\n
\n\n
Note:
\n\n1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
Given an array of integers arr
, write a function that returns true
if and only if the number of occurrences of each value in the array is unique.
\n
Example 1:
\n\nInput: arr = [1,2,2,1,1,3]\nOutput: true\nExplanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.\n\n
Example 2:
\n\nInput: arr = [1,2]\nOutput: false\n\n\n
Example 3:
\n\nInput: arr = [-3,0,1,-3,1,1,1,-3,10,0]\nOutput: true\n\n\n
\n
Constraints:
\n\n1 <= arr.length <= 1000
-1000 <= arr[i] <= 1000
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
\n\nGiven a balanced string s
split it in the maximum amount of balanced strings.
Return the maximum amount of splitted balanced strings.
\n\n\n
Example 1:
\n\nInput: s = \"RLRRLLRLRL\"\nOutput: 4\nExplanation: s can be split into \"RL\", \"RRLL\", \"RL\", \"RL\", each substring contains same number of 'L' and 'R'.\n\n\n
Example 2:
\n\nInput: s = \"RLLLLRRRLR\"\nOutput: 3\nExplanation: s can be split into \"RL\", \"LLLRRR\", \"LR\", each substring contains same number of 'L' and 'R'.\n\n\n
Example 3:
\n\nInput: s = \"LLLLRRRR\"\nOutput: 1\nExplanation: s can be split into \"LLLLRRRR\".\n\n\n
Example 4:
\n\nInput: s = \"RLRRRLLRLL\"\nOutput: 2\nExplanation: s can be split into \"RL\", \"RRRLLRLL\", since each substring contains an equal number of 'L' and 'R'\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 1000
s[i] = 'L' or 'R'
Given a function f(x, y)
and a value z
, return all positive integer pairs x
and y
where f(x,y) == z
.
The function is constantly increasing, i.e.:
\n\nf(x, y) < f(x + 1, y)
f(x, y) < f(x, y + 1)
The function interface is defined like this:
\n\ninterface CustomFunction {\npublic:\n // Returns positive integer f(x, y) for any given positive integer x and y.\n int f(int x, int y);\n};\n\n\n
For custom testing purposes you're given an integer function_id
and a target z
as input, where function_id
represent one function from an secret internal list, on the examples you'll know only two functions from the list.
You may return the solutions in any order.
\n\n\n
Example 1:
\n\nInput: function_id = 1, z = 5\nOutput: [[1,4],[2,3],[3,2],[4,1]]\nExplanation: function_id = 1 means that f(x, y) = x + y\n\n
Example 2:
\n\nInput: function_id = 2, z = 5\nOutput: [[1,5],[5,1]]\nExplanation: function_id = 2 means that f(x, y) = x * y\n\n\n
\n
Constraints:
\n\n1 <= function_id <= 9
1 <= z <= 100
f(x, y) == z
will be on the range 1 <= x, y <= 1000
f(x, y)
will fit in 32 bit signed integer if 1 <= x, y <= 1000
Given n
and m
which are the dimensions of a matrix initialized by zeros and given an array indices
where indices[i] = [ri, ci]
. For each pair of [ri, ci]
you have to increment all cells in row ri
and column ci
by 1.
Return the number of cells with odd values in the matrix after applying the increment to all indices
.
\n
Example 1:
\nInput: n = 2, m = 3, indices = [[0,1],[1,1]]\nOutput: 6\nExplanation: Initial matrix = [[0,0,0],[0,0,0]].\nAfter applying first increment it becomes [[1,2,1],[0,1,0]].\nThe final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.\n\n\n
Example 2:
\nInput: n = 2, m = 2, indices = [[1,1],[0,0]]\nOutput: 0\nExplanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.\n\n\n
\n
Constraints:
\n\n1 <= n <= 50
1 <= m <= 50
1 <= indices.length <= 100
0 <= indices[i][0] < n
0 <= indices[i][1] < m
On a plane there are n
points with integer coordinates points[i] = [xi, yi]
. Your task is to find the minimum time in seconds to visit all points.
You can move according to the next rules:
\n\n\n
Example 1:
\nInput: points = [[1,1],[3,4],[-1,0]]\nOutput: 7\nExplanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0] \nTime from [1,1] to [3,4] = 3 seconds \nTime from [3,4] to [-1,0] = 4 seconds\nTotal time = 7 seconds\n\n
Example 2:
\n\nInput: points = [[3,2],[-2,2]]\nOutput: 5\n\n\n
\n
Constraints:
\n\npoints.length == n
1 <= n <= 100
points[i].length == 2
-1000 <= points[i][0], points[i][1] <= 1000
n
, return the difference between the product of its digits and the sum of its digits.\n\n
Example 1:
\n\nInput: n = 234\nOutput: 15 \nExplanation: \nProduct of digits = 2 * 3 * 4 = 24 \nSum of digits = 2 + 3 + 4 = 9 \nResult = 24 - 9 = 15\n\n\n
Example 2:
\n\nInput: n = 4421\nOutput: 21\nExplanation: \nProduct of digits = 4 * 4 * 2 * 1 = 32 \nSum of digits = 4 + 4 + 2 + 1 = 11 \nResult = 32 - 11 = 21\n\n\n
\n
Constraints:
\n\n1 <= n <= 10^5
Given head
which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list.
\n\n\n
Example 1:
\nInput: head = [1,0,1]\nOutput: 5\nExplanation: (101) in base 2 = (5) in base 10\n\n\n
Example 2:
\n\nInput: head = [0]\nOutput: 0\n\n\n
Example 3:
\n\nInput: head = [1]\nOutput: 1\n\n\n
Example 4:
\n\nInput: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]\nOutput: 18880\n\n\n
Example 5:
\n\nInput: head = [0,0]\nOutput: 0\n\n\n
\n
Constraints:
\n\n30
.0
or 1
.nums
of integers, return how many of them contain an even number of digits.\n\n
Example 1:
\n\nInput: nums = [12,345,2,6,7896]\nOutput: 2\nExplanation: \n12 contains 2 digits (even number of digits). \n345 contains 3 digits (odd number of digits). \n2 contains 1 digit (odd number of digits). \n6 contains 1 digit (odd number of digits). \n7896 contains 4 digits (even number of digits). \nTherefore only 12 and 7896 contain an even number of digits.\n\n\n
Example 2:
\n\nInput: nums = [555,901,482,1771]\nOutput: 1 \nExplanation: \nOnly 1771 contains an even number of digits.\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 500
1 <= nums[i] <= 10^5
Given an array arr
, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1
.
After doing so, return the array.
\n\n\n
Example 1:
\nInput: arr = [17,18,5,4,6,1]\nOutput: [18,6,6,6,1,-1]\n\n
\n
Constraints:
\n\n1 <= arr.length <= 10^4
1 <= arr[i] <= 10^5
Given an integer n
, return any array containing n
unique integers such that they add up to 0.
\n
Example 1:
\n\nInput: n = 5\nOutput: [-7,-1,1,3,4]\nExplanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].\n\n\n
Example 2:
\n\nInput: n = 3\nOutput: [-1,0,1]\n\n\n
Example 3:
\n\nInput: n = 1\nOutput: [0]\n\n\n
\n
Constraints:
\n\n1 <= n <= 1000
Given a string s
formed by digits ('0'
- '9'
) and '#'
. We want to map s
to English lowercase characters as follows:
'a'
to 'i')
are represented by ('1'
to '9'
) respectively.'j'
to 'z')
are represented by ('10#'
to '26#'
) respectively. Return the string formed after mapping.
\n\nIt's guaranteed that a unique mapping will always exist.
\n\n\n
Example 1:
\n\nInput: s = \"10#11#12\"\nOutput: \"jkab\"\nExplanation: \"j\" -> \"10#\" , \"k\" -> \"11#\" , \"a\" -> \"1\" , \"b\" -> \"2\".\n\n\n
Example 2:
\n\nInput: s = \"1326#\"\nOutput: \"acz\"\n\n\n
Example 3:
\n\nInput: s = \"25#\"\nOutput: \"y\"\n\n\n
Example 4:
\n\nInput: s = \"12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#\"\nOutput: \"abcdefghijklmnopqrstuvwxyz\"\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 1000
s[i]
only contains digits letters ('0'
-'9'
) and '#'
letter.s
will be valid string such that mapping is always possible.We are given a list nums
of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]]
(with i >= 0
). For each such pair, there are freq
elements with value val
concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
Return the decompressed list.
\n\n\n
Example 1:
\n\nInput: nums = [1,2,3,4]\nOutput: [2,4,4,4]\nExplanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array [2].\nThe second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4].\nAt the end the concatenation [2] + [4,4,4] is [2,4,4,4].\n\n\n
Example 2:
\n\nInput: nums = [1,1,2,3]\nOutput: [1,3,3]\n\n\n
\n
Constraints:
\n\n2 <= nums.length <= 100
nums.length % 2 == 0
1 <= nums[i] <= 100
Given a positive integer num
consisting only of digits 6 and 9.
Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).
\n\n\n
Example 1:
\n\nInput: num = 9669\nOutput: 9969\nExplanation: \nChanging the first digit results in 6669.\nChanging the second digit results in 9969.\nChanging the third digit results in 9699.\nChanging the fourth digit results in 9666. \nThe maximum number is 9969.\n\n\n
Example 2:
\n\nInput: num = 9996\nOutput: 9999\nExplanation: Changing the last digit 6 to 9 results in the maximum number.\n\n
Example 3:
\n\nInput: num = 9999\nOutput: 9999\nExplanation: It is better not to apply any change.\n\n
\n
Constraints:
\n\n1 <= num <= 10^4
num
's digits are 6 or 9.Given a m * n
matrix mat
of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k
weakest rows in the matrix ordered from the weakest to the strongest.
A row i is weaker than row j, if the number of soldiers in row i is less than the number of soldiers in row j, or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row, that is, always ones may appear first and then zeros.
\n\n\n
Example 1:
\n\nInput: mat = \n[[1,1,0,0,0],\n [1,1,1,1,0],\n [1,0,0,0,0],\n [1,1,0,0,0],\n [1,1,1,1,1]], \nk = 3\nOutput: [2,0,3]\nExplanation: \nThe number of soldiers for each row is: \nrow 0 -> 2 \nrow 1 -> 4 \nrow 2 -> 1 \nrow 3 -> 2 \nrow 4 -> 5 \nRows ordered from the weakest to the strongest are [2,0,3,1,4]\n\n\n
Example 2:
\n\nInput: mat = \n[[1,0,0,0],\n [1,1,1,1],\n [1,0,0,0],\n [1,0,0,0]], \nk = 2\nOutput: [0,2]\nExplanation: \nThe number of soldiers for each row is: \nrow 0 -> 1 \nrow 1 -> 4 \nrow 2 -> 1 \nrow 3 -> 1 \nRows ordered from the weakest to the strongest are [0,2,3,1]\n\n\n
\n
Constraints:
\n\nm == mat.length
n == mat[i].length
2 <= n, m <= 100
1 <= k <= m
matrix[i][j]
is either 0 or 1.Given a non-negative integer num
, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
\n
Example 1:
\n\nInput: num = 14\nOutput: 6\nExplanation: \nStep 1) 14 is even; divide by 2 and obtain 7. \nStep 2) 7 is odd; subtract 1 and obtain 6.\nStep 3) 6 is even; divide by 2 and obtain 3. \nStep 4) 3 is odd; subtract 1 and obtain 2. \nStep 5) 2 is even; divide by 2 and obtain 1. \nStep 6) 1 is odd; subtract 1 and obtain 0.\n\n\n
Example 2:
\n\nInput: num = 8\nOutput: 4\nExplanation: \nStep 1) 8 is even; divide by 2 and obtain 4. \nStep 2) 4 is even; divide by 2 and obtain 2. \nStep 3) 2 is even; divide by 2 and obtain 1. \nStep 4) 1 is odd; subtract 1 and obtain 0.\n\n\n
Example 3:
\n\nInput: num = 123\nOutput: 12\n\n\n
\n
Constraints:
\n\n0 <= num <= 10^6
Given a m * n
matrix grid
which is sorted in non-increasing order both row-wise and column-wise.
Return the number of negative numbers in grid
.
\n
Example 1:
\n\nInput: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]\nOutput: 8\nExplanation: There are 8 negatives number in the matrix.\n\n\n
Example 2:
\n\nInput: grid = [[3,2],[1,0]]\nOutput: 0\n\n\n
Example 3:
\n\nInput: grid = [[1,-1],[-1,-1]]\nOutput: 3\n\n\n
Example 4:
\n\nInput: grid = [[-1]]\nOutput: 1\n\n\n
\n
Constraints:
\n\nm == grid.length
n == grid[i].length
1 <= m, n <= 100
-100 <= grid[i][j] <= 100
Given an integer array arr
. You have to sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.
Return the sorted array.
\n\n\n
Example 1:
\n\nInput: arr = [0,1,2,3,4,5,6,7,8]\nOutput: [0,1,2,4,8,3,5,6,7]\nExplantion: [0] is the only integer with 0 bits.\n[1,2,4,8] all have 1 bit.\n[3,5,6] have 2 bits.\n[7] has 3 bits.\nThe sorted array by bits is [0,1,2,4,8,3,5,6,7]\n\n\n
Example 2:
\n\nInput: arr = [1024,512,256,128,64,32,16,8,4,2,1]\nOutput: [1,2,4,8,16,32,64,128,256,512,1024]\nExplantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.\n\n\n
Example 3:
\n\nInput: arr = [10000,10000]\nOutput: [10000,10000]\n\n\n
Example 4:
\n\nInput: arr = [2,3,5,7,11,13,17,19]\nOutput: [2,3,5,17,7,11,13,19]\n\n\n
Example 5:
\n\nInput: arr = [10,100,1000,10000]\nOutput: [10,100,10000,1000]\n\n\n
\n
Constraints:
\n\n1 <= arr.length <= 500
0 <= arr[i] <= 10^4
Given the array nums
, for each nums[i]
find out how many numbers in the array are smaller than it. That is, for each nums[i]
you have to count the number of valid j's
such that j != i
and nums[j] < nums[i]
.
Return the answer in an array.
\n\n\n
Example 1:
\n\nInput: nums = [8,1,2,2,3]\nOutput: [4,0,1,1,3]\nExplanation: \nFor nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3). \nFor nums[1]=1 does not exist any smaller number than it.\nFor nums[2]=2 there exist one smaller number than it (1). \nFor nums[3]=2 there exist one smaller number than it (1). \nFor nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).\n\n\n
Example 2:
\n\nInput: nums = [6,5,4,8]\nOutput: [2,1,0,3]\n\n\n
Example 3:
\n\nInput: nums = [7,7,7,7]\nOutput: [0,0,0,0]\n\n\n
\n
Constraints:
\n\n2 <= nums.length <= 500
0 <= nums[i] <= 100
Given a string s
. You should re-order the string using the following algorithm:
s
and append it to the result.s
which is greater than the last appended character to the result and append it.s
and append it to the result.s
which is smaller than the last appended character to the result and append it.s
.In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
\n\nReturn the result string after sorting s
with this algorithm.
\n
Example 1:
\n\nInput: s = \"aaaabbbbcccc\"\nOutput: \"abccbaabccba\"\nExplanation: After steps 1, 2 and 3 of the first iteration, result = \"abc\"\nAfter steps 4, 5 and 6 of the first iteration, result = \"abccba\"\nFirst iteration is done. Now s = \"aabbcc\" and we go back to step 1\nAfter steps 1, 2 and 3 of the second iteration, result = \"abccbaabc\"\nAfter steps 4, 5 and 6 of the second iteration, result = \"abccbaabccba\"\n\n\n
Example 2:
\n\nInput: s = \"rat\"\nOutput: \"art\"\nExplanation: The word \"rat\" becomes \"art\" after re-ordering it with the mentioned algorithm.\n\n\n
Example 3:
\n\nInput: s = \"leetcode\"\nOutput: \"cdelotee\"\n\n\n
Example 4:
\n\nInput: s = \"ggggggg\"\nOutput: \"ggggggg\"\n\n\n
Example 5:
\n\nInput: s = \"spo\"\nOutput: \"ops\"\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 500
s
contains only lower-case English letters.Given an integer n
, return a string with n
characters such that each character in such string occurs an odd number of times.
The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.
\n\n\n
Example 1:
\n\nInput: n = 4\nOutput: \"pppz\"\nExplanation: \"pppz\" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as \"ohhh\" and \"love\".\n\n\n
Example 2:
\n\nInput: n = 2\nOutput: \"xy\"\nExplanation: \"xy\" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as \"ag\" and \"ur\".\n\n\n
Example 3:
\n\nInput: n = 7\nOutput: \"holasss\"\n\n\n
\n
Constraints:
\n\n1 <= n <= 500
Given a m * n
matrix of distinct numbers, return all lucky numbers in the matrix in any order.
A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
\n\n\n
Example 1:
\n\nInput: matrix = [[3,7,8],[9,11,13],[15,16,17]]\nOutput: [15]\nExplanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column\n\n\n
Example 2:
\n\nInput: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]\nOutput: [12]\nExplanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.\n\n\n
Example 3:
\n\nInput: matrix = [[7,8],[1,2]]\nOutput: [7]\n\n\n
\n
Constraints:
\n\nm == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 10^5
.Given two arrays of integers nums
and index
. Your task is to create target array under the following rules:
index[i]
the value nums[i]
in target array.nums
and index.
Return the target array.
\n\nIt is guaranteed that the insertion operations will be valid.
\n\n\n
Example 1:
\n\nInput: nums = [0,1,2,3,4], index = [0,1,2,2,1]\nOutput: [0,4,1,3,2]\nExplanation:\nnums index target\n0 0 [0]\n1 1 [0,1]\n2 2 [0,1,2]\n3 2 [0,1,3,2]\n4 1 [0,4,1,3,2]\n\n\n
Example 2:
\n\nInput: nums = [1,2,3,4,0], index = [0,1,2,3,0]\nOutput: [0,1,2,3,4]\nExplanation:\nnums index target\n1 0 [1]\n2 1 [1,2]\n3 2 [1,2,3]\n4 3 [1,2,3,4]\n0 0 [0,1,2,3,4]\n\n\n
Example 3:
\n\nInput: nums = [1], index = [0]\nOutput: [1]\n\n\n
\n
Constraints:
\n\n1 <= nums.length, index.length <= 100
nums.length == index.length
0 <= nums[i] <= 100
0 <= index[i] <= i
Given the array nums
, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.
If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.
\n\nNote that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.
\n\n\n
Example 1:
\n\nInput: nums = [4,3,10,9,8]\nOutput: [10,9] \nExplanation: The subsequences [10,9] and [10,8] are minimal such that the sum of their elements is strictly greater than the sum of elements not included, however, the subsequence [10,9] has the maximum total sum of its elements. \n\n\n
Example 2:
\n\nInput: nums = [4,4,7,6,7]\nOutput: [7,7,6] \nExplanation: The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to returned in non-decreasing order. \n\n\n
Example 3:
\n\nInput: nums = [6]\nOutput: [6]\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 500
1 <= nums[i] <= 100
Given the array candies
and the integer extraCandies
, where candies[i]
represents the number of candies that the ith kid has.
For each kid check if there is a way to distribute extraCandies
among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.
\n
Example 1:
\n\nInput: candies = [2,3,5,1,3], extraCandies = 3\nOutput: [true,true,true,false,true] \nExplanation: \nKid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids. \nKid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. \nKid 3 has 5 candies and this is already the greatest number of candies among the kids. \nKid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies. \nKid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. \n\n\n
Example 2:
\n\nInput: candies = [4,2,1,1,2], extraCandies = 1\nOutput: [true,false,false,false,false] \nExplanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.\n\n\n
Example 3:
\n\nInput: candies = [12,1,12], extraCandies = 10\nOutput: [true,false,true]\n\n\n
\n
Constraints:
\n\n2 <= candies.length <= 100
1 <= candies[i] <= 100
1 <= extraCandies <= 50
You are given the array paths
, where paths[i] = [cityAi, cityBi]
means there exists a direct path going from cityAi
to cityBi
. Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
\n\n\n
Example 1:
\n\nInput: paths = [[\"London\",\"New York\"],[\"New York\",\"Lima\"],[\"Lima\",\"Sao Paulo\"]]\nOutput: \"Sao Paulo\" \nExplanation: Starting at \"London\" city you will reach \"Sao Paulo\" city which is the destination city. Your trip consist of: \"London\" -> \"New York\" -> \"Lima\" -> \"Sao Paulo\".\n\n\n
Example 2:
\n\nInput: paths = [[\"B\",\"C\"],[\"D\",\"B\"],[\"C\",\"A\"]]\nOutput: \"A\"\nExplanation: All possible trips are: \n\"D\" -> \"B\" -> \"C\" -> \"A\". \n\"B\" -> \"C\" -> \"A\". \n\"C\" -> \"A\". \n\"A\". \nClearly the destination city is \"A\".\n\n\n
Example 3:
\n\nInput: paths = [[\"A\",\"Z\"]]\nOutput: \"Z\"\n\n\n
\n
Constraints:
\n\n1 <= paths.length <= 100
paths[i].length == 2
1 <= cityAi.length, cityBi.length <= 10
cityAi != cityBi
Given an array target
and an integer n
. In each iteration, you will read a number from list = {1,2,3..., n}
.
Build the target
array using the following operations:
list
, and push it in the array.You are guaranteed that the target array is strictly increasing, only containing numbers between 1 to n
inclusive.
Return the operations to build the target array.
\n\nYou are guaranteed that the answer is unique.
\n\n\n
Example 1:
\n\nInput: target = [1,3], n = 3\nOutput: [\"Push\",\"Push\",\"Pop\",\"Push\"]\nExplanation: \nRead number 1 and automatically push in the array -> [1]\nRead number 2 and automatically push in the array then Pop it -> [1]\nRead number 3 and automatically push in the array -> [1,3]\n\n\n
Example 2:
\n\nInput: target = [1,2,3], n = 3\nOutput: [\"Push\",\"Push\",\"Push\"]\n\n\n
Example 3:
\n\nInput: target = [1,2], n = 4\nOutput: [\"Push\",\"Push\"]\nExplanation: You only need to read the first 2 numbers and stop.\n\n\n
Example 4:
\n\nInput: target = [2,3,4], n = 4\nOutput: [\"Push\",\"Pop\",\"Push\",\"Push\",\"Push\"]\n\n\n
\n
Constraints:
\n\n1 <= target.length <= 100
1 <= target[i] <= 100
1 <= n <= 100
target
is strictly increasing.Given two integer arrays startTime
and endTime
and given an integer queryTime
.
The ith
student started doing their homework at the time startTime[i]
and finished it at time endTime[i]
.
Return the number of students doing their homework at time queryTime
. More formally, return the number of students where queryTime
lays in the interval [startTime[i], endTime[i]]
inclusive.
\n
Example 1:
\n\nInput: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4\nOutput: 1\nExplanation: We have 3 students where:\nThe first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.\nThe second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.\nThe third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.\n\n\n
Example 2:
\n\nInput: startTime = [4], endTime = [4], queryTime = 4\nOutput: 1\nExplanation: The only student was doing their homework at the queryTime.\n\n\n
Example 3:
\n\nInput: startTime = [4], endTime = [4], queryTime = 5\nOutput: 0\n\n\n
Example 4:
\n\nInput: startTime = [1,1,1,1], endTime = [1,3,2,4], queryTime = 7\nOutput: 0\n\n\n
Example 5:
\n\nInput: startTime = [9,8,7,6,5,4,3,2,1], endTime = [10,10,10,10,10,10,10,10,10], queryTime = 5\nOutput: 5\n\n\n
\n
Constraints:
\n\nstartTime.length == endTime.length
1 <= startTime.length <= 100
1 <= startTime[i] <= endTime[i] <= 1000
1 <= queryTime <= 1000
nums
, you will choose two different indices i
and j
of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1)
.\n\n
Example 1:
\n\nInput: nums = [3,4,5,2]\nOutput: 12 \nExplanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. \n\n\n
Example 2:
\n\nInput: nums = [1,5,4,5]\nOutput: 16\nExplanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.\n\n\n
Example 3:
\n\nInput: nums = [3,7]\nOutput: 12\n\n\n
\n
Constraints:
\n\n2 <= nums.length <= 500
1 <= nums[i] <= 10^3
Given the array nums
consisting of 2n
elements in the form [x1,x2,...,xn,y1,y2,...,yn]
.
Return the array in the form [x1,y1,x2,y2,...,xn,yn]
.
\n
Example 1:
\n\nInput: nums = [2,5,1,3,4,7], n = 3\nOutput: [2,3,5,4,1,7] \nExplanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].\n\n\n
Example 2:
\n\nInput: nums = [1,2,3,4,4,3,2,1], n = 4\nOutput: [1,4,2,3,3,2,4,1]\n\n\n
Example 3:
\n\nInput: nums = [1,1,2,2], n = 2\nOutput: [1,2,1,2]\n\n\n
\n
Constraints:
\n\n1 <= n <= 500
nums.length == 2n
1 <= nums[i] <= 10^3
Given the array prices
where prices[i]
is the price of the ith
item in a shop. There is a special discount for items in the shop, if you buy the ith
item, then you will receive a discount equivalent to prices[j]
where j
is the minimum index such that j > i
and prices[j] <= prices[i]
, otherwise, you will not receive any discount at all.
Return an array where the ith
element is the final price you will pay for the ith
item of the shop considering the special discount.
\n
Example 1:
\n\nInput: prices = [8,4,6,2,3]\nOutput: [4,2,4,2,3]\nExplanation: \nFor item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4. \nFor item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2. \nFor item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4. \nFor items 3 and 4 you will not receive any discount at all.\n\n\n
Example 2:
\n\nInput: prices = [1,2,3,4,5]\nOutput: [1,2,3,4,5]\nExplanation: In this case, for all items, you will not receive any discount at all.\n\n\n
Example 3:
\n\nInput: prices = [10,1,1,6]\nOutput: [9,0,1,6]\n\n\n
\n
Constraints:
\n\n1 <= prices.length <= 500
1 <= prices[i] <= 10^3
Given an array nums
. We define a running sum of an array as runningSum[i] = sum(nums[0]\u2026nums[i])
.
Return the running sum of nums
.
\n
Example 1:
\n\nInput: nums = [1,2,3,4]\nOutput: [1,3,6,10]\nExplanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].\n\n
Example 2:
\n\nInput: nums = [1,1,1,1,1]\nOutput: [1,2,3,4,5]\nExplanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].\n\n
Example 3:
\n\nInput: nums = [3,1,2,10,1]\nOutput: [3,4,6,16,17]\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
Given an integer n
and an integer start
.
Define an array nums
where nums[i] = start + 2*i
(0-indexed) and n == nums.length
.
Return the bitwise XOR of all elements of nums
.
\n
Example 1:
\n\nInput: n = 5, start = 0\nOutput: 8\nExplanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.\nWhere \"^\" corresponds to bitwise XOR operator.\n\n\n
Example 2:
\n\nInput: n = 4, start = 3\nOutput: 8\nExplanation: Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.\n\n
Example 3:
\n\nInput: n = 1, start = 7\nOutput: 7\n\n\n
Example 4:
\n\nInput: n = 10, start = 5\nOutput: 2\n\n\n
\n
Constraints:
\n\n1 <= n <= 1000
0 <= start <= 1000
n == nums.length
Given an array of unique integers salary
where salary[i]
is the salary of the employee i
.
Return the average salary of employees excluding the minimum and maximum salary.
\n\n\n
Example 1:
\n\nInput: salary = [4000,3000,1000,2000]\nOutput: 2500.00000\nExplanation: Minimum salary and maximum salary are 1000 and 4000 respectively.\nAverage salary excluding minimum and maximum salary is (2000+3000)/2= 2500\n\n\n
Example 2:
\n\nInput: salary = [1000,2000,3000]\nOutput: 2000.00000\nExplanation: Minimum salary and maximum salary are 1000 and 3000 respectively.\nAverage salary excluding minimum and maximum salary is (2000)/1= 2000\n\n\n
Example 3:
\n\nInput: salary = [6000,5000,4000,3000,2000,1000]\nOutput: 3500.00000\n\n\n
Example 4:
\n\nInput: salary = [8000,9000,2000,3000,6000,1000]\nOutput: 4750.00000\n\n\n
\n
Constraints:
\n\n3 <= salary.length <= 100
10^3 <= salary[i] <= 10^6
salary[i]
is unique.10^-5
of the actual value will be accepted as correct.Given an array of numbers arr
. A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Return true
if the array can be rearranged to form an arithmetic progression, otherwise, return false
.
\n
Example 1:
\n\nInput: arr = [3,5,1]\nOutput: true\nExplanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.\n\n\n
Example 2:
\n\nInput: arr = [1,2,4]\nOutput: false\nExplanation: There is no way to reorder the elements to obtain an arithmetic progression.\n\n\n
\n
Constraints:
\n\n2 <= arr.length <= 1000
-10^6 <= arr[i] <= 10^6
Given an array of integers nums
.
A pair (i,j)
is called good if nums[i]
== nums[j]
and i
< j
.
Return the number of good pairs.
\n\n\n
Example 1:
\n\nInput: nums = [1,2,3,1,1,3]\nOutput: 4\nExplanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.\n\n\n
Example 2:
\n\nInput: nums = [1,1,1,1]\nOutput: 6\nExplanation: Each pair in the array are good.\n\n\n
Example 3:
\n\nInput: nums = [1,2,3]\nOutput: 0\n\n\n
\n
Constraints:
\n\n1 <= nums.length <= 100
1 <= nums[i] <= 100
Given a string s
and an integer array indices
of the same length.
The string s
will be shuffled such that the character at the ith
position moves to indices[i]
in the shuffled string.
Return the shuffled string.
\n\n\n
Example 1:
\nInput: s = \"codeleet\", indices
= [4,5,6,7,0,2,1,3]\nOutput: \"leetcode\"\nExplanation: As shown, \"codeleet\" becomes \"leetcode\" after shuffling.\n
\n\nExample 2:
\n\nInput: s = \"abc\", indices
= [0,1,2]\nOutput: \"abc\"\nExplanation: After shuffling, each character remains in its position.\n
\n\nExample 3:
\n\nInput: s = \"aiohn\", indices
= [3,1,4,2,0]\nOutput: \"nihao\"\n
\n\nExample 4:
\n\nInput: s = \"aaiougrt\", indices
= [4,0,2,6,7,3,1,5]\nOutput: \"arigatou\"\n
\n\nExample 5:
\n\nInput: s = \"art\", indices
= [1,0,2]\nOutput: \"rat\"\n
\n\n\n
Constraints:
\n\ns.length == indices.length == n
1 <= n <= 100
s
contains only lower-case English letters.0 <= indices[i] < n
indices
are unique (i.e. indices
is a permutation of the integers from 0
to n - 1
).Given an array of integers arr
, and three integers a
, b
and c
. You need to find the number of good triplets.
A triplet (arr[i], arr[j], arr[k])
is good if the following conditions are true:
0 <= i < j < k < arr.length
|arr[i] - arr[j]| <= a
|arr[j] - arr[k]| <= b
|arr[i] - arr[k]| <= c
Where |x|
denotes the absolute value of x
.
Return the number of good triplets.
\n\n\n
Example 1:
\n\nInput: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3\nOutput: 4\nExplanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].\n\n\n
Example 2:
\n\nInput: arr = [1,1,2,2,3], a = 0, b = 0, c = 1\nOutput: 0\nExplanation: No triplet satisfies all conditions.\n\n\n
\n
Constraints:
\n\n3 <= arr.length <= 100
0 <= arr[i] <= 1000
0 <= a, b, c <= 1000
Given a square matrix mat
, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
\n\n\n
Example 1:
\nInput: mat = [[1,2,3],\n [4,5,6],\n [7,8,9]]\nOutput: 25\nExplanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25\nNotice that element mat[1][1] = 5 is counted only once.\n\n\n
Example 2:
\n\nInput: mat = [[1,1,1,1],\n [1,1,1,1],\n [1,1,1,1],\n [1,1,1,1]]\nOutput: 8\n\n\n
Example 3:
\n\nInput: mat = [[5]]\nOutput: 5\n\n\n
\n
Constraints:
\n\nn == mat.length == mat[i].length
1 <= n <= 100
1 <= mat[i][j] <= 100
Given an array of positive integers arr
, calculate the sum of all possible odd-length subarrays.
A subarray is a contiguous subsequence of the array.
\n\nReturn the sum of all odd-length subarrays of arr
.
\n
Example 1:
\n\nInput: arr = [1,4,2,5,3]\nOutput: 58\nExplanation: The odd-length subarrays of arr and their sums are:\n[1] = 1\n[4] = 4\n[2] = 2\n[5] = 5\n[3] = 3\n[1,4,2] = 7\n[4,2,5] = 11\n[2,5,3] = 10\n[1,4,2,5,3] = 15\nIf we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58\n\n
Example 2:
\n\nInput: arr = [1,2]\nOutput: 3\nExplanation: There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.\n\n
Example 3:
\n\nInput: arr = [10,11,12]\nOutput: 66\n\n\n
\n
Constraints:
\n\n1 <= arr.length <= 100
1 <= arr[i] <= 1000
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
\n\nImplement the ParkingSystem
class:
ParkingSystem(int big, int medium, int small)
Initializes object of the ParkingSystem
class. The number of slots for each parking space are given as part of the constructor.bool addCar(int carType)
Checks whether there is a parking space of carType
for the car that wants to get into the parking lot. carType
can be of three kinds: big, medium, or small, which are represented by 1
, 2
, and 3
respectively. A car can only park in a parking space of its carType
. If there is no space available, return false
, else park the car in that size space and return true
.\n
Example 1:
\n\nInput\n[\"ParkingSystem\", \"addCar\", \"addCar\", \"addCar\", \"addCar\"]\n[[1, 1, 0], [1], [2], [3], [1]]\nOutput\n[null, true, true, false, false]\n\nExplanation\nParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);\nparkingSystem.addCar(1); // return true because there is 1 available slot for a big car\nparkingSystem.addCar(2); // return true because there is 1 available slot for a medium car\nparkingSystem.addCar(3); // return false because there is no available slot for a small car\nparkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.\n\n\n
\n
Constraints:
\n\n0 <= big, medium, small <= 1000
carType
is 1
, 2
, or 3
1000
calls will be made to addCar
A string is a valid parentheses string (denoted VPS) if it meets one of the following:
\n\n\"\"
, or a single character not equal to \"(\"
or \")\"
,AB
(A
concatenated with B
), where A
and B
are VPS's, or(A)
, where A
is a VPS.We can similarly define the nesting depth depth(S)
of any VPS S
as follows:
depth(\"\") = 0
depth(A + B) = max(depth(A), depth(B))
, where A
and B
are VPS'sdepth(\"(\" + A + \")\") = 1 + depth(A)
, where A
is a VPS.For example, \"\"
, \"()()\"
, and \"()(()())\"
are VPS's (with nesting depths 0, 1, and 2), and \")(\"
and \"(()\"
are not VPS's.
Given a VPS represented as string s
, return the nesting depth of s
.
\n
Example 1:
\n\nInput: s = \"(1+(2*3)+((8)/4))+1\"\nOutput: 3\nExplanation: Digit 8 is inside of 3 nested parentheses in the string.\n\n\n
Example 2:
\n\nInput: s = \"(1)+((2))+(((3)))\"\nOutput: 3\n\n\n
Example 3:
\n\nInput: s = \"1+(2*3)/(2-1)\"\nOutput: 1\n\n\n
Example 4:
\n\nInput: s = \"1\"\nOutput: 0\n\n\n
\n
Constraints:
\n\n1 <= s.length <= 100
s
consists of digits 0-9
and characters '+'
, '-'
, '*'
, '/'
, '('
, and ')'
.s
is a VPS.Write a function that reverses a string. The input string is given as an array of characters char[]
.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
\n\nYou may assume all the characters consist of printable ascii characters.
\n\n\n\n
Example 1:
\n\nInput: [\"h\",\"e\",\"l\",\"l\",\"o\"]\nOutput: [\"o\",\"l\",\"l\",\"e\",\"h\"]\n\n\n
Example 2:
\n\nInput: [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]\nOutput: [\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]\n\n
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
\n\nGiven two integers x
and y
, calculate the Hamming distance.
Note:
\n0 \u2264 x
, y
< 231.\n
Example:\n
Input: x = 1, y = 4\n\nOutput: 2\n\nExplanation:\n1 (0 0 0 1)\n4 (0 1 0 0)\n \u2191 \u2191\n\nThe above arrows point to positions where the corresponding bits are different.\n\n
The Fibonacci numbers, commonly denoted F(n)
form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0
and 1
. That is,
F(0) = 0, F(1) = 1\nF(N) = F(N - 1) + F(N - 2), for N > 1.\n\n\n
Given N
, calculate F(N)
.
\n\n
Example 1:
\n\nInput: 2\nOutput: 1\nExplanation: F(2) = F(1) + F(0) = 1 + 0 = 1.\n\n\n
Example 2:
\n\nInput: 3\nOutput: 2\nExplanation: F(3) = F(2) + F(1) = 1 + 1 = 2.\n\n\n
Example 3:
\n\nInput: 4\nOutput: 3\nExplanation: F(4) = F(3) + F(2) = 2 + 1 = 3.\n\n\n
\n\n
Note:
\n\n0 \u2264 N
\u2264 30.
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
\n\nExample 1:
\n
Input: \"Let's take LeetCode contest\"\nOutput: \"s'teL ekat edoCteeL tsetnoc\"\n\n\n\n
Note:\nIn the string, each word is separated by single space and there will not be any extra space in the string.\n
Given a n-ary tree, find its maximum depth.
\n\nThe maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
\n\nNary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
\n\n\n
Example 1:
\n\nInput: root = [1,null,3,2,4,null,5,6]\nOutput: 3\n\n\n
Example 2:
\n\nInput: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]\nOutput: 5\n\n\n
\n
Constraints:
\n\n1000
.[0, 10^4]
.\nGiven an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.\n
\n\nExample 1:
\n
Input: [1,4,3,2]\n\nOutput: 4\nExplanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).\n\n\n\n
Note:
\n
Given an n-ary tree, return the preorder traversal of its nodes' values.
\n\nNary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
\n\n\n\n
Follow up:
\n\nRecursive solution is trivial, could you do it iteratively?
\n\n\n
Example 1:
\n\nInput: root = [1,null,3,2,4,null,5,6]\nOutput: [1,3,5,6,2,4]\n\n\n
Example 2:
\n\nInput: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]\nOutput: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]\n\n\n
\n
Constraints:
\n\n1000
[0, 10^4]
Given an n-ary tree, return the postorder traversal of its nodes' values.
\n\nNary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
\n\n\n\n
Follow up:
\n\nRecursive solution is trivial, could you do it iteratively?
\n\n\n
Example 1:
\n\nInput: root = [1,null,3,2,4,null,5,6]\nOutput: [5,6,3,2,4,1]\n\n\n
Example 2:
\n\nInput: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]\nOutput: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]\n\n\n
\n
Constraints:
\n\n1000
[0, 10^4]
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
\n\nYou need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
\n\nExample 1:
\n\nInput: \n\tTree 1 Tree 2 \n 1 2 \n / \\ / \\ \n 3 2 1 3 \n / \\ \\ \n 5 4 7 \nOutput: \nMerged tree:\n\t 3\n\t / \\\n\t 4 5\n\t / \\ \\ \n\t 5 4 7\n\n\n
\n\n
Note: The merging process must start from the root nodes of both trees.
\nThere is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
\n\nThe move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
\n\nNote: The way that the robot is \"facing\" is irrelevant. \"R\" will always make the robot move to the right once, \"L\" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
\n\n\n
Example 1:
\n\nInput: moves = \"UD\"\nOutput: true\nExplanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.\n\n\n
Example 2:
\n\nInput: moves = \"LL\"\nOutput: false\nExplanation: The robot moves left twice. It ends up two \"moves\" to the left of the origin. We return false because it is not at the origin at the end of its moves.\n\n\n
Example 3:
\n\nInput: moves = \"RRDD\"\nOutput: false\n\n\n
Example 4:
\n\nInput: moves = \"LDRRLRUULR\"\nOutput: false\n\n\n
\n
Constraints:
\n\n1 <= moves.length <= 2 * 104
moves
only contains the characters 'U'
, 'D'
, 'L'
and 'R'
.Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.
\n\nFor example,
\n\nGiven the tree:\n 4\n / \\\n 2 7\n / \\\n 1 3\n\nAnd the value to search: 2\n\n\n
You should return this subtree:
\n\n2 \n / \\ \n 1 3\n\n\n
In the example above, if we want to search the value 5
, since there is no node with value 5
, we should return NULL
.
Note that an empty tree is represented by NULL
, therefore you would see the expected output (serialized tree format) as []
, not null
.
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
\n\n\n\n
Example 1:
\n\nInput: \"Hello\"\nOutput: \"hello\"\n\n\n
Example 2:
\n\nInput: \"here\"\nOutput: \"here\"\n\n\n
Example 3:
\n\nInput: \"LOVELY\"\nOutput: \"lovely\"\n\n
\nA self-dividing number is a number that is divisible by every digit it contains.\n
\nFor example, 128 is a self-dividing number because 128 % 1 == 0
, 128 % 2 == 0
, and 128 % 8 == 0
.\n
\nAlso, a self-dividing number is not allowed to contain the digit zero.\n
\nGiven a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.\n
\nExample 1:
\n
Input: \nleft = 1, right = 22\nOutput: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]\n\n\n\n
Note:\n
1 <= left <= right <= 10000
.You're given strings J
representing the types of stones that are jewels, and S
representing the stones you have. Each character in S
is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J
are guaranteed distinct, and all characters in J
and S
are letters. Letters are case sensitive, so \"a\"
is considered a different type of stone from \"A\"
.
Example 1:
\n\nInput: J = \"aA\", S = \"aAAbbbb\"\nOutput: 3\n\n\n
Example 2:
\n\nInput: J = \"z\", S = \"ZZ\"\nOutput: 0\n\n\n
Note:
\n\nS
and J
will consist of letters and have length at most 50.J
are distinct.International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: \"a\"
maps to \".-\"
, \"b\"
maps to \"-...\"
, \"c\"
maps to \"-.-.\"
, and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
\n\n[\".-\",\"-...\",\"-.-.\",\"-..\",\".\",\"..-.\",\"--.\",\"....\",\"..\",\".---\",\"-.-\",\".-..\",\"--\",\"-.\",\"---\",\".--.\",\"--.-\",\".-.\",\"...\",\"-\",\"..-\",\"...-\",\".--\",\"-..-\",\"-.--\",\"--..\"]\n\n
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, \"cab\" can be written as \"-.-..--...\", (which is the concatenation \"-.-.\" + \".-\" + \"-...
\"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
\n\nExample:\nInput: words = [\"gin\", \"zen\", \"gig\", \"msg\"]\nOutput: 2\nExplanation: \nThe transformation of each word is:\n\"gin\" -> \"--...-.\"\n\"zen\" -> \"--...-.\"\n\"gig\" -> \"--...--.\"\n\"msg\" -> \"--...--.\"\n\nThere are 2 different transformations, \"--...-.\" and \"--...--.\".\n\n\n
Note:
\n\nwords
will be at most 100
.words[i]
will have length in range [1, 12]
.words[i]
will only consist of lowercase letters.A website domain like \"discuss.leetcode.com\" consists of various subdomains. At the top level, we have \"com\", at the next level, we have \"leetcode.com\", and at the lowest level, \"discuss.leetcode.com\". When we visit a domain like \"discuss.leetcode.com\", we will also visit the parent domains \"leetcode.com\" and \"com\" implicitly.
\n\nNow, call a \"count-paired domain\" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be \"9001 discuss.leetcode.com\".
\n\nWe are given a list cpdomains
of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.
Example 1:\nInput: \n[\"9001 discuss.leetcode.com\"]\nOutput: \n[\"9001 discuss.leetcode.com\", \"9001 leetcode.com\", \"9001 com\"]\nExplanation: \nWe only have one website domain: \"discuss.leetcode.com\". As discussed above, the subdomain \"leetcode.com\" and \"com\" will also be visited. So they will all be visited 9001 times.\n\n\n\n
Example 2:\nInput: \n[\"900 google.mail.com\", \"50 yahoo.com\", \"1 intel.mail.com\", \"5 wiki.org\"]\nOutput: \n[\"901 mail.com\",\"50 yahoo.com\",\"900 google.mail.com\",\"5 wiki.org\",\"5 org\",\"1 intel.mail.com\",\"951 com\"]\nExplanation: \nWe will visit \"google.mail.com\" 900 times, \"yahoo.com\" 50 times, \"intel.mail.com\" once and \"wiki.org\" 5 times. For the subdomains, we will visit \"mail.com\" 900 + 1 = 901 times, \"com\" 900 + 50 + 1 = 951 times, and \"org\" 5 times.\n\n\n\n
Notes:
\n\ncpdomains
will not exceed 100
. 100
.10000
.Given a string S
and a character C
, return an array of integers representing the shortest distance from the character C
in the string.
Example 1:
\n\nInput: S = \"loveleetcode\", C = 'e'\nOutput: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]\n\n\n
\n\n
Note:
\n\nS
string length is in [1, 10000].
C
is a single character, and guaranteed to be in string S
.S
and C
are lowercase.Given a binary matrix A
, we want to flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0]
horizontally results in [0, 1, 1]
.
To invert an image means that each 0
is replaced by 1
, and each 1
is replaced by 0
. For example, inverting [0, 1, 1]
results in [1, 0, 0]
.
Example 1:
\n\nInput: [[1,1,0],[1,0,1],[0,0,0]]\nOutput: [[1,0,0],[0,1,0],[1,1,1]]\nExplanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].\nThen, invert the image: [[1,0,0],[0,1,0],[1,1,1]]\n\n\n
Example 2:
\n\nInput: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]\nOutput: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\nExplanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].\nThen invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\n\n\n
Notes:
\n\n1 <= A.length = A[0].length <= 20
0 <= A[i][j] <= 1
Let's call an array arr
a mountain if the following properties hold:
arr.length >= 3
i
with 0 < i < arr.length - 1
such that:\n\tarr[0] < arr[1] < ... arr[i-1] < arr[i]
arr[i] > arr[i+1] > ... > arr[arr.length - 1]
Given an integer array arr that is guaranteed to be a mountain, return any i
such that arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
.
\n
Example 1:
\nInput: arr = [0,1,0]\nOutput: 1\n
Example 2:
\nInput: arr = [0,2,1,0]\nOutput: 1\n
Example 3:
\nInput: arr = [0,10,5,2]\nOutput: 1\n
Example 4:
\nInput: arr = [3,4,5,1]\nOutput: 2\n
Example 5:
\nInput: arr = [24,69,100,99,79,78,67,36,26,19]\nOutput: 2\n\n
\n
Constraints:
\n\n3 <= arr.length <= 104
0 <= arr[i] <= 106
arr
is guaranteed to be a mountain array.Given a non-empty, singly linked list with head node head
, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
\n\n\n\n
Example 1:
\n\nInput: [1,2,3,4,5]\nOutput: Node 3 from this list (Serialization: [3,4,5])\nThe returned node has value 3. (The judge's serialization of this node is [3,4,5]).\nNote that we returned a ListNode object ans, such that:\nans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.\n\n\n
Example 2:
\n\nInput: [1,2,3,4,5,6]\nOutput: Node 4 from this list (Serialization: [4,5,6])\nSince the list has two middle nodes with values 3 and 4, we return the second one.\n\n\n
\n\n
Note:
\n\n1
and 100
.On a N * N
grid, we place some 1 * 1 * 1
cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j]
represents a tower of v
cubes placed on top of grid cell (i, j)
.
Now we view the projection of these cubes onto the xy, yz, and zx planes.
\n\nA projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.
\n\nHere, we are viewing the \"shadow\" when looking at the cubes from the top, the front, and the side.
\n\nReturn the total area of all three projections.
\n\n\n\n