Another day, another editorial solution
I first use a Disjoint Set Union (Union-Find) to determine the connected component (power grid) of each station. For every component root I build a min-heap of all station ids in that component (initially all stations are online). I keep an online boolean array. When a station goes offline I simply mark online[id] = False. For a query [1, x]:
I slide a window of length k over nums. For each window I count frequencies of values (values are bounded <= 50), build pairs (freq, value) for values present in the window, sort those pairs by frequency descending and value descending (so ties favor the larger value), then keep only the top x pairs and sum freq * value for those pairs. If the window has fewer than x distinct values, the x-sum is simply the sum of the entire window.
class Solution:
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:I sweep the string left to right and treat each run of consecutive same-colored balloons as a group. To avoid adjacent equal colors I must remove all but one balloon in each group; the minimum cost for a group is the sum of its removal times minus the maximum time in that group. Equivalently, while scanning, whenever the current balloon has the same color as the previous one, I remove the cheaper of the two (add min(prev_time, cur_time) to the answer) and keep the larger time as the survivor for further comparisons.
class Solution:
def minCost(self, colors: str, neededTime: List[int]) -> int:I build a 2D grid representation where I mark walls and guards, then for each guard I scan outward in the four cardinal directions until I hit a wall or another guard, marking every empty cell I pass as guarded. Finally I count grid cells that are still empty and unguarded. This works because the product m * n ≤ 1e5, so an explicit grid and linear scans are efficient.
class Solution:
def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:I first convert nums into a set for O(1) lookups. Then, I use a dummy node to simplify edge cases like when the head itself needs to be removed. I traverse the linked list with a pointer. If the current node’s value is in the set, I skip it by linking to curr.next; otherwise, I move forward. Finally, I return dummy.next as the new head of the modified list.
class Solution:
def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:I scan the array once while keeping a set of numbers I have already seen. Whenever I encounter a number that is already in the set, it must be one of the sneaky repeated numbers, so I add it to the result list. Since there are exactly two repeats, I stop once I found both.
class Solution:
def getSneakyNumbers(self, nums: List[int]) -> List[int]:My approach is to track how many times the array increases. I start by setting the number of operations to the first element since that’s how many increments are needed to reach it from zero. Then, as I move through the array, I only add the positive differences between consecutive elements—because whenever a value increases compared to the previous one, that increase represents new operations needed. This gives the minimum number of operations efficiently in one pass.
class Solution:
def minNumberOperations(self, target: List[int]) -> int: