[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. ...

November 29, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2470. Number of Subarrays With LCM Equal to K

2470. Number of Subarrays With LCM Equal to K Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Math、Number Theory \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 Given an integer array nums and an integer k, return the number of subarrays of nums where the least common multiple of the subarray’s elements is k. A subarray is a contiguous non-empty sequence of elements within an array. The least common multiple of an array is the smallest positive integer that is divisible by all the array elements. ...

November 29, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2469. Convert the Temperature

2469. Convert the Temperature Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Math \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius. You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit]. Return the array ans. Answers within 10^-5 of the actual answer will be accepted. Note that: Kelvin = Celsius + 273.15 Fahrenheit = Celsius * 1.80 + 32.00 Example 1: ...

November 29, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 446. Arithmetic Slices II - Subsequence

446. Arithmetic Slices II - Subsequence Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Array、Dynamic Programming 一、題目 Given an integer array nums, return the number of all the arithmetic subsequences of nums. A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, [1, 3, 5, 7, 9], [7, 7, 7, 7], and [3, -1, -5, -9] are arithmetic sequences. For example, [1, 1, 2, 5, 7] is not an arithmetic sequence. A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array. For example, [2,5,10] is a subsequence of [1,2,1,2,4,1,5,10]. The test cases are generated so that the answer fits in 32-bit integer. Example 1: ...

November 28, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2488. Count Subarrays With Median K

2488. Count Subarrays With Median K Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Array、Hash Table、Prefix Sum \(\color{blue}\textsf{Weekly Contest 321}\) 一、題目 You are given an array nums of size n consisting of distinct integers from 1 to n and a positive integer k. Return the number of non-empty subarrays in nums that have a median equal to k. Note: The median of an array is the middle element after sorting the array in ascending order. If the array is of even length, the median is the left middle element. For example, the median of [2,3,1,4] is 2, and the median of [8,4,3,5,1] is 4. A subarray is a contiguous part of an array. Example 1: ...

November 27, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2487. Remove Nodes From Linked List

2487. Remove Nodes From Linked List Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Linked List、Stack、Recursion、Monotonic Stack \(\color{blue}\textsf{Weekly Contest 321}\) 一、題目 You are given the head of a linked list. Remove every node which has a node with strictly greater value anywhere to the right side of it. Return the head of the modified linked list. Example 1: Input: head = [5,2,13,3,8] Output: [13,8] Explanation: The nodes that should be removed are 5, 2 and 3. Node 13 is to the right of node 5. Node 13 is to the right of node 2. Node 8 is to the right of node 3. Example 2: ...

November 27, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2486. Accept Characters to String to Make Subsequence

2486. Accept Characters to String to Make Subsequence Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Two Pointer、String、Greedy \(\color{blue}\textsf{Weekly Contest 321}\) 一、題目 You are given two strings s and t consisting of only lowercase English letters. Return the minimum number of characters that need to be appended to the end of s so that t becomes a subsequence of s. A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. ...

November 27, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2485. Find the Pivot Integer

2485. Find the Pivot Integer Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Math、Prefix Sum \(\color{blue}\textsf{Weekly Contest 321}\) 一、題目 Given a positive integer n, find the pivot integer x such that: The sum of all elements between 1 and x inclusively equals the sum of all elements between x and n inclusively. Return the pivot integer x. If no such integer exists, return -1. It is guaranteed that there will be at most one pivot index for the given input. Example 1: ...

November 27, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 1235. Maximum Profit in Job Scheduling

1235. Maximum Profit in Job Scheduling Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Array、Binary Search、Dynamic Programming、Sorting 一、題目 We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i]. You’re given the startTime, endTime and profit arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range. If you choose a job that ends at time X you will be able to start another job that starts at time X. ...

November 26, 2022 · 3 分鐘 · Rain Hu

[LeetCode] 1143. Longest Common Subsequence

1143. Longest Common Subsequence Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String、Dynamic Programming 一、題目 Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings. Example 1: ...

November 24, 2022 · 2 分鐘 · Rain Hu