[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

[LeetCode] 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Binary Search、Dynamic Programming 一、題目 Given an integer array nums, return the length of the longest strictly increasing subsequence Example 1: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Example 2: Input: nums = [0,1,0,3,2,3] Output: 4 Example 3: Input: nums = [7,7,7,7,7,7,7] Output: 1 Constraints: 1 <= nums.length <= 2500 -104 <= nums[i] <= 104 Follow up: Can you come up with an algorithm that runs in O(n log n) time complexity ...

November 24, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2468. Split Message Based on Limit

2468. Split Message Based on Limit Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: String、Binary Search \(\color{blue}\textsf{Biweekly Contest 91}\) 一、題目 You are given a string, message, and a positive integer, limit. You must split message into one or more parts based on limit. Each resulting part should have the suffix "<a/b>", where "b" is to be replaced with the total number of parts and "a" is to be replaced with the index of the part, starting from 1 and going up to b. Additionally, the length of each resulting part (including its suffix) should be equal to limit, except for the last part whose length can be at most limit. ...

November 24, 2022 · 4 分鐘 · Rain Hu

[LeetCode] 2467. Most Profitable Path in a Tree

2467. Most Profitable Path in a Tree Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Tree、Depth-First Search、Breadth-First Search、Graph \(\color{blue}\textsf{Biweekly Contest 91}\) 一、題目 There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents: ...

November 24, 2022 · 4 分鐘 · Rain Hu