319 challenges in the catalogue. Browse freely; sign in to open the editor and submit.
Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
0 <= word1.length, word2.length <= 500word1 and word2 consist of lowercase English letters.You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi 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
Explanation: Nodes 0, 1, and 2 form one component. Nodes 3 and 4 form a second component.
Example 2:
Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]
Output: 1
Explanation: All nodes are connected in a single component.
1 <= n <= 20001 <= edges.length <= 5000edges[i].length == 20 <= ai <= bi < nai != biGiven the root of a binary tree, return the inorder traversal of its nodes' values.
An inorder traversal visits the nodes in the following order:
Input: root = [1, null, 2, 3]
Output: [1, 3, 2]
Explanation:
The tree looks like:
1
\
2
/
3
Input: root = []
Output: []
[0, 100].-100 <= Node.val <= 100Can you implement it iteratively (without recursion)?
CREATE TABLE Delivery (
delivery_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
customer_pref_delivery_date DATE
);
If the customer's preferred delivery date is the same as the order date, then the order is called immediate; otherwise, it is called scheduled.
The first order of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has exactly one first order.
Write a SQL query to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.
Input:
Delivery table: | delivery_id | customer_id | order_date | customer_pref_delivery_date | |-------------|-------------|------------|-----------------------------| | 1 | 1 | 2019-08-01 | 2019-08-02 | | 2 | 2 | 2019-08-02 | 2019-08-02 | | 3 | 1 | 2019-08-11 | 2019-08-12 | | 4 | 3 | 2019-08-24 | 2019-08-24 | | 5 | 3 | 2019-08-21 | 2019-08-22 | | 6 | 2 | 2019-08-11 | 2019-08-13 | | 7 | 4 | 2019-08-09 | 2019-08-09 |
Output:
| immediate_percentage | |----------------------| | 50.00 |
Explanation:
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = []
Output: []
Example 3:
Input: list1 = [], list2 = [0]
Output: [0]
[0, 50].-100 <= Node.val <= 100list1 and list2 are sorted in non-decreasing order.Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Example 1:
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1]
Output: [1]
[0, 100].0 <= Node.val <= 100Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
1 <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4nums is sorted in non-decreasing order.Follow up: Squaring each element and sorting the new array is very trivial, could you find an $O(n)$ solution using a different approach?
Implement a promiseAny function that behaves like Promise.any. It takes an array of promises and returns a single promise that:
AggregateError containing all rejection reasons if ALL promises reject.This is the inverse of Promise.all — it succeeds if any promise succeeds.
function promiseAny<T>(promises: Array<T | Promise<T>>): Promise<T>
promises — An array of promises or plain values.A promise that resolves with the first fulfilled value.
const result = await promiseAny([
Promise.reject('error1'),
Promise.resolve(42),
Promise.resolve(100)
]);
// 42 (first to resolve)
try {
await promiseAny([
Promise.reject('a'),
Promise.reject('b'),
Promise.reject('c')
]);
} catch (e) {
// AggregateError with errors: ['a', 'b', 'c']
}
const result = await promiseAny([1, Promise.reject('err'), 3]);
// 1 (plain value resolves immediately)
CREATE TABLE Customers (
customer_id INT,
year INT,
revenue INT,
PRIMARY KEY (customer_id, year)
);
(customer_id, year) is the primary key. Each row contains the customer ID, the year, and the revenue for that customer in that year. Revenue can be negative (refunds/losses).
Write a SQL query to find customers whose total revenue across all years is strictly positive (greater than 0).
Return the result table with column customer_id in any order.
Input:
Customers table: | customer_id | year | revenue | |-------------|------|---------| | 1 | 2018 | 50 | | 1 | 2019 | -10 | | 2 | 2018 | -20 | | 2 | 2019 | -30 | | 3 | 2018 | 100 |
Output:
| customer_id | |-------------| | 1 | | 3 |
Explanation: Customer 1 has total revenue 50 + (-10) = 40 > 0. Customer 2 has total revenue -20 + (-30) = -50 < 0. Customer 3 has total revenue 100 > 0.
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6
Example 2:
Input: lists = []
Output: []
Example 3:
Input: lists = [[]]
Output: []
k == lists.length0 <= k <= 10^40 <= lists[i].length <= 500-10^4 <= lists[i][j] <= 10^4lists[i] is sorted in ascending order.lists[i].length will not exceed 10^4.There is a new alien language that uses the English alphabet. However, the order among the letters is unknown to you.
You are given a list of strings words from the alien language's dictionary, where the strings in words are sorted lexicographically by the rules of this new language.
Return a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language's rules. If there is no valid ordering, return "". If there are multiple valid orderings, return any of them.
Example 1:
Input: words = ["wrt","wrf","er","ett","rftt"]
Output: "wertf"
Explanation:
- From "wrt" and "wrf", we know 't' < 'f'.
- From "wrt" and "er", we know 'w' < 'e'.
- From "er" and "ett", we know 'r' < 't'.
- From "ett" and "rftt", we know 'e' < 'r'.
The valid ordering is "wertf".
Example 2:
Input: words = ["z","x"]
Output: "zx"
Explanation: From "z" to "x", we know 'z' < 'x'.
Example 3:
Input: words = ["z","x","z"]
Output: ""
Explanation: The order is invalid, so return "".
1 <= words.length <= 1001 <= words[i].length <= 100words[i] consists of only lowercase English letters.The Lowest Common Ancestor (LCA) of two nodes $u$ and $v$ in a rooted tree is the deepest node that is an ancestor of both $u$ and $v$.
While a simple recursive approach can find the LCA in $O(N)$ time per query, Binary Lifting allows us to answer each LCA query in $O(\log N)$ time after an $O(N \log N)$ preprocessing step. This is essential for large trees with many queries.
Implement a class TreeAncestor that supports:
TreeAncestor(int n, int[][] adj, int root): Preprocesses the tree with n nodes (labeled 0 to n-1) given its adjacency list.int getLCA(int u, int v): Returns the LCA of nodes u and v.Input: n = 7, adj = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], root = 0
Queries: getLCA(3, 4) returns 1, getLCA(3, 5) returns 0.
1 <= n <= 5 * 10^40 <= u, v < n5 * 10^4 calls to getLCA.