Find the majority element in an array.
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Input: nums = [3, 2, 3]
Output: 3
Explanation: The number 3 appears 2 times, which is strictly greate…
Search a 2D matrix where each row and column is sorted.
Write an efficient algorithm that searches for a value target in an m x n integer matrix. Integers in each row are sorted, and the first integer of each row is greater than the last integer of the previous row.
Input: matrix = [[1,4],[2,5]], target = 20
Output: FALSE
Explanation:…
Clone an Undirected Graph
Clone an undirected graph.
Input: adjList = [[]]
Output: [[]]
Explanation: Creates a new, deeply cloned graph containing only one single node with zero connected neighbors.
Pacific and Atlantic Water Flow
Calculate water flow from a matrix to the Pacific and Atlantic oceans.
Input: heights = [[2,1],[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]
Explanation: All cells can flow to both oceans because water can move to adjacent cells of equal or lower height or directly off the edges.
Longest Substring with Two Distinct Characters
Find the longest substring with at most two distinct characters.
Input: s = "ccaabbb"
Output: 5
Explanation: The substring "aabbb" contains exactly two distinct characters and has a maximum length of 5.