title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
Solution | is-graph-bipartite | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool check(vector<vector<int>>& graph, int src, vector<int>& color)\n {\n color[src] = 0;\n queue<int>q;\n q.push(src);\n while(!q.empty())\n {\n int u = q.front();\n q.pop();\n for(int i=0;i<graph[u].size()... | 1 | There is an **undirected** graph with `n` nodes, where each node is numbered between `0` and `n - 1`. You are given a 2D array `graph`, where `graph[u]` is an array of nodes that node `u` is adjacent to. More formally, for each `v` in `graph[u]`, there is an undirected edge between node `u` and node `v`. The graph has ... | null |
Solution | is-graph-bipartite | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool check(vector<vector<int>>& graph, int src, vector<int>& color)\n {\n color[src] = 0;\n queue<int>q;\n q.push(src);\n while(!q.empty())\n {\n int u = q.front();\n q.pop();\n for(int i=0;i<graph[u].size()... | 1 | You are given two integer arrays of the same length `nums1` and `nums2`. In one operation, you are allowed to swap `nums1[i]` with `nums2[i]`.
* For example, if `nums1 = [1,2,3,8]`, and `nums2 = [5,6,7,4]`, you can swap the element at `i = 3` to obtain `nums1 = [1,2,3,4]` and `nums2 = [5,6,7,8]`.
Return _the minimu... | null |
[Python] Checking for odd length cycles | is-graph-bipartite | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nOne characterization of bipartite graphs is graphs that do not have odd length cycles. So we can check for this by doing a BFS and if we see a node we\'ve already visited, we see how long ago it was. We do this component-wise. It\'s not... | 1 | There is an **undirected** graph with `n` nodes, where each node is numbered between `0` and `n - 1`. You are given a 2D array `graph`, where `graph[u]` is an array of nodes that node `u` is adjacent to. More formally, for each `v` in `graph[u]`, there is an undirected edge between node `u` and node `v`. The graph has ... | null |
[Python] Checking for odd length cycles | is-graph-bipartite | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nOne characterization of bipartite graphs is graphs that do not have odd length cycles. So we can check for this by doing a BFS and if we see a node we\'ve already visited, we see how long ago it was. We do this component-wise. It\'s not... | 1 | You are given two integer arrays of the same length `nums1` and `nums2`. In one operation, you are allowed to swap `nums1[i]` with `nums2[i]`.
* For example, if `nums1 = [1,2,3,8]`, and `nums2 = [5,6,7,4]`, you can swap the element at `i = 3` to obtain `nums1 = [1,2,3,4]` and `nums2 = [5,6,7,8]`.
Return _the minimu... | null |
Image Explanation🏆- [Both BFS & DFS ways] - C++/Java/Python | is-graph-bipartite | 1 | 1 | # Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Is Graph Bipartite?` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n - Link in LeetCode Profile\n`Is Graph Bipartite?` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n -> bool:\n color = [-1] * len(graph)\n for v in range(len(graph)):\n if color[v] == -1:\n stack = [v]\n color[v] = 0\n while stack:\n node = stack.po... | 1 | There is an **undirected** graph with `n` nodes, where each node is numbered between `0` and `n - 1`. You are given a 2D array `graph`, where `graph[u]` is an array of nodes that node `u` is adjacent to. More formally, for each `v` in `graph[u]`, there is an undirected edge between node `u` and node `v`. The graph has ... | null |
Easiest Python Solution | is-graph-bipartite | 0 | 1 | \n# Code\n```\nclass Solution:\n def isBipartite(self, graph: List[List[int]]) -> bool:\n color = [-1] * len(graph)\n for v in range(len(graph)):\n if color[v] == -1:\n stack = [v]\n color[v] = 0\n while stack:\n node = stack.po... | 1 | You are given two integer arrays of the same length `nums1` and `nums2`. In one operation, you are allowed to swap `nums1[i]` with `nums2[i]`.
* For example, if `nums1 = [1,2,3,8]`, and `nums2 = [5,6,7,4]`, you can swap the element at `i = 3` to obtain `nums1 = [1,2,3,4]` and `nums2 = [5,6,7,8]`.
Return _the minimu... | null |
Solution | k-th-smallest-prime-fraction | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n double cur_large;\n double ans=99999;\n int ans_row,ans_col;\n int cur_row,cur_col;\n int smaller_count(double mid,vector<int>& arr)\n {\n int row=0, col=0;\n int counter=0;\n cur_large=0;\n for(int i=0;i<arr.size();i++)\n {\n ... | 2 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
Solution | k-th-smallest-prime-fraction | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n double cur_large;\n double ans=99999;\n int ans_row,ans_col;\n int cur_row,cur_col;\n int smaller_count(double mid,vector<int>& arr)\n {\n int row=0, col=0;\n int counter=0;\n cur_large=0;\n for(int i=0;i<arr.size();i++)\n {\n ... | 2 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
easy.......python solution | k-th-smallest-prime-fraction | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
easy.......python solution | k-th-smallest-prime-fraction | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
[Explained] Easiest Python Solution | k-th-smallest-prime-fraction | 0 | 1 | # [Explained] Easiest Python Solution\n\nNote: It is not the most optimal solution, but it is easy and beginner friendly approach.\n```\nclass Solution:\n\n\tdef kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n\t\tif len(arr) > 2:\n\t\t\tres = [] # list for storing the list: [prime fraction of ar... | 2 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
[Explained] Easiest Python Solution | k-th-smallest-prime-fraction | 0 | 1 | # [Explained] Easiest Python Solution\n\nNote: It is not the most optimal solution, but it is easy and beginner friendly approach.\n```\nclass Solution:\n\n\tdef kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n\t\tif len(arr) > 2:\n\t\t\tres = [] # list for storing the list: [prime fraction of ar... | 2 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
[Python 3] MaxHeap | k-th-smallest-prime-fraction | 0 | 1 | ```\nclass Solution:\n def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n res = []\n \n for i in range(len(arr)):\n for j in range(i + 1, len(arr)):\n heappush(res, (-(arr[i] / arr[j]), arr[i], arr[j]))\n \n if len(r... | 5 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
[Python 3] MaxHeap | k-th-smallest-prime-fraction | 0 | 1 | ```\nclass Solution:\n def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n res = []\n \n for i in range(len(arr)):\n for j in range(i + 1, len(arr)):\n heappush(res, (-(arr[i] / arr[j]), arr[i], arr[j]))\n \n if len(r... | 5 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
Binary Search + Sliding Window (Beats 91.21%) | k-th-smallest-prime-fraction | 0 | 1 | # Intuition\n\nBinary search the possible range of fraction `[0, 1]`. For each possible value `v`, we use sliding window (two pointers) to count the number (`cnt`) of fractions < v, as well as record the largest fraction `l/r` that is < v.\n\nDuring binary search, when `cnt == k` we can return the result `[l, r]`.\n\n#... | 4 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
Binary Search + Sliding Window (Beats 91.21%) | k-th-smallest-prime-fraction | 0 | 1 | # Intuition\n\nBinary search the possible range of fraction `[0, 1]`. For each possible value `v`, we use sliding window (two pointers) to count the number (`cnt`) of fractions < v, as well as record the largest fraction `l/r` that is < v.\n\nDuring binary search, when `cnt == k` we can return the result `[l, r]`.\n\n#... | 4 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
Python3 | Solved Using Sorting and Trying Every Possible Pairings | k-th-smallest-prime-fraction | 0 | 1 | ```\nclass Solution:\n #Time-Complexity: O(n^2 + n^2log(n^2)) -> O(n^2*log(n^2)) ->O(n^2*log(n))\n #Space-Complexity: O(n^2)\n def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n array = []\n for i in range(0, len(arr)-1):\n numerator = arr[i]\n for j ... | 0 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
Python3 | Solved Using Sorting and Trying Every Possible Pairings | k-th-smallest-prime-fraction | 0 | 1 | ```\nclass Solution:\n #Time-Complexity: O(n^2 + n^2log(n^2)) -> O(n^2*log(n^2)) ->O(n^2*log(n))\n #Space-Complexity: O(n^2)\n def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n array = []\n for i in range(0, len(arr)-1):\n numerator = arr[i]\n for j ... | 0 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
Python3 easy solution (beats 78.8%) | k-th-smallest-prime-fraction | 0 | 1 | # Code\n```\nclass Solution:\n def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n consider = []\n n = len(arr)\n\n for i in range(n):\n for j in range(max(n-k,i+1),n):\n consider.append((arr[i],arr[j]))\n\n consider.sort(key=lambda x: x[0]... | 1 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
Python3 easy solution (beats 78.8%) | k-th-smallest-prime-fraction | 0 | 1 | # Code\n```\nclass Solution:\n def kthSmallestPrimeFraction(self, arr: List[int], k: int) -> List[int]:\n consider = []\n n = len(arr)\n\n for i in range(n):\n for j in range(max(n-k,i+1),n):\n consider.append((arr[i],arr[j]))\n\n consider.sort(key=lambda x: x[0]... | 1 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
Python with heap. Don't need to put all N^2 pairs into heap | k-th-smallest-prime-fraction | 0 | 1 | # Intuition\nPut at most len(arr)-1 pairs into heap. Put a new pair in for every pair popped.\n\nE.g \n[1,3,4,5,7,11,13,17] (8 numbers)\nk = 2\n\nDo you really need to put all 64 pairs in????? NOO\nk = 2 so its either [1, 17], [3, 17] or [1, 13]\nSo you put [1,17], [3, 17] into heap.\n\nTake [1,17] from heap, put [1, 1... | 0 | You are given a sorted integer array `arr` containing `1` and **prime** numbers, where all the integers of `arr` are unique. You are also given an integer `k`.
For every `i` and `j` where `0 <= i < j < arr.length`, we consider the fraction `arr[i] / arr[j]`.
Return _the_ `kth` _smallest fraction considered_. Return y... | null |
Python with heap. Don't need to put all N^2 pairs into heap | k-th-smallest-prime-fraction | 0 | 1 | # Intuition\nPut at most len(arr)-1 pairs into heap. Put a new pair in for every pair popped.\n\nE.g \n[1,3,4,5,7,11,13,17] (8 numbers)\nk = 2\n\nDo you really need to put all 64 pairs in????? NOO\nk = 2 so its either [1, 17], [3, 17] or [1, 13]\nSo you put [1,17], [3, 17] into heap.\n\nTake [1,17] from heap, put [1, 1... | 0 | There is a directed graph of `n` nodes with each node labeled from `0` to `n - 1`. The graph is represented by a **0-indexed** 2D integer array `graph` where `graph[i]` is an integer array of nodes adjacent to node `i`, meaning there is an edge from node `i` to each node in `graph[i]`.
A node is a **terminal node** if... | null |
Solution | cheapest-flights-within-k-stops | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {\n vector<pair<int,int>>adj[n];\n int edges = flights.size();\n for(int i=0; i<edges; i++) {\n int u = flights[i][0];\n int v = flights[i][1];\n ... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
Solution | cheapest-flights-within-k-stops | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {\n vector<pair<int,int>>adj[n];\n int edges = flights.size();\n for(int i=0; i<edges; i++) {\n int u = flights[i][0];\n int v = flights[i][1];\n ... | 1 | You are given an `m x n` binary `grid`, where each `1` represents a brick and `0` represents an empty space. A brick is **stable** if:
* It is directly connected to the top of the grid, or
* At least one other brick in its four adjacent cells is **stable**.
You are also given an array `hits`, which is a sequence ... | null |
SIMPLE PYTHON SOLUTION USING HEAP SORT | cheapest-flights-within-k-stops | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(KLOGN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N^2)\n<!-- Add your space complexity here, e... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
SIMPLE PYTHON SOLUTION USING HEAP SORT | cheapest-flights-within-k-stops | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(KLOGN)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N^2)\n<!-- Add your space complexity here, e... | 1 | You are given an `m x n` binary `grid`, where each `1` represents a brick and `0` represents an empty space. A brick is **stable** if:
* It is directly connected to the top of the grid, or
* At least one other brick in its four adjacent cells is **stable**.
You are also given an array `hits`, which is a sequence ... | null |
Python BFS solution | cheapest-flights-within-k-stops | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nK stops means we can do K+1 times bfs.\nKeep track the shortest price to each node, if there are cheaper ways to reach the node, update the cost.\n\n# Code\n```\nclass Solution:\n def findCheapestPrice(self, n: int, flights: List[List[... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
Python BFS solution | cheapest-flights-within-k-stops | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nK stops means we can do K+1 times bfs.\nKeep track the shortest price to each node, if there are cheaper ways to reach the node, update the cost.\n\n# Code\n```\nclass Solution:\n def findCheapestPrice(self, n: int, flights: List[List[... | 1 | You are given an `m x n` binary `grid`, where each `1` represents a brick and `0` represents an empty space. A brick is **stable** if:
* It is directly connected to the top of the grid, or
* At least one other brick in its four adjacent cells is **stable**.
You are also given an array `hits`, which is a sequence ... | null |
Intuitive Heap Approach | cheapest-flights-within-k-stops | 0 | 1 | Maintain a heap to store all the upcoming nodes to visit (currentPrice, currentNode, currentSteps). Steps is to determine the number of stops so far and this number cannot exceed k. When you visit a node, mark it as visited and push all of its neighbors into our current heap. If you meet the destination, update the res... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
Intuitive Heap Approach | cheapest-flights-within-k-stops | 0 | 1 | Maintain a heap to store all the upcoming nodes to visit (currentPrice, currentNode, currentSteps). Steps is to determine the number of stops so far and this number cannot exceed k. When you visit a node, mark it as visited and push all of its neighbors into our current heap. If you meet the destination, update the res... | 1 | You are given an `m x n` binary `grid`, where each `1` represents a brick and `0` represents an empty space. A brick is **stable** if:
* It is directly connected to the top of the grid, or
* At least one other brick in its four adjacent cells is **stable**.
You are also given an array `hits`, which is a sequence ... | null |
My simply python BFS solution. | cheapest-flights-within-k-stops | 0 | 1 | \n# Complexity\n- Time complexity: $$O(n*k)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findCheapestPrice(self, n, flights, src, dst, k):\n adj = [[] for _ in range(n)]\n ... | 1 | There are `n` cities connected by some number of flights. You are given an array `flights` where `flights[i] = [fromi, toi, pricei]` indicates that there is a flight from city `fromi` to city `toi` with cost `pricei`.
You are also given three integers `src`, `dst`, and `k`, return _**the cheapest price** from_ `src` _... | Perform a breadth-first-search, where the nodes are the puzzle boards and edges are if two puzzle boards can be transformed into one another with one move. |
My simply python BFS solution. | cheapest-flights-within-k-stops | 0 | 1 | \n# Complexity\n- Time complexity: $$O(n*k)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def findCheapestPrice(self, n, flights, src, dst, k):\n adj = [[] for _ in range(n)]\n ... | 1 | You are given an `m x n` binary `grid`, where each `1` represents a brick and `0` represents an empty space. A brick is **stable** if:
* It is directly connected to the top of the grid, or
* At least one other brick in its four adjacent cells is **stable**.
You are also given an array `hits`, which is a sequence ... | null |
Easy Understandable Python Code | rotated-digits | 0 | 1 | \n\n# Approach\n- Valid if N contains ATLEAST ```ONE 2, 5, 6, 9```\n AND ``` NO 3, 4 or 7```\n\n\n# Code\n```\nclass Solution:\n def rotatedDigits(self, n: int) -> int:\n invalid = [3,4,7]\n good = 0\n check = False\n\n for i in range(1,n+1):\n j = i\n \n whi... | 4 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
Easy Understandable Python Code | rotated-digits | 0 | 1 | \n\n# Approach\n- Valid if N contains ATLEAST ```ONE 2, 5, 6, 9```\n AND ``` NO 3, 4 or 7```\n\n\n# Code\n```\nclass Solution:\n def rotatedDigits(self, n: int) -> int:\n invalid = [3,4,7]\n good = 0\n check = False\n\n for i in range(1,n+1):\n j = i\n \n whi... | 4 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
O(nLogbase10n):Time|O(1):Space|Dict | rotated-digits | 0 | 1 | # Intuition\n## PLEASE UPVOTE \nThe goal of the "rotatedDigits" function is to count how many numbers in the range from 1 to n are considered "good" after a specific type of digit rotation. A number is considered "good" if, after rotating its digits, it becomes a different number. Rotating a number means replacing its ... | 1 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
O(nLogbase10n):Time|O(1):Space|Dict | rotated-digits | 0 | 1 | # Intuition\n## PLEASE UPVOTE \nThe goal of the "rotatedDigits" function is to count how many numbers in the range from 1 to n are considered "good" after a specific type of digit rotation. A number is considered "good" if, after rotating its digits, it becomes a different number. Rotating a number means replacing its ... | 1 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
Solution | rotated-digits | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool good(int x)\n {\n bool ans=false;\n int y;\n while(x>0)\n {\n y=x%10;\n switch(y)\n {\n case 0:\n case 1:\n case 8:\n break;\n cas... | 2 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
Solution | rotated-digits | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool good(int x)\n {\n bool ans=false;\n int y;\n while(x>0)\n {\n y=x%10;\n switch(y)\n {\n case 0:\n case 1:\n case 8:\n break;\n cas... | 2 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
Easy understand solution python O(1) space | rotated-digits | 0 | 1 | ```\ndef rotatedDigits(self, N: int) -> int:\n count = 0\n for d in range(1, N+1):\n d = str(d)\n if \'3\' in d or \'4\' in d or \'7\' in d:\n continue\n if \'2\' in d or \'5\' in d or \'6\' in d or \'9\' in d:\n count+=1\n return count... | 28 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
Easy understand solution python O(1) space | rotated-digits | 0 | 1 | ```\ndef rotatedDigits(self, N: int) -> int:\n count = 0\n for d in range(1, N+1):\n d = str(d)\n if \'3\' in d or \'4\' in d or \'7\' in d:\n continue\n if \'2\' in d or \'5\' in d or \'6\' in d or \'9\' in d:\n count+=1\n return count... | 28 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
Typical DIGIT DP | rotated-digits | 0 | 1 | If you know digit dp then this is trivial\n\nIf you contain a 3 or 4 or 7 then this number cannot be good\n\nIf you contain 2 or 5 or 6 or 9 then this number is automatically good provided that it doesnt contain any 3 or 4 or 7\n\n0,1,8 do nothing. \n\nStore pareamter good, good = True means that the number is going to... | 1 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
Typical DIGIT DP | rotated-digits | 0 | 1 | If you know digit dp then this is trivial\n\nIf you contain a 3 or 4 or 7 then this number cannot be good\n\nIf you contain 2 or 5 or 6 or 9 then this number is automatically good provided that it doesnt contain any 3 or 4 or 7\n\n0,1,8 do nothing. \n\nStore pareamter good, good = True means that the number is going to... | 1 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
Intuitive exhaustive case solution | rotated-digits | 0 | 1 | # Code\n```\nclass Solution:\n def rotatedDigits(self, n: int) -> int:\n invalid = [3,4,7]\n good = 0\n check = False\n\n for i in range(1,n+1):\n j = i\n \n while j != 0:\n d = j % 10\n \n if d == 2 or d == 5 or d ... | 0 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
Intuitive exhaustive case solution | rotated-digits | 0 | 1 | # Code\n```\nclass Solution:\n def rotatedDigits(self, n: int) -> int:\n invalid = [3,4,7]\n good = 0\n check = False\n\n for i in range(1,n+1):\n j = i\n \n while j != 0:\n d = j % 10\n \n if d == 2 or d == 5 or d ... | 0 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
Easy understandable solution ||python3 | rotated-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
Easy understandable solution ||python3 | rotated-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
788: Solution with step by step explanation | rotated-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n def isGoodNumber(num: int) -> bool:\n```\n\nInside rotatedDigits, we define a helper function isGoodNumber that takes an integer num and returns a boolean. This function will be used to determine if a number is ... | 0 | An integer `x` is a **good** if after rotating each digit individually by 180 degrees, we get a valid number that is different from `x`. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. For example:
* `0`, `1`, and `8` rotate to themsel... | Use a binary search. We'll binary search the monotone function "possible(D) = can we use K or less gas stations to ensure each adjacent distance between gas stations is at most D?" |
788: Solution with step by step explanation | rotated-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n def isGoodNumber(num: int) -> bool:\n```\n\nInside rotatedDigits, we define a helper function isGoodNumber that takes an integer num and returns a boolean. This function will be used to determine if a number is ... | 0 | International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
* `'a'` maps to `".- "`,
* `'b'` maps to `"-... "`,
* `'c'` maps to `"-.-. "`, and so on.
For convenience, the full table for the `26` letters of the English alphabet is given below:
\[ ... | null |
Solution | escape-the-ghosts | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {\n int my_distance_from_target = abs(target[0]) + abs(target[1]);\n for( auto &g : ghosts )\n {\n if( my_distance_from_target >= ( abs(g[0] - target[0]) + abs(g[1] - target[1])... | 1 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
Solution | escape-the-ghosts | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {\n int my_distance_from_target = abs(target[0]) + abs(target[1]);\n for( auto &g : ghosts )\n {\n if( my_distance_from_target >= ( abs(g[0] - target[0]) + abs(g[1] - target[1])... | 1 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
Python Solution for reference | escape-the-ghosts | 0 | 1 | \n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n\n # If any of the ghosts reaches the targert before \n # you, then you lose\n def time(x,y):\n a,b = target\n return max(x,a)-min(x,a) + max(y,b)-min(y,b)\n \n ... | 1 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
Python Solution for reference | escape-the-ghosts | 0 | 1 | \n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n\n # If any of the ghosts reaches the targert before \n # you, then you lose\n def time(x,y):\n a,b = target\n return max(x,a)-min(x,a) + max(y,b)-min(y,b)\n \n ... | 1 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
Python3 | escape-the-ghosts | 0 | 1 | \n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n a=abs(target[0])+abs(target[1])\n for i ,j in ghosts:\n b=abs(target[0]-i)+abs(target[1]-j)\n if b<=a:\n return False\n return True\n``` | 1 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
Python3 | escape-the-ghosts | 0 | 1 | \n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n a=abs(target[0])+abs(target[1])\n for i ,j in ghosts:\n b=abs(target[0]-i)+abs(target[1]-j)\n if b<=a:\n return False\n return True\n``` | 1 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
Python 3 || 2 lines, w/ a brief explanation || T/M: 86% / 72% | escape-the-ghosts | 0 | 1 | The problem reduces to whether any ghost`g`has a *manhattan-distance* to`target`that is less than the *manhattan-distance* to`target`from`(0,0)`.\n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n \n dist = lambda x : abs(x[0] - target[0]) + abs(x[1] -... | 4 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
Python 3 || 2 lines, w/ a brief explanation || T/M: 86% / 72% | escape-the-ghosts | 0 | 1 | The problem reduces to whether any ghost`g`has a *manhattan-distance* to`target`that is less than the *manhattan-distance* to`target`from`(0,0)`.\n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n \n dist = lambda x : abs(x[0] - target[0]) + abs(x[1] -... | 4 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
✅ Mathematics is the ultimate savior. | escape-the-ghosts | 0 | 1 | Calculate the `Manhattan distance` between `you` and `all the ghosts` to the `target`. If any of the ghosts are `closer` to the `target` than you are, `you lose`. Otherwise, `you win`.\n\n\nI came across about 3 to 4 problems on LeetCode that involved the concept of "Manhattan distance" in various scenarios. It\'s good... | 0 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
✅ Mathematics is the ultimate savior. | escape-the-ghosts | 0 | 1 | Calculate the `Manhattan distance` between `you` and `all the ghosts` to the `target`. If any of the ghosts are `closer` to the `target` than you are, `you lose`. Otherwise, `you win`.\n\n\nI came across about 3 to 4 problems on LeetCode that involved the concept of "Manhattan distance" in various scenarios. It\'s good... | 0 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
789: Beats 100.00%, Solution with step by step explanation | escape-the-ghosts | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nplayer_distance = abs(target[0]) + abs(target[1])\n```\nWe begin by calculating the distance the player needs to cover to reach the target.\nWe use the Manhattan distance, which is a way to compute the distance between ... | 0 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
789: Beats 100.00%, Solution with step by step explanation | escape-the-ghosts | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nplayer_distance = abs(target[0]) + abs(target[1])\n```\nWe begin by calculating the distance the player needs to cover to reach the target.\nWe use the Manhattan distance, which is a way to compute the distance between ... | 0 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
Python Beats 100% | escape-the-ghosts | 0 | 1 | \n def calculate_distance(self, pointa, pointb):\n return abs(pointa[0] - pointb[0]) + abs(pointa[1] - pointb[1]) \n\n\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n my_distance = self.calculate_distance(target, [0,0])\n ghost_distances = [self.calculate_dista... | 0 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
Python Beats 100% | escape-the-ghosts | 0 | 1 | \n def calculate_distance(self, pointa, pointb):\n return abs(pointa[0] - pointb[0]) + abs(pointa[1] - pointb[1]) \n\n\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n my_distance = self.calculate_distance(target, [0,0])\n ghost_distances = [self.calculate_dista... | 0 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
Ye old one liner | escape-the-ghosts | 0 | 1 | # Intuition\nOptimal strategy for ghosts is to always go straight for the target\n\n# Code\n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n return sum(abs(i) for i in target) < min(sum(abs(x-y) for x,y in zip(target, i)) for i in ghosts)\n``` | 0 | You are playing a simplified PAC-MAN game on an infinite 2-D grid. You start at the point `[0, 0]`, and you are given a destination point `target = [xtarget, ytarget]` that you are trying to get to. There are several ghosts on the map with their starting positions given as a 2D array `ghosts`, where `ghosts[i] = [xi, y... | null |
Ye old one liner | escape-the-ghosts | 0 | 1 | # Intuition\nOptimal strategy for ghosts is to always go straight for the target\n\n# Code\n```\nclass Solution:\n def escapeGhosts(self, ghosts: List[List[int]], target: List[int]) -> bool:\n return sum(abs(i) for i in target) < min(sum(abs(x-y) for x,y in zip(target, i)) for i in ghosts)\n``` | 0 | You are given an integer array `nums`.
You should move each element of `nums` into one of the two arrays `A` and `B` such that `A` and `B` are non-empty, and `average(A) == average(B)`.
Return `true` if it is possible to achieve that and `false` otherwise.
**Note** that for an array `arr`, `average(arr)` is the sum ... | null |
Python3 | Recursion + Memoization | domino-and-tromino-tiling | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nMOD = (10**9) + 7\nclass Solution:\n def numTilings(self, n: int) -> int:\n memo = {}\n\n def state(t1... | 1 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
Python3 | Recursion + Memoization | domino-and-tromino-tiling | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nMOD = (10**9) + 7\nclass Solution:\n def numTilings(self, n: int) -> int:\n memo = {}\n\n def state(t1... | 1 | You are given a string `s` of lowercase English letters and an array `widths` denoting **how many pixels wide** each lowercase English letter is. Specifically, `widths[0]` is the width of `'a'`, `widths[1]` is the width of `'b'`, and so on.
You are trying to write `s` across several lines, where **each line is no long... | null |
✅ [Python/JAVA/C/C++] DP || Image Visualized Explanation || 100% Faster || O(N) | domino-and-tromino-tiling | 1 | 1 | * dp[i] denotes the number of ways to tile an 2 * (i + 1) board, note that dp is 0-indexed.\n\t* Intuitively, dp[0] = 1 and dp[1] = 2\n* dpa[i] denotes the number of ways to tile an 2 * i board and 1 more square left below(or above symmetrically).\n\t* Intuitively, dpa[0] = 0 and dpa[1] = 1\n\t* I just explained the ca... | 161 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
✅ [Python/JAVA/C/C++] DP || Image Visualized Explanation || 100% Faster || O(N) | domino-and-tromino-tiling | 1 | 1 | * dp[i] denotes the number of ways to tile an 2 * (i + 1) board, note that dp is 0-indexed.\n\t* Intuitively, dp[0] = 1 and dp[1] = 2\n* dpa[i] denotes the number of ways to tile an 2 * i board and 1 more square left below(or above symmetrically).\n\t* Intuitively, dpa[0] = 0 and dpa[1] = 1\n\t* I just explained the ca... | 161 | You are given a string `s` of lowercase English letters and an array `widths` denoting **how many pixels wide** each lowercase English letter is. Specifically, `widths[0]` is the width of `'a'`, `widths[1]` is the width of `'b'`, and so on.
You are trying to write `s` across several lines, where **each line is no long... | null |
Python recursive-recursion easy to understand with explanation. | domino-and-tromino-tiling | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRecursion\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nYou start from left to right.\nThe helper function takes 2 arguments , i for the tile number and half to say whether i is just half space(half =true) or i i... | 1 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
Python recursive-recursion easy to understand with explanation. | domino-and-tromino-tiling | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRecursion\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nYou start from left to right.\nThe helper function takes 2 arguments , i for the tile number and half to say whether i is just half space(half =true) or i i... | 1 | You are given a string `s` of lowercase English letters and an array `widths` denoting **how many pixels wide** each lowercase English letter is. Specifically, `widths[0]` is the width of `'a'`, `widths[1]` is the width of `'b'`, and so on.
You are trying to write `s` across several lines, where **each line is no long... | null |
27ms 98.89% 13.8mb 99.21% Python explained! | domino-and-tromino-tiling | 0 | 1 | # Intuition\nFirst look at the numbers really good and long.\nSearch patterns and the according offsets to reach our values...\nLike sum, fibonacci etc.\n|i | n | sum|off| \n|--|--:|--:|-:|\n1|1|1|+1\n2|2|3|+2\n3|5|8|+3\n4|11|19|+5\n5|24|43|+10\n6|53|96|+21\n7|117|213|+45\n8|258|471|+98\n9|569|1040|+215\n10|1255|\n\nFi... | 32 | You have two types of tiles: a `2 x 1` domino shape and a tromino shape. You may rotate these shapes.
Given an integer n, return _the number of ways to tile an_ `2 x n` _board_. Since the answer may be very large, return it **modulo** `109 + 7`.
In a tiling, every square must be covered by a tile. Two tilings are dif... | Where can the 0 be placed in an ideal permutation? What about the 1? |
27ms 98.89% 13.8mb 99.21% Python explained! | domino-and-tromino-tiling | 0 | 1 | # Intuition\nFirst look at the numbers really good and long.\nSearch patterns and the according offsets to reach our values...\nLike sum, fibonacci etc.\n|i | n | sum|off| \n|--|--:|--:|-:|\n1|1|1|+1\n2|2|3|+2\n3|5|8|+3\n4|11|19|+5\n5|24|43|+10\n6|53|96|+21\n7|117|213|+45\n8|258|471|+98\n9|569|1040|+215\n10|1255|\n\nFi... | 32 | You are given a string `s` of lowercase English letters and an array `widths` denoting **how many pixels wide** each lowercase English letter is. Specifically, `widths[0]` is the width of `'a'`, `widths[1]` is the width of `'b'`, and so on.
You are trying to write `s` across several lines, where **each line is no long... | null |
Solution | custom-sort-string | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n string customSortString(string order, string s) {\n unordered_map<char,int>um;\n for(auto j:s)\n um[j]++;\n string ans;\n for(auto it:order)\n {\n for(int i=0;i<um[it];i++)\n ans+=it;\n um[it]=0;\n ... | 1 | You are given two strings order and s. All the characters of `order` are **unique** and were sorted in some custom order previously.
Permute the characters of `s` so that they match the order that `order` was sorted. More specifically, if a character `x` occurs before a character `y` in `order`, then `x` should occur ... | Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right. |
Solution | custom-sort-string | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n string customSortString(string order, string s) {\n unordered_map<char,int>um;\n for(auto j:s)\n um[j]++;\n string ans;\n for(auto it:order)\n {\n for(int i=0;i<um[it];i++)\n ans+=it;\n um[it]=0;\n ... | 1 | There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
A city's **skyline** is t... | null |
User Friendly soln | custom-sort-string | 0 | 1 | # Intuition\n\nBeats 99% time and space \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Hashmap\n\n# Complexity\n- Time complexity:\n- O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexi... | 1 | You are given two strings order and s. All the characters of `order` are **unique** and were sorted in some custom order previously.
Permute the characters of `s` so that they match the order that `order` was sorted. More specifically, if a character `x` occurs before a character `y` in `order`, then `x` should occur ... | Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right. |
User Friendly soln | custom-sort-string | 0 | 1 | # Intuition\n\nBeats 99% time and space \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Hashmap\n\n# Complexity\n- Time complexity:\n- O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexi... | 1 | There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
A city's **skyline** is t... | null |
Python 1 liner | custom-sort-string | 0 | 1 | # Code\n```\nclass Solution:\n def customSortString(self, order: str, s: str) -> str:\n return "".join(sorted(s, key= lambda x: order.find(x)))\n``` | 1 | You are given two strings order and s. All the characters of `order` are **unique** and were sorted in some custom order previously.
Permute the characters of `s` so that they match the order that `order` was sorted. More specifically, if a character `x` occurs before a character `y` in `order`, then `x` should occur ... | Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right. |
Python 1 liner | custom-sort-string | 0 | 1 | # Code\n```\nclass Solution:\n def customSortString(self, order: str, s: str) -> str:\n return "".join(sorted(s, key= lambda x: order.find(x)))\n``` | 1 | There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
A city's **skyline** is t... | null |
791: Beats 95.81%, Solution with step by step explanation | custom-sort-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe use Python\'s Counter from the collections module to achieve this. It creates a dictionary where keys are characters of s and values are their corresponding frequencies.\n\n```\ns_count = Counter(s)\n```\nWe utilize a lis... | 0 | You are given two strings order and s. All the characters of `order` are **unique** and were sorted in some custom order previously.
Permute the characters of `s` so that they match the order that `order` was sorted. More specifically, if a character `x` occurs before a character `y` in `order`, then `x` should occur ... | Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right. |
791: Beats 95.81%, Solution with step by step explanation | custom-sort-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe use Python\'s Counter from the collections module to achieve this. It creates a dictionary where keys are characters of s and values are their corresponding frequencies.\n\n```\ns_count = Counter(s)\n```\nWe utilize a lis... | 0 | There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
A city's **skyline** is t... | null |
Python Easy Solution || Sorting || Hashmap | custom-sort-string | 0 | 1 | # Code\n```\nclass Solution:\n def customSortString(self, order: str, s: str) -> str:\n dic={}\n dic1={}\n rem=""\n lis=[]\n for i in range(len(order)):\n dic[order[i]]=i\n for i in dic:\n dic1[dic[i]]=i\n for i in s:\n if i in dic:\n ... | 2 | You are given two strings order and s. All the characters of `order` are **unique** and were sorted in some custom order previously.
Permute the characters of `s` so that they match the order that `order` was sorted. More specifically, if a character `x` occurs before a character `y` in `order`, then `x` should occur ... | Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right. |
Python Easy Solution || Sorting || Hashmap | custom-sort-string | 0 | 1 | # Code\n```\nclass Solution:\n def customSortString(self, order: str, s: str) -> str:\n dic={}\n dic1={}\n rem=""\n lis=[]\n for i in range(len(order)):\n dic[order[i]]=i\n for i in dic:\n dic1[dic[i]]=i\n for i in s:\n if i in dic:\n ... | 2 | There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
A city's **skyline** is t... | null |
✅ Simple yet interview friendly | Faster than 99.97% | Custom Sorting in Python | custom-sort-string | 0 | 1 | **Logic for Ranking**\n* There can only be 26 characters in the alphabet so if we can create a rank array of size 26 initially all values as 26\n ```rank = [26, 26, . . . . , 26] ```\n \n* We can rank all characters by giving them 0-25 Order and all characters that are not in Order string will have their value as 26 so... | 8 | You are given two strings order and s. All the characters of `order` are **unique** and were sorted in some custom order previously.
Permute the characters of `s` so that they match the order that `order` was sorted. More specifically, if a character `x` occurs before a character `y` in `order`, then `x` should occur ... | Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right. |
✅ Simple yet interview friendly | Faster than 99.97% | Custom Sorting in Python | custom-sort-string | 0 | 1 | **Logic for Ranking**\n* There can only be 26 characters in the alphabet so if we can create a rank array of size 26 initially all values as 26\n ```rank = [26, 26, . . . . , 26] ```\n \n* We can rank all characters by giving them 0-25 Order and all characters that are not in Order string will have their value as 26 so... | 8 | There is a city composed of `n x n` blocks, where each block contains a single building shaped like a vertical square prism. You are given a **0-indexed** `n x n` integer matrix `grid` where `grid[r][c]` represents the **height** of the building located in the block at row `r` and column `c`.
A city's **skyline** is t... | null |
PYTHON|| FASTER THAN 99.8%|| SC 99%|| EASY | number-of-matching-subsequences | 0 | 1 | ```\n def sub(st):\n pos = -1\n for char in st:\n pos = s.find(char, pos+1)\n if pos == -1: return False\n return True\n return sum(map(sub, words))\n\t```\nIF THIS HELP U KINDLY UPVOTE THIS TO HELP OTHERS TO GET THIS SOLUTION\nIF U DONT GET I... | 15 | Given a string `s` and an array of strings `words`, return _the number of_ `words[i]` _that is a subsequence of_ `s`.
A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
* For exa... | null |
PYTHON|| FASTER THAN 99.8%|| SC 99%|| EASY | number-of-matching-subsequences | 0 | 1 | ```\n def sub(st):\n pos = -1\n for char in st:\n pos = s.find(char, pos+1)\n if pos == -1: return False\n return True\n return sum(map(sub, words))\n\t```\nIF THIS HELP U KINDLY UPVOTE THIS TO HELP OTHERS TO GET THIS SOLUTION\nIF U DONT GET I... | 15 | There are two types of soup: **type A** and **type B**. Initially, we have `n` ml of each type of soup. There are four kinds of operations:
1. Serve `100` ml of **soup A** and `0` ml of **soup B**,
2. Serve `75` ml of **soup A** and `25` ml of **soup B**,
3. Serve `50` ml of **soup A** and `50` ml of **soup B**, an... | null |
📌 Easy-Approach || Well-explained || 95% faster 🐍 | number-of-matching-subsequences | 0 | 1 | ## IDEA:\n\n\uD83D\uDC49 finding the index of each character in "s" then virtually cutting "s" => from index+1 to last.\n\uD83D\uDC49 repeating above point for all the characters in word, if any ch not found in s then it will return -1.\n\ne.g.-\n\ts="abgheaf"\n\tw=["gef", "gefk"]\n\tindex = -1\n* \tfirst found index o... | 29 | Given a string `s` and an array of strings `words`, return _the number of_ `words[i]` _that is a subsequence of_ `s`.
A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
* For exa... | null |
📌 Easy-Approach || Well-explained || 95% faster 🐍 | number-of-matching-subsequences | 0 | 1 | ## IDEA:\n\n\uD83D\uDC49 finding the index of each character in "s" then virtually cutting "s" => from index+1 to last.\n\uD83D\uDC49 repeating above point for all the characters in word, if any ch not found in s then it will return -1.\n\ne.g.-\n\ts="abgheaf"\n\tw=["gef", "gefk"]\n\tindex = -1\n* \tfirst found index o... | 29 | There are two types of soup: **type A** and **type B**. Initially, we have `n` ml of each type of soup. There are four kinds of operations:
1. Serve `100` ml of **soup A** and `0` ml of **soup B**,
2. Serve `75` ml of **soup A** and `25` ml of **soup B**,
3. Serve `50` ml of **soup A** and `50` ml of **soup B**, an... | null |
Solution | preimage-size-of-factorial-zeroes-function | 1 | 1 | ```C++ []\nclass Solution {\n#include <random>\npublic:\n int preimageSizeFZF(int k) {\n vector<long long> magicNum;\n long long Num = 0;\n long long power_5 = 5;\n while(Num < 1000000000){\n power_5 = power_5 * 5;\n Num = (power_5 - 1) / 4;\n magicNum.pus... | 1 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
Solution | preimage-size-of-factorial-zeroes-function | 1 | 1 | ```C++ []\nclass Solution {\n#include <random>\npublic:\n int preimageSizeFZF(int k) {\n vector<long long> magicNum;\n long long Num = 0;\n long long power_5 = 5;\n while(Num < 1000000000){\n power_5 = power_5 * 5;\n Num = (power_5 - 1) / 4;\n magicNum.pus... | 1 | Sometimes people repeat letters to represent extra feeling. For example:
* `"hello " -> "heeellooo "`
* `"hi " -> "hiiii "`
In these strings like `"heeellooo "`, we have groups of adjacent letters that are all the same: `"h "`, `"eee "`, `"ll "`, `"ooo "`.
You are given a string `s` and an array of query strings... | null |
✔ Python3 Solution | Binary Search | preimage-size-of-factorial-zeroes-function | 0 | 1 | # Binary Search\n```\nclass Solution:\n def preimageSizeFZF(self, k):\n l, r = -1, k\n while l != r:\n n = c = m = (l + r + 1) >> 1\n while n and c <= k: c += (n := n // 5)\n if c < k: l = m\n elif c > k: r = m - 1\n else: return 5\n return ... | 1 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
✔ Python3 Solution | Binary Search | preimage-size-of-factorial-zeroes-function | 0 | 1 | # Binary Search\n```\nclass Solution:\n def preimageSizeFZF(self, k):\n l, r = -1, k\n while l != r:\n n = c = m = (l + r + 1) >> 1\n while n and c <= k: c += (n := n // 5)\n if c < k: l = m\n elif c > k: r = m - 1\n else: return 5\n return ... | 1 | Sometimes people repeat letters to represent extra feeling. For example:
* `"hello " -> "heeellooo "`
* `"hi " -> "hiiii "`
In these strings like `"heeellooo "`, we have groups of adjacent letters that are all the same: `"h "`, `"eee "`, `"ll "`, `"ooo "`.
You are given a string `s` and an array of query strings... | null |
Python 3 || 12 lines, bin search, w/explanation || T/M: 88% / 98% | preimage-size-of-factorial-zeroes-function | 0 | 1 | The number of zeros in the decimal representation of an integer is equal to the count of factors of`10` in the integer. The number of `10`-factors in an integer, in turn, is equal to the lesser of the counts of `5`-factors and of `2`-factors in that integer. For an integer n!, it is the count `5`-factors because the co... | 4 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
Python 3 || 12 lines, bin search, w/explanation || T/M: 88% / 98% | preimage-size-of-factorial-zeroes-function | 0 | 1 | The number of zeros in the decimal representation of an integer is equal to the count of factors of`10` in the integer. The number of `10`-factors in an integer, in turn, is equal to the lesser of the counts of `5`-factors and of `2`-factors in that integer. For an integer n!, it is the count `5`-factors because the co... | 4 | Sometimes people repeat letters to represent extra feeling. For example:
* `"hello " -> "heeellooo "`
* `"hi " -> "hiiii "`
In these strings like `"heeellooo "`, we have groups of adjacent letters that are all the same: `"h "`, `"eee "`, `"ll "`, `"ooo "`.
You are given a string `s` and an array of query strings... | null |
PYTHON SIMPLE BINARY SEARCH BEATS 94%! | preimage-size-of-factorial-zeroes-function | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe notice that every occurence of 5 we are added 1 \'0\'\nour approach will be to Binary Search the number of 5\'s needed to get k (if even possible)\n* we have to loo... | 0 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
PYTHON SIMPLE BINARY SEARCH BEATS 94%! | preimage-size-of-factorial-zeroes-function | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe notice that every occurence of 5 we are added 1 \'0\'\nour approach will be to Binary Search the number of 5\'s needed to get k (if even possible)\n* we have to loo... | 0 | Sometimes people repeat letters to represent extra feeling. For example:
* `"hello " -> "heeellooo "`
* `"hi " -> "hiiii "`
In these strings like `"heeellooo "`, we have groups of adjacent letters that are all the same: `"h "`, `"eee "`, `"ll "`, `"ooo "`.
You are given a string `s` and an array of query strings... | null |
binary search w/ counting trailing zeros | runtime beats 71.08% | preimage-size-of-factorial-zeroes-function | 1 | 1 | # Intuition\nThe problem involves counting the number of non-negative integers x such that the number of trailing zeroes in x! (factorial of x) is equal to a given value k.\n\n# Approach\nHere, `atMost_k` function calculates the number of integers less than or equal to a given value with k trailing zeros by performing ... | 0 | Let `f(x)` be the number of zeroes at the end of `x!`. Recall that `x! = 1 * 2 * 3 * ... * x` and by convention, `0! = 1`.
* For example, `f(3) = 0` because `3! = 6` has no zeroes at the end, while `f(11) = 2` because `11! = 39916800` has two zeroes at the end.
Given an integer `k`, return the number of non-negativ... | Think of the L and R's as people on a horizontal line, where X is a space. The people can't cross each other, and also you can't go from XRX to RXX. |
binary search w/ counting trailing zeros | runtime beats 71.08% | preimage-size-of-factorial-zeroes-function | 1 | 1 | # Intuition\nThe problem involves counting the number of non-negative integers x such that the number of trailing zeroes in x! (factorial of x) is equal to a given value k.\n\n# Approach\nHere, `atMost_k` function calculates the number of integers less than or equal to a given value with k trailing zeros by performing ... | 0 | Sometimes people repeat letters to represent extra feeling. For example:
* `"hello " -> "heeellooo "`
* `"hi " -> "hiiii "`
In these strings like `"heeellooo "`, we have groups of adjacent letters that are all the same: `"h "`, `"eee "`, `"ll "`, `"ooo "`.
You are given a string `s` and an array of query strings... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.