Posts

Showing posts with the label medium

2332. The Latest Time to Catch a Bus

https://leetcode.com/problems/the-latest-time-to-catch-a-bus/description/ You are given a  0-indexed  integer array  buses  of length  n , where  buses[i]  represents the departure time of the  i th  bus. You are also given a  0-indexed  integer array  passengers  of length  m , where  passengers[j]  represents the arrival time of the  j th  passenger. All bus departure times are unique. All passenger arrival times are unique. You are given an integer  capacity , which represents the  maximum  number of passengers that can get on each bus. When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at  x  minutes if you arrive at  y  minutes where  y <= x , and the bus is not full. Passengers with the  earliest  arrival times get on the bus first. More formally when a bus arrives, either: If...

323. Number of Connected Components in an Undirected Graph

Image
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/ You have a graph of  n  nodes. You are given an integer  n  and an array  edges  where  edges[i] = [a i , b i ]  indicates that there is an edge between  a i  and  b i  in the graph. Return  the number of connected components in the graph .   Example 1: Input: n = 5, edges = [[0,1],[1,2],[3,4]] Output: 2 Example 2: Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] Output: 1   Constraints: 1 <= n <= 2000 1 <= edges.length <= 5000 edges[i].length == 2 0 <= a i <= b i < n a i != b i There are no repeated edges. ---- Related problems https://sweip.blogspot.com/2020/05/200-number-of-islands.html --- Time - O(V + E) Space - O(V + E) --- ---

583. Delete Operation for Two Strings

https://leetcode.com/problems/delete-operation-for-two-strings/description/ Given two strings  word1  and  word2 , return  the minimum number of  steps  required to make   word1   and   word2   the same . In one  step , you can delete exactly one character in either string.   Example 1: Input: word1 = "sea", word2 = "eat" Output: 2 Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea". Example 2: Input: word1 = "leetcode", word2 = "etco" Output: 4   Constraints: 1 <= word1.length, word2.length <= 500 word1  and  word2  consist of only lowercase English letters. --- Time - O(M * N) Space - O(M * N) --- ---- ----