diff --git a/Week_01/id_26/LeetCode_15_26.py b/Week_01/id_26/LeetCode_15_26.py index 8eb97948..cc5ed56d 100644 --- a/Week_01/id_26/LeetCode_15_26.py +++ b/Week_01/id_26/LeetCode_15_26.py @@ -3,6 +3,9 @@ def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] + 思路: + 1) 防止重复结果,所以先排序,遍历时跳过相邻的重复值 + 2) 二分查找,大于0则右测向左移动,小于0则左边向右移动 """ nums.sort() ret, n = [], len(nums) diff --git a/Week_01/id_26/LeetCode_26_26.py b/Week_01/id_26/LeetCode_26_26.py index d3d2161c..80904ef3 100644 --- a/Week_01/id_26/LeetCode_26_26.py +++ b/Week_01/id_26/LeetCode_26_26.py @@ -6,8 +6,7 @@ def removeDuplicates(self, nums): """ k = 1 for n in nums: - if n != nums[k-1]: + if n != nums[k - 1]: nums[k] = n k += 1 return k - \ No newline at end of file diff --git a/Week_02/id_26/LeetCode_101_26.py b/Week_02/id_26/LeetCode_101_26.py new file mode 100644 index 00000000..821c749b --- /dev/null +++ b/Week_02/id_26/LeetCode_101_26.py @@ -0,0 +1,66 @@ +# +# @lc app=leetcode.cn id=101 lang=python +# +# [101] 对称二叉树 +# +# https://leetcode-cn.com/problems/symmetric-tree/description/ +# +# algorithms +# Easy (46.65%) +# Likes: 351 +# Dislikes: 0 +# Total Accepted: 34.7K +# Total Submissions: 74.4K +# Testcase Example: '[1,2,2,3,4,4,3]' +# +# 给定一个二叉树,检查它是否是镜像对称的。 +# +# 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 +# +# ⁠ 1 +# ⁠ / \ +# ⁠ 2 2 +# ⁠/ \ / \ +# 3 4 4 3 +# +# +# 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: +# +# ⁠ 1 +# ⁠ / \ +# ⁠ 2 2 +# ⁠ \ \ +# ⁠ 3 3 +# +# +# 说明: +# +# 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。 +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def isSymmetric(self, root): + """ + :type root: TreeNode + :rtype: bool + """ + + def _isSymmetric(node1, node2): + if not node1 and not node2: + return True + if not node1 or not node2: + return False + if node1.val != node2.val: + return False + return _isSymmetric(node1.left, node2.right) and _isSymmetric( + node1.right, node2.left) + + return _isSymmetric(root, root) diff --git a/Week_02/id_26/LeetCode_102_26.py b/Week_02/id_26/LeetCode_102_26.py new file mode 100644 index 00000000..76655d91 --- /dev/null +++ b/Week_02/id_26/LeetCode_102_26.py @@ -0,0 +1,65 @@ +# +# @lc app=leetcode.cn id=102 lang=python +# +# [102] 二叉树的层次遍历 +# +# https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/ +# +# algorithms +# Medium (56.47%) +# Likes: 213 +# Dislikes: 0 +# Total Accepted: 28.4K +# Total Submissions: 50.2K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。 +# +# 例如: +# 给定二叉树: [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# +# 返回其层次遍历结果: +# +# [ +# ⁠ [3], +# ⁠ [9,20], +# ⁠ [15,7] +# ] +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def levelOrder(self, root): + """ + :type root: TreeNode + :rtype: List[List[int]] + """ + if not root: + return [] + ret, layer = [], [root] + while layer: + tmp_layer, line = [], [] + for i in layer: + line.append(i.val) + if i.left: + tmp_layer.append(i.left) + if i.right: + tmp_layer.append(i.right) + layer = tmp_layer + ret.append(line) + return ret diff --git a/Week_02/id_26/LeetCode_103_26.py b/Week_02/id_26/LeetCode_103_26.py new file mode 100644 index 00000000..d567c83f --- /dev/null +++ b/Week_02/id_26/LeetCode_103_26.py @@ -0,0 +1,68 @@ +# +# @lc app=leetcode.cn id=103 lang=python +# +# [103] 二叉树的锯齿形层次遍历 +# +# https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/description/ +# +# algorithms +# Medium (49.74%) +# Likes: 64 +# Dislikes: 0 +# Total Accepted: 11.7K +# Total Submissions: 23.5K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。 +# +# 例如: +# 给定二叉树 [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# +# 返回锯齿形层次遍历如下: +# +# [ +# ⁠ [3], +# ⁠ [20,9], +# ⁠ [15,7] +# ] +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def zigzagLevelOrder(self, root): + """ + 思路:层序遍历,每隔一层换一个方向遍历 + :type root: TreeNode + :rtype: List[List[int]] + """ + if not root: + return [] + line, ret, rot = [root], [], 1 + while line: + tmp1, tmp2 = [], [] + for i in line: + if i.left: + tmp1.append(i.left) + if i.right: + tmp1.append(i.right) + for j in line[::rot]: + tmp2.append(j.val) + ret.append(tmp2) + line = tmp1 + rot *= -1 + return ret diff --git a/Week_02/id_26/LeetCode_111_26.py b/Week_02/id_26/LeetCode_111_26.py new file mode 100644 index 00000000..396edc39 --- /dev/null +++ b/Week_02/id_26/LeetCode_111_26.py @@ -0,0 +1,55 @@ +# +# @lc app=leetcode.cn id=111 lang=python +# +# [111] 二叉树的最小深度 +# +# https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/ +# +# algorithms +# Easy (38.70%) +# Likes: 125 +# Dislikes: 0 +# Total Accepted: 19K +# Total Submissions: 49K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,找出其最小深度。 +# +# 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 +# +# 说明: 叶子节点是指没有子节点的节点。 +# +# 示例: +# +# 给定二叉树 [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# 返回它的最小深度  2. +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def minDepth(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + left = self.minDepth(root.left) + right = self.minDepth(root.right) + if not left or not right: + return 1 + left + right + return 1 + min(left, right) diff --git a/Week_02/id_26/LeetCode_1_26.py b/Week_02/id_26/LeetCode_1_26.py new file mode 100644 index 00000000..7a0033a4 --- /dev/null +++ b/Week_02/id_26/LeetCode_1_26.py @@ -0,0 +1,43 @@ +# +# @lc app=leetcode.cn id=1 lang=python +# +# [1] 两数之和 +# +# https://leetcode-cn.com/problems/two-sum/description/ +# +# algorithms +# Easy (46.10%) +# Likes: 5365 +# Dislikes: 0 +# Total Accepted: 397.2K +# Total Submissions: 861.4K +# Testcase Example: '[2,7,11,15]\n9' +# +# 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 +# +# 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 +# +# 示例: +# +# 给定 nums = [2, 7, 11, 15], target = 9 +# +# 因为 nums[0] + nums[1] = 2 + 7 = 9 +# 所以返回 [0, 1] +# +# +# + + +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + s = {} + for i in range(len(nums)): + tmp = target - nums[i] + if tmp in s: + return [s[tmp], i] + s[nums[i]] = i diff --git a/Week_02/id_26/LeetCode_220_26.py b/Week_02/id_26/LeetCode_220_26.py new file mode 100644 index 00000000..9065a0fc --- /dev/null +++ b/Week_02/id_26/LeetCode_220_26.py @@ -0,0 +1,117 @@ +# +# @lc app=leetcode.cn id=220 lang=python +# +# [220] 存在重复元素 III +# +# https://leetcode-cn.com/problems/contains-duplicate-iii/description/ +# +# algorithms +# Medium (24.08%) +# Likes: 63 +# Dislikes: 0 +# Total Accepted: 5.1K +# Total Submissions: 20.9K +# Testcase Example: '[1,2,3,1]\n3\n0' +# +# 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j +# 之间的差的绝对值最大为 ķ。 +# +# 示例 1: +# +# 输入: nums = [1,2,3,1], k = 3, t = 0 +# 输出: true +# +# 示例 2: +# +# 输入: nums = [1,0,1,1], k = 1, t = 2 +# 输出: true +# +# 示例 3: +# +# 输入: nums = [1,5,9,1,5,9], k = 2, t = 3 +# 输出: false +# +# + + +class Solution(object): + def containsNearbyAlmostDuplicate(self, nums, k, t): + """ + :type nums: List[int] + :type k: int + :type t: int + :rtype: bool + 解法1:暴力求解,复杂度O(n*k),容易超时,最后一个用例过不了。面向用例编程 - -。真的香 + """ + if not nums: + return False + n = len(nums) + wins = set() + for i in range(n): + if t == 0: + if nums[i] in wins: + return True + else: + for v in wins: + if abs(nums[i] - v) <= t: + return True + wins.add(nums[i]) + if len(wins) == k + 1: + wins.remove(nums[i - k]) + return False + + def containsNearbyAlmostDuplicate2(self, nums, k, t): + """ + :type nums: List[int] + :type k: int + :type t: int + :rtype: bool + 解法2:来自https://leetcode.com/problems/contains-duplicate-iii/discuss/61756/Python-OrderedDict + 证明=》 + 如果: | nums[i] - nums[j] | <= t 式a + 等价: | nums[i] / t - nums[j] / t | <= 1 式b + 推出: | floor(nums[i] / t) - floor(nums[j] / t) | <= 1 式c + ​等价: floor(nums[j] / t) ∈ {floor(nums[i] / t) - 1, floor(nums[i] / t), floor(nums[i] / t) + 1} 式d + 其中式b是式c的充分非必要条件,因为逆否命题与原命题等价,所以: + 如果: floor(nums[j] / t) ∉ {floor(nums[i] / t) - 1, floor(nums[i] / t), floor(nums[i] / t) + 1} 非d + 推出: | nums[i] - nums[j] | > t 非a + """ + import collections + if k < 1 or t < 0: + return False + dic = collections.OrderedDict() + for n in nums: + key = n if not t else n // t + for m in (dic.get(key - 1), dic.get(key), dic.get(key + 1)): + if m is not None and abs(n - m) <= t: + return True + if len(dic) == k: + dic.popitem(False) + dic[key] = n + return False + + + def containsNearbyAlmostDuplicate3(self, nums, k, t): + if t < 0: + return False + cache = {} + for i in range(len(nums)): + if i - k > 0: + bucket_id_to_delete = nums[i - k - 1] // (t + 1) + del cache[bucket_id_to_delete] + bucket_id = nums[i] // (t + 1) + condition1 = (bucket_id in cache) + condition2 = ((bucket_id - 1 in cache + and abs(cache[bucket_id - 1] - nums[i]) <= t)) + condition3 = ((bucket_id + 1 in cache + and abs(cache[bucket_id + 1] - nums[i]) <= t)) + if condition1 or condition2 or condition3: + return True + cache[bucket_id] = nums[i] + return False + + +# print(Solution().containsNearbyAlmostDuplicate([1, 2, 3, 1], 3, 0)) +# print(Solution().containsNearbyAlmostDuplicate([1, 0, 1, 1], 1, 2)) +# print(Solution().containsNearbyAlmostDuplicate([1, 5, 9, 1, 5, 9], 2, 3)) +# print(Solution().containsNearbyAlmostDuplicate([2, 2], 3, 0)) diff --git a/Week_02/id_26/LeetCode_235_26.py b/Week_02/id_26/LeetCode_235_26.py new file mode 100644 index 00000000..af7b31be --- /dev/null +++ b/Week_02/id_26/LeetCode_235_26.py @@ -0,0 +1,89 @@ +# +# @lc app=leetcode.cn id=235 lang=python +# +# [235] 二叉搜索树的最近公共祖先 +# +# https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/ +# +# algorithms +# Easy (59.44%) +# Likes: 122 +# Dislikes: 0 +# Total Accepted: 15.7K +# Total Submissions: 26.3K +# Testcase Example: '[6,2,8,0,4,7,9,null,null,3,5]\n2\n8' +# +# 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 +# +# 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x +# 的深度尽可能大(一个节点也可以是它自己的祖先)。” +# +# 例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5] +# +# +# +# +# +# 示例 1: +# +# 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 +# 输出: 6 +# 解释: 节点 2 和节点 8 的最近公共祖先是 6。 +# +# +# 示例 2: +# +# 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 +# 输出: 2 +# 解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。 +# +# +# +# 说明: +# +# +# 所有节点的值都是唯一的。 +# p、q 为不同节点且均存在于给定的二叉搜索树中。 +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def lowestCommonAncestor1(self, root, p, q): + """ + :type root: TreeNode + :type p: TreeNode + :type q: TreeNode + :rtype: TreeNode + 解法1:和二叉树的解法一样 + """ + if not root or root == q or root == p: + return root + left = self.lowestCommonAncestor(root.left, p, q) + right = self.lowestCommonAncestor(root.right, p, q) + if not left: + return right + if not right: + return left + return root + + def lowestCommonAncestor(self, root, p, q): + """ + :type root: TreeNode + :type p: TreeNode + :type q: TreeNode + :rtype: TreeNode + 解法2:利用二叉搜索树的特性 + """ + if p.val < root.val > q.val: + return self.lowestCommonAncestor(root.left, p, q) + if p.val > root.val < q.val: + return self.lowestCommonAncestor(root.right, p, q) + return root diff --git a/Week_02/id_26/LeetCode_236_26.py b/Week_02/id_26/LeetCode_236_26.py new file mode 100644 index 00000000..6cd059c5 --- /dev/null +++ b/Week_02/id_26/LeetCode_236_26.py @@ -0,0 +1,75 @@ +# +# @lc app=leetcode.cn id=236 lang=python +# +# [236] 二叉树的最近公共祖先 +# +# https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ +# +# algorithms +# Medium (54.67%) +# Likes: 180 +# Dislikes: 0 +# Total Accepted: 14.4K +# Total Submissions: 26.4K +# Testcase Example: '[3,5,1,6,2,0,8,null,null,7,4]\n5\n1' +# +# 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 +# +# 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x +# 的深度尽可能大(一个节点也可以是它自己的祖先)。” +# +# 例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4] +# +# +# +# +# +# 示例 1: +# +# 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 +# 输出: 3 +# 解释: 节点 5 和节点 1 的最近公共祖先是节点 3。 +# +# +# 示例 2: +# +# 输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 +# 输出: 5 +# 解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。 +# +# +# +# +# 说明: +# +# +# 所有节点的值都是唯一的。 +# p、q 为不同节点且均存在于给定的二叉树中。 +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def lowestCommonAncestor(self, root, p, q): + """ + :type root: TreeNode + :type p: TreeNode + :type q: TreeNode + :rtype: TreeNode + """ + if not root or root == q or root == p: + return root + left = self.lowestCommonAncestor(root.left, p, q) + right = self.lowestCommonAncestor(root.right, p, q) + if not left: + return right + if not right: + return left + return root diff --git a/Week_02/id_26/LeetCode_242_26.py b/Week_02/id_26/LeetCode_242_26.py new file mode 100644 index 00000000..27a1942c --- /dev/null +++ b/Week_02/id_26/LeetCode_242_26.py @@ -0,0 +1,74 @@ +# +# @lc app=leetcode.cn id=242 lang=python +# +# [242] 有效的字母异位词 +# +# https://leetcode-cn.com/problems/valid-anagram/description/ +# +# algorithms +# Easy (53.28%) +# Likes: 82 +# Dislikes: 0 +# Total Accepted: 32.3K +# Total Submissions: 60.6K +# Testcase Example: '"anagram"\n"nagaram"' +# +# 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 +# +# 示例 1: +# +# 输入: s = "anagram", t = "nagaram" +# 输出: true +# +# +# 示例 2: +# +# 输入: s = "rat", t = "car" +# 输出: false +# +# 说明: +# 你可以假设字符串只包含小写字母。 +# +# 进阶: +# 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? +# +# + + +class Solution(object): + """ + 解法1:排序比较 + 解法2:清点字母数量 + """ + def isAnagram1(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + return sorted(s) == sorted(t) + + def isAnagram2(self, s, t): + """ + :type s: str + :type t: str + :rtype: bool + """ + if len(s) != len(t): + return False + map = {} + for i in s: + if i not in map: + map[i] = 0 + map[i] += 1 + for i in t: + if i not in map: + return False + map[i] -= 1 + if map[i] < 0: + return False + for i in map: + if map[i] != 0: + return False + return True + diff --git a/Week_02/id_26/LeetCode_297_26.py b/Week_02/id_26/LeetCode_297_26.py new file mode 100644 index 00000000..166bc74a --- /dev/null +++ b/Week_02/id_26/LeetCode_297_26.py @@ -0,0 +1,108 @@ +# +# @lc app=leetcode.cn id=297 lang=python +# +# [297] 二叉树的序列化与反序列化 +# +# https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/description/ +# +# algorithms +# Hard (39.27%) +# Likes: 49 +# Dislikes: 0 +# Total Accepted: 4.7K +# Total Submissions: 12K +# Testcase Example: '[1,2,3,null,null,4,5]' +# +# +# 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。 +# +# 请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / +# 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。 +# +# 示例: +# +# 你可以将以下二叉树: +# +# ⁠ 1 +# ⁠ / \ +# ⁠ 2 3 +# ⁠ / \ +# ⁠ 4 5 +# +# 序列化为 "[1,2,3,null,null,4,5]" +# +# 提示: 这与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode +# 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。 +# +# 说明: 不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的。 +# +# +# Definition for a binary tree node. + + +class TreeNode(object): + def __init__(self, x): + self.val = x + self.left = None + self.right = None + + +class Codec: + def serialize(self, root): + """Encodes a tree to a single string. + + :type root: TreeNode + :rtype: str + """ + if not root: + return '' + line, ret = [root], [str(root.val)] + while line: + next_line = [] + for node in line: + if not node.left: + ret.append('') + else: + next_line.append(node.left) + ret.append(str(node.left.val)) + if not node.right: + ret.append('') + else: + next_line.append(node.right) + ret.append(str(node.right.val)) + line = next_line + return ','.join(ret) + + def deserialize(self, data): + """Decodes your encoded data to tree. + + :type data: str + :rtype: TreeNode + """ + if not data: + return None + data = data.split(',')[::-1] + root = TreeNode(int(data.pop())) + line = [root] + while line and data: + next_line = [] + for node in line: + left, right = data.pop(), data.pop() + if left != '': + node.left = TreeNode(int(left)) + next_line.append(node.left) + if right != '': + node.right = TreeNode(int(right)) + next_line.append(node.right) + line = next_line + return root + + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.deserialize('1') +# tmp = codec.deserialize('1,2,3,,,4,5') +# tmp = codec.serialize(tmp) +# print(tmp) + +# codec.deserialize(codec.serialize(root)) diff --git a/Week_02/id_26/LeetCode_315_26.py b/Week_02/id_26/LeetCode_315_26.py new file mode 100644 index 00000000..14ec5a97 --- /dev/null +++ b/Week_02/id_26/LeetCode_315_26.py @@ -0,0 +1,90 @@ +# +# @lc app=leetcode.cn id=315 lang=python +# +# [315] 计算右侧小于当前元素的个数 +# +# https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/description/ +# +# algorithms +# Hard (37.52%) +# Likes: 63 +# Dislikes: 0 +# Total Accepted: 2.9K +# Total Submissions: 7.8K +# Testcase Example: '[5,2,6,1]' +# +# 给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是  nums[i] 右侧小于 +# nums[i] 的元素的数量。 +# +# 示例: +# +# 输入: [5,2,6,1] +# 输出: [2,1,1,0] +# 解释: +# 5 的右侧有 2 个更小的元素 (2 和 1). +# 2 的右侧仅有 1 个更小的元素 (1). +# 6 的右侧有 1 个更小的元素 (1). +# 1 的右侧有 0 个更小的元素. +# +# +# 思路: 构建一个二叉搜索树,左子树小于等于根节点,右子树大于根节点 +# 节点内记录下标,所有左节点的个数,以及右侧小于该节点的总数 +# 若插入节点小于等于当前节点,则当前节点的左节点总数+1 +# 若插入节点大于当前节点,则当前节点的右侧小于该节点的总数=当前节点的左节点总数+1(当前节点) +# 最后深度遍历 + + +class BST(object): + def __init__(self, index, val): + self.left = None + self.right = None + self.index = index + self.val = val + # 右侧小于该节点的总数 + self.count = 0 + # 左子树总数 + self.left_count = 0 + + def insert(self, node): + if node.val <= self.val: + self.left_count += 1 + if not self.left: + self.left = node + else: + self.left.insert(node) + else: + node.count += self.left_count + 1 + if not self.right: + self.right = node + else: + self.right.insert(node) + + +class Solution(object): + def countSmaller(self, nums): + """ + :type nums: List[int] + :rtype: List[int] + """ + if not nums: + return [] + nums = nums[::-1] + root = BST(0, nums[0]) + for i in range(1, len(nums)): + root.insert(BST(i, nums[i])) + ret = [0] * len(nums) + + def _dfs(root): + if not root: + return ret + ret[root.index] = root.count + _dfs(root.left) + _dfs(root.right) + return ret + + return _dfs(root)[::-1] + + +# print(Solution().countSmaller([5, 2, 6, 1])) +# print(Solution().countSmaller([1, 2, 7, 8, 5])) +# print(Solution().countSmaller([-1, -1])) diff --git a/Week_02/id_26/LeetCode_3_26.py b/Week_02/id_26/LeetCode_3_26.py new file mode 100644 index 00000000..acde72eb --- /dev/null +++ b/Week_02/id_26/LeetCode_3_26.py @@ -0,0 +1,65 @@ +# +# @lc app=leetcode.cn id=3 lang=python +# +# [3] 无重复字符的最长子串 +# +# https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/ +# +# algorithms +# Medium (29.78%) +# Likes: 1897 +# Dislikes: 0 +# Total Accepted: 135.1K +# Total Submissions: 453.3K +# Testcase Example: '"abcabcbb"' +# +# 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 +# +# 示例 1: +# +# 输入: "abcabcbb" +# 输出: 3 +# 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 +# +# +# 示例 2: +# +# 输入: "bbbbb" +# 输出: 1 +# 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 +# +# +# 示例 3: +# +# 输入: "pwwkew" +# 输出: 3 +# 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 +# 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。 +# +# +# + + +class Solution(object): + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + map = {} + j = 0 + ret = 0 + for i in range(len(s)): + if s[i] in map: + j = max(map[s[i]], j) + ret = max(ret, i - j + 1) + map[s[i]] = i + 1 + return ret + + +print(Solution().lengthOfLongestSubstring('abcabcbb')) +print(Solution().lengthOfLongestSubstring('pwwkew')) +print(Solution().lengthOfLongestSubstring('bbbbb')) +print(Solution().lengthOfLongestSubstring(' ')) +print(Solution().lengthOfLongestSubstring('dvdf')) +print(Solution().lengthOfLongestSubstring('abba')) diff --git a/Week_02/id_26/LeetCode_692_26.py b/Week_02/id_26/LeetCode_692_26.py new file mode 100644 index 00000000..e8c36211 --- /dev/null +++ b/Week_02/id_26/LeetCode_692_26.py @@ -0,0 +1,77 @@ +# +# @lc app=leetcode.cn id=692 lang=python +# +# [692] 前K个高频单词 +# +# https://leetcode-cn.com/problems/top-k-frequent-words/description/ +# +# algorithms +# Medium (39.56%) +# Likes: 37 +# Dislikes: 0 +# Total Accepted: 2K +# Total Submissions: 5.1K +# Testcase Example: '["i", "love", "leetcode", "i", "love", "coding"]\n2' +# +# 给一非空的单词列表,返回前 k 个出现次数最多的单词。 +# +# 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。 +# +# 示例 1: +# +# +# 输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 +# 输出: ["i", "love"] +# 解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。 +# ⁠ 注意,按字母顺序 "i" 在 "love" 之前。 +# +# +# +# +# 示例 2: +# +# +# 输入: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], +# k = 4 +# 输出: ["the", "is", "sunny", "day"] +# 解析: "the", "is", "sunny" 和 "day" 是出现次数最多的四个单词, +# ⁠ 出现次数依次为 4, 3, 2 和 1 次。 +# +# +# +# +# 注意: +# +# +# 假定 k 总为有效值, 1 ≤ k ≤ 集合元素数。 +# 输入的单词均由小写字母组成。 +# +# +# +# +# 扩展练习: +# +# +# 尝试以 O(n log k) 时间复杂度和 O(n) 空间复杂度解决。 +# +# +# + + +class Solution(object): + def topKFrequent(self, words, k): + """ + :type words: List[str] + :type k: int + :rtype: List[str] + """ + map = {} + for i in words: + map[i] = map.get(i, 0) + 1 + ret = sorted(map, key=lambda word: (-map[word], word)) + return ret[:k] + + +# print(Solution().topKFrequent( +# ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], +# 2)) diff --git a/Week_02/id_26/LeetCode_783_26.py b/Week_02/id_26/LeetCode_783_26.py new file mode 100644 index 00000000..e78c1f14 --- /dev/null +++ b/Week_02/id_26/LeetCode_783_26.py @@ -0,0 +1,72 @@ +# +# @lc app=leetcode.cn id=783 lang=python +# +# [783] 二叉搜索树结点最小距离 +# +# https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/description/ +# +# algorithms +# Easy (51.59%) +# Likes: 24 +# Dislikes: 0 +# Total Accepted: 3.2K +# Total Submissions: 6.2K +# Testcase Example: '[4,2,6,1,3,null,null]' +# +# 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值。 +# +# 示例: +# +# +# 输入: root = [4,2,6,1,3,null,null] +# 输出: 1 +# 解释: +# 注意,root是树结点对象(TreeNode object),而不是数组。 +# +# 给定的树 [4,2,6,1,3,null,null] 可表示为下图: +# +# ⁠ 4 +# ⁠ / \ +# ⁠ 2 6 +# ⁠ / \ +# ⁠ 1 3 +# +# 最小的差值是 1, 它是节点1和节点2的差值, 也是节点3和节点2的差值。 +# +# 注意: +# +# +# 二叉树的大小范围在 2 到 100。 +# 二叉树总是有效的,每个节点的值都是整数,且不重复。 +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def minDiffInBST(self, root): + """ + :type root: TreeNode + :rtype: int + """ + self.ans = 31 << 1 + self.last = -31 << 1 + + def dfs(root): + if not root: + return + if root.left: + dfs(root.left) + self.ans = min(self.ans, root.val - self.last) + self.last = root.val + if root.right: + dfs(root.right) + + dfs(root) + return self.ans diff --git a/Week_02/id_26/LeetCode_938_26.py b/Week_02/id_26/LeetCode_938_26.py new file mode 100644 index 00000000..f57f99e3 --- /dev/null +++ b/Week_02/id_26/LeetCode_938_26.py @@ -0,0 +1,73 @@ +# +# @lc app=leetcode.cn id=938 lang=python +# +# [938] 二叉搜索树的范围和 +# +# https://leetcode-cn.com/problems/range-sum-of-bst/description/ +# +# algorithms +# Easy (74.72%) +# Likes: 39 +# Dislikes: 0 +# Total Accepted: 4.8K +# Total Submissions: 6.4K +# Testcase Example: '[10,5,15,3,7,null,18]\n7\n15' +# +# 给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和。 +# +# 二叉搜索树保证具有唯一的值。 +# +# +# +# 示例 1: +# +# 输入:root = [10,5,15,3,7,null,18], L = 7, R = 15 +# 输出:32 +# +# +# 示例 2: +# +# 输入:root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 +# 输出:23 +# +# +# +# +# 提示: +# +# +# 树中的结点数量最多为 10000 个。 +# 最终的答案保证小于 2^31。 +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def rangeSumBST(self, root, L, R): + """ + :type root: TreeNode + :type L: int + :type R: int + :rtype: int + """ + self.ans = 0 + + def _rangeSumBST(root): + if not root: + return + if root.val > L: + _rangeSumBST(root.left) + if L <= root.val <= R: + self.ans += root.val + if root.val < R: + _rangeSumBST(root.right) + + _rangeSumBST(root) + return self.ans diff --git a/Week_02/id_26/LeetCode_98_26.py b/Week_02/id_26/LeetCode_98_26.py new file mode 100644 index 00000000..f87a78cf --- /dev/null +++ b/Week_02/id_26/LeetCode_98_26.py @@ -0,0 +1,93 @@ +# +# @lc app=leetcode.cn id=98 lang=python +# +# [98] 验证二叉搜索树 +# +# https://leetcode-cn.com/problems/validate-binary-search-tree/description/ +# +# algorithms +# Medium (26.07%) +# Likes: 204 +# Dislikes: 0 +# Total Accepted: 25K +# Total Submissions: 95.8K +# Testcase Example: '[2,1,3]' +# +# 给定一个二叉树,判断其是否是一个有效的二叉搜索树。 +# +# 假设一个二叉搜索树具有如下特征: +# +# +# 节点的左子树只包含小于当前节点的数。 +# 节点的右子树只包含大于当前节点的数。 +# 所有左子树和右子树自身必须也是二叉搜索树。 +# +# +# 示例 1: +# +# 输入: +# ⁠ 2 +# ⁠ / \ +# ⁠ 1 3 +# 输出: true +# +# +# 示例 2: +# +# 输入: +# ⁠ 5 +# ⁠ / \ +# ⁠ 1 4 +# / \ +# 3 6 +# 输出: false +# 解释: 输入为: [5,1,4,null,null,3,6]。 +# 根节点的值为 5 ,但是其右子节点值为 4 。 +# +# +# +# Definition for a binary tree node. + +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def isValidBST(self, root): + """ + :type root: TreeNode + :rtype: bool + 解法1:当前节点的值为 左子树的最大值 and 右子树的最小值 + """ + + def _isValidBST(root, min, max): + if not root: + return True + if root.val >= max or root.val <= min: + return False + return _isValidBST(root.left, min, root.val) and _isValidBST( + root.right, root.val, max) + + return _isValidBST(root, -1 << 32, 1 << 31) + + def isValidBST2(self, root): + """ + :type root: TreeNode + :rtype: bool + 解法2:中序遍历递增 + """ + self.last = -1 << 32 + + def _isValidBST(root): + if not root: + return True + left = _isValidBST(root.left) + ret = root.val > self.last + self.last = root.val + right = _isValidBST(root.right) + return left and ret and right + + return _isValidBST(root) diff --git a/Week_03/id_26/LeetCode_102_26.py b/Week_03/id_26/LeetCode_102_26.py new file mode 100644 index 00000000..f665cc69 --- /dev/null +++ b/Week_03/id_26/LeetCode_102_26.py @@ -0,0 +1,64 @@ +# +# @lc app=leetcode.cn id=102 lang=python +# +# [102] 二叉树的层次遍历 +# +# https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/ +# +# algorithms +# Medium (56.47%) +# Likes: 213 +# Dislikes: 0 +# Total Accepted: 28.4K +# Total Submissions: 50.2K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。 +# +# 例如: +# 给定二叉树: [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# +# 返回其层次遍历结果: +# +# [ +# ⁠ [3], +# ⁠ [9,20], +# ⁠ [15,7] +# ] +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def levelOrder(self, root): + """ + :type root: TreeNode + :rtype: List[List[int]] + """ + if not root: + return [] + layer, ans = [root], [] + while layer: + next_layer, vals = [], [] + for node in layer: + vals.append(node.val) + if node.left: + next_layer.append(node.left) + if node.right: + next_layer.append(node.right) + layer, ans = next_layer, ans + [vals] + return ans diff --git a/Week_03/id_26/LeetCode_104_26.py b/Week_03/id_26/LeetCode_104_26.py new file mode 100644 index 00000000..b7f88f05 --- /dev/null +++ b/Week_03/id_26/LeetCode_104_26.py @@ -0,0 +1,50 @@ +# +# @lc app=leetcode.cn id=104 lang=python +# +# [104] 二叉树的最大深度 +# +# https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/ +# +# algorithms +# Easy (69.23%) +# Likes: 297 +# Dislikes: 0 +# Total Accepted: 51.2K +# Total Submissions: 73.9K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,找出其最大深度。 +# +# 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 +# +# 说明: 叶子节点是指没有子节点的节点。 +# +# 示例: +# 给定二叉树 [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# 返回它的最大深度 3 。 +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def maxDepth(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) diff --git a/Week_03/id_26/LeetCode_107_26.py b/Week_03/id_26/LeetCode_107_26.py new file mode 100644 index 00000000..50583c6c --- /dev/null +++ b/Week_03/id_26/LeetCode_107_26.py @@ -0,0 +1,63 @@ +# +# @lc app=leetcode.cn id=107 lang=python +# +# [107] 二叉树的层次遍历 II +# +# https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/description/ +# +# algorithms +# Easy (60.77%) +# Likes: 108 +# Dislikes: 0 +# Total Accepted: 17.6K +# Total Submissions: 28.8K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) +# +# 例如: +# 给定二叉树 [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# +# 返回其自底向上的层次遍历为: +# +# [ +# ⁠ [15,7], +# ⁠ [9,20], +# ⁠ [3] +# ] +# +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +import collections + + +class Solution(object): + def levelOrderBottom(self, root): + """ + :type root: TreeNode + :rtype: List[List[int]] + """ + queue, ans = collections.deque([(root, 0)]), [] + while queue: + node, level = queue.popleft() + if node: + if len(ans) < level+1: + ans.insert(0, []) + ans[-level-1] += [node.val] + queue.append((node.left, level+1)) + queue.append((node.right, level+1)) + return ans diff --git a/Week_03/id_26/LeetCode_111_26.py b/Week_03/id_26/LeetCode_111_26.py new file mode 100644 index 00000000..396edc39 --- /dev/null +++ b/Week_03/id_26/LeetCode_111_26.py @@ -0,0 +1,55 @@ +# +# @lc app=leetcode.cn id=111 lang=python +# +# [111] 二叉树的最小深度 +# +# https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/ +# +# algorithms +# Easy (38.70%) +# Likes: 125 +# Dislikes: 0 +# Total Accepted: 19K +# Total Submissions: 49K +# Testcase Example: '[3,9,20,null,null,15,7]' +# +# 给定一个二叉树,找出其最小深度。 +# +# 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 +# +# 说明: 叶子节点是指没有子节点的节点。 +# +# 示例: +# +# 给定二叉树 [3,9,20,null,null,15,7], +# +# ⁠ 3 +# ⁠ / \ +# ⁠ 9 20 +# ⁠ / \ +# ⁠ 15 7 +# +# 返回它的最小深度  2. +# +# +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution(object): + def minDepth(self, root): + """ + :type root: TreeNode + :rtype: int + """ + if not root: + return 0 + left = self.minDepth(root.left) + right = self.minDepth(root.right) + if not left or not right: + return 1 + left + right + return 1 + min(left, right) diff --git a/Week_03/id_26/LeetCode_200_26.py b/Week_03/id_26/LeetCode_200_26.py new file mode 100644 index 00000000..5c612da5 --- /dev/null +++ b/Week_03/id_26/LeetCode_200_26.py @@ -0,0 +1,74 @@ +# +# @lc app=leetcode.cn id=200 lang=python +# +# [200] 岛屿数量 +# +# https://leetcode-cn.com/problems/number-of-islands/description/ +# +# algorithms +# Medium (43.81%) +# Likes: 167 +# Dislikes: 0 +# Total Accepted: 16K +# Total Submissions: 36.5K +# Testcase Example: '[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]' +# +# 给定一个由 '1'(陆地)和 +# '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。 +# +# 示例 1: +# +# 输入: +# 11110 +# 11010 +# 11000 +# 00000 +# +# 输出: 1 +# +# +# 示例 2: +# +# 输入: +# 11000 +# 11000 +# 00100 +# 00011 +# +# 输出: 3 +# +# +# + + +class Solution(object): + def numIslands(self, grid): + """ + :type grid: List[List[str]] + :rtype: int + """ + + dx, dy = [1, -1, 0, 0], [0, 0, 1, -1] + + def sink(i, j): + if i < 0 or i == len(grid) or j < 0 or j == len( + grid[i]) or grid[i][j] == '0': + return 0 + grid[i][j] = '0' + for k in range(4): + sink(i + dx[k], j + dy[k]) + return 1 + + ans = 0 + for i in range(len(grid)): + for j in range(len(grid[i])): + ans += sink(i, j) + return ans + + +# print(Solution().numIslands([["1", "1", "1", "1", "0"], +# ["1", "1", "0", "1", "0"], +# ["1", "1", "0", "0", "0"], +# ["0", "0", "0", "0", "0"]])) + +# print(Solution().numIslands([])) diff --git a/Week_03/id_26/LeetCode_295_26.py b/Week_03/id_26/LeetCode_295_26.py new file mode 100644 index 00000000..03a5f132 --- /dev/null +++ b/Week_03/id_26/LeetCode_295_26.py @@ -0,0 +1,83 @@ +# +# @lc app=leetcode.cn id=295 lang=python +# +# [295] 数据流的中位数 +# +# https://leetcode-cn.com/problems/find-median-from-data-stream/description/ +# +# algorithms +# Hard (35.43%) +# Likes: 46 +# Dislikes: 0 +# Total Accepted: 3.3K +# Total Submissions: 9.2K +# Testcase Example: '["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"]\n[[],[1],[2],[],[3],[]]' +# +# 中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。 +# +# 例如, +# +# [2,3,4] 的中位数是 3 +# +# [2,3] 的中位数是 (2 + 3) / 2 = 2.5 +# +# 设计一个支持以下两种操作的数据结构: +# +# +# void addNum(int num) - 从数据流中添加一个整数到数据结构中。 +# double findMedian() - 返回目前所有元素的中位数。 +# +# +# 示例: +# +# addNum(1) +# addNum(2) +# findMedian() -> 1.5 +# addNum(3) +# findMedian() -> 2 +# +# 进阶: +# +# +# 如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法? +# 如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法? +# +# +# +""" +维护大顶堆、小顶堆,满足以下条件: + +大顶堆中数字的个数 - 小顶堆数字的个数 = 0 or 1 +大顶堆的最大值 <= 小顶堆的最小值 +当长度为偶数时,中位数为大小顶堆的平均数 当长度为奇数时,中位数为大顶堆的最大值 + +注:python的heapq中只有小顶堆,所以存入时取负数则为大顶堆 +""" +import heapq + + +class MedianFinder(object): + def __init__(self): + """ + initialize your data structure here. + """ + self.len = 0 + self.max_heap, self.min_heap = [], [] + + def addNum(self, num): + """ + :type num: int + :rtype: None + """ + self.len += 1 + heapq.heappush(self.min_heap, -heapq.heappushpop(self.max_heap, num)) + if len(self.min_heap) > len(self.max_heap): + heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap)) + + def findMedian(self): + """ + :rtype: float + """ + if self.len & 1 == 0: + return (self.max_heap[0] - self.min_heap[0]) / 2.0 + return self.max_heap[0] diff --git a/Week_03/id_26/LeetCode_329_26.py b/Week_03/id_26/LeetCode_329_26.py new file mode 100644 index 00000000..34927ea5 --- /dev/null +++ b/Week_03/id_26/LeetCode_329_26.py @@ -0,0 +1,98 @@ +# +# @lc app=leetcode.cn id=329 lang=python +# +# [329] 矩阵中的最长递增路径 +# +# https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/description/ +# +# algorithms +# Hard (37.94%) +# Likes: 53 +# Dislikes: 0 +# Total Accepted: 2.8K +# Total Submissions: 7.2K +# Testcase Example: '[[9,9,4],[6,6,8],[2,1,1]]' +# +# 给定一个整数矩阵,找出最长递增路径的长度。 +# +# 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。 +# +# 示例 1: +# +# 输入: nums = +# [ +# ⁠ [9,9,4], +# ⁠ [6,6,8], +# ⁠ [2,1,1] +# ] +# 输出: 4 +# 解释: 最长递增路径为 [1, 2, 6, 9]。 +# +# 示例 2: +# +# 输入: nums = +# [ +# ⁠ [3,4,5], +# ⁠ [3,2,6], +# ⁠ [2,2,1] +# ] +# 输出: 4 +# 解释: 最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。 +# +# +# + + +class Solution(object): + def longestIncreasingPath(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: int + 解法1:时间复杂度O(n^2) + """ + if not matrix or not matrix[0]: + return 0 + m, n = len(matrix), len(matrix[0]) + sm = [] + for i in range(m): + for j in range(n): + sm.append((matrix[i][j], i, j,)) + sm.sort() + + dxy = [0, 1, 0, -1, 0] + dp = [[1] * n for i in range(m)] + for v, i, j in sm: + for k in range(4): + di, dj = i + dxy[k], j + dxy[k+1] + if 0 <= di < m and 0 <= dj < n and matrix[i][j] > matrix[di][dj]: + dp[i][j] = max(dp[i][j], dp[di][dj]+1) + return max(dp[i][j] for j in range(n) for i in range(m)) + + def longestIncreasingPath2(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: int + 解法2:时间复杂度O(n^3) 会超时 + """ + if not matrix: + return 0 + self.ret = 0 + dxy = [0, 1, 0, -1, 0] + + def dfs(i, j, path=[], cache=cache): + if i < 0 or j < 0 or i == m or j == n or (path and matrix[i][j] <= path[-1]): + self.ret = max(self.ret, len(path)) + return + for n in range(4): + dfs(i + dxy[n], j + dxy[n+1], path + [matrix[i][j]], cache) + for i in range(len(matrix)): + for j in range(len(matrix[i])): + dfs(i, j, []) + return self.ret + + +print(Solution().longestIncreasingPath([[1]])) +print(Solution().longestIncreasingPath([])) +print(Solution().longestIncreasingPath([[9, 9, 4], [6, 6, 8], [2, 1, 1]])) +print(Solution().longestIncreasingPath([[3, 4, 5], [3, 2, 6], [2, 2, 1]])) +# print(Solution().longestIncreasingPath([[75,26,75,88,8,6,38,34,20,80,91,96,44,86,65,81,84,73,72,17,50,92,80,31,23,75,85,86,46,65,57,42,58,20,64,1,79,46,75,8,28,74,81,40,4,45,86,90,34,4,73,99,84,11,41,30,85,18,76,28,3,36,45,29,80,24,32,56,84,37,96,57,37,23,33,12,38,56,25,31,71,18,85,33,93,58,44,46,54,5,76,47,17,24,20,38,47,9,58,59,95,86,58,74,70,38,21,68,25,34,21,62,65,91,6,45,8,61,14,20,41,62,65,81,64,97,94,46,6,92,47,25,33,17,49,46,21,56,48,9,78,56,27],[32,45,77,3,58,22,53,66,52,80,71,75,32,81,23,81,20,44,33,77,10,38,82,0,74,25,90,62,97,79,80,98,55,83,46,52,40,37,89,78,52,93,5,78,30,36,3,39,13,54,32,78,45,20,78,0,53,57,73,59,13,40,48,92,15,22,71,85,91,61,86,47,59,38,68,14,90,59,0,0,81,21,93,60,92,49,16,21,96,62,80,8,25,4,84,39,99,96,92,94,66,53,62,56,59,1,7,12,4,64,7,26,47,75,67,93,70,77,45,50,50,5,33,99,97,55,14,27,24,12,21,92,39,25,19,19,29,87,11,34,75,47,70],[50,16,69,59,20,11,95,83,0,20,96,29,30,18,98,24,26,65,85,90,41,69,22,1,61,84,28,4,46,81,7,59,47,71,27,33,34,75,31,36,11,41,0,68,60,58,44,9,37,7,45,78,27,92,23,62,57,32,93,70,31,45,64,74,75,24,53,9,81,38,22,95,86,42,65,90,1,98,37,45,7,18,22,82,13,64,68,64,52,98,39,93,33,83,31,17,67,44,19,22,34,60,87,9,14,14,19,63,77,47,14,9,78,65,77,74,67,97,58,70,18,58,93,24,68,35,79,38,83,95,64,25,90,9,75,36,79,22,70,30,13,71,15],[20,75,8,50,93,93,79,20,24,51,71,70,48,33,5,40,0,6,48,34,90,72,61,41,78,8,74,97,24,55,68,22,78,46,12,97,33,32,34,3,71,38,94,42,53,2,73,67,79,61,0,3,65,88,84,33,35,58,81,42,17,73,96,66,24,82,53,14,4,42,94,46,30,7,90,32,23,21,47,96,68,79,26,53,26,62,70,84,89,29,63,19,93,21,49,92,96,52,46,89,47,85,32,66,85,84,60,62,46,25,23,13,58,66,83,2,80,90,43,9,38,36,69,2,23,1,93,38,16,3,7,74,74,16,98,74,49,51,23,14,53,14,53],[29,5,75,94,7,80,21,63,85,13,84,75,77,92,33,50,51,4,32,93,21,56,35,69,50,28,89,16,61,5,8,89,5,53,5,88,89,62,89,59,3,23,68,48,93,40,11,26,44,38,27,68,57,86,2,53,63,68,45,7,64,74,95,79,75,79,14,17,18,18,48,65,27,90,33,49,15,55,92,11,36,48,12,3,88,96,50,54,44,9,20,63,13,20,68,31,94,78,43,80,83,87,49,58,70,92,83,33,25,12,9,0,0,29,90,15,40,50,21,37,93,49,90,32,61,6,58,53,63,94,87,81,75,70,60,35,68,5,66,61,24,53,97],[96,87,58,22,11,71,20,45,12,51,98,34,83,40,61,44,33,33,48,71,33,84,83,41,34,67,99,61,40,83,68,71,1,57,86,95,92,87,21,89,24,10,8,61,16,25,41,68,64,85,71,3,4,65,10,1,56,89,52,45,35,60,98,70,54,35,65,98,61,57,7,13,38,16,61,87,18,22,22,29,0,50,67,39,93,41,59,66,82,34,35,41,57,42,78,67,83,68,11,23,79,53,28,85,0,36,91,47,49,10,56,76,6,96,89,6,80,10,25,42,70,45,36,58,67,12,50,79,43,77,1,25,41,71,56,45,0,11,97,46,74,60,90],[27,55,4,59,37,30,66,85,56,78,6,16,24,20,0,72,43,32,93,53,17,88,43,13,45,28,3,43,99,9,2,73,52,89,65,27,39,15,67,71,72,56,78,70,47,13,52,85,61,17,32,46,7,92,72,29,34,22,52,2,36,85,10,61,14,5,8,37,53,70,35,55,30,94,44,98,10,13,74,57,91,14,79,17,83,71,96,41,59,80,24,97,47,67,13,2,81,0,60,83,83,96,63,77,34,62,88,81,17,77,12,35,48,71,41,48,36,52,6,21,64,72,10,72,28,38,8,35,57,20,85,19,0,40,11,36,61,6,7,46,70,13,47],[21,0,36,59,86,34,85,65,61,21,80,11,63,81,83,77,69,66,47,52,52,5,47,99,13,22,16,96,53,67,36,75,81,20,5,0,47,83,3,34,72,42,10,41,99,99,57,93,60,12,67,25,54,82,64,39,60,16,22,0,66,61,55,52,28,42,98,48,7,77,69,22,92,19,61,16,59,33,41,11,84,48,46,42,6,70,32,70,76,45,7,10,2,95,1,74,58,64,99,84,23,0,83,11,59,17,96,35,3,57,66,5,1,16,66,21,79,90,78,34,51,48,30,87,4,22,18,85,82,60,88,60,94,26,94,26,53,66,33,22,12,40,9],[26,6,90,85,90,97,14,83,94,83,14,89,78,40,66,99,48,33,44,93,6,97,37,10,79,40,84,75,61,92,18,86,74,0,5,54,48,57,9,15,97,81,53,91,59,98,62,64,13,11,75,66,40,26,65,18,46,82,90,70,21,10,2,12,32,19,21,40,7,26,72,29,96,29,67,92,27,79,3,40,30,92,38,44,18,91,8,71,79,21,76,6,46,75,57,75,57,87,87,1,68,64,79,33,40,86,94,64,65,6,93,44,25,0,99,21,59,80,44,0,45,98,7,90,67,45,10,60,58,87,85,12,62,63,85,84,79,6,80,82,94,67,15],[7,20,74,84,26,87,67,89,66,14,72,97,60,44,99,70,7,93,57,77,21,37,12,31,30,14,87,28,28,65,35,22,6,0,99,7,87,51,6,0,32,93,70,84,27,30,29,89,95,87,95,70,67,58,34,8,78,25,86,86,61,86,21,18,85,93,43,90,17,30,56,17,27,41,67,27,99,44,35,86,28,60,94,52,52,48,41,26,97,64,78,59,25,30,89,22,82,35,90,44,26,0,33,48,31,19,24,86,92,82,54,51,95,60,29,35,47,56,22,31,54,31,17,37,52,32,79,83,52,10,0,5,71,66,22,52,34,12,33,49,78,5,89],[13,68,67,90,63,32,93,35,51,53,93,37,42,62,20,72,56,81,50,4,14,17,59,82,10,95,46,85,20,53,76,57,3,46,69,51,0,39,68,86,63,64,56,27,53,11,79,71,95,69,90,23,57,78,64,77,50,12,78,2,67,1,44,12,71,66,81,89,7,72,88,79,51,84,74,27,50,79,13,68,58,85,28,25,0,8,88,68,39,77,24,58,39,34,92,28,34,23,97,48,63,15,77,76,39,16,65,0,25,7,13,40,65,97,7,61,94,21,28,1,6,15,64,98,11,13,53,40,59,58,39,83,68,44,64,51,50,97,47,32,18,25,51],[66,85,72,46,85,47,43,67,10,74,54,85,42,78,64,58,17,51,38,89,75,6,63,92,83,65,74,53,5,26,42,76,82,14,10,22,95,47,92,25,91,8,61,1,61,27,12,47,30,30,80,61,68,17,36,17,13,35,25,11,92,75,50,3,56,36,41,52,28,94,82,99,62,36,81,59,77,83,5,17,63,79,44,56,46,37,7,36,80,53,94,78,36,83,61,60,1,18,94,66,56,8,44,84,13,42,29,18,0,7,75,71,12,63,29,74,81,19,64,25,64,75,66,83,51,13,56,32,48,61,7,5,94,86,45,53,31,13,38,40,54,60,27],[13,70,78,47,31,92,33,27,90,33,5,40,41,83,29,57,22,21,8,32,49,64,55,71,57,69,22,19,7,1,64,51,8,16,19,39,7,77,21,49,62,64,30,21,79,77,51,17,27,25,65,0,94,56,35,25,96,94,68,5,88,80,37,48,0,26,75,9,90,66,86,32,50,39,7,41,1,85,44,89,74,15,93,72,51,67,2,34,43,22,32,31,31,6,74,3,76,96,50,72,96,83,72,5,48,65,75,39,46,83,70,51,55,95,47,39,34,68,0,44,53,33,66,38,76,48,91,6,40,87,99,68,92,98,36,16,48,3,91,52,40,26,23],[21,83,41,41,42,34,9,13,26,19,36,25,53,19,42,32,71,4,74,59,18,92,61,44,42,96,99,55,39,5,19,14,69,9,70,64,5,22,44,78,88,16,89,46,13,96,91,30,56,81,77,30,14,14,9,42,9,79,92,10,12,90,74,3,69,46,1,67,47,84,98,66,14,88,87,29,1,3,20,71,3,51,93,99,3,48,78,24,31,22,94,77,85,20,23,34,15,45,86,7,28,62,67,52,48,76,63,77,30,48,34,89,22,52,53,27,98,28,0,32,54,47,50,68,43,25,37,51,57,51,94,48,53,1,34,98,30,43,75,36,92,82,50],[87,43,11,41,13,74,47,50,5,75,74,90,30,76,18,82,75,49,99,71,85,58,33,93,72,80,22,42,7,90,71,99,49,24,5,25,39,16,1,86,60,79,30,65,24,36,19,88,16,93,85,51,79,62,30,66,44,90,88,48,29,8,37,12,35,71,4,22,87,79,70,34,67,60,19,68,22,51,24,59,48,11,42,74,82,61,96,50,63,12,81,41,84,59,45,52,6,99,24,95,61,56,50,11,15,56,85,69,51,63,12,93,50,97,17,82,14,20,15,44,50,58,66,54,44,95,63,36,36,75,64,4,78,70,53,48,73,46,94,43,56,95,9],[88,30,70,34,65,58,63,91,54,32,32,90,34,39,27,7,32,7,57,56,50,26,58,23,6,20,49,43,30,53,1,33,62,69,58,34,11,6,13,7,42,41,94,70,30,23,48,69,11,90,28,44,80,49,76,63,38,44,75,18,26,49,29,50,65,21,68,5,42,32,31,13,85,9,32,33,32,32,5,77,63,1,68,89,30,70,23,19,82,57,87,46,20,61,60,54,45,8,86,45,96,58,8,39,43,64,42,92,82,43,35,35,34,56,9,87,68,62,59,45,22,71,72,20,91,93,42,11,16,64,99,21,52,12,37,83,28,26,1,17,40,12,93],[47,13,95,58,17,3,20,53,54,64,91,48,55,88,20,30,35,16,19,83,94,18,52,65,19,35,76,47,47,4,15,60,29,91,51,25,3,62,85,73,19,34,16,10,23,57,79,92,67,78,32,74,98,75,4,65,52,25,89,5,39,52,79,67,32,82,38,13,26,41,21,98,94,97,21,6,21,40,58,93,43,46,27,84,88,6,16,16,22,70,60,75,64,75,65,63,37,96,46,10,13,61,20,48,48,41,44,19,53,29,0,81,11,32,65,69,45,63,66,95,1,42,59,10,43,93,73,63,33,38,94,96,44,84,45,57,9,26,80,79,17,58,54],[47,86,47,68,97,88,94,31,41,9,32,46,52,78,59,66,8,70,72,38,3,4,77,28,18,69,15,58,57,39,9,91,16,10,5,38,32,50,12,82,95,66,98,27,75,8,84,13,38,65,74,50,31,64,89,59,97,23,9,16,65,27,45,73,27,98,92,70,14,62,88,73,69,48,34,39,9,90,20,53,52,58,31,35,84,45,54,36,86,29,68,56,31,4,62,14,65,33,32,24,35,48,70,57,22,2,49,7,56,31,56,64,90,93,72,27,46,94,6,48,92,42,28,0,15,66,13,18,17,49,15,12,91,89,39,46,90,2,94,78,50,44,58],[19,61,30,37,84,81,76,52,82,68,41,8,26,29,86,53,14,0,28,43,30,14,28,78,7,42,58,47,84,29,32,92,45,81,89,86,45,30,18,21,3,14,87,78,1,25,31,10,38,97,11,13,83,68,58,35,97,26,32,62,85,63,7,38,8,9,38,94,90,23,88,79,3,88,93,44,2,39,58,80,82,65,34,33,45,10,42,58,27,57,94,56,89,62,22,22,17,90,40,83,71,97,27,49,81,33,89,51,67,7,98,32,49,67,85,83,54,30,85,4,23,16,49,81,93,68,68,58,87,63,61,43,81,68,86,55,2,99,84,66,61,45,71],[97,0,4,28,82,45,61,65,32,14,94,37,24,11,63,37,37,32,56,31,95,10,25,97,42,14,98,2,59,57,94,62,24,22,36,47,72,86,55,29,22,19,68,6,31,77,62,2,52,13,7,59,74,80,50,57,81,8,55,15,28,18,68,46,15,52,98,10,59,13,81,90,31,52,61,80,28,66,80,19,50,77,61,22,34,65,44,46,76,89,18,71,26,16,84,88,68,34,10,49,8,7,45,98,1,46,10,23,60,6,75,87,23,82,9,90,97,55,64,51,60,29,98,86,5,95,90,7,2,15,21,75,94,22,81,65,87,16,66,54,22,65,22],[78,39,65,97,68,32,74,48,36,23,87,43,63,96,27,79,91,6,68,7,38,31,96,55,90,18,68,70,83,8,0,53,88,43,69,61,96,83,72,86,13,95,68,58,60,45,89,77,25,58,41,51,76,29,84,56,61,76,79,45,60,60,9,88,50,1,43,14,62,69,70,56,3,8,88,91,23,58,55,49,8,8,98,74,30,81,2,41,63,72,88,64,11,75,94,83,64,88,99,79,49,52,9,52,6,48,22,25,97,48,15,20,41,44,70,99,77,10,6,52,35,26,41,99,6,17,2,5,37,93,56,33,27,90,12,48,0,17,31,1,79,10,84],[96,99,20,65,23,19,10,80,19,53,50,55,33,34,45,93,96,65,22,23,60,6,44,47,86,16,2,58,14,99,6,2,75,82,26,33,5,25,51,57,65,66,37,5,54,23,33,99,91,41,26,92,99,48,9,18,8,79,24,97,21,10,26,25,39,47,97,32,78,7,58,99,41,86,50,40,63,21,99,44,38,68,69,2,10,83,87,93,60,83,93,42,57,48,3,14,50,61,78,98,77,40,19,60,29,3,14,48,72,72,60,1,17,62,31,62,26,29,0,56,85,72,75,49,59,37,85,39,14,89,87,89,79,90,22,7,72,95,71,4,52,92,59],[16,72,48,45,12,83,84,85,56,50,57,98,95,58,81,72,58,57,66,66,51,19,64,58,28,57,18,67,61,83,41,86,83,84,81,18,23,80,97,82,75,53,50,88,80,79,7,15,58,73,36,38,28,10,29,60,48,94,75,69,55,99,14,71,13,39,38,31,52,22,92,58,51,13,79,51,84,50,21,19,99,24,80,56,50,51,63,4,25,38,34,14,33,29,17,7,62,22,74,34,27,88,37,75,3,94,66,10,78,26,25,8,93,13,21,3,83,27,48,50,13,61,72,69,36,47,44,78,58,33,33,69,31,31,88,19,31,71,24,1,71,23,61],[18,76,19,56,31,95,2,13,46,42,10,56,2,92,61,65,72,94,36,56,32,29,58,28,67,8,2,3,49,14,10,51,64,29,52,12,68,16,55,79,55,34,23,7,89,16,3,54,28,44,80,11,5,93,42,15,30,84,70,66,97,60,19,68,55,54,99,98,83,60,58,28,38,72,83,60,94,45,15,28,75,18,87,40,54,6,10,95,94,69,75,38,57,65,1,0,56,49,28,69,95,91,49,89,7,4,62,95,34,51,96,83,25,39,98,21,22,77,7,58,97,86,85,78,26,85,68,52,40,9,3,25,40,74,37,54,62,11,80,13,65,97,80],[23,82,91,5,50,89,38,60,22,32,23,96,14,71,35,74,81,42,70,7,91,15,89,64,2,2,99,33,93,49,84,75,56,53,62,97,82,96,80,90,44,15,64,96,27,24,80,16,38,36,0,62,74,90,40,2,95,56,62,93,88,52,24,94,22,8,40,96,97,70,52,89,29,50,49,1,25,89,3,96,14,44,24,55,98,95,42,38,48,56,19,28,97,25,8,48,97,61,18,64,36,63,29,88,99,80,30,79,10,35,33,46,65,92,40,94,24,38,88,13,68,98,62,46,92,88,57,6,64,97,82,87,30,4,36,99,11,78,40,90,16,81,14],[1,42,48,78,15,92,17,89,90,61,82,43,51,58,86,18,0,77,7,38,31,79,47,21,47,61,64,26,98,67,7,89,62,83,38,41,70,44,42,63,74,53,15,58,28,26,21,85,29,78,85,49,94,24,2,52,56,40,82,75,20,50,33,59,81,74,4,45,7,30,52,79,24,49,58,84,43,57,72,11,10,61,21,3,50,93,53,76,53,22,44,21,34,81,16,28,44,16,59,50,34,86,12,60,49,85,31,3,73,30,24,15,78,52,64,29,9,13,38,66,66,78,17,48,73,69,95,91,42,74,95,69,35,51,81,96,57,25,94,29,38,49,47],[15,85,64,74,1,21,40,11,49,33,71,6,90,37,81,83,93,13,50,64,80,55,1,7,88,87,13,66,73,75,59,40,48,57,89,2,69,85,54,87,50,51,2,67,73,47,98,86,70,33,25,25,69,4,21,38,43,47,49,12,67,18,15,7,98,67,39,9,67,0,24,11,57,58,0,55,79,33,18,34,41,99,11,71,71,63,10,11,75,18,81,52,75,0,70,12,68,14,6,96,93,92,82,22,42,46,95,72,98,96,76,78,59,23,3,2,90,12,45,0,97,92,77,94,50,86,71,60,39,83,2,96,34,56,70,8,16,83,72,36,65,17,34],[43,0,92,75,54,30,80,1,12,52,0,4,54,90,10,62,10,30,13,58,12,34,40,51,77,48,47,51,4,24,34,36,72,7,90,6,3,83,85,26,20,10,86,84,39,45,49,0,15,58,69,84,31,38,43,94,82,41,17,8,20,91,13,10,5,37,78,39,20,89,24,21,50,0,83,71,14,62,56,85,85,87,2,10,60,64,99,61,61,44,47,27,59,87,0,61,6,98,88,0,82,73,42,61,21,85,99,42,31,25,69,91,86,8,14,50,12,94,35,72,85,65,29,80,55,54,1,21,11,97,15,83,61,62,81,43,17,46,41,10,50,10,86],[16,18,58,17,42,63,55,99,29,11,63,36,86,62,66,50,85,9,23,56,37,13,64,6,22,71,15,42,61,27,1,17,92,55,81,96,19,80,23,79,23,53,7,19,19,69,66,9,57,55,96,54,79,61,19,18,46,33,31,9,4,21,47,52,42,95,13,71,97,56,46,31,83,10,49,27,21,31,38,44,33,82,3,14,15,26,91,51,20,50,79,83,95,89,22,50,70,97,25,1,48,52,60,55,11,42,20,24,82,6,14,53,46,61,85,75,78,88,72,31,91,23,99,35,29,47,64,94,97,89,25,86,48,34,18,19,19,29,33,89,16,64,7],[51,88,76,29,42,19,63,39,88,72,86,46,98,56,10,92,38,98,72,79,87,41,38,6,43,54,12,32,19,44,98,47,61,54,83,60,38,84,3,39,96,86,62,69,99,7,60,74,33,77,13,81,64,91,70,34,39,12,22,12,29,2,16,32,73,60,50,70,48,72,99,75,22,38,19,12,87,49,71,99,71,79,24,8,84,54,68,1,23,90,88,14,59,82,69,13,94,34,58,59,52,35,36,33,81,86,33,62,86,60,92,73,63,68,14,7,76,25,85,95,24,47,36,90,3,96,23,82,72,30,26,78,85,80,74,93,75,57,45,20,15,10,75],[98,11,18,5,71,75,70,9,60,4,27,68,15,50,40,52,19,22,53,27,4,43,84,33,65,23,28,50,50,74,36,47,56,82,16,7,56,22,67,99,5,12,59,32,46,39,74,49,44,62,37,63,28,75,72,81,14,72,9,57,67,52,11,94,21,20,19,40,25,12,41,29,72,4,66,58,31,59,45,98,90,47,61,56,99,1,62,41,9,16,20,28,44,45,97,78,52,9,29,2,51,42,64,81,51,13,93,16,41,9,35,86,55,25,58,98,49,24,67,26,17,82,36,25,12,92,75,80,33,33,74,67,69,47,85,48,5,13,95,61,73,63,42],[70,61,44,91,83,74,25,79,28,10,70,29,92,33,41,16,67,73,76,40,98,61,52,82,6,90,41,61,49,10,29,56,22,71,20,60,89,77,51,34,94,12,76,16,99,53,10,10,25,70,18,42,52,81,9,95,62,34,78,74,25,22,40,91,50,57,1,14,31,95,82,24,6,99,60,8,20,88,44,84,36,66,35,69,99,9,28,38,94,57,54,74,44,55,11,28,22,35,15,5,81,27,84,93,86,84,45,70,18,51,6,2,9,60,31,63,89,15,44,74,20,16,54,2,22,61,39,19,77,62,94,3,60,22,46,80,71,73,69,68,21,82,69],[87,4,40,74,12,78,20,67,75,40,26,15,89,44,45,80,84,68,76,86,90,79,12,71,91,53,52,47,5,46,41,48,61,20,46,32,18,88,39,45,8,32,50,75,39,16,7,97,43,91,15,99,18,14,24,24,52,47,68,67,11,16,11,16,94,19,94,41,36,57,18,92,7,82,90,96,53,52,43,6,12,10,34,74,58,82,87,44,81,10,37,81,42,43,37,80,47,99,85,29,3,52,50,13,82,20,25,57,43,52,68,29,6,61,21,97,46,49,37,65,12,55,71,66,34,27,46,78,0,89,65,36,89,66,35,94,66,48,8,21,79,90,91],[99,7,47,0,93,79,34,57,7,30,51,98,8,0,33,51,42,45,68,32,19,18,93,20,35,27,90,53,50,59,72,47,26,69,77,7,98,56,98,52,15,87,81,79,84,96,40,28,77,42,94,85,16,16,87,38,19,45,92,98,6,19,93,93,62,58,31,63,25,26,34,82,68,11,28,64,22,80,9,85,58,59,45,76,96,74,88,8,99,52,72,41,79,90,18,81,22,68,21,15,40,20,35,70,51,50,67,27,70,87,70,93,50,68,86,1,12,1,34,63,18,48,39,8,4,52,46,39,11,28,90,70,4,86,13,57,24,96,99,7,28,82,1],[25,81,64,17,93,30,11,55,71,41,89,9,98,65,62,17,74,8,83,87,26,70,37,54,51,18,69,12,72,97,67,45,44,2,21,81,10,47,4,82,24,19,11,82,0,33,47,55,2,50,63,0,3,28,64,16,63,37,68,25,95,39,74,79,3,46,78,68,14,44,63,99,97,21,25,69,44,78,64,25,89,77,77,9,91,75,83,14,34,71,23,79,42,7,25,93,98,68,46,32,4,43,20,63,11,98,85,96,28,51,72,57,49,71,48,85,59,76,55,4,4,13,84,7,35,34,59,41,20,44,34,78,92,66,60,9,53,59,29,21,22,0,39],[74,31,71,82,22,72,97,64,43,84,1,37,74,8,82,81,67,82,95,42,42,26,63,10,77,72,84,22,68,0,53,78,73,10,59,98,62,57,36,89,14,31,59,76,43,59,25,93,37,28,65,79,75,54,32,22,76,80,88,25,59,49,50,0,88,51,43,67,10,90,24,30,57,21,54,58,85,95,17,67,37,13,81,43,22,97,76,73,61,4,95,96,44,49,67,34,30,98,4,23,69,91,56,23,80,17,67,31,39,41,99,73,82,34,28,26,60,45,66,27,12,25,2,72,9,0,56,3,67,54,82,0,37,12,34,13,84,92,69,23,70,83,60],[66,1,67,26,94,70,36,44,46,17,95,77,57,19,47,77,36,95,76,36,55,80,57,93,8,32,49,61,62,6,3,7,67,4,56,49,70,83,89,12,36,1,57,39,18,81,21,30,75,27,36,94,33,35,99,16,59,68,38,31,68,38,99,11,10,53,67,96,58,53,20,0,69,71,88,5,21,10,57,3,71,82,93,18,92,14,96,85,6,25,28,5,17,36,5,66,87,35,43,69,69,50,78,79,92,95,76,62,69,45,1,48,69,1,4,61,45,41,86,95,94,13,26,34,52,54,36,94,45,63,54,0,25,24,37,17,89,65,4,2,5,83,32],[41,95,50,21,1,40,23,28,82,30,15,38,16,31,25,88,93,44,40,21,66,84,63,95,98,5,74,1,7,51,60,15,54,69,55,99,46,60,13,2,57,86,8,97,3,56,44,18,22,57,22,36,35,31,13,98,57,67,41,39,26,45,43,36,72,78,28,71,91,87,67,25,42,99,82,18,0,52,69,76,77,95,21,30,74,38,12,32,48,84,86,57,98,4,78,57,69,66,67,93,21,52,65,67,68,76,0,27,39,80,30,52,97,58,98,24,83,98,25,36,62,99,18,96,88,5,39,60,22,2,2,8,32,18,98,34,35,90,6,9,3,26,72],[1,76,67,24,42,31,77,84,42,91,52,72,40,59,90,95,55,56,41,68,25,46,91,79,97,71,24,48,91,17,59,51,67,1,85,11,75,30,31,65,39,87,96,40,64,41,12,70,77,49,31,65,27,42,69,93,3,60,66,79,47,4,35,63,82,32,45,24,24,51,54,27,77,18,42,26,17,58,87,75,2,74,13,2,28,30,25,25,19,18,57,14,10,96,42,83,78,25,74,1,49,87,29,43,58,60,43,79,83,7,94,75,43,28,47,74,65,82,59,58,98,81,69,58,39,51,19,28,15,84,49,4,37,5,20,93,65,5,50,58,33,8,31],[90,26,61,51,29,49,83,52,58,53,53,48,45,31,50,32,82,70,25,69,93,65,38,58,89,90,89,31,16,28,44,53,20,49,15,19,68,90,13,77,3,41,14,4,93,34,80,84,1,90,52,10,66,72,73,76,71,21,88,43,71,80,11,91,9,99,4,39,65,37,99,77,99,1,20,85,37,51,2,46,77,74,85,21,77,39,35,87,30,98,99,13,9,24,2,55,40,68,54,34,6,34,73,35,18,4,91,66,58,63,63,51,21,96,32,29,73,18,51,97,59,54,52,42,1,17,1,31,59,88,51,72,19,48,28,59,62,8,72,71,43,26,77],[64,82,24,45,91,7,77,70,35,26,11,12,53,59,34,63,92,24,35,17,94,22,16,84,76,9,85,77,53,56,43,68,91,4,16,60,10,10,83,67,1,67,55,67,62,68,80,65,10,47,33,14,99,93,53,48,88,60,77,35,23,21,63,69,25,30,70,85,50,6,69,47,83,21,33,65,2,99,11,22,53,70,55,36,4,23,92,49,13,11,5,94,14,27,46,79,38,14,8,24,8,79,74,26,1,18,21,7,8,40,27,42,13,47,32,6,24,5,21,6,61,66,94,19,53,1,64,55,54,50,48,89,23,30,67,23,51,60,63,63,77,24,11],[43,94,47,64,16,7,66,11,68,50,48,31,24,32,75,67,25,13,19,25,53,27,85,56,38,52,67,24,14,52,14,52,42,40,94,79,97,46,14,48,98,37,30,76,43,28,13,34,0,75,69,79,38,9,55,60,12,26,9,78,9,13,78,53,34,43,32,9,47,96,45,37,21,50,20,33,62,4,0,75,73,7,39,70,53,57,29,49,45,75,44,59,94,93,96,79,65,54,36,90,5,78,60,69,16,81,18,74,66,32,91,51,43,67,31,3,63,94,25,12,48,65,6,30,55,20,89,38,15,47,32,47,39,23,33,74,68,90,84,84,84,49,98],[10,90,21,19,44,34,32,94,12,86,82,89,83,50,60,68,50,25,40,22,6,17,55,67,64,33,5,9,93,50,63,14,3,19,50,60,3,55,59,48,19,14,55,45,50,89,43,85,6,14,54,14,33,55,79,3,21,90,41,66,51,26,57,26,91,10,91,50,75,59,96,68,73,21,16,12,95,2,13,37,18,15,86,18,43,40,90,63,35,19,20,59,69,11,95,30,86,82,74,8,32,52,7,25,80,34,41,56,71,15,69,14,40,76,74,88,0,78,41,34,71,17,52,32,11,84,25,22,63,16,44,75,26,36,99,23,78,94,62,62,96,55,99],[22,38,56,15,69,81,20,43,8,96,76,67,16,61,99,72,39,55,43,17,47,42,9,0,74,64,64,99,22,12,21,44,77,53,19,93,65,28,66,64,57,83,63,11,4,20,18,36,40,65,36,85,92,26,65,15,39,53,29,8,68,50,31,91,89,80,23,40,48,75,5,61,30,76,58,41,16,54,79,88,29,3,75,4,86,88,22,59,33,18,28,1,67,74,76,57,20,8,69,11,25,45,47,61,2,70,2,45,64,53,13,64,59,99,99,57,64,43,98,80,16,71,54,29,64,57,19,16,58,29,31,16,78,82,85,29,86,26,83,43,28,88,52],[1,89,41,64,86,43,95,76,83,4,7,70,10,63,97,82,95,45,42,5,82,85,66,39,76,52,36,92,63,3,51,75,26,27,86,3,81,11,72,99,31,38,47,37,78,24,48,88,68,59,40,82,84,23,37,97,19,18,7,86,77,54,22,84,95,65,46,51,28,84,26,25,17,89,42,67,14,42,76,89,31,35,43,3,19,99,19,27,69,24,32,84,29,37,29,10,73,4,97,97,41,93,54,89,20,86,78,83,38,7,83,49,12,65,54,23,88,75,5,26,70,44,58,99,34,20,85,13,15,59,18,13,17,20,26,39,45,55,97,65,62,69,4],[68,20,78,66,15,13,57,25,20,79,92,87,98,74,38,8,93,50,30,93,34,12,75,88,30,26,90,55,39,38,53,19,98,75,33,87,74,83,74,93,30,75,39,25,38,63,97,84,62,57,46,4,61,71,23,19,92,34,91,61,2,50,82,9,11,15,70,28,85,66,97,32,77,34,93,80,31,6,83,96,76,80,93,29,13,15,43,63,96,38,72,91,12,90,78,36,11,19,53,58,60,54,20,71,35,21,91,97,48,24,57,12,63,20,54,81,54,67,97,21,94,9,3,89,85,98,95,62,82,20,75,48,50,35,93,24,57,87,40,36,45,87,38],[32,27,41,74,11,66,62,79,55,30,52,7,98,68,25,22,70,0,4,11,60,51,32,13,48,24,57,24,49,67,92,28,37,40,37,67,96,86,88,66,15,96,17,96,15,78,13,11,8,25,95,33,20,92,83,28,28,22,50,80,91,46,19,1,5,18,62,76,88,21,88,61,75,32,31,59,40,6,48,29,72,89,7,70,20,41,30,94,31,1,7,66,79,53,76,22,86,69,36,57,90,52,72,26,89,50,23,95,36,49,28,46,34,48,79,13,21,49,3,70,5,99,99,96,48,60,55,10,27,89,54,66,76,14,90,19,0,62,16,4,44,86,55],[28,71,50,84,54,31,52,4,57,82,14,18,46,0,12,55,71,12,83,0,80,3,25,75,5,88,44,55,46,3,50,98,24,29,71,72,37,26,3,98,11,82,68,41,42,76,33,50,99,65,9,7,66,26,85,56,96,50,84,63,42,22,41,17,70,50,40,71,7,49,78,2,28,82,91,95,46,11,62,70,71,98,40,10,32,67,95,7,40,24,26,20,71,21,30,16,17,29,11,40,59,17,60,22,72,60,46,49,9,91,76,45,39,88,60,50,23,74,54,93,92,72,46,35,58,3,57,63,94,82,73,87,1,37,86,99,82,26,44,70,20,77,30],[37,43,94,34,22,62,49,15,58,58,57,29,31,81,70,27,30,25,49,15,0,70,39,19,40,15,75,95,42,42,68,41,7,22,39,32,34,73,34,50,85,92,15,65,65,90,39,80,96,46,91,83,89,73,4,64,27,7,15,70,14,67,53,36,59,9,57,68,43,53,9,26,20,30,22,7,40,16,33,79,83,33,6,30,75,86,87,24,95,74,30,45,97,79,12,96,73,72,95,28,56,32,76,52,42,82,82,7,68,80,6,16,49,5,56,13,12,45,14,56,35,99,12,80,88,79,53,20,65,27,14,33,21,68,80,15,34,90,36,42,46,40,87],[80,60,15,72,42,43,99,22,48,85,47,34,62,62,53,43,73,27,54,64,84,71,49,28,3,78,67,22,18,77,58,31,55,74,39,90,11,45,51,90,46,35,98,27,37,75,36,46,54,55,88,63,31,58,5,90,22,84,45,66,97,2,38,59,12,48,29,82,86,61,29,95,41,15,88,11,52,84,89,32,20,9,62,78,0,41,58,7,29,33,48,23,21,84,74,31,52,74,0,36,37,47,81,73,95,47,5,34,99,96,78,47,99,0,72,68,9,20,59,63,22,26,75,45,7,43,44,38,82,12,30,97,6,48,48,87,20,44,40,56,58,56,86],[15,22,18,98,52,80,71,13,77,13,97,16,8,16,0,55,1,83,70,53,15,39,4,69,30,61,66,94,33,37,87,89,94,65,77,35,99,60,71,58,30,14,95,25,61,62,22,20,12,78,28,50,92,87,37,61,54,6,61,43,17,80,13,99,14,78,10,48,18,96,32,64,2,99,79,10,22,67,26,53,90,88,6,4,83,33,50,30,95,74,29,63,49,51,27,21,11,78,85,77,78,34,21,3,48,63,19,17,13,54,19,96,0,80,80,33,60,39,72,54,36,78,13,97,48,2,52,42,63,47,7,70,1,49,30,65,89,44,22,88,45,14,77],[48,58,55,85,99,19,4,73,8,1,9,67,3,66,61,89,67,8,45,50,7,5,17,64,72,84,70,56,85,70,45,89,38,63,89,74,87,1,60,88,49,44,36,98,35,39,45,61,34,58,51,26,3,69,57,90,75,14,42,0,49,0,37,87,53,20,49,12,73,12,14,54,48,3,94,81,94,95,4,96,61,15,94,33,83,85,19,68,43,11,51,63,99,43,96,53,53,73,19,65,0,58,44,94,81,30,40,70,23,2,51,59,84,13,97,78,35,98,42,59,42,91,46,46,34,52,49,57,42,41,56,94,85,6,73,85,68,23,21,39,36,89,94],[6,69,72,29,97,11,22,66,42,66,73,67,31,21,33,52,27,29,77,96,31,13,96,13,62,22,56,38,61,7,43,22,3,22,78,53,97,74,95,95,70,38,66,77,31,81,80,81,82,93,1,94,8,33,68,24,97,45,49,10,8,17,40,97,76,22,24,78,88,10,72,73,34,67,93,81,14,66,68,99,22,82,64,22,81,8,47,2,39,36,36,86,12,73,48,88,49,97,25,39,28,49,32,32,58,48,67,70,16,40,57,44,72,81,11,38,38,13,19,87,3,80,26,1,33,89,7,68,29,24,39,41,82,64,58,12,75,37,20,17,41,30,23],[19,67,52,93,11,67,19,14,75,78,31,74,92,63,87,41,51,84,82,35,96,34,10,47,89,57,90,23,97,57,5,95,4,11,45,99,44,3,15,19,26,43,48,54,51,53,92,95,95,7,63,37,43,48,93,8,3,70,16,69,42,27,69,51,27,73,84,77,18,44,33,2,24,2,31,47,77,13,80,51,90,69,52,20,86,52,95,2,6,32,94,38,50,29,66,49,8,53,67,71,4,76,56,19,65,56,81,57,90,94,3,63,65,35,47,65,70,62,65,91,62,3,12,30,99,53,11,4,51,29,0,8,95,77,17,86,9,64,64,55,79,87,92],[38,29,40,66,23,35,30,11,9,43,66,22,80,85,13,98,40,80,92,98,49,50,41,49,76,21,99,59,74,52,9,48,20,85,24,41,63,48,36,32,23,14,93,1,40,57,11,5,53,43,95,2,89,11,69,21,75,99,8,3,77,92,13,3,33,16,68,70,69,70,99,73,70,37,61,36,40,78,80,41,87,84,96,75,14,30,77,8,81,56,95,58,38,52,5,25,10,16,99,63,73,44,25,86,84,69,53,76,70,81,36,1,35,83,22,69,57,72,81,65,53,60,21,71,10,51,22,98,93,67,5,47,98,27,96,15,62,90,86,91,90,98,23],[35,44,41,10,16,29,32,49,99,95,36,33,22,96,82,52,81,84,79,27,19,13,82,73,68,1,9,47,15,14,47,57,45,37,64,31,85,32,67,61,3,7,94,92,19,90,92,55,63,22,19,61,94,73,70,5,44,20,12,72,23,16,83,6,0,60,61,46,11,43,93,12,65,54,95,29,83,65,86,34,20,7,7,26,12,11,58,94,29,83,88,78,68,56,96,42,64,33,72,86,14,53,13,16,60,62,0,89,5,48,63,71,88,35,46,6,9,59,67,9,85,38,82,28,33,52,91,8,0,58,90,63,54,51,95,83,6,33,79,54,50,21,16],[41,94,15,22,53,94,20,36,38,80,89,44,75,46,38,67,58,90,76,53,10,0,77,81,71,9,26,18,71,44,73,56,43,31,65,42,0,85,43,38,49,8,21,4,62,75,90,52,26,16,96,66,50,39,14,48,58,70,21,43,29,13,35,72,83,47,35,74,59,65,20,96,57,35,32,24,57,68,89,72,10,27,25,10,92,71,15,10,77,29,90,36,14,61,89,47,91,94,56,58,61,75,91,11,8,98,76,50,13,30,91,44,20,62,31,43,63,29,96,63,20,21,9,51,70,62,36,10,64,80,20,86,38,45,91,91,1,73,68,76,89,92,32],[23,77,95,58,39,11,8,69,10,31,19,63,54,48,62,35,53,38,68,83,14,54,58,88,83,43,56,28,4,10,27,59,66,97,96,62,68,89,28,61,4,34,47,50,3,60,60,27,35,73,7,49,62,11,46,16,19,35,54,80,41,27,42,81,8,12,22,63,16,29,21,96,4,17,69,40,12,39,72,16,93,85,33,31,14,58,25,33,56,90,89,41,10,76,25,90,90,38,13,58,33,65,24,36,47,12,1,12,76,16,27,61,38,30,62,83,22,97,12,15,26,82,15,41,21,94,36,21,7,15,16,45,97,83,60,11,49,50,54,10,31,56,76],[23,70,88,17,31,18,67,78,96,84,28,96,98,45,8,15,67,17,51,73,82,30,51,33,29,1,49,41,14,60,94,51,42,70,50,72,42,70,77,32,78,31,8,58,83,2,51,11,87,96,49,30,28,38,60,99,28,59,43,82,53,61,46,33,97,66,2,41,68,46,30,92,77,66,2,79,16,51,54,31,26,89,71,68,40,68,34,11,73,84,66,45,9,25,73,52,21,5,18,88,29,15,38,96,71,10,86,25,61,94,53,36,64,76,91,51,88,63,66,76,91,51,72,10,20,56,58,86,74,88,88,87,98,75,34,36,14,26,19,5,38,82,73],[76,41,36,42,82,85,61,35,43,16,82,54,16,89,90,37,3,75,30,74,32,93,59,9,82,34,7,63,50,68,31,37,77,96,5,37,6,36,13,9,10,41,42,12,16,64,47,47,9,73,92,81,69,72,7,64,0,88,92,41,83,21,1,90,3,2,4,46,39,40,71,69,59,82,49,54,82,49,54,88,18,32,86,69,64,26,36,86,37,36,74,0,15,23,76,59,12,98,13,50,17,73,39,62,54,18,63,0,98,29,81,78,93,27,6,22,41,79,4,20,5,82,28,1,11,17,8,73,66,25,27,45,20,71,68,37,57,94,79,78,27,62,3],[44,67,63,91,68,34,39,92,85,41,33,71,53,39,95,9,58,68,7,51,63,83,46,64,65,94,58,94,72,58,62,80,47,6,88,31,99,73,98,25,85,4,94,7,90,26,69,67,17,38,71,32,3,91,94,94,19,0,69,43,1,59,24,8,89,76,37,42,35,91,97,89,49,14,93,83,93,34,80,2,11,49,59,23,95,8,36,74,6,27,60,47,59,34,92,48,18,17,75,77,55,44,18,78,80,60,69,11,30,29,78,50,43,76,16,35,46,58,67,56,18,96,98,81,76,75,46,58,40,15,97,32,9,64,67,54,26,4,39,80,73,88,49],[27,26,11,13,25,75,57,27,44,60,38,68,66,58,13,98,59,14,28,69,54,7,94,63,47,60,24,58,26,29,51,20,41,14,59,40,24,99,64,20,62,3,41,78,65,83,37,77,79,21,75,84,55,18,19,26,67,94,20,43,68,8,93,47,69,74,63,65,93,43,16,22,42,35,41,64,55,84,79,65,90,79,65,29,25,27,43,81,12,82,38,54,78,61,7,90,17,55,91,38,75,89,9,81,32,69,63,13,15,29,35,28,49,48,84,78,65,80,42,34,37,71,40,3,34,23,50,65,90,4,55,37,23,30,19,50,49,41,94,66,86,38,56],[41,30,99,94,16,17,86,60,99,49,86,93,81,41,68,95,93,8,90,60,78,69,94,77,71,23,90,35,62,88,71,72,60,46,44,15,63,49,29,16,18,2,13,74,1,54,6,69,65,84,56,64,63,84,61,80,38,27,60,60,50,60,67,29,49,85,84,85,22,68,57,11,45,31,28,6,58,50,14,14,8,12,42,62,82,68,75,40,62,1,79,7,42,70,84,3,37,72,94,58,82,59,6,13,71,95,97,93,57,68,88,4,76,13,41,15,11,70,40,49,7,13,36,91,81,91,65,60,82,25,10,53,30,93,87,61,65,88,95,39,66,89,88],[89,68,36,48,85,16,66,51,7,94,4,55,85,21,39,21,6,38,86,73,71,83,19,62,65,57,65,47,77,72,76,66,81,49,23,80,75,95,43,74,21,13,76,30,27,95,33,54,95,96,57,38,94,21,61,29,35,24,54,15,17,76,79,10,22,33,76,20,9,36,45,3,36,58,78,20,91,58,78,50,84,40,60,42,16,99,18,10,28,39,7,87,6,90,22,0,98,22,25,72,8,61,46,7,8,67,67,78,45,64,1,10,92,6,33,76,48,98,94,31,0,53,96,85,77,5,82,7,38,3,36,53,27,68,99,79,81,90,13,58,67,77,18],[86,14,2,74,89,95,62,54,9,15,58,69,48,88,34,71,73,90,3,75,98,90,37,87,49,34,58,58,64,87,7,15,82,57,73,73,15,76,75,58,44,52,18,66,17,38,13,28,12,6,16,84,0,1,69,3,66,68,76,76,47,56,72,34,50,74,17,0,5,7,68,1,96,92,85,11,46,27,82,69,62,41,62,26,58,97,88,14,86,75,34,25,19,83,65,47,22,49,80,34,24,28,73,47,93,23,96,32,87,43,76,83,5,65,83,56,56,61,78,69,77,15,22,91,16,38,13,39,90,74,77,30,55,96,56,21,95,45,81,73,63,27,27],[42,18,32,81,44,15,41,86,31,43,80,18,4,60,47,57,45,37,40,72,2,63,58,39,43,58,93,70,98,50,50,13,53,29,71,18,65,66,50,13,54,47,93,36,19,36,81,73,26,53,4,49,77,70,6,7,60,61,44,10,67,37,33,36,37,14,81,76,30,94,23,2,46,15,83,51,51,10,41,68,97,4,46,77,7,47,64,59,25,62,66,25,96,79,46,48,33,73,47,34,93,66,88,39,68,37,58,19,6,13,54,59,35,36,82,70,27,99,89,92,37,75,31,36,77,40,3,78,89,98,10,80,57,10,9,41,60,51,62,85,23,22,88],[25,88,87,59,10,15,85,72,10,9,86,19,25,46,41,20,94,27,5,52,15,82,39,91,95,69,15,94,79,85,44,99,60,63,82,83,40,22,22,37,96,8,21,9,61,60,81,35,12,93,17,57,29,20,36,5,7,47,66,12,11,58,63,0,26,84,77,60,24,32,21,61,95,71,25,90,29,17,65,38,43,79,20,59,24,39,66,15,34,99,80,22,91,61,84,30,15,8,52,19,42,37,74,19,98,41,34,62,44,82,60,60,88,38,0,28,66,55,56,45,27,9,50,29,57,90,27,81,24,63,39,11,91,29,50,63,63,27,88,61,90,64,51],[9,83,65,15,49,9,0,7,92,23,0,69,64,81,69,34,66,73,52,54,82,17,76,48,34,64,96,71,71,7,93,44,94,63,13,51,30,20,54,65,59,78,37,96,73,13,1,27,79,90,80,69,62,18,46,81,74,33,73,71,67,60,58,46,26,1,97,5,59,80,25,33,59,64,19,58,30,74,48,26,10,90,38,31,0,57,35,84,21,96,30,24,94,39,84,99,5,16,14,57,39,71,9,88,78,92,39,95,8,41,57,46,3,26,15,74,56,14,67,73,15,78,86,77,56,94,61,95,48,90,0,15,11,39,26,33,14,14,56,38,99,0,72],[39,64,26,18,72,69,97,74,33,94,14,45,72,20,28,43,94,41,8,66,59,71,2,8,61,18,52,75,33,54,73,89,54,11,97,72,2,13,74,88,83,24,78,44,27,76,92,72,81,34,17,25,37,27,38,87,14,67,84,95,94,42,55,44,66,75,40,16,77,66,90,49,96,80,71,27,68,30,81,30,51,19,59,68,84,74,50,3,75,80,79,61,10,43,11,50,68,49,62,95,47,13,79,10,75,47,77,53,49,95,84,20,9,38,55,59,34,34,79,21,85,77,32,76,14,81,95,57,95,28,88,41,81,9,79,13,52,11,11,67,3,51,38],[13,34,33,28,31,94,61,79,35,27,90,52,33,20,4,80,26,67,56,94,45,51,48,45,47,36,54,62,44,62,85,32,99,67,71,28,76,75,56,51,10,38,76,87,48,33,67,15,13,45,61,88,75,53,12,89,69,70,55,13,17,94,15,13,69,7,65,32,5,34,38,0,59,93,30,64,68,89,88,19,18,64,92,37,36,34,42,99,89,63,47,1,17,39,3,97,78,26,22,67,99,67,41,5,92,0,80,68,13,63,77,24,5,93,96,73,71,48,98,72,96,66,25,66,93,1,34,36,88,61,22,39,85,63,15,70,10,52,14,75,24,25,26],[54,75,43,9,86,50,78,20,14,27,12,5,36,12,75,95,70,18,99,93,37,36,64,37,59,26,96,81,32,69,78,73,45,14,74,71,14,58,75,39,79,84,80,35,46,11,95,72,15,52,53,12,12,12,31,99,48,62,37,32,36,99,41,88,6,95,11,55,26,81,25,44,2,49,98,72,1,45,63,75,12,94,94,45,57,20,23,32,98,0,14,51,70,7,8,68,11,11,21,88,69,15,74,95,83,32,70,91,64,3,57,13,11,23,94,15,28,64,26,81,11,94,35,47,24,84,73,9,50,96,17,70,48,83,35,80,70,87,22,68,44,41,47],[82,98,49,2,15,4,69,39,7,73,22,53,95,28,46,49,51,2,40,58,38,91,50,60,30,71,0,71,8,65,34,62,93,68,35,17,39,54,26,77,67,83,34,3,23,82,39,81,78,99,89,64,71,49,51,17,91,43,89,55,65,66,82,66,32,1,41,40,74,22,96,74,60,5,8,66,79,31,56,35,45,49,88,12,61,15,55,74,25,40,40,37,41,52,98,63,97,10,59,94,48,35,7,84,81,23,15,5,0,91,88,92,29,25,62,13,60,77,21,7,29,48,95,82,60,67,46,33,73,57,61,96,29,30,54,17,29,62,90,72,17,26,53],[67,42,61,86,24,45,14,2,12,54,70,77,93,68,62,97,88,24,56,27,72,6,43,26,91,53,44,23,96,90,23,67,74,63,27,91,72,95,59,37,68,66,58,73,11,75,58,59,60,41,5,65,53,6,17,10,91,33,43,37,32,64,23,55,24,42,2,67,33,70,40,79,85,51,44,53,11,53,67,78,57,97,34,20,24,34,89,70,85,84,10,87,41,78,89,84,13,21,34,96,64,27,64,84,53,46,53,32,89,24,40,7,54,0,98,62,63,68,79,16,37,32,33,79,86,40,83,7,56,73,35,36,28,28,32,63,78,42,87,90,18,42,18],[72,5,10,78,52,58,18,58,45,2,87,36,60,63,38,34,4,94,65,29,26,2,75,68,64,78,53,80,27,97,18,89,29,76,67,68,26,92,84,31,68,98,42,7,10,45,89,73,10,12,35,29,2,45,44,95,64,98,81,23,62,2,8,24,18,68,14,6,56,37,92,38,58,56,20,67,61,43,72,38,73,16,37,97,35,74,13,82,67,6,31,4,93,86,9,52,85,53,80,98,7,70,16,22,42,67,7,10,89,62,23,66,10,79,8,23,71,40,8,33,96,42,79,35,79,53,84,25,21,88,29,50,37,62,83,20,2,53,69,90,6,53,95],[44,33,49,8,10,1,3,9,39,71,98,81,37,88,38,33,14,88,63,53,65,11,13,13,72,34,12,55,34,12,5,27,45,69,22,69,70,65,24,38,37,86,22,96,96,80,93,12,92,98,62,24,67,62,28,3,77,3,57,97,46,63,56,91,89,75,70,70,3,23,98,25,10,74,69,50,2,91,86,18,67,44,75,9,53,48,95,76,12,64,89,39,30,87,81,3,68,70,97,76,70,27,53,33,20,13,61,1,2,49,74,62,26,21,41,37,73,19,28,80,68,82,86,56,67,56,89,84,5,71,94,55,45,61,38,6,12,87,21,5,12,14,5],[8,2,54,16,68,1,29,89,24,46,1,93,9,55,34,7,61,88,5,19,55,78,9,37,53,57,59,56,40,28,31,58,5,35,27,87,29,48,64,22,26,94,32,29,2,8,57,80,14,71,61,34,51,72,15,56,41,33,82,59,8,34,46,41,88,8,87,23,98,98,90,34,81,72,83,48,47,5,93,50,99,52,74,53,65,75,27,5,58,57,56,75,22,95,20,30,57,50,91,1,45,83,1,29,78,39,3,93,95,14,46,44,85,36,53,14,3,43,37,44,64,36,11,70,2,57,77,42,88,80,50,43,35,63,15,80,78,4,72,31,96,51,33],[63,99,98,33,2,58,5,5,52,0,72,14,5,60,38,5,97,26,35,16,14,32,67,22,74,10,90,5,32,36,28,44,98,9,55,83,97,89,3,83,57,21,6,31,0,60,29,57,10,38,47,35,6,59,9,37,84,10,78,51,46,28,82,44,22,44,27,23,23,62,3,55,60,52,93,58,35,33,58,31,29,28,84,20,27,74,7,47,45,92,56,9,15,79,27,46,10,95,41,0,76,63,4,49,4,23,11,88,5,4,93,64,43,89,9,18,49,35,96,30,97,2,4,40,57,99,69,24,66,34,18,94,96,29,22,6,22,16,93,65,64,1,59],[65,39,72,39,62,40,39,88,45,13,15,17,35,93,54,11,15,47,50,10,46,27,52,93,8,17,38,46,84,71,79,66,84,81,97,81,87,72,57,15,44,12,3,36,87,43,89,41,40,24,81,32,52,60,87,85,96,9,70,91,33,65,40,76,91,94,84,20,94,52,35,25,90,23,47,12,27,79,12,8,61,10,97,61,7,73,12,70,11,71,30,8,78,63,42,76,92,13,18,16,6,30,79,35,65,85,29,58,0,17,86,70,40,68,6,57,72,4,9,25,39,94,68,72,92,66,34,90,45,23,26,25,69,28,34,88,24,38,68,62,14,49,5],[93,99,86,18,11,34,15,63,89,45,31,72,64,69,96,11,86,64,95,12,18,15,63,32,99,23,15,73,71,64,46,95,67,44,21,43,72,5,88,41,30,86,9,52,24,97,46,91,28,83,98,40,75,69,20,12,82,78,3,50,42,58,46,65,18,45,24,24,39,59,68,69,92,24,88,6,12,91,48,32,97,92,91,23,34,87,21,12,93,37,31,78,11,45,87,98,45,76,40,74,34,29,47,1,43,65,85,29,31,16,30,3,59,97,66,52,45,28,7,86,54,0,10,29,25,41,99,16,53,16,64,35,94,42,21,38,43,11,91,34,53,3,52],[4,44,56,12,56,5,84,10,47,66,39,0,72,57,81,52,45,96,36,88,5,2,24,98,84,85,72,91,55,44,45,66,35,59,59,20,37,33,96,37,22,38,74,90,49,23,45,11,62,40,38,69,72,16,63,42,91,84,86,52,6,98,14,39,67,40,21,74,25,26,44,42,93,89,49,91,10,74,23,56,35,71,1,54,22,5,88,13,22,40,27,51,38,39,77,49,13,83,2,83,31,60,87,8,18,43,56,56,11,59,52,95,39,15,76,44,13,25,42,18,26,33,46,58,32,9,6,2,1,77,37,78,8,18,70,70,30,88,67,52,99,2,38],[52,28,79,17,34,52,99,6,95,79,91,2,40,3,70,82,64,43,68,48,24,51,73,60,66,77,24,19,68,93,42,5,43,20,83,23,48,60,56,20,86,13,26,89,94,43,70,28,21,51,50,10,84,20,73,38,46,87,27,34,59,48,60,80,24,52,60,94,91,31,47,87,92,35,82,46,3,12,50,9,42,68,18,15,7,55,13,39,19,11,94,17,49,33,48,40,86,88,64,42,74,71,33,27,90,43,48,56,84,79,70,45,43,83,44,71,8,97,94,52,86,6,24,88,7,74,73,17,23,43,41,55,10,95,58,45,90,39,87,37,7,59,26],[91,76,8,32,6,79,56,52,88,69,44,64,16,76,6,84,57,33,78,93,33,42,0,29,9,97,83,31,24,91,36,89,0,37,43,27,20,37,95,58,88,5,53,97,59,36,33,4,79,50,85,82,12,75,33,40,26,95,89,40,33,85,11,88,19,50,55,71,13,5,51,73,40,82,88,36,62,72,96,39,37,64,68,47,12,56,54,4,69,24,63,70,76,28,62,55,97,54,45,54,37,92,86,52,74,63,43,18,46,11,46,95,55,68,7,77,89,49,35,68,57,51,51,26,49,29,83,17,69,31,29,7,16,25,51,89,93,6,24,68,97,74,43],[68,90,38,49,62,95,67,57,50,7,74,3,46,28,38,90,7,40,91,95,94,86,16,94,46,51,18,84,28,29,4,0,19,74,98,7,60,30,81,35,55,42,68,11,44,33,42,53,72,9,28,95,11,11,10,96,7,32,70,2,36,12,46,39,28,90,64,33,33,42,59,89,2,3,4,17,67,12,31,54,33,33,40,4,10,3,70,67,45,34,36,32,15,92,7,7,63,70,68,79,79,35,66,76,96,97,91,27,23,21,9,5,62,95,96,3,84,90,49,6,52,38,78,96,41,71,71,57,27,47,77,53,25,81,41,18,5,69,18,70,64,36,76],[48,3,96,29,40,51,77,90,34,12,42,41,47,54,87,43,78,1,41,77,6,87,49,13,25,24,70,11,94,98,81,33,77,7,55,37,23,21,94,29,25,20,82,61,29,75,47,42,7,40,79,98,55,26,77,53,61,54,9,15,63,6,44,32,75,23,41,81,71,69,62,59,46,77,49,42,85,62,46,93,8,41,94,74,8,80,78,42,32,19,46,18,36,19,97,27,78,82,62,68,61,55,69,48,37,78,20,72,0,5,70,83,59,36,49,27,56,87,20,27,0,38,4,16,58,76,41,1,67,57,69,40,81,83,88,86,14,37,70,99,23,67,66],[96,18,37,9,90,73,74,44,72,44,88,39,69,85,87,46,67,61,60,86,96,75,10,89,71,98,87,0,7,47,32,41,57,12,63,9,67,77,61,72,22,13,93,84,28,41,59,85,39,64,7,58,15,99,58,42,63,84,95,41,36,19,24,41,93,69,42,86,2,17,64,34,40,58,8,31,79,50,41,42,65,4,29,1,23,60,61,33,9,69,75,90,9,86,40,53,65,90,4,23,63,13,11,36,71,67,30,47,49,71,87,34,39,66,46,94,10,83,14,51,9,83,28,50,72,87,74,85,52,93,67,25,82,48,19,68,74,96,75,85,17,73,27],[95,52,25,52,58,78,88,69,97,12,20,85,62,91,91,16,25,30,49,84,24,91,96,22,28,29,21,22,64,52,60,60,18,5,81,33,36,1,71,22,1,53,80,63,62,67,2,81,25,88,3,2,79,82,47,74,42,25,95,64,57,59,72,1,32,72,35,18,12,60,72,57,89,16,49,34,89,41,35,49,9,73,33,2,26,14,50,38,12,97,45,88,47,76,12,65,11,21,6,18,16,51,54,27,84,58,53,57,43,35,60,8,9,10,74,88,86,25,56,54,52,22,8,72,83,64,30,33,33,34,62,26,29,46,30,26,9,18,99,73,1,26,79],[6,37,70,10,15,39,9,7,98,52,99,51,66,4,27,76,66,68,42,87,82,82,49,88,84,38,25,23,54,15,94,25,92,17,21,70,24,36,99,41,26,22,32,50,42,75,65,90,17,66,42,98,61,37,99,19,64,82,86,6,28,16,35,25,16,9,92,16,43,21,48,55,95,29,22,85,21,79,90,94,56,78,96,94,20,18,58,93,2,84,50,59,94,81,16,41,84,97,42,38,63,19,78,45,60,57,80,48,23,73,49,98,17,59,12,87,99,42,9,36,14,2,68,2,80,69,43,3,19,69,12,0,81,14,34,31,89,57,27,80,92,12,16],[43,63,83,93,46,57,20,75,5,57,83,88,68,32,80,63,64,5,5,81,89,1,37,92,36,10,79,94,48,91,69,85,5,54,60,79,1,55,29,73,68,81,95,79,23,36,60,83,44,66,57,49,44,22,13,71,77,43,30,65,77,29,80,54,12,87,44,55,2,97,93,56,28,48,46,44,81,36,82,36,99,11,54,29,36,29,57,69,74,76,74,41,93,57,88,33,84,8,22,48,9,74,91,58,11,17,39,72,90,34,17,39,76,10,74,5,46,2,34,81,19,9,63,57,50,0,65,7,0,75,94,50,88,19,35,44,27,48,8,65,84,76,54],[58,4,13,94,63,12,8,55,42,99,43,45,2,56,34,28,80,21,47,51,18,9,51,46,70,13,93,0,18,45,30,68,38,43,19,42,89,1,44,9,78,51,93,12,35,61,27,73,0,87,80,79,99,32,8,24,32,0,89,39,5,56,94,93,47,5,88,31,67,49,5,8,51,59,51,28,89,39,18,75,91,89,98,65,54,43,43,56,22,6,30,25,38,10,93,25,21,13,88,59,20,13,25,30,74,31,9,45,47,97,99,39,5,53,69,25,34,84,15,79,59,45,70,74,29,34,22,61,8,29,59,20,48,88,77,86,79,23,95,35,85,87,8],[68,70,85,49,3,96,42,6,64,37,22,80,58,63,44,7,85,11,75,94,36,88,62,52,10,86,94,66,30,84,2,39,99,30,14,51,15,19,73,69,60,94,57,84,41,24,32,73,52,92,31,27,24,91,12,92,38,87,49,0,30,35,61,93,79,20,60,54,77,3,22,4,82,52,61,26,55,95,74,88,82,5,23,68,53,74,24,54,11,94,76,15,91,94,48,45,85,26,22,14,81,49,48,62,67,17,68,42,33,64,27,91,40,64,66,92,86,60,17,38,13,19,59,30,53,70,24,80,16,90,86,47,99,76,99,0,6,22,20,12,73,31,86],[19,70,71,27,27,98,33,20,75,79,90,46,93,56,38,36,22,77,97,34,73,47,45,19,33,48,64,34,81,56,94,48,59,75,35,83,50,39,6,70,6,2,6,32,76,77,23,36,58,15,16,15,80,53,95,13,50,41,21,45,52,14,14,19,41,0,9,45,83,87,98,16,94,93,61,6,51,50,96,96,17,11,41,28,89,53,18,12,31,87,46,27,57,80,85,89,74,12,55,71,88,72,1,18,40,97,57,60,63,40,66,73,51,81,29,23,62,4,7,73,24,25,14,82,80,40,70,42,9,75,82,63,75,29,84,38,44,73,52,2,26,19,20]])) diff --git a/Week_03/id_26/LeetCode_429_26.py b/Week_03/id_26/LeetCode_429_26.py new file mode 100644 index 00000000..600f9504 --- /dev/null +++ b/Week_03/id_26/LeetCode_429_26.py @@ -0,0 +1,71 @@ +# +# @lc app=leetcode.cn id=429 lang=python +# +# [429] N叉树的层序遍历 +# +# https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/description/ +# +# algorithms +# Easy (61.76%) +# Likes: 36 +# Dislikes: 0 +# Total Accepted: 6.5K +# Total Submissions: 10.6K +# Testcase Example: '{"$id":"1","children":[{"$id":"2","children":[{"$id":"5","children":[],"val":5},{"$id":"6","children":[],"val":6}],"val":3},{"$id":"3","children":[],"val":2},{"$id":"4","children":[],"val":4}],"val":1}' +# +# 给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。 +# +# 例如,给定一个 3叉树 : +# +# +# +# +# +# +# +# 返回其层序遍历: +# +# [ +# ⁠ [1], +# ⁠ [3,2,4], +# ⁠ [5,6] +# ] +# +# +# +# +# 说明: +# +# +# 树的深度不会超过 1000。 +# 树的节点总数不会超过 5000。 +# +# +""" +# Definition for a Node. +class Node(object): + def __init__(self, val, children): + self.val = val + self.children = children +""" + + +class Solution(object): + def levelOrder(self, root): + """ + :type root: Node + :rtype: List[List[int]] + 时间复杂度:O(N) + 空间复杂度:O(N) + """ + if not root: + return [] + layer, ans = [root], [] + while layer: + next_layer, vals = [], [] + for node in layer: + vals.append(node.val) + next_layer += node.children + layer = next_layer + ans.append(vals) + return ans diff --git a/Week_03/id_26/LeetCode_547_26.py b/Week_03/id_26/LeetCode_547_26.py new file mode 100644 index 00000000..262870bc --- /dev/null +++ b/Week_03/id_26/LeetCode_547_26.py @@ -0,0 +1,122 @@ +# +# @lc app=leetcode.cn id=547 lang=python +# +# [547] 朋友圈 +# +# https://leetcode-cn.com/problems/friend-circles/description/ +# +# algorithms +# Medium (50.43%) +# Likes: 100 +# Dislikes: 0 +# Total Accepted: 6.5K +# Total Submissions: 12.9K +# Testcase Example: '[[1,1,0],[1,1,0],[0,0,1]]' +# +# 班上有 N 名学生。其中有些人是朋友,有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C +# 的朋友。所谓的朋友圈,是指所有朋友的集合。 +# +# 给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j +# 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。 +# +# 示例 1: +# +# +# 输入: +# [[1,1,0], +# ⁠[1,1,0], +# ⁠[0,0,1]] +# 输出: 2 +# 说明:已知学生0和学生1互为朋友,他们在一个朋友圈。 +# 第2个学生自己在一个朋友圈。所以返回2。 +# +# +# 示例 2: +# +# +# 输入: +# [[1,1,0], +# ⁠[1,1,1], +# ⁠[0,1,1]] +# 输出: 1 +# 说明:已知学生0和学生1互为朋友,学生1和学生2互为朋友,所以学生0和学生2也是朋友,所以他们三个在一个朋友圈,返回1。 +# +# +# 注意: +# +# +# N 在[1,200]的范围内。 +# 对于所有学生,有M[i][i] = 1。 +# 如果有M[i][j] = 1,则有M[j][i] = 1。 +# +# +# + + +class Solution(object): + + def findCircleNum(self, M): + """ + :type M: List[List[int]] + :rtype: int + 解法1:dfs + 深度遍历,用集合存储已经访问过的节点,如果访问过则跳过,否则朋友圈+1 + """ + visited, ans = set(), 0 + + def dfs(i): + for j in range(len(M[i])): + if M[i][j] and j not in visited: + visited.add(j) + dfs(j) + + for i in range(len(M)): + if i not in visited: + dfs(i) + ans += 1 + return ans + + def findCircleNum2(self, M): + """ + :type M: List[List[int]] + :rtype: int + 解法2:并查集 + 首先每个人都是自己的朋友圈,所以先建立一个对应关系指向自己 + 如果i和j是朋友,则i的朋友圈合并j的朋友圈,即j所在圈的根节点指向i所在圈的根节点 + 最后数一下指向自己的节点有几个,即根节点的数量,即朋友圈的数量 + """ + n = len(M) + circles = {i: i for i in range(n)} + + def find(i): + if i == circles[i]: + return i + circles[i] = find(circles[i]) + return circles[i] + + for i in range(n): + for j in range(i + 1, n): + if M[i][j] == 1: + circles[find(i)] = find(j) + + return sum([1 for k, v in circles.items() if k == v]) + + +print(Solution().findCircleNum2([[1, 0, 0], [0, 1, 0], [0, 0, 1]])) +print(Solution().findCircleNum2([[1, 1, 0], [1, 1, 0], [0, 0, 1]])) +print(Solution().findCircleNum2([[1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0], + [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0], + [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1], + [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]] + )) diff --git a/Week_03/id_26/LeetCode_703_26.py b/Week_03/id_26/LeetCode_703_26.py new file mode 100644 index 00000000..d5f93fdb --- /dev/null +++ b/Week_03/id_26/LeetCode_703_26.py @@ -0,0 +1,66 @@ +# +# @lc app=leetcode.cn id=703 lang=python +# +# [703] 数据流中的第K大元素 +# +# https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/description/ +# +# algorithms +# Easy (38.59%) +# Likes: 56 +# Dislikes: 0 +# Total Accepted: 5.1K +# Total Submissions: 13.1K +# Testcase Example: '["KthLargest","add","add","add","add","add"]\n[[3,[4,5,8,2]],[3],[5],[10],[9],[4]]' +# +# 设计一个找到数据流中第K大元素的类(class)。注意是排序后的第K大元素,不是第K个不同的元素。 +# +# 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中的初始元素。每次调用 +# KthLargest.add,返回当前数据流中第K大的元素。 +# +# 示例: +# +# +# int k = 3; +# int[] arr = [4,5,8,2]; +# KthLargest kthLargest = new KthLargest(3, arr); +# kthLargest.add(3);   // returns 4 +# kthLargest.add(5);   // returns 5 +# kthLargest.add(10);  // returns 5 +# kthLargest.add(9);   // returns 8 +# kthLargest.add(4);   // returns 8 +# +# +# 说明: +# 你可以假设 nums 的长度≥ k-1 且k ≥ 1。 +# +# + +import heapq + + +class KthLargest(object): + def __init__(self, k, nums): + """ + :type k: int + :type nums: List[int] + """ + self.heap = [] + self.k = k + map(self.add, nums) + + def add(self, val): + """ + :type val: int + :rtype: int + """ + if len(self.heap) < self.k: + heapq.heappush(self.heap, val) + elif val > self.heap[0]: + heapq.heapreplace(self.heap, val) + return self.heap[0] + + +# Your KthLargest object will be instantiated and called as such: +# obj = KthLargest(k, nums) +# param_1 = obj.add(val) diff --git a/Week_03/id_26/LeetCode_802_26.py b/Week_03/id_26/LeetCode_802_26.py new file mode 100644 index 00000000..a686876c --- /dev/null +++ b/Week_03/id_26/LeetCode_802_26.py @@ -0,0 +1,107 @@ +# +# @lc app=leetcode.cn id=802 lang=python +# +# [802] 找到最终的安全状态 +# +# https://leetcode-cn.com/problems/find-eventual-safe-states/description/ +# +# algorithms +# Medium (37.58%) +# Likes: 14 +# Dislikes: 0 +# Total Accepted: 543 +# Total Submissions: 1.5K +# Testcase Example: '[[1,2],[2,3],[5],[0],[5],[],[]]' +# +# 在有向图中, 我们从某个节点和每个转向处开始, 沿着图的有向边走。 如果我们到达的节点是终点 (即它没有连出的有向边), 我们停止。 +# +# 现在, 如果我们最后能走到终点,那么我们的起始节点是最终安全的。 更具体地说, 存在一个自然数 K,  无论选择从哪里开始行走, 我们走了不到 K +# 步后必能停止在一个终点。 +# +# 哪些节点最终是安全的? 结果返回一个有序的数组。 +# +# 该有向图有 N 个节点,标签为 0, 1, ..., N-1, 其中 N 是 graph 的节点数.  图以以下的形式给出: graph[i] 是节点 j +# 的一个列表,满足 (i, j) 是图的一条有向边。 +# +# +# 示例: +# 输入:graph = [[1,2],[2,3],[5],[0],[5],[],[]] +# 输出:[2,4,5,6] +# 这里是上图的示意图。 +# +# +# +# +# +# 提示: +# +# +# graph 节点数不超过 10000. +# 图的边数不会超过 32000. +# 每个 graph[i] 被排序为不同的整数列表, 在区间 [0, graph.length - 1] 中选取。 +# +# +# + +import collections + + +class Solution(object): + def eventualSafeNodes1(self, graph): + """ + :type graph: List[List[int]] + :rtype: List[int] + This is equal to find nodes which doesn't lead to a circle in any path. + We can solve it by walking along the path reversely. + + Find nodes with out degree 0, they are terminal nodes, + we remove them from graph and they are added to result + For nodes who are connected terminal nodes, + since terminal nodes are removed, + we decrease in-nodes' out degree by 1 and if its out degree equals to 0, + it become new terminal nodes + Repeat 2 until no terminal nodes can be found. + """ + n = len(graph) + out_degree = collections.defaultdict(int) + in_nodes = collections.defaultdict(list) + + queue = [] + ret = [] + for i in range(n): + out_degree[i] = len(graph[i]) + if out_degree[i] == 0: + queue.append(i) + for j in graph[i]: + in_nodes[j].append(i) + + while queue: + term_node = queue.pop(0) + ret.append(term_node) + for in_node in in_nodes[term_node]: + out_degree[in_node] -= 1 + if out_degree[in_node] == 0: + queue.append(in_node) + return sorted(ret) + + def eventualSafeNodes(self, graph): + n = len(graph) + out_degree, in_nodes = [],[[] for i in range(n)] + queue, ans= [], set() + + for i in range(len(graph)): + out_degree.append(len(graph[i])) + if not graph[i]: + queue.append(i) + for j in graph[i]: + in_nodes[j].append(i) + + while queue: + i = queue.pop() + ans.add(i) + for j in in_nodes[i]: + out_degree[j] -= 1 + if out_degree[j] == 0: + queue.append(j) + return sorted(ans) +