Leetcode Daily Question
2.45K subscribers
517 files
2.16K links
Why are you asking me to do Leetcode for this CSS job?
Download Telegram
Leetcode.com 2021-08-06
🟡 429.n-ary-tree-level-order-traversal

🏷️ Tags
#tree #breadth_first_search

Description
Given an n-ary tree, return the level order traversal of its nodes' values.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example
Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]
Leetcode-cn.com 2021-08-08
🟢 1137.n-th-tribonacci-number

🏷️ Tags
#memoization #math #dynamic_programming

Description
泰波那契序列 Tn 定义如下:

T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2

给你整数 n,请返回第 n 个泰波那契数 Tn 的值。

Example
输入:n = 4
输出:4
解释:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
Leetcode.com 2021-08-07
🔴 132.palindrome-partitioning-ii

🏷️ Tags
#string #dynamic_programming

Description
Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

Example
Input: s = "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
Leetcode-cn.com 2021-08-09
🟡 313.super-ugly-number

🏷️ Tags
#array #hash_table #math #dynamic_programming #heap_priority_queue

Description
超级丑数 是一个正整数,并满足其所有质因数都出现在质数数组 primes 中。

给你一个整数 n 和一个整数数组 primes ,返回第 n 个 超级丑数 。

题目数据保证第 n 个 超级丑数 在 32-bit 带符号整数范围内。

Example
输入:n = 12, primes = [2,7,13,19]
输出:32
解释:给定长度为 4 的质数数组 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。
Leetcode.com 2021-08-08
🔴 1632.rank-transform-of-a-matrix

🏷️ Tags
#greedy #union_find #graph #topological_sort #array #matrix

Description
Given an m x n matrix, return a new matrix answer where answer[row][col] is the rank of matrix[row][col].

The rank is an integer that represents how large an element is compared to other elements. It is calculated using the following rules:


The rank is an integer starting from 1.
If two elements p and q are in the same row or column, then:

If p < q then rank(p) < rank(q)
If p == q then rank(p) == rank(q)
If p > q then rank(p) > rank(q)


The rank should be as small as possible.


It is guaranteed that answer is unique under the given rules.

Example
Input: matrix = [[1,2],[3,4]]
Output: [[1,2],[2,3]]
Explanation:
The rank of matrix[0][0] is 1 because it is the smallest integer in its row and column.
The rank of matrix[0][1] is 2 because matrix[0][1] > matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][0] is 2 because matrix[1][0] > matrix[0][0] and matrix[0][0] is rank 1.
The rank of matrix[1][1] is 3 because matrix[1][1] > matrix[0][1], matrix[1][1] > matrix[1][0], and both matrix[0][1] and matrix[1][0] are rank 2.
Leetcode.com 2021-08-09
🟢 415.add-strings

🏷️ Tags
#math #string #simulation

Description
Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

Example
Input: num1 = "11", num2 = "123"
Output: "134"
Leetcode-cn.com 2021-08-12
🟡 516.longest-palindromic-subsequence

🏷️ Tags
#string #dynamic_programming

Description
给你一个字符串 s ,找出其中最长的回文子序列,并返回该序列的长度。

子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。

 

Example
输入:s = "bbbab"
输出:4
解释:一个可能的最长回文子序列为 "bbbb" 。
Leetcode.com 2021-08-11
🟡 954.array-of-doubled-pairs

🏷️ Tags
#greedy #array #hash_table #sorting

Description
Given an array of integers arr of even length, return true if and only if it is possible to reorder it such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2.

Example
Input: arr = [3,1,3,6]
Output: false
Leetcode-cn.com 2021-08-13
🔴 233.number-of-digit-one

🏷️ Tags
#recursion #math #dynamic_programming

Description
给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。

 

Example
输入:n = 13
输出:6
Leetcode.com 2021-08-12
🟡 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
给你一份 n 位朋友的亲近程度列表,其中 n 总是 偶数 。

对每位朋友 ipreferences[i] 包含一份 按亲近程度从高到低排列 的朋友列表。换句话说,排在列表前面的朋友与 i 的亲近程度比排在列表后面的朋友更高。每个列表中的朋友均以 0n-1 之间的整数表示。

所有的朋友被分成几对,配对情况以列表 pairs 给出,其中 pairs[i] = [xi, yi] 表示 xiyi 配对,且 yixi 配对。

但是,这样的配对情况可能会是其中部分朋友感到不开心。在 xy 配对且 uv 配对的情况下,如果同时满足下述两个条件,x 就会不开心:


xu 的亲近程度胜过 xy,且
ux 的亲近程度胜过 uv


返回 不开心的朋友的数目 。

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 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
给你一个大小为 m x n 的网格和一个球。球的起始坐标为 [startRow, startColumn] 。你可以将球移到在四个方向上相邻的单元格内(可以穿过网格边界到达网格之外)。你 最多 可以移动 maxMove 次球。

给你五个整数 mnmaxMovestartRow 以及 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 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
给你一个字符串 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 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
可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:

'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 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
输入:"hello"
输出:"holle"
Leetcode.com 2021-08-18
🟡 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).