Leetcode 2021-05-15
🟢 13.roman-to-integer
1️⃣ Tags
#math #string
2️⃣ Description
Roman numerals are represented by seven different symbols:
For example,
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
Given a roman numeral, convert it to an integer.
3️⃣ Example
🟢 13.roman-to-integer
1️⃣ Tags
#math #string
2️⃣ Description
Roman numerals are represented by seven different symbols:
I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example,
2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900.Given a roman numeral, convert it to an integer.
3️⃣ Example
Input: s = "III"
Output: 3
Leetcode 2021-05-16
🟡 421.maximum-xor-of-two-numbers-in-an-array
1️⃣ Tags
#bit_manipulation #trie
2️⃣ Description
Given an integer array
Follow up: Could you do this in
3️⃣ Example
🟡 421.maximum-xor-of-two-numbers-in-an-array
1️⃣ Tags
#bit_manipulation #trie
2️⃣ Description
Given an integer array
nums, return the maximum result of nums[i] XOR nums[j], where 0 ≤ i ≤ j < n.Follow up: Could you do this in
O(n) runtime?3️⃣ Example
Input: nums = [3,10,5,25,2,8]
Output: 28
Explanation: The maximum result is 5 XOR 25 = 28.
Leetcode 2021-05-17
🟢 993.cousins-in-binary-tree
🏷️ Tags
#tree #breadth_first_search
Description
In a binary tree, the root node is at depth
Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
We are given the
Return
Example
🟢 993.cousins-in-binary-tree
🏷️ Tags
#tree #breadth_first_search
Description
In a binary tree, the root node is at depth
0, and children of each depth k node are at depth k+1.Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
We are given the
root of a binary tree with unique values, and the values x and y of two different nodes in the tree.Return
true if and only if the nodes corresponding to the values x and y are cousins.Example
Input: root = [1,2,3,4], x = 4, y = 3
Output: false
Leetcode 2021-05-18
🟡 1442.count-triplets-that-can-form-two-arrays-of-equal-xor
🏷️ Tags
#bit_manipulation #array #math
Description
Given an array of integers
We want to select three indices
Let's define
Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (
Example
🟡 1442.count-triplets-that-can-form-two-arrays-of-equal-xor
🏷️ Tags
#bit_manipulation #array #math
Description
Given an array of integers
arr.We want to select three indices
i, j and k where (0 <= i < j <= k < arr.length).Let's define
a and b as follows:a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]Note that ^ denotes the bitwise-xor operation.
Return the number of triplets (
i, j and k) Where a == b.Example
Input: arr = [2,3,1,6,7]
Output: 4
Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
Leetcode 2021-05-19
🟡 1738.find-kth-largest-xor-coordinate-value
🏷️ Tags
#array
Description
You are given a 2D
The value of coordinate
Find the
Example
🟡 1738.find-kth-largest-xor-coordinate-value
🏷️ Tags
#array
Description
You are given a 2D
matrix of size m x n, consisting of non-negative integers. You are also given an integer k.The value of coordinate
(a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexed).Find the
kth largest value (1-indexed) of all the coordinates of matrix.Example
Input: matrix = [[5,2],[1,6]], k = 1
Output: 7
Explanation: The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.
Leetcode 2021-05-20
🟡 692.top-k-frequent-words
🏷️ Tags
#heap #trie #hash_table
Description
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
Example
🟡 692.top-k-frequent-words
🏷️ Tags
#heap #trie #hash_table
Description
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
Example
Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Leetcode 2021-05-21
🟡 1035.uncrossed-lines
🏷️ Tags
#array
Description
We write the integers of
Now, we may draw connecting lines: a straight line connecting two numbers
The line we draw does not intersect any other connecting (non-horizontal) line.
Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.
Return the maximum number of connecting lines we can draw in this way.
Example
🟡 1035.uncrossed-lines
🏷️ Tags
#array
Description
We write the integers of
nums1 and nums2 (in the order they are given) on two separate horizontal lines.Now, we may draw connecting lines: a straight line connecting two numbers
nums1[i] and nums2[j] such that:nums1[i] == nums2[j];The line we draw does not intersect any other connecting (non-horizontal) line.
Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.
Return the maximum number of connecting lines we can draw in this way.
Example
Input: nums1 = [1,4,2], nums2 = [1,2,4]
Output: 2
Explanation: We can draw 2 uncrossed lines as in the diagram.
We cannot draw 3 uncrossed lines, because the line from nums1[1]=4 to nums2[2]=4 will intersect the line from nums1[2]=2 to nums2[1]=2.
Leetcode 2021-05-22
🔴 810.chalkboard-xor-game
🏷️ Tags
#math
Description
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses. (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)
Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.
Return True if and only if Alice wins the game, assuming both players play optimally.
Example
🔴 810.chalkboard-xor-game
🏷️ Tags
#math
Description
We are given non-negative integers nums[i] which are written on a chalkboard. Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses. (Also, we'll say the bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.)
Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.
Return True if and only if Alice wins the game, assuming both players play optimally.
Example
Input: nums = [1, 1, 2]
Output: false
Explanation:
Alice has two choices: erase 1 or erase 2.
If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose.
If Alice erases 2 first, now nums becomes [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
Leetcode 2021-05-23
🔴 1707.maximum-xor-with-an-element-from-array
🏷️ Tags
#bit_manipulation #trie
Description
You are given an array
The answer to the
Return an integer array
Example
🔴 1707.maximum-xor-with-an-element-from-array
🏷️ Tags
#bit_manipulation #trie
Description
You are given an array
nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi].The answer to the
ith query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1.Return an integer array
answer where answer.length == queries.length and answer[i] is the answer to the ith query.Example
Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
Output: [3,3,7]
Explanation:
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
2) 1 XOR 2 = 3.
3) 5 XOR 2 = 7.
Leetcode 2021-05-24
🔴 664.strange-printer
🏷️ Tags
#depth_first_search #dynamic_programming
Description
There is a strange printer with the following two special properties:
The printer can only print a sequence of the same character each time.
At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
Given a string
Example
🔴 664.strange-printer
🏷️ Tags
#depth_first_search #dynamic_programming
Description
There is a strange printer with the following two special properties:
The printer can only print a sequence of the same character each time.
At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.
Given a string
s, return the minimum number of turns the printer needed to print it.Example
Input: s = "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".
Leetcode 2021-05-25
🔴 1787.make-the-xor-of-all-segments-equal-to-zero
🏷️ Tags
#dynamic_programming
Description
You are given an array
Return the minimum number of elements to change in the array such that the
Example
🔴 1787.make-the-xor-of-all-segments-equal-to-zero
🏷️ Tags
#dynamic_programming
Description
You are given an array
nums and an integer k. The XOR of a segment [left, right] where left <= right is the XOR of all the elements with indices between left and right, inclusive: nums[left] XOR nums[left+1] XOR ... XOR nums[right].Return the minimum number of elements to change in the array such that the
XOR of all segments of size k is equal to zero.Example
Input: nums = [1,2,0,3,0], k = 1
Output: 3
Explanation: Modify the array from [1,2,0,3,0] to from [0,0,0,0,0].
Leetcode 2021-05-26
🟡 1190.reverse-substrings-between-each-pair-of-parentheses
🏷️ Tags
#stack
Description
You are given a string
Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any brackets.
Example
🟡 1190.reverse-substrings-between-each-pair-of-parentheses
🏷️ Tags
#stack
Description
You are given a string
s that consists of lower case English letters and brackets. Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any brackets.
Example
Input: s = "(abcd)"
Output: "dcba"
Leetcode 2021-05-27
🟢 461.hamming-distance
🏷️ Tags
#bit_manipulation
Description
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers
Example
🟢 461.hamming-distance
🏷️ Tags
#bit_manipulation
Description
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers
x and y, return the Hamming distance between them.Example
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
Leetcode 2021-05-28
🟡 477.total-hamming-distance
🏷️ Tags
#bit_manipulation
Description
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given an integer array
Example
🟡 477.total-hamming-distance
🏷️ Tags
#bit_manipulation
Description
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given an integer array
nums, return the sum of Hamming distances between all the pairs of the integers in nums.Example
Input: nums = [4,14,2]
Output: 6
Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case).
The answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
Leetcode 2021-05-29
🔴 1074.number-of-submatrices-that-sum-to-target
🏷️ Tags
#array #dynamic_programming #sliding_window
Description
Given a
A submatrix
Two submatrices
Example
🔴 1074.number-of-submatrices-that-sum-to-target
🏷️ Tags
#array #dynamic_programming #sliding_window
Description
Given a
matrix and a target, return the number of non-empty submatrices that sum to target.A submatrix
x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.Two submatrices
(x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.Example
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.
Leetcode 2021-05-30
🟢 231.power-of-two
🏷️ Tags
#bit_manipulation #math
Description
Given an integer
An integer
Example
🟢 231.power-of-two
🏷️ Tags
#bit_manipulation #math
Description
Given an integer
n, return true if it is a power of two. Otherwise, return false.An integer
n is a power of two, if there exists an integer x such that n == 2x.Example
Input: n = 1
Output: true
Explanation: 20 = 1
Leetcode 2021-05-31
🟢 342.power-of-four
🏷️ Tags
#bit_manipulation
Description
Given an integer
An integer
Example
🟢 342.power-of-four
🏷️ Tags
#bit_manipulation
Description
Given an integer
n, return true if it is a power of four. Otherwise, return false.An integer
n is a power of four, if there exists an integer x such that n == 4x.Example
Input: n = 16
Output: true
Leetcode 2021-06-01
🟡 1744.can-you-eat-your-favorite-candy-on-your-favorite-day
🏷️ Tags
#math
Description
You are given a (0-indexed) array of positive integers
You play a game with the following rules:
You start eating candies on day
You cannot eat any candy of type
You must eat at least one candy per day until you have eaten all the candies.
Construct a boolean array
Return the constructed array
Example
🟡 1744.can-you-eat-your-favorite-candy-on-your-favorite-day
🏷️ Tags
#math
Description
You are given a (0-indexed) array of positive integers
candiesCount where candiesCount[i] represents the number of candies of the ith type you have. You are also given a 2D array queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi].You play a game with the following rules:
You start eating candies on day
0.You cannot eat any candy of type
i unless you have eaten all candies of type i - 1.You must eat at least one candy per day until you have eaten all the candies.
Construct a boolean array
answer such that answer.length == queries.length and answer[i] is true if you can eat a candy of type favoriteTypei on day favoriteDayi without eating more than dailyCapi candies on any day, and false otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.Return the constructed array
answer.Example
Input: candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
Output: [true,false,true]
Explanation:
1- If you eat 2 candies (type 0) on day 0 and 2 candies (type 0) on day 1, you will eat a candy of type 0 on day 2.
2- You can eat at most 4 candies each day.
If you eat 4 candies every day, you will eat 4 candies (type 0) on day 0 and 4 candies (type 0 and type 1) on day 1.
On day 2, you can only eat 4 candies (type 1 and type 2), so you cannot eat a candy of type 4 on day 2.
3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.
Leetcode 2021-06-02
🟡 523.continuous-subarray-sum
🏷️ Tags
#math #dynamic_programming
Description
Given an integer array
An integer
Example
🟡 523.continuous-subarray-sum
🏷️ Tags
#math #dynamic_programming
Description
Given an integer array
nums and an integer k, return true if nums has a continuous subarray of size at least two whose elements sum up to a multiple of k, or false otherwise.An integer
x is a multiple of k if there exists an integer n such that x = n * k. 0 is always a multiple of k.Example
Input: nums = [23,2,4,6,7], k = 6
Output: true
Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
Leetcode 2021-06-03
🟡 525.contiguous-array
🏷️ Tags
#hash_table
Description
Given a binary array
Example
🟡 525.contiguous-array
🏷️ Tags
#hash_table
Description
Given a binary array
nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.Example
Input: nums = [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.