[LeetCode] Peaks in Array II

4017. Peaks in Array II

You are given an integer array nums of length n and a 2D integer array queries.

A subarray nums[i..j] is called a peak subarray if:

  • Its length is at least 3.
  • There exists an index k such that i < k < j and:
    • nums[k] > nums[k - 1]
    • nums[k] > nums[k + 1]

You have to process queries of two types:

  • [1, l_i, r_i]: Calculate the number of peak subarrays fully contained within nums[l_i..r_i].
  • [2, index_i, val_i]: Update nums[index_i] to val_i. This update applies to all subsequent queries.

Return an array answer, where answer[i] is the answer to the i^th query of type 1 in the order they appear.

Read more
[LeetCode] Maximum Area of Two Non-Overlapping Square Submatrices

4016. Maximum Area of Two Non-Overlapping Square Submatrices

You are given a 2D integer matrix mat of size m × n, where:

  • mat[r][c] == 1 means the cell at row r and column c is usable.
  • mat[r][c] == 0 means it is not usable.

Your task is to find two submatrices that satisfy the following conditions:

  • Both submatrices must be squares of the same side length k.
  • The two submatrices must not share any cell.
  • Each submatrix can only cover cells where mat[r][c] == 1.

Return the maximum possible area of each of the two squares. If it is not possible to choose two such squares, return 0.

Read more
[LeetCode] Elevator Requests I

4020. Elevator Requests I

You are given an integer n denoting the number of floors in a building, where the floors are numbered from 0 to n - 1.

You are also given an integer array requests, where requests represents the sequence of floor requests.

An elevator starts at floor 0 and follows these rules:

  • The elevator moves one floor per second.
  • The elevator serves requests in the given order.
  • If the elevator is already on the requested floor, no movement is needed.
  • After serving a request, the elevator immediately starts moving toward the next request.

Return the total time in seconds required to serve all requests.

Read more
[LeetCode] Minimum Operations to Make a Rotated Palindrome I

4021. Minimum Operations to Make a Rotated Palindrome I

You are given a string s consisting of lowercase English letters.

You can perform the following operations any number of times (including zero) and in any order:

  • Increment: Choose any index i and replace s[i] with the next lowercase English letter. The letter after 'z' is 'a'.
  • Left rotate: Move the first character of the string to the end.

Return the minimum number of operations required to make s a palindrome.

Read more
[LeetCode] Weighted Sum of a Tree

4015. Weighted Sum of a Tree

You are given an integer array parent of length n representing a rooted tree with nodes labeled from 0 to n - 1.

The tree is rooted at node 0, so parent[0] = -1. For each node i where 1 <= i <= n - 1, parent[i] denotes the parent of node i.

You are also given an integer array nums of length n, where nums[i] denotes the value of node i.

The weight of a node i at depth d is nums[i] * (h - d + 1), where h is the height of the tree.

Return the sum of the weights of all nodes in the tree.

The depth of a node is the number of nodes on the path from the root to that node, inclusive, with the root having depth 1.

The height of the tree is the maximum depth among all nodes in the tree.

Read more
[LeetCode] Elevator Requests II

4023. Elevator Requests II

You are given an integer n denoting the number of floors in a building, where the floors are numbered from 0 to n - 1.

You are also given an integer start, representing the floor where the elevator begins, and an integer array requests, where requests[i] is a floor that the elevator is requested to reach. All floors in requests are distinct.

At time 0, the elevator is on floor start, and all requests are made simultaneously.

During each second before all requests are fulfilled, the elevator moves exactly one floor, either up or down. A request is fulfilled instantly when the elevator reaches its requested floor. If start appears in requests, that request is fulfilled at time 0.

For each second that a request remains unfulfilled, you receive 1 penalty. Equivalently, a request fulfilled at time t contributes t to the total penalty.

Return the minimum total penalty required to fulfill all requests.

Read more
[LeetCode] K-th Digit in Infinite String

4022. K-th Digit in Infinite String

You are given an integer k.

An infinite string is formed by concatenating the decimal representations of the positive integers, without separators.

For every nonnegative integer b, block b contains the positive integers from 10 * b through 10 * b + 9. The integers in each block are appended as follows:

  • If b is even, append the integers in increasing order.
  • If b is odd, append the integers in decreasing order.

Therefore, the string starts with the integers 1 through 9, followed by 19 through 10, then 20 through 29, then 39 through 30, and so on.

Return the k^th digit (1-indexed) of this string.

Read more
[LeetCode] Elevator Requests III

4027. Elevator Requests III

You are given an integer n denoting the number of floors in a building, where the floors are numbered from 0 to n - 1.

You are also given an integer start and a 2D integer array requests, where requests[i] = [arrival_i, floor_i] indicates that a request for floor_i is made at time arrival_i.

At time 0, the elevator is at floor start.

At each second, the elevator may move up by 1 floor, move down by 1 floor, or remain on its current floor.

A request can be fulfilled only at or after its arrival time; it is fulfilled instantly when the elevator is on its requested floor at any time from its arrival time onward.

Return the minimum time needed to fulfill all requests.

Read more
[LeetCode] Check ASCII Palindromic

4030. Check ASCII Palindromic

You are given a string s consisting of lowercase English letters.

Construct a binary string by replacing each character in s with the 8-bit binary representation of its ASCII value, including leading zeros, while preserving the original order of the characters.

Return true if the resulting binary string is a palindrome. Otherwise, return false.

Read more
[LeetCode] Maximum Gap Between Stations

4026. Maximum Gap Between Stations

You are given two strings skill and station of lengths n and m, respectively.

skill[i] represents the skill of worker i, and station[j] represents the skill supported by station j.

You must assign every worker to a distinct station. Let j_i be the index of the station assigned to worker i. A valid assignment must satisfy:

  • station[j_i] == skill[i] for every 0 <= i < n.
  • The assigned station indices must be strictly increasing in worker order, meaning j_0 < j_1 < ... < j_n - 1.

The gap of an assignment is the maximum difference between the station indices assigned to two consecutive workers. In other words, it is max(j_i - j_i - 1) over all 1 <= i < n.

If there is only one worker, the gap is 0.

Return the maximum possible gap among all valid assignments. It is guaranteed that at least one valid assignment exists.

Read more