View

LeetCode

[LeetCode] 493. Reverse Pairs ProblemExplanationFor an array `[x_1, \dots , x_n],` suppose `(i, j)` is a reverse pair, meaning `x_i > 2x_j`. If there exist indices `k` and `t` such that ``i ``x_i \leq x_{k} \quad \text{and} \quad x_{t} \leq x_j,``then ``x_{k} \ge x_i > 2x_j \quad \text{and} \quad x_i > 2x_j \ge 2x_{t}.`` From these inequalities, it follows that the pairs `(k, t)` , `(i, t)`, and `(k, j)` must also be reverse.. Show More
[LeetCode] 2681. Power of Heroes ProblemExplanationThe power of a group of heroes is defined by the square of the maximum strength multiplied by the minimum strength: ``Power = (max)^2 \times min.`` To solve this efficiently, sort the array `[x_0, x_1, \dots , x_{n-1}]` in ascending order. Once sorted, for any subset with minimum index `i` and maximum index `j` `\{x_i , \dots , x_j\}` `(i To calculate the total power, analyze t.. Show More
[LeetCode] 1590. Make Sum Divisible by p ProblemExplanationWe are given an array `[x_0,x_1,x_2,\dots ,x_{n-1}] ,` and a positive integer `p`. Let `S` represent the total sum of the array and `R` the total remainder, defined as `R = S \bmod p.` Our goal is to remove one contiguous subarray `[x_i,\dots ,x_j]` such that the remaining sum is divisible by `p`: ``(S-(x_i + \cdots + x_j)) \bmod p = 0.``This condition is equivalent to `` (x_i .. Show More
[LeetCode] 1354. Construct Target Array with Multiple Sums ProblemExplanationConstructing the target array forward from `[1, 1, …, 1]` is infeasible because each step allows any element to be replaced by the sum of the array, leading to an exponential number of possible sequences. Instead, we reverse the process and work backward FROM the target array to determine whether it can be reduced to the base case.Why Max Heap?When reversing the process, the el.. Show More
[LeetCode] 84. Largest Rectangle in Histogram ProblemExplanationOur goal is to find the largest rectangular area that can be formed using one or more adjacent bars in a histogram. Instead of a brute-force solution that tries every possible range of bars to compute the area, we can solve the problem more efficiently using a monotonic stack. A monotonic stack is a stack that keeps its elements in either increasing or decreasing order. In this.. Show More
[LeetCode] 389. Find the Difference ProblemExplanationThe XOR (^) operation has two fundamental properties:Self-inverse: ##x^x=0##Identity with zero: ##x^0=x##It also has two additional properties that ensure the order of XOR operations does not matter.Commutative: ##a^b=b^a##Associative: ##(a^b)^c=a^(b^c)##Because of these properties, identical values XOR-ed together cancel each other out regardless of their order.Solutionclass S.. Show More