-
LeetCode 2389: Longest Subsequence With Limited Sum
link If we had nums sorted, we could compute its list of cumulative sums. For a query, its insert position in this list of cumulative sums would give the max length of subsequence for that query. Time: . Space: .
-
LeetCode 1385: Find the Distance Value Between Two Arrays
link For each element from arr1 try against all elements in arr2. If arr1 has elements and arr2 has elements, time: , space: . Insight: For a number in nums1, if the closest number in nums2 is and , then all other numbers in nums2 must also be more than distance away from . If…
-
LeetCode 2115: Find All Possible Recipes from Given Supplies
link We build a directed graph with vertex set as the union of recipes and ingredients. An edge represents . A topological order of recipes from this graph will give us the recipes we can make. We use in-degree approach. Say we start with supplies as the sources, therefore vertices with in-degree = 0. Then…
-
Scalability
With a given amount of resource, as QPS increases, a system will eventually hit the maximum QPS it can support. We can also look at how system’s output rate (response/sec) is related to its input rate (request/sec). A system is scalable if by adding more resources to it, we can move the max throughput level…
-
LeetCode 2246: Longest Path With Different Adjacent Characters
link If we had a simpler tree where for edge , s[u] != s[v], then our problem would be finding the length of the longest path in the tree. We can reduce the original problem to the above simpler problem by building an undirected graph having an edge only between different char’s. This is equivalent…
-
LeetCode 2392: Build a Matrix With Conditions
link The row indices of the numbers must be a permutation of row indices . Say we only have rowConditions — no conditions on the columns. We can build a directed graph with vertex set and edge set . Then, from a topological ordering of , we can find a valid permuation of row indices…
-
LeetCode 210: Course Schedule II
We build a directed graph where vertex set is the courses and an edge represents . So, input [0, 1] would be added as the edge . If the graph does not have a cycle, it is a DAG and we emit the courses in topological order. DFS Presence of back edge signals cycle in…
-
LeetCode 207: Course Schedule
link Check if the directed graph has cycle. An edge represents . In the input, [0, 1] means 1 has to be done before 0. So, we add edge in our graph. DFS Check if graph has back edge. Time and space complexity is that of DFS: . In-degree Check if all courses can be…
-
Reliability
Measured by Mean Time Between Failures (MTBF) and Mean Time To Recover (MTTR). MTBF . Note, the numerator excludes downtime which may be contributed by maintenance or repair. In other words, . It measures how often good time had been punctured by failures. We want high MTBF. MTTR . Measures how good the repair mechanism…