Leetcode.com 2021-08-12
🟡 49.group-anagrams
🏷️ Tags
#hash_table #string #sorting
Description
Given an array of strings
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example
🟡 49.group-anagrams
🏷️ Tags
#hash_table #string #sorting
Description
Given an array of strings
strs, group the anagrams together. You can return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Leetcode-cn.com 2021-08-14
🟡 1583.count-unhappy-friends
🏷️ Tags
#array #simulation
Description
给你一份
对每位朋友
所有的朋友被分成几对,配对情况以列表
但是,这样的配对情况可能会是其中部分朋友感到不开心。在
返回 不开心的朋友的数目 。
Example
🟡 1583.count-unhappy-friends
🏷️ Tags
#array #simulation
Description
给你一份
n 位朋友的亲近程度列表,其中 n 总是 偶数 。对每位朋友
i,preferences[i] 包含一份 按亲近程度从高到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0 到 n-1 之间的整数表示。所有的朋友被分成几对,配对情况以列表
pairs 给出,其中 pairs[i] = [xi, yi] 表示 xi 与 yi 配对,且 yi 与 xi 配对。但是,这样的配对情况可能会是其中部分朋友感到不开心。在
x 与 y 配对且 u 与 v 配对的情况下,如果同时满足下述两个条件,x 就会不开心:x 与 u 的亲近程度胜过 x 与 y,且u 与 x 的亲近程度胜过 u 与 v返回 不开心的朋友的数目 。
Example
输入:n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
输出:2
解释:
朋友 1 不开心,因为:
- 1 与 0 配对,但 1 与 3 的亲近程度比 1 与 0 高,且
- 3 与 1 的亲近程度比 3 与 2 高。
朋友 3 不开心,因为:
- 3 与 2 配对,但 3 与 1 的亲近程度比 3 与 2 高,且
- 1 与 3 的亲近程度比 1 与 0 高。
朋友 0 和 2 都是开心的。
Leetcode.com 2021-08-13
🟡 73.set-matrix-zeroes
🏷️ Tags
#array #hash_table #matrix
Description
Given an
You must do it in place.
Example
🟡 73.set-matrix-zeroes
🏷️ Tags
#array #hash_table #matrix
Description
Given an
m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's, and return the matrix.You must do it in place.
Example
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Leetcode-cn.com 2021-08-15
🟡 576.out-of-boundary-paths
🏷️ Tags
#dynamic_programming
Description
给你一个大小为
给你五个整数
Example
🟡 576.out-of-boundary-paths
🏷️ Tags
#dynamic_programming
Description
给你一个大小为
m x n 的网格和一个球。球的起始坐标为 [startRow, startColumn] 。你可以将球移到在四个方向上相邻的单元格内(可以穿过网格边界到达网格之外)。你 最多 可以移动 maxMove 次球。给你五个整数
m、n、maxMove、startRow 以及 startColumn ,找出并返回可以将球移出边界的路径数量。因为答案可能非常大,返回对 109 + 7 取余 后的结果。Example
输入:m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
输出:6
Leetcode.com 2021-08-14
🔴 546.remove-boxes
🏷️ Tags
#memoization #array #dynamic_programming
Description
You are given several
You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (i.e., composed of
Return the maximum points you can get.
Example
🔴 546.remove-boxes
🏷️ Tags
#memoization #array #dynamic_programming
Description
You are given several
boxes with different colors represented by different positive numbers.You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (i.e., composed of
k boxes, k >= 1), remove them and get k * k points.Return the maximum points you can get.
Example
Input: boxes = [1,3,2,2,2,3,4,3,1]
Output: 23
Explanation:
[1, 3, 2, 2, 2, 3, 4, 3, 1]
----> [1, 3, 3, 4, 3, 1] (3*3=9 points)
----> [1, 3, 3, 3, 1] (1*1=1 points)
----> [1, 1] (3*3=9 points)
----> [] (2*2=4 points)
Leetcode-cn.com 2021-08-17
🟢 551.student-attendance-record-i
🏷️ Tags
#string
Description
给你一个字符串
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
按 总出勤 计,学生缺勤(
学生 不会 存在 连续 3 天或 3 天以上的迟到(
如果学生可以获得出勤奖励,返回
Example
🟢 551.student-attendance-record-i
🏷️ Tags
#string
Description
给你一个字符串
s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:'A':Absent,缺勤'L':Late,迟到'P':Present,到场如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
按 总出勤 计,学生缺勤(
'A')严格 少于两天。学生 不会 存在 连续 3 天或 3 天以上的迟到(
'L')记录。如果学生可以获得出勤奖励,返回
true ;否则,返回 false 。Example
输入:s = "PPALLP"
输出:true
解释:学生缺勤次数少于 2 次,且不存在 3 天或以上的连续迟到记录。
Leetcode.com 2021-08-16
🟢 303.range-sum-query-immutable
🏷️ Tags
#design #array #prefix_sum
Description
Given an integer array
Calculate the sum of the elements of
Implement the
Example
🟢 303.range-sum-query-immutable
🏷️ Tags
#design #array #prefix_sum
Description
Given an integer array
nums, handle multiple queries of the following type:Calculate the sum of the elements of
nums between indices left and right inclusive where left <= right.Implement the
NumArray class:NumArray(int[] nums) Initializes the object with the integer array nums.int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).Example
Input
["NumArray", "sumRange", "sumRange", "sumRange"]
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
Output
[null, 1, -1, -3]
Explanation
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
Leetcode-cn.com 2021-08-18
🔴 552.student-attendance-record-ii
🏷️ Tags
#dynamic_programming
Description
可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
按 总出勤 计,学生缺勤(
学生 不会 存在 连续 3 天或 3 天以上的迟到(
给你一个整数
Example
🔴 552.student-attendance-record-ii
🏷️ Tags
#dynamic_programming
Description
可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
'A':Absent,缺勤'L':Late,迟到'P':Present,到场如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
按 总出勤 计,学生缺勤(
'A')严格 少于两天。学生 不会 存在 连续 3 天或 3 天以上的迟到(
'L')记录。给你一个整数
n ,表示出勤记录的长度(次数)。请你返回记录长度为 n 时,可能获得出勤奖励的记录情况 数量 。答案可能很大,所以返回对 109 + 7 取余 的结果。Example
输入:n = 2
输出:8
解释:
有 8 种长度为 2 的记录将被视为可奖励:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
只有"AA"不会被视为可奖励,因为缺勤次数为 2 次(需要少于 2 次)。
Leetcode.com 2021-08-17
🟡 1448.count-good-nodes-in-binary-tree
🏷️ Tags
#tree #depth_first_search #breadth_first_search #binary_tree
Description
Given a binary tree
Return the number of good nodes in the binary tree.
Example
🟡 1448.count-good-nodes-in-binary-tree
🏷️ Tags
#tree #depth_first_search #breadth_first_search #binary_tree
Description
Given a binary tree
root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.Return the number of good nodes in the binary tree.
Example
Input: root = [3,1,4,3,null,1,5]
Output: 4
Explanation: Nodes in blue are good.
Root Node (3) is always a good node.
Node 4 -> (3,4) is the maximum value in the path starting from the root.
Node 5 -> (3,4,5) is the maximum value in the path
Node 3 -> (3,1,3) is the maximum value in the path.
Leetcode-cn.com 2021-08-19
🟢 345.reverse-vowels-of-a-string
🏷️ Tags
#two_pointers #string
Description
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
Example
🟢 345.reverse-vowels-of-a-string
🏷️ Tags
#two_pointers #string
Description
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
Example
输入:"hello"
输出:"holle"
Leetcode.com 2021-08-18
🟡 91.decode-ways
🏷️ Tags
#string #dynamic_programming
Description
A message containing letters from
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example,
Note that the grouping
Given a string
The answer is guaranteed to fit in a 32-bit integer.
Example
🟡 91.decode-ways
🏷️ Tags
#string #dynamic_programming
Description
A message containing letters from
A-Z can be encoded into numbers using the following mapping:
'A' -> "1"
'B' -> "2"
...
'Z' -> "26"
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example,
"11106" can be mapped into:"AAJF" with the grouping (1 1 10 6)"KJF" with the grouping (11 10 6)Note that the grouping
(1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".Given a string
s containing only digits, return the number of ways to decode it.The answer is guaranteed to fit in a 32-bit integer.
Example
Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).
Leetcode-cn.com 2021-08-20
🟢 541.reverse-string-ii
🏷️ Tags
#two_pointers #string
Description
给定一个字符串
如果剩余字符少于
如果剩余字符小于
Example
🟢 541.reverse-string-ii
🏷️ Tags
#two_pointers #string
Description
给定一个字符串
s 和一个整数 k,从字符串开头算起,每 2k 个字符反转前 k 个字符。如果剩余字符少于
k 个,则将剩余字符全部反转。如果剩余字符小于
2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。Example
输入:s = "abcdefg", k = 2
输出:"bacdfeg"
Leetcode.com 2021-08-19
🟡 1339.maximum-product-of-splitted-binary-tree
🏷️ Tags
#tree #depth_first_search #binary_tree
Description
Given the
Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo
Note that you need to maximize the answer before taking the mod and not after taking it.
Example
🟡 1339.maximum-product-of-splitted-binary-tree
🏷️ Tags
#tree #depth_first_search #binary_tree
Description
Given the
root of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo
109 + 7.Note that you need to maximize the answer before taking the mod and not after taking it.
Example
Input: root = [1,2,3,4,5,6]
Output: 110
Explanation: Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
Leetcode-cn.com 2021-08-21
🟡 443.string-compression
🏷️ Tags
#two_pointers #string
Description
给你一个字符数组
从一个空字符串
如果这一组长度为
否则,需要向
压缩后得到的字符串
请在 修改完输入数组后 ,返回该数组的新长度。
你必须设计并实现一个只使用常量额外空间的算法来解决此问题。
Example
🟡 443.string-compression
🏷️ Tags
#two_pointers #string
Description
给你一个字符数组
chars ,请使用下述算法压缩:从一个空字符串
s 开始。对于 chars 中的每组 连续重复字符 :如果这一组长度为
1 ,则将字符追加到 s 中。否则,需要向
s 追加字符,后跟这一组的长度。压缩后得到的字符串
s 不应该直接返回 ,需要转储到字符数组 chars 中。需要注意的是,如果组长度为 10 或 10 以上,则在 chars 数组中会被拆分为多个字符。请在 修改完输入数组后 ,返回该数组的新长度。
你必须设计并实现一个只使用常量额外空间的算法来解决此问题。
Example
输入:chars = ["a","a","b","b","c","c","c"]
输出:返回 6 ,输入数组的前 6 个字符应该是:["a","2","b","2","c","3"]
解释:
"aa" 被 "a2" 替代。"bb" 被 "b2" 替代。"ccc" 被 "c3" 替代。
Leetcode.com 2021-08-20
🟡 36.valid-sudoku
🏷️ Tags
#array #hash_table #matrix
Description
Determine if a
Each row must contain the digits
Each column must contain the digits
Each of the nine
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
Example
🟡 36.valid-sudoku
🏷️ Tags
#array #hash_table #matrix
Description
Determine if a
9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits
1-9 without repetition.Each column must contain the digits
1-9 without repetition.Each of the nine
3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
Example
Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
Leetcode-cn.com 2021-08-22
🟡 789.escape-the-ghosts
🏷️ Tags
#array #math
Description
你在进行一个简化版的吃豆人游戏。你从
每一回合,你和阻碍者们可以同时向东,西,南,北四个方向移动,每次可以移动到距离原位置 1 个单位 的新位置。当然,也可以选择 不动 。所有动作 同时 发生。
如果你可以在任何阻碍者抓住你 之前 到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱成功。
只有在你有可能成功逃脱时,输出
Example
🟡 789.escape-the-ghosts
🏷️ Tags
#array #math
Description
你在进行一个简化版的吃豆人游戏。你从
[0, 0] 点开始出发,你的目的地是 target = [xtarget, ytarget] 。地图上有一些阻碍者,以数组 ghosts 给出,第 i 个阻碍者从 ghosts[i] = [xi, yi] 出发。所有输入均为 整数坐标 。每一回合,你和阻碍者们可以同时向东,西,南,北四个方向移动,每次可以移动到距离原位置 1 个单位 的新位置。当然,也可以选择 不动 。所有动作 同时 发生。
如果你可以在任何阻碍者抓住你 之前 到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱成功。
只有在你有可能成功逃脱时,输出
true ;否则,输出 false 。Example
输入:ghosts = [[1,0],[0,3]], target = [0,1]
输出:true
解释:你可以直接一步到达目的地 (0,1) ,在 (1, 0) 或者 (0, 3) 位置的阻碍者都不可能抓住你。
Leetcode.com 2021-08-21
🔴 37.sudoku-solver
🏷️ Tags
#array #backtracking #matrix
Description
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits
Each of the digits
Each of the digits
The
Example
🔴 37.sudoku-solver
🏷️ Tags
#array #backtracking #matrix
Description
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits
1-9 must occur exactly once in each row.Each of the digits
1-9 must occur exactly once in each column.Each of the digits
1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.The
'.' character indicates empty cells.Example
Input: board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
Explanation: The input board is shown above and the only valid solution is shown below:
Leetcode.com 2021-08-22
🔴 850.rectangle-area-ii
🏷️ Tags
#segment_tree #array #ordered_set #line_sweep
Description
We are given a list of (axis-aligned)
Find the total area covered by all
Example
🔴 850.rectangle-area-ii
🏷️ Tags
#segment_tree #array #ordered_set #line_sweep
Description
We are given a list of (axis-aligned)
rectangles. Each rectangle[i] = [xi1, yi1, xi2, yi2] , where (xi1, yi1) are the coordinates of the bottom-left corner, and (xi2, yi2) are the coordinates of the top-right corner of the ith rectangle.Find the total area covered by all
rectangles in the plane. Since the answer may be too large, return it modulo 109 + 7.Example
Input: rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
Output: 6
Explanation: As illustrated in the picture.
Leetcode.com 2021-08-23
🟢 653.two-sum-iv-input-is-a-bst
🏷️ Tags
#tree #depth_first_search #breadth_first_search #binary_search_tree #hash_table #two_pointers #binary_tree
Description
Given the
Example
🟢 653.two-sum-iv-input-is-a-bst
🏷️ Tags
#tree #depth_first_search #breadth_first_search #binary_search_tree #hash_table #two_pointers #binary_tree
Description
Given the
root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.Example
Input: root = [5,3,6,2,4,null,7], k = 9
Output: true
Leetcode-cn.com 2021-08-24
🟡 787.cheapest-flights-within-k-stops
🏷️ Tags
#depth_first_search #breadth_first_search #graph #dynamic_programming #shortest_path #heap_priority_queue
Description
有
现在给定所有的城市和航班,以及出发城市
Example
🟡 787.cheapest-flights-within-k-stops
🏷️ Tags
#depth_first_search #breadth_first_search #graph #dynamic_programming #shortest_path #heap_priority_queue
Description
有
n 个城市通过一些航班连接。给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 toi 抵达 pricei。现在给定所有的城市和航班,以及出发城市
src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ,并返回该价格。 如果不存在这样的路线,则输出 -1。Example
输入:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
输出: 200
解释:
城市航班图如下
从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。