[LeetCode] 2501. Longest Square Streak in an Array

2501. Longest Square Streak in an Array Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Hash Table、Binary Search、Dynamic Programming、Sorting \(\color{blue}\textsf{Weekly Contest 323}\) 一、題目 You are given an integer array nums. A subsequence of nums is called a square streak if: The length of the subsequence is at least 2, and after sorting the subsequence, each element (except the first element) is the square of the previous number. Return the length of the longest square streak in nums, or return -1 if there is no square streak. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1: ...

<span title='2022-12-11 22:42:14 +0800 +0800'>December 11, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 2500. Delete Greatest Value in Each Row

2500. Delete Greatest Value in Each Row Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Array、Hash Table、Binary Search、Dynamic Programming、Sorting \(\color{blue}\textsf{Weekly Contest 323}\) 一、題目 You are given an m x n matrix grid consisting of positive integers. Perform the following operation until grid becomes empty: Delete the element with the greatest value from each row. If multiple such elements exist, delete any of them. Add the maximum of deleted elements to the answer. Note that the number of columns decreases by one after each operation. Return the answer after performing the operations described above. Example 1: ...

<span title='2022-12-11 22:42:12 +0800 +0800'>December 11, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 124. Binary Tree Maximum Path Sum

124. Binary Tree Maximum Path Sum Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Dynamic Programming、Tree、Depth-First Search、Binary Tree 一、題目 A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum path sum of any non-empty path. Example 1: ...

<span title='2022-12-11 21:53:00 +0800 +0800'>December 11, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 1339. Maximum Product of Splitted Binary Tree

1339. Maximum Product of Splitted Binary Tree Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Tree、Depth-First Search、Binary Tree 一、題目 Given the root of a binary tree, split the bianry tree into two subtrees by removing one edge such that the product of the sums of the subtreesis maximized. Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 10^9 + 7. Note that you need to maximize the answer before taking the mod and not after taking it. ...

<span title='2022-12-10 20:12:36 +0800 +0800'>December 10, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 1026. Maximum Difference Between Node and Ancestor

1026. Maximum Difference Between Node and Ancestor Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Tree、Depth-First Search、Binary Tree 一、題目 Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b. A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b. ...

<span title='2022-12-10 00:49:10 +0800 +0800'>December 10, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 872. Leaf-Similar Trees

872. Leaf-Similar Trees Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Tree、Depth-First Search、Binary Tree 一、題目 Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). Two binary trees are considered leaf-similar if their leaf value sequence is the same. Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar. ...

<span title='2022-12-08 21:41:10 +0800 +0800'>December 8, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 938. Range Sum of BST

938. Range Sum of BST Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Tree、Depth-First Search、Binary Search Tree、Binary Tree 一、題目 Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. Example 1: Input: root = [10,5,15,3,7,null,18], low = 7, high = 15 Output: 32 Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32. Example 2: ...

<span title='2022-12-07 23:41:31 +0800 +0800'>December 7, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 328. Odd Even Linked List

328. Odd Even Linked List Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Linked List 一、題目 Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the problem in O(1) extra space complexity and O(n) time complexity. ...

<span title='2022-12-06 23:56:11 +0800 +0800'>December 6, 2022</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 2472. Maximum Number of Non-overlapping Palindrome Substrings

2472. Maximum Number of Non-overlapping Palindrome Substrings Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: String、Dynamic Programming \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 You are given a string s and a positive integer k. Select a set of non-overlapping substrings from the string s that satisfy the following conditions: The length of each substring is at least k. Each substring is a palindrome. Return the maximum number of substrings in an optimal selection. A substring is a contiguous sequence of characters within a string. Example 1: ...

<span title='2022-11-29 23:55:22 +0800 +0800'>November 29, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[LeetCode] 2471. Minimum Number of Operations to Sort a Binary Tree by Level

2471. Minimum Number of Operations to Sort a Binary Tree by Level Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Tree、Breadth-First Search、Binary Tree \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 You are given the root of a binary tree with unique values. In one operation, you can choose any two nodes at the same level and swap their values. Return the minimum number of operations needed to make the values at each level sorted in a strictly increasing order. The level of a node is the number of edges along the path between it and the root node. ...

<span title='2022-11-29 23:55:20 +0800 +0800'>November 29, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu