I actually tried solving this question but I failed severally. So I used the editorial.

I calculated the total sum from 1 to n
using the formula n * (n + 1) // 2
. Then, I found how many numbers are divisible by m
(n // m
) and used the arithmetic series formula to get their sum. Finally, I subtracted the divisible sum from the total to get the difference between non-divisible and divisible sums.
class Solution:
I solved this problem using a topological sort because the presence of a cycle would make it impossible to define valid paths. I used Kahn's algorithm to detect cycles and simultaneously calculated the most frequent color occurrences using dynamic programming. For each node, I maintained a frequency table for all 26 lowercase letters. When visiting a node's neighbor, I propagated the color frequencies accordingly. Finally, if all nodes were visited, I returned the maximum color frequency seen on any path; otherwise, I returned -1 for the cycle case.
class Solution:
def largestPathValue(self, colors: str, edges: List[List[int]]) -> int:
I use a Counter to count occurrences of each word. For non-palindromic words (like "ab" and "ba"), I find the matching reverse pairs and count the minimum between them. For palindromic words (like "gg", "cc"), I use pairs and reserve one (if available) for the center of the palindrome. Each pair contributes 4 characters (2 + 2), and the center adds 2.
class Solution:
def longestPalindrome(self, words: List[str]) -> int:
I iterate through each word using enumerate to keep track of the index. If the character x is found in the word, I add the index to the result list. Then, I return the list of indices.
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
I approached the problem by realizing I need to ensure each index i in nums is decremented at least nums[i] times. I used a difference array to simulate how many times each index is affected by the remaining queries. Then, I binary searched on the number of queries I could safely remove while still converting nums into a zero array. 486 / 824 testcases passed.
Then the editorial
I first checked if the first row or first column contains any zero since they’ll be used as markers. Then, I used the rest of the matrix to mark rows and columns that should be zeroed by updating the first row and column accordingly. After marking, I iterated again and updated the matrix cells to zero based on these markers. Finally, I handled the first row and first column separately, zeroing them out if needed.
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
To solve this, I used a difference array to track how many times each index gets decremented. For each query, I marked the start of the decrement range and subtracted at the end+1 to simulate range updates. Then I applied the accumulated decrements and ensured I never subtracted more than the current value in nums. If all values become zero after processing, I return True; otherwise, I return False.
Then I moved to the editorial
I started by sorting the sides so I could easily apply the triangle inequality check, which states that the sum of the two smaller sides must be greater than the largest side. If this condition fails, it's not a valid triangle. Otherwise, I checked the type: if all sides are equal, it's equilateral; if exactly two sides are equal, it's isosceles; and if all sides are different, it's scalene.
class Solution:
def triangleType(self, nums: List[int]) -> str: