Leetcode.com 2021-07-09
🟡 300.longest-increasing-subsequence
🏷️ Tags
#array #binary_search #dynamic_programming
Description
Given an integer array
A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example,
Example
🟡 300.longest-increasing-subsequence
🏷️ Tags
#array #binary_search #dynamic_programming
Description
Given an integer array
nums, return the length of the longest strictly increasing subsequence.A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example,
[3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].Example
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Leetcode-cn.com 2021-07-11
🟡 274.h-index
🏷️ Tags
#array #counting_sort #sorting
Description
给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。且其余的 N - h 篇论文每篇被引用次数 不超过 h 次。
例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。
Example
🟡 274.h-index
🏷️ Tags
#array #counting_sort #sorting
Description
给定一位研究者论文被引用次数的数组(被引用次数是非负整数)。编写一个方法,计算出研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。且其余的 N - h 篇论文每篇被引用次数 不超过 h 次。
例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。
Example
输入:citations = [3,0,6,1,5]
输出:3
解释:给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。
百度百科
h-index
h-index又称为h指数或h因子(h-factor),是一种评价学术成就的新方法。h代表“高引用次数”(high citations),一名科研人员的h指数是指他至多有h篇论文分别被引用了至少h次。h指数能够比较准确地反映一个人的学术成就。一个人的h指数越高,则表明他的论文影响力越大。h指数就像其他指标一样,不适合用于跨学科的比较。h-index的发明者为赫希。
Leetcode.com 2021-07-10
🔴 639.decode-ways-ii
🏷️ 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
In addition to the mapping above, an encoded message may contain the
Given a string
Since the answer may be very large, return it modulo
Example
🔴 639.decode-ways-ii
🏷️ 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".In addition to the mapping above, an encoded message may contain the
'*' character, which can represent any digit from '1' to '9' ('0' is excluded). For example, the encoded message "1*" may represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19". Decoding "1*" is equivalent to decoding any of the encoded messages it can represent.Given a string
s containing digits and the '*' character, return the number of ways to decode it.Since the answer may be very large, return it modulo
109 + 7.Example
Input: s = "*"
Output: 9
Explanation: The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9".
Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively.
Hence, there are a total of 9 ways to decode "*".
Leetcode-cn.com 2021-07-12
🟡 275.h-index-ii
🏷️ Tags
#array #binary_search
Description
给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照 升序排列 。编写一个方法,计算出研究者的 h 指数。
h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数不多于 h 次。)"
Example
🟡 275.h-index-ii
🏷️ Tags
#array #binary_search
Description
给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照 升序排列 。编写一个方法,计算出研究者的 h 指数。
h 指数的定义: “h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (N 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。(其余的 N - h 篇论文每篇被引用次数不多于 h 次。)"
Example
输入: citations = [0,1,3,5,6]
输出: 3
解释: 给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 0, 1, 3, 5, 6 次。
由于研究者有 3 篇论文每篇至少被引用了 3 次,其余两篇论文每篇被引用不多于 3 次,所以她的 h 指数是 3。
百度百科
h-index
h-index又称为h指数或h因子(h-factor),是一种评价学术成就的新方法。h代表“高引用次数”(high citations),一名科研人员的h指数是指他至多有h篇论文分别被引用了至少h次。h指数能够比较准确地反映一个人的学术成就。一个人的h指数越高,则表明他的论文影响力越大。h指数就像其他指标一样,不适合用于跨学科的比较。h-index的发明者为赫希。
Leetcode.com 2021-07-11
🔴 295.find-median-from-data-stream
🏷️ Tags
#design #two_pointers #data_stream #sorting #heap_priority_queue
Description
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
For example, for
For example, for
Implement the MedianFinder class:
Example
🔴 295.find-median-from-data-stream
🏷️ Tags
#design #two_pointers #data_stream #sorting #heap_priority_queue
Description
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
For example, for
arr = [2,3,4], the median is 3.For example, for
arr = [2,3], the median is (2 + 3) / 2 = 2.5.Implement the MedianFinder class:
MedianFinder() initializes the MedianFinder object.void addNum(int num) adds the integer num from the data stream to the data structure.double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.Example
Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]
Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
Leetcode-cn.com 2021-07-13
🔴 218.the-skyline-problem
🏷️ Tags
#binary_indexed_tree #segment_tree #array #divide_and_conquer #ordered_set #line_sweep #heap_priority_queue
Description
城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回由这些建筑物形成的 天际线 。
每个建筑物的几何信息由数组
天际线 应该表示为由 “关键点” 组成的列表,格式
注意:输出天际线中不得有连续的相同高度的水平线。例如
Example
🔴 218.the-skyline-problem
🏷️ Tags
#binary_indexed_tree #segment_tree #array #divide_and_conquer #ordered_set #line_sweep #heap_priority_queue
Description
城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回由这些建筑物形成的 天际线 。
每个建筑物的几何信息由数组
buildings 表示,其中三元组 buildings[i] = [lefti, righti, heighti] 表示:lefti 是第 i 座建筑物左边缘的 x 坐标。righti 是第 i 座建筑物右边缘的 x 坐标。heighti 是第 i 座建筑物的高度。天际线 应该表示为由 “关键点” 组成的列表,格式
[[x1,y1],[x2,y2],...] ,并按 x 坐标 进行 排序 。关键点是水平线段的左端点。列表中最后一个点是最右侧建筑物的终点,y 坐标始终为 0 ,仅用于标记天际线的终点。此外,任何两个相邻建筑物之间的地面都应被视为天际线轮廓的一部分。注意:输出天际线中不得有连续的相同高度的水平线。例如
[...[2 3], [4 5], [7 5], [11 5], [12 7]...] 是不正确的答案;三条高度为 5 的线应该在最终输出中合并为一个:[...[2 3], [4 5], [12 7], ...]Example
输入:buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
输出:[[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
解释:
图 A 显示输入的所有建筑物的位置和高度,
图 B 显示由这些建筑物形成的天际线。图 B 中的红点表示输出列表中的关键点。
Leetcode.com 2021-07-12
🟢 205.isomorphic-strings
🏷️ Tags
#hash_table #string
Description
Given two strings
Two strings
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example
🟢 205.isomorphic-strings
🏷️ Tags
#hash_table #string
Description
Given two strings
s and t, determine if they are isomorphic.Two strings
s and t are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example
Input: s = "egg", t = "add"
Output: true
Leetcode.com 2021-07-13
🟡 162.find-peak-element
🏷️ Tags
#array #binary_search
Description
A peak element is an element that is strictly greater than its neighbors.
Given an integer array
You may imagine that
You must write an algorithm that runs in
Example
🟡 162.find-peak-element
🏷️ Tags
#array #binary_search
Description
A peak element is an element that is strictly greater than its neighbors.
Given an integer array
nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.You may imagine that
nums[-1] = nums[n] = -∞.You must write an algorithm that runs in
O(log n) time.Example
Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Leetcode-cn.com 2021-07-16
🟢 剑指 Offer 53 - I.zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof
🏷️ Tags
#array #binary_search
Description
统计一个数字在排序数组中出现的次数。
Example
🟢 剑指 Offer 53 - I.zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof
🏷️ Tags
#array #binary_search
Description
统计一个数字在排序数组中出现的次数。
Example
输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
Leetcode.com 2021-07-15
🟡 611.valid-triangle-number
🏷️ Tags
#greedy #array #two_pointers #binary_search #sorting
Description
Given an integer array
Example
🟡 611.valid-triangle-number
🏷️ Tags
#greedy #array #two_pointers #binary_search #sorting
Description
Given an integer array
nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Example
Input: nums = [2,2,3,4]
Output: 3
Explanation: Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3
Leetcode-cn.com 2021-07-17
🟢 剑指 Offer 42.lian-xu-zi-shu-zu-de-zui-da-he-lcof
🏷️ Tags
#array #divide_and_conquer #dynamic_programming
Description
输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。
要求时间复杂度为O(n)。
Example
🟢 剑指 Offer 42.lian-xu-zi-shu-zu-de-zui-da-he-lcof
🏷️ Tags
#array #divide_and_conquer #dynamic_programming
Description
输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。
要求时间复杂度为O(n)。
Example
输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
Leetcode.com 2021-07-16
🟡 18.4sum
🏷️ Tags
#array #two_pointers #sorting
Description
Given an array
You may return the answer in any order.
Example
🟡 18.4sum
🏷️ Tags
#array #two_pointers #sorting
Description
Given an array
nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:0 <= a, b, c, d < na, b, c, and d are distinct.nums[a] + nums[b] + nums[c] + nums[d] == targetYou may return the answer in any order.
Example
Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Leetcode-cn.com 2021-07-18
🟡 面试题 10.02.group-anagrams-lcci
🏷️ Tags
#hash_table #string #sorting
Description
编写一种方法,对字符串数组进行排序,将所有变位词组合在一起。变位词是指字母相同,但排列不同的字符串。
注意:本题相对原题稍作修改
Example
🟡 面试题 10.02.group-anagrams-lcci
🏷️ Tags
#hash_table #string #sorting
Description
编写一种方法,对字符串数组进行排序,将所有变位词组合在一起。变位词是指字母相同,但排列不同的字符串。
注意:本题相对原题稍作修改
Example
输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Leetcode.com 2021-07-17
🔴 927.three-equal-parts
🏷️ Tags
#array #math
Description
You are given an array
If it is possible, return any
All three parts have equal binary values.
If it is not possible, return
Note that the entire part is used when considering what binary value it represents. For example,
Example
🔴 927.three-equal-parts
🏷️ Tags
#array #math
Description
You are given an array
arr which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value.If it is possible, return any
[i, j] with i + 1 < j, such that:arr[0], arr[1], ..., arr[i] is the first part,arr[i + 1], arr[i + 2], ..., arr[j - 1] is the second part, andarr[j], arr[j + 1], ..., arr[arr.length - 1] is the third part.All three parts have equal binary values.
If it is not possible, return
[-1, -1].Note that the entire part is used when considering what binary value it represents. For example,
[1,1,0] represents 6 in decimal, not 3. Also, leading zeros are allowed, so [0,1,1] and [1,1] represent the same value.Example
Input: arr = [1,0,1,0,1]
Output: [0,3]
Leetcode-cn.com 2021-07-19
🟡 1838.frequency-of-the-most-frequent-element
🏷️ Tags
#array #binary_search #prefix_sum #sliding_window
Description
元素的 频数 是该元素在一个数组中出现的次数。
给你一个整数数组
执行最多
Example
🟡 1838.frequency-of-the-most-frequent-element
🏷️ Tags
#array #binary_search #prefix_sum #sliding_window
Description
元素的 频数 是该元素在一个数组中出现的次数。
给你一个整数数组
nums 和一个整数 k 。在一步操作中,你可以选择 nums 的一个下标,并将该下标对应元素的值增加 1 。执行最多
k 次操作后,返回数组中最高频元素的 最大可能频数 。Example
输入:nums = [1,2,4], k = 5
输出:3
解释:对第一个元素执行 3 次递增操作,对第二个元素执 2 次递增操作,此时 nums = [4,4,4] 。
4 是数组中最高频元素,频数是 3 。
Leetcode.com 2021-07-18
🔴 25.reverse-nodes-in-k-group
🏷️ Tags
#recursion #linked_list
Description
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Example
🔴 25.reverse-nodes-in-k-group
🏷️ Tags
#recursion #linked_list
Description
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Example
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Leetcode-cn.com 2021-07-20
🟡 1877.minimize-maximum-pair-sum-in-array
🏷️ Tags
#greedy #array #two_pointers #sorting
Description
一个数对
比方说,如果我们有数对
给你一个长度为 偶数
最大数对和 的值 最小 。
请你在最优数对划分的方案下,返回最小的 最大数对和 。
Example
🟡 1877.minimize-maximum-pair-sum-in-array
🏷️ Tags
#greedy #array #two_pointers #sorting
Description
一个数对
(a,b) 的 数对和 等于 a + b 。最大数对和 是一个数对数组中最大的 数对和 。比方说,如果我们有数对
(1,5) ,(2,3) 和 (4,4),最大数对和 为 max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8 。给你一个长度为 偶数
n 的数组 nums ,请你将 nums 中的元素分成 n / 2 个数对,使得:nums 中每个元素 恰好 在 一个 数对中,且最大数对和 的值 最小 。
请你在最优数对划分的方案下,返回最小的 最大数对和 。
Example
输入:nums = [3,5,2,3]
输出:7
解释:数组中的元素可以分为数对 (3,3) 和 (5,2) 。
最大数对和为 max(3+3, 5+2) = max(6, 7) = 7 。
Leetcode.com 2021-07-19
🟢 235.lowest-common-ancestor-of-a-binary-search-tree
🏷️ Tags
#tree #depth_first_search #binary_search_tree #binary_tree
Description
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
Example
🟢 235.lowest-common-ancestor-of-a-binary-search-tree
🏷️ Tags
#tree #depth_first_search #binary_search_tree #binary_tree
Description
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”Example
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Leetcode.com 2021-07-20
🟡 384.shuffle-an-array
🏷️ Tags
#array #math #randomized
Description
Given an integer array
Implement the
Example
🟡 384.shuffle-an-array
🏷️ Tags
#array #math #randomized
Description
Given an integer array
nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.Implement the
Solution class:Solution(int[] nums) Initializes the object with the integer array nums.int[] reset() Resets the array to its original configuration and returns it.int[] shuffle() Returns a random shuffling of the array.Example
Input
["Solution", "shuffle", "reset", "shuffle"]
[[[1, 2, 3]], [], [], []]
Output
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
Explanation
Solution solution = new Solution([1, 2, 3]);
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
// Any permutation of [1,2,3] must be equally likely to be returned.
// Example: return [3, 1, 2]
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
Leetcode-cn.com 2021-07-22
🟡 138.copy-list-with-random-pointer
🏷️ Tags
#hash_table #linked_list
Description
给你一个长度为
构造这个链表的 深拷贝。 深拷贝应该正好由
例如,如果原链表中有
返回复制链表的头节点。
用一个由
你的代码 只 接受原链表的头节点
Example
🟡 138.copy-list-with-random-pointer
🏷️ Tags
#hash_table #linked_list
Description
给你一个长度为
n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。构造这个链表的 深拷贝。 深拷贝应该正好由
n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。例如,如果原链表中有
X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。返回复制链表的头节点。
用一个由
n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:val:一个表示 Node.val 的整数。random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。你的代码 只 接受原链表的头节点
head 作为传入参数。Example
输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]