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 |
|---|---|---|---|---|---|---|---|
Python Easy Solution | find-eventual-safe-states | 0 | 1 | \n\n# Code\n```\nclass Solution(object):\n def eventualSafeNodes(self, graph):\n """\n :type graph: List[List[int]]\n :rtype: List[int]\n """\n n=len(graph)\n safe={}\n ans=[]\n def dfs(i):\n if i in safe:\n return safe[i]\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 |
Python Easy Solution | find-eventual-safe-states | 0 | 1 | \n\n# Code\n```\nclass Solution(object):\n def eventualSafeNodes(self, graph):\n """\n :type graph: List[List[int]]\n :rtype: List[int]\n """\n n=len(graph)\n safe={}\n ans=[]\n def dfs(i):\n if i in safe:\n return safe[i]\n ... | 1 | A **valid encoding** of an array of `words` is any reference string `s` and array of indices `indices` such that:
* `words.length == indices.length`
* The reference string `s` ends with the `'#'` character.
* For each index `indices[i]`, the **substring** of `s` starting from `indices[i]` and up to (but not incl... | null |
Solution | find-eventual-safe-states | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool dfs(vector<int>&vis,vector<int>&path,int start,vector<vector<int>>&graph,vector<int>&ans)\n {\n vis[start]=1;\n path[start]=1;\n for(auto it:graph[start])\n {\n if(vis[it]==0)\n {\n if(dfs(vis,path,it,graph... | 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 |
Solution | find-eventual-safe-states | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool dfs(vector<int>&vis,vector<int>&path,int start,vector<vector<int>>&graph,vector<int>&ans)\n {\n vis[start]=1;\n path[start]=1;\n for(auto it:graph[start])\n {\n if(vis[it]==0)\n {\n if(dfs(vis,path,it,graph... | 2 | A **valid encoding** of an array of `words` is any reference string `s` and array of indices `indices` such that:
* `words.length == indices.length`
* The reference string `s` ends with the `'#'` character.
* For each index `indices[i]`, the **substring** of `s` starting from `indices[i]` and up to (but not incl... | null |
Solution | bricks-falling-when-hit | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool isConnected(vector<vector<bool>>& vis, int& i, int& j){\n if(i==0)\n return true;\n \n if(i>0 && vis[i-1][j])\n return true;\n if(j>0 && vis[i][j-1])\n return true;\n if(i<vis.size()-1 && vis[i+1][j])\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 |
Solution | bricks-falling-when-hit | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool isConnected(vector<vector<bool>>& vis, int& i, int& j){\n if(i==0)\n return true;\n \n if(i>0 && vis[i-1][j])\n return true;\n if(j>0 && vis[i][j-1])\n return true;\n if(i<vis.size()-1 && vis[i+1][j])\n ... | 1 | Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
The **distance** between two indices `i` and `j` is `abs(i - j)`, where... | null |
803: Solution with step by step explanation | bricks-falling-when-hit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nclass Solution:\n def hitBricks(self, grid: List[List[int]], hits: List[List[int]]) -> List[int]:\n # Depth-First Search function to mark connected bricks\n def dfs(x, y):\n # Check for valid... | 0 | 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 |
803: Solution with step by step explanation | bricks-falling-when-hit | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nclass Solution:\n def hitBricks(self, grid: List[List[int]], hits: List[List[int]]) -> List[int]:\n # Depth-First Search function to mark connected bricks\n def dfs(x, y):\n # Check for valid... | 0 | Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
The **distance** between two indices `i` and `j` is `abs(i - j)`, where... | null |
Union Find Solution | bricks-falling-when-hit | 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 | 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 |
Union Find Solution | bricks-falling-when-hit | 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 | Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
The **distance** between two indices `i` and `j` is `abs(i - j)`, where... | null |
🌟 || Reverse Logic Intuition || 🌟 | bricks-falling-when-hit | 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 | 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 |
🌟 || Reverse Logic Intuition || 🌟 | bricks-falling-when-hit | 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 | Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
The **distance** between two indices `i` and `j` is `abs(i - j)`, where... | null |
Python 3 | DFS | Explanation | bricks-falling-when-hit | 0 | 1 | - We can do it on regular order, which will take `O(KMN)`, where `K = len(hits), M = len(grid), N = len(grid[0])`\n- If we do it reversely of `hits`, it will take `O(MN) + O(K)` since we can utilize the known information (we know how many nodes is already connected), all points will be at most be visited twice (mark as... | 6 | 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 3 | DFS | Explanation | bricks-falling-when-hit | 0 | 1 | - We can do it on regular order, which will take `O(KMN)`, where `K = len(hits), M = len(grid), N = len(grid[0])`\n- If we do it reversely of `hits`, it will take `O(MN) + O(K)` since we can utilize the known information (we know how many nodes is already connected), all points will be at most be visited twice (mark as... | 6 | Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
The **distance** between two indices `i` and `j` is `abs(i - j)`, where... | null |
[Python3] union-find | bricks-falling-when-hit | 0 | 1 | \n```\nclass UnionFind: \n \n def __init__(self, n): \n self.parent = list(range(n))\n self.rank = [1] * n\n \n def find(self, p): \n if p != self.parent[p]: \n self.parent[p] = self.find(self.parent[p])\n return self.parent[p]\n \n def union(self, p, q):\n ... | 2 | 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 |
[Python3] union-find | bricks-falling-when-hit | 0 | 1 | \n```\nclass UnionFind: \n \n def __init__(self, n): \n self.parent = list(range(n))\n self.rank = [1] * n\n \n def find(self, p): \n if p != self.parent[p]: \n self.parent[p] = self.find(self.parent[p])\n return self.parent[p]\n \n def union(self, p, q):\n ... | 2 | Given a string `s` and a character `c` that occurs in `s`, return _an array of integers_ `answer` _where_ `answer.length == s.length` _and_ `answer[i]` _is the **distance** from index_ `i` _to the **closest** occurrence of character_ `c` _in_ `s`.
The **distance** between two indices `i` and `j` is `abs(i - j)`, where... | null |
Python | Set w/ DP | Beats 95% | unique-morse-code-words | 0 | 1 | # Complexity\n- Time complexity: O(S)\n- Space complexity: O(S) \n\n# Code\n```\nclass Solution:\n def uniqueMorseRepresentations(self, words: List[str]) -> int:\n # potential questions\n # can we expect all lower case\n # can we expect empty words dict\n # will the input be v... | 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 |
Python | Set w/ DP | Beats 95% | unique-morse-code-words | 0 | 1 | # Complexity\n- Time complexity: O(S)\n- Space complexity: O(S) \n\n# Code\n```\nclass Solution:\n def uniqueMorseRepresentations(self, words: List[str]) -> int:\n # potential questions\n # can we expect all lower case\n # can we expect empty words dict\n # will the input be v... | 1 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
Easy solution || Line By Line Explanation || Python || Java || C++ || Ruby | unique-morse-code-words | 1 | 1 | # Beats\n\n\n# Intuition\nThe problem asks for the number of different transformations among a list of words, where each word can be represented as a concatenation of Morse code representations for its lette... | 15 | 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 solution || Line By Line Explanation || Python || Java || C++ || Ruby | unique-morse-code-words | 1 | 1 | # Beats\n\n\n# Intuition\nThe problem asks for the number of different transformations among a list of words, where each word can be represented as a concatenation of Morse code representations for its lette... | 15 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
🔥Clean, Simple 95% using ASCII🔥 | unique-morse-code-words | 0 | 1 | # Approach\n1. Create a list morse that contains Morse code representations for lowercase letters.\n2. For each word in the words list:\n- Use a list comprehension to convert each character to its corresponding Morse code.\n- Determine the index by subtracting the ASCII value of \'a\' (97) from the ASCII value of the c... | 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 |
🔥Clean, Simple 95% using ASCII🔥 | unique-morse-code-words | 0 | 1 | # Approach\n1. Create a list morse that contains Morse code representations for lowercase letters.\n2. For each word in the words list:\n- Use a list comprehension to convert each character to its corresponding Morse code.\n- Determine the index by subtracting the ASCII value of \'a\' (97) from the ASCII value of the c... | 1 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
Simple Easy Solution | Beats 99% | 30 ms | Accepted 🔥🚀🚀 | unique-morse-code-words | 0 | 1 | # Code\n```\nclass Solution:\n def uniqueMorseRepresentations(self, x: List[str]) -> int:\n a=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]\n return len(set(["".join([a[ord(i)-97] for i i... | 3 | 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 |
Simple Easy Solution | Beats 99% | 30 ms | Accepted 🔥🚀🚀 | unique-morse-code-words | 0 | 1 | # Code\n```\nclass Solution:\n def uniqueMorseRepresentations(self, x: List[str]) -> int:\n a=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]\n return len(set(["".join([a[ord(i)-97] for i i... | 3 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
Python Elegant & Short | Two lines | No loops | unique-morse-code-words | 0 | 1 | \n\tclass Solution:\n\t\t"""\n\t\tTime: O(n)\n\t\tMemory: O(n)\n\t\t"""\n\n\t\tMORSE = {\n\t\t\t\'a\': \'.-\', \'b\': \'-...\', \'c\': \'-.-.\', \'d\': \'-..\', \'e\': \'.\', \'f\': \'..-.\', \'g\': \'--.\',\n\t\t\t\'h\': \'....\', \'i\': \'..\', \'j\': \'.---\', \'k\': \'-.-\', \'l\': \'.-..\', \'m\': \'--\... | 5 | 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 |
Python Elegant & Short | Two lines | No loops | unique-morse-code-words | 0 | 1 | \n\tclass Solution:\n\t\t"""\n\t\tTime: O(n)\n\t\tMemory: O(n)\n\t\t"""\n\n\t\tMORSE = {\n\t\t\t\'a\': \'.-\', \'b\': \'-...\', \'c\': \'-.-.\', \'d\': \'-..\', \'e\': \'.\', \'f\': \'..-.\', \'g\': \'--.\',\n\t\t\t\'h\': \'....\', \'i\': \'..\', \'j\': \'.---\', \'k\': \'-.-\', \'l\': \'.-..\', \'m\': \'--\... | 5 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
My simple Python3 Solution | unique-morse-code-words | 0 | 1 | \n# Code\n```\nclass Solution:\n def uniqueMorseRepresentations(self, words: List[str]) -> int:\n m = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]\n a = "abcdefghijklmnopqrstuvwxyz"\n ... | 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 |
My simple Python3 Solution | unique-morse-code-words | 0 | 1 | \n# Code\n```\nclass Solution:\n def uniqueMorseRepresentations(self, words: List[str]) -> int:\n m = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]\n a = "abcdefghijklmnopqrstuvwxyz"\n ... | 2 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
Solution | unique-morse-code-words | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int uniqueMorseRepresentations(vector<string>& words) {\n vector<string> d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};\n ... | 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 |
Solution | unique-morse-code-words | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int uniqueMorseRepresentations(vector<string>& words) {\n vector<string> d = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};\n ... | 4 | You are given two **0-indexed** integer arrays `fronts` and `backs` of length `n`, where the `ith` card has the positive integer `fronts[i]` printed on the front and `backs[i]` printed on the back. Initially, each card is placed on a table such that the front number is facing up and the other is facing down. You may fl... | null |
Solution | split-array-with-same-average | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool splitArraySameAverage(vector<int>& a) {\n int n = a.size(), tot = accumulate(a.begin(), a.end(), 0);\n int M = tot / 2 + 1, N = n / 2 + 1;\n vector <bool> vis(M * N, 0);\n vector <int> q; q.reserve(N/2 * M);\n int cnt = 0;\n for (i... | 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 |
Solution | split-array-with-same-average | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool splitArraySameAverage(vector<int>& a) {\n int n = a.size(), tot = accumulate(a.begin(), a.end(), 0);\n int M = tot / 2 + 1, N = n / 2 + 1;\n vector <bool> vis(M * N, 0);\n vector <int> q; q.reserve(N/2 * M);\n int cnt = 0;\n for (i... | 1 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
Time: O(2n/2)O(2n/2) Space: O(2n/2)O(2n/2) | split-array-with-same-average | 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 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 |
Time: O(2n/2)O(2n/2) Space: O(2n/2)O(2n/2) | split-array-with-same-average | 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 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
Simple Math Explanation | split-array-with-same-average | 0 | 1 | ```\nWe need to split array in 2 parts such that the avg is equal\n\n```\n`We know,`\n`SubArrayB + SubArrayA = Sum`---------`eq(1)`\n`X + Y = N `-------------------------------`eq(2)`\n `Where X = len(SubArrayA), Y = len(SubArrayB) , N = len(array)`\n\n`To Prove :` $\\frac{SubArrayA}{X}$ = $\\frac{SubArrayB}{Y}$\n`From... | 2 | 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 |
Simple Math Explanation | split-array-with-same-average | 0 | 1 | ```\nWe need to split array in 2 parts such that the avg is equal\n\n```\n`We know,`\n`SubArrayB + SubArrayA = Sum`---------`eq(1)`\n`X + Y = N `-------------------------------`eq(2)`\n `Where X = len(SubArrayA), Y = len(SubArrayB) , N = len(array)`\n\n`To Prove :` $\\frac{SubArrayA}{X}$ = $\\frac{SubArrayB}{Y}$\n`From... | 2 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
short python | split-array-with-same-average | 0 | 1 | ```\nclass Solution:\n def splitArraySameAverage(self, A: List[int]) -> bool:\n total = sum(A)\n # The ans does not change if we scale or shift the array\n for i in range(len(A)): A[i] = len(A) * A[i] - total\n # The above ensures that sum(A)=Avg(A)=avg(B)=avg(C)=sum(B)=sum(C)=0\n ... | 7 | 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 |
short python | split-array-with-same-average | 0 | 1 | ```\nclass Solution:\n def splitArraySameAverage(self, A: List[int]) -> bool:\n total = sum(A)\n # The ans does not change if we scale or shift the array\n for i in range(len(A)): A[i] = len(A) * A[i] - total\n # The above ensures that sum(A)=Avg(A)=avg(B)=avg(C)=sum(B)=sum(C)=0\n ... | 7 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
Share | split-array-with-same-average | 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 | 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 |
Share | split-array-with-same-average | 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 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
805: Solution with step by step explanation | split-array-with-same-average | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ntotal_sum = sum(nums)\nn = len(nums)\nif all(total_sum * i % n != 0 for i in range(1, n // 2 + 1)):\n return False\n```\n\nThe sum of all numbers in the array is calculated and stored in total_sum. n is the number of... | 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 |
805: Solution with step by step explanation | split-array-with-same-average | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ntotal_sum = sum(nums)\nn = len(nums)\nif all(total_sum * i % n != 0 for i in range(1, n // 2 + 1)):\n return False\n```\n\nThe sum of all numbers in the array is calculated and stored in total_sum. n is the number of... | 0 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
Bitshifted Subset Calculations | Commented and Explained | split-array-with-same-average | 0 | 1 | # Intuition and Approach \n<!-- Describe your first thoughts on how to solve this problem. -->\nFrom the problem, we can notice a few key factors for this implementation. \nThe first is that the number of values can go from 1 to 30. This gives us at most 30 bits we can work within for a bitmask, giving us a chance to u... | 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 |
Bitshifted Subset Calculations | Commented and Explained | split-array-with-same-average | 0 | 1 | # Intuition and Approach \n<!-- Describe your first thoughts on how to solve this problem. -->\nFrom the problem, we can notice a few key factors for this implementation. \nThe first is that the number of values can go from 1 to 30. This gives us at most 30 bits we can work within for a bitmask, giving us a chance to u... | 0 | Given an array of unique integers, `arr`, where each integer `arr[i]` is strictly greater than `1`.
We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
Return _the number of binary tre... | null |
Python3 Runtime: 42ms 67.78% Memory: 13.9mb 19.81% | number-of-lines-to-write-string | 0 | 1 | ```\nclass Solution:\n# Runtime: 42ms 67.78% Memory: 13.9mb 19.81%\n# O(n) || O(1)\n def numberOfLines(self, widths, s):\n newLine = 1\n \n width = 0\n \n for char in s:\n charWidth = widths[ord(char) - ord(\'a\')]\n \n if charWidth + width > 10... | 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 |
Python3 Runtime: 42ms 67.78% Memory: 13.9mb 19.81% | number-of-lines-to-write-string | 0 | 1 | ```\nclass Solution:\n# Runtime: 42ms 67.78% Memory: 13.9mb 19.81%\n# O(n) || O(1)\n def numberOfLines(self, widths, s):\n newLine = 1\n \n width = 0\n \n for char in s:\n charWidth = widths[ord(char) - ord(\'a\')]\n \n if charWidth + width > 10... | 1 | You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin " (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:
* If a word begins with a vowel (`'a'`, `... | null |
✅ Beats 99.76% solutions, ✅ Easy to understand O(n) tc Python code by ✅ BOLT CODING ✅ | number-of-lines-to-write-string | 0 | 1 | # Explanation\nAt first we are initializing pix to 0, min no of lines which will be there is always 1, i for keeping count of index, and a which is ascii of \'a\'.\nWe are then iterating through the s - list of words. In case pix size is <= 100 we increment the pix, incase pix > 100 we increment the line number and ini... | 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 |
✅ Beats 99.76% solutions, ✅ Easy to understand O(n) tc Python code by ✅ BOLT CODING ✅ | number-of-lines-to-write-string | 0 | 1 | # Explanation\nAt first we are initializing pix to 0, min no of lines which will be there is always 1, i for keeping count of index, and a which is ascii of \'a\'.\nWe are then iterating through the s - list of words. In case pix size is <= 100 we increment the pix, incase pix > 100 we increment the line number and ini... | 1 | You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin " (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:
* If a word begins with a vowel (`'a'`, `... | null |
Beats 89.81% of users with Python3 | number-of-lines-to-write-string | 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 | 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 |
Beats 89.81% of users with Python3 | number-of-lines-to-write-string | 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 | You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin " (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:
* If a word begins with a vowel (`'a'`, `... | null |
Python Simple Solution!! | number-of-lines-to-write-string | 0 | 1 | \n# Code\n```\nclass Solution:\n def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n\n lines, counter = [], 0\n for letter in s:\n index: int = ord(letter) - 97\n if counter + widths[index] > 100:\n lines.append(counter)\n counter = 0\n... | 0 | 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 Simple Solution!! | number-of-lines-to-write-string | 0 | 1 | \n# Code\n```\nclass Solution:\n def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n\n lines, counter = [], 0\n for letter in s:\n index: int = ord(letter) - 97\n if counter + widths[index] > 100:\n lines.append(counter)\n counter = 0\n... | 0 | You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin " (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:
* If a word begins with a vowel (`'a'`, `... | null |
Python solution | number-of-lines-to-write-string | 0 | 1 | ```\nclass Solution:\n def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n alphabet: str = "abcdefghijklmnopqrstuvwxyz"\n line, total = 0, 0\n\n for letter in s:\n pixel = widths[alphabet.index(letter)]\n if line + pixel > 100:\n total += 1\n ... | 0 | 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 solution | number-of-lines-to-write-string | 0 | 1 | ```\nclass Solution:\n def numberOfLines(self, widths: List[int], s: str) -> List[int]:\n alphabet: str = "abcdefghijklmnopqrstuvwxyz"\n line, total = 0, 0\n\n for letter in s:\n pixel = widths[alphabet.index(letter)]\n if line + pixel > 100:\n total += 1\n ... | 0 | You are given a string `sentence` that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin " (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:
* If a word begins with a vowel (`'a'`, `... | null |
python3 solution using 2 arrays beats 98% O(N) space | max-increase-to-keep-city-skyline | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nhere a contains maximum in each row\nhere b cointains maximum in each column\nc has max in each column which will be appendded to b\nafter having these we just just need to compare a[i] and b[j] whichever is lower to maintain skyline and ... | 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 |
python3 solution using 2 arrays beats 98% O(N) space | max-increase-to-keep-city-skyline | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nhere a contains maximum in each row\nhere b cointains maximum in each column\nc has max in each column which will be appendded to b\nafter having these we just just need to compare a[i] and b[j] whichever is lower to maintain skyline and ... | 1 | There are `n` persons on a social media website. You are given an integer array `ages` where `ages[i]` is the age of the `ith` person.
A Person `x` will not send a friend request to a person `y` (`x != y`) if any of the following conditions is true:
* `age[y] <= 0.5 * age[x] + 7`
* `age[y] > age[x]`
* `age[y] >... | null |
Python3 | 100% time (32ms), 97% memory | Fast int math | soup-servings | 0 | 1 | \n\n\n# Intuition\nMost of this follows similar concepts as the editorial, but instead of adding complexity with dividing b... | 3 | 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 |
Python3 | 100% time (32ms), 97% memory | Fast int math | soup-servings | 0 | 1 | \n\n\n# Intuition\nMost of this follows similar concepts as the editorial, but instead of adding complexity with dividing b... | 3 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Easy Explanation with comment + Video Explanation in Depth || C++ || java || python | soup-servings | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nExplore all the options we have and calculate the probability smartly by reducing n.\n\nFor detailed explanation you can refer to my youtube channel (hindi Language)\nhttps://youtu.be/z7L9f8Lt_lc\n or link in my profile.Here,you can find ... | 28 | 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 Explanation with comment + Video Explanation in Depth || C++ || java || python | soup-servings | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nExplore all the options we have and calculate the probability smartly by reducing n.\n\nFor detailed explanation you can refer to my youtube channel (hindi Language)\nhttps://youtu.be/z7L9f8Lt_lc\n or link in my profile.Here,you can find ... | 28 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Python3 👍||⚡100% faster beats 🔥|| clean solution with critical point tips! || simple explain || | soup-servings | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nAs the value of N increases, the winning rate of A approaches 1,therefore, you need to find the critical point, which can significantly... | 9 | 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 |
Python3 👍||⚡100% faster beats 🔥|| clean solution with critical point tips! || simple explain || | soup-servings | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nAs the value of N increases, the winning rate of A approaches 1,therefore, you need to find the critical point, which can significantly... | 9 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Python short and clean. Functional programming. | soup-servings | 0 | 1 | # Approach\nTL;DR, Similar to [Editorial solution. Approach 2](https://leetcode.com/problems/soup-servings/editorial/) but shorter and functional.\n\n<!-- # Complexity\n- Time complexity: $$O(1)$$\n\n- Space complexity: $$O(1)$$ -->\n\n# Code\nHardcoded saturation point (i.e, `m = 200`)\n```python\nclass Solution:\n ... | 1 | 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 |
Python short and clean. Functional programming. | soup-servings | 0 | 1 | # Approach\nTL;DR, Similar to [Editorial solution. Approach 2](https://leetcode.com/problems/soup-servings/editorial/) but shorter and functional.\n\n<!-- # Complexity\n- Time complexity: $$O(1)$$\n\n- Space complexity: $$O(1)$$ -->\n\n# Code\nHardcoded saturation point (i.e, `m = 200`)\n```python\nclass Solution:\n ... | 1 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Python3 97% faster concise code based on cache | soup-servings | 0 | 1 | ```\nclass Solution:\n def soupServings(self, n: int) -> float:\n if n > 4800:\n return 1\n @cache\n def serve(quantityA: int, quantityB: int) -> float:\n if quantityA <= 0 and quantityB <= 0:\n return 0.5\n if quantityA <= 0:\n retu... | 1 | 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 |
Python3 97% faster concise code based on cache | soup-servings | 0 | 1 | ```\nclass Solution:\n def soupServings(self, n: int) -> float:\n if n > 4800:\n return 1\n @cache\n def serve(quantityA: int, quantityB: int) -> float:\n if quantityA <= 0 and quantityB <= 0:\n return 0.5\n if quantityA <= 0:\n retu... | 1 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
BFS with explanation | soup-servings | 0 | 1 | # Intuition\nFirst it is obvious that above some specific value n the result will be 1. So once you have more or less working solution you can find this value and hardcode it. I saw that 5000 is good enough you can definitely tweek this value with binary search to find the first one.\n\nThen you maintain the frontier w... | 1 | 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 |
BFS with explanation | soup-servings | 0 | 1 | # Intuition\nFirst it is obvious that above some specific value n the result will be 1. So once you have more or less working solution you can find this value and hardcode it. I saw that 5000 is good enough you can definitely tweek this value with binary search to find the first one.\n\nThen you maintain the frontier w... | 1 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Solution | soup-servings | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n double memo[200][200];\n double soupServings(int N) {\n return N > 4800 ? 1.0 : f((N + 24) / 25, (N + 24) / 25);\n }\n double f(int a, int b) {\n if (a <= 0 && b <= 0) return 0.5;\n if (a <= 0) return 1;\n if (b <= 0) return 0;\n ... | 1 | 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 | soup-servings | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n double memo[200][200];\n double soupServings(int N) {\n return N > 4800 ? 1.0 : f((N + 24) / 25, (N + 24) / 25);\n }\n double f(int a, int b) {\n if (a <= 0 && b <= 0) return 0.5;\n if (a <= 0) return 1;\n if (b <= 0) return 0;\n ... | 1 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Python3 Solution | soup-servings | 0 | 1 | \n```\nclass Solution:\n def soupServings(self, n: int) -> float:\n if n>4275:\n return 1\n n/=25 \n @cache\n def dfs(a,b):\n if a<=0 and b>0:\n return 1\n\n elif a<=0 and b<=0:\n return 0.5\n\n elif a>0 and b<=0... | 2 | 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 |
Python3 Solution | soup-servings | 0 | 1 | \n```\nclass Solution:\n def soupServings(self, n: int) -> float:\n if n>4275:\n return 1\n n/=25 \n @cache\n def dfs(a,b):\n if a<=0 and b>0:\n return 1\n\n elif a<=0 and b<=0:\n return 0.5\n\n elif a>0 and b<=0... | 2 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
🍲 [VIDEO] 100% Soup Servings: A Dive into Dynamic Programming and Probability 🎲 | soup-servings | 1 | 1 | # Intuition\nAt first glance, this problem is about making random choices and observing the outcome. This brings to mind the field of probability. However, since we\'re also dealing with several states (the amounts of soup A and B), it becomes clear that we need to use dynamic programming.\n\nhttps://youtu.be/S-9NmIj2f... | 45 | 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 |
🍲 [VIDEO] 100% Soup Servings: A Dive into Dynamic Programming and Probability 🎲 | soup-servings | 1 | 1 | # Intuition\nAt first glance, this problem is about making random choices and observing the outcome. This brings to mind the field of probability. However, since we\'re also dealing with several states (the amounts of soup A and B), it becomes clear that we need to use dynamic programming.\n\nhttps://youtu.be/S-9NmIj2f... | 45 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Ex-Amazon explains a solution with a video, Python, JavaScript, Java and C++ | soup-servings | 1 | 1 | Python solution beats 100%.\n\n\n\n# Solution Video\n## *** Please upvote for this article. *** \n\nhttps://youtu.be/6Wcl2nhDVyo\n\n# Sub... | 14 | 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 |
Ex-Amazon explains a solution with a video, Python, JavaScript, Java and C++ | soup-servings | 1 | 1 | Python solution beats 100%.\n\n\n\n# Solution Video\n## *** Please upvote for this article. *** \n\nhttps://youtu.be/6Wcl2nhDVyo\n\n# Sub... | 14 | You have `n` jobs and `m` workers. You are given three arrays: `difficulty`, `profit`, and `worker` where:
* `difficulty[i]` and `profit[i]` are the difficulty and the profit of the `ith` job, and
* `worker[j]` is the ability of `jth` worker (i.e., the `jth` worker can only complete a job with difficulty at most `... | null |
Solution | expressive-words | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool check(string s, string w) {\n int m = s.size(), n = w.size();\n int i = 0, j = 0;\n for (int i2 = 0, j2 = 0; i < m && j < n; i = i2, j = j2) {\n if (s[i] != w[j]) return false;\n while(i2 < m && s[i2] == s[i]) i2++;\n ... | 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 |
Solution | expressive-words | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool check(string s, string w) {\n int m = s.size(), n = w.size();\n int i = 0, j = 0;\n for (int i2 = 0, j2 = 0; i < m && j < n; i = i2, j = j2) {\n if (s[i] != w[j]) return false;\n while(i2 < m && s[i2] == s[i]) i2++;\n ... | 1 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
python: easy to understand with helper function | expressive-words | 0 | 1 | # Code\n```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n # edge cases\n if len(s) == 0 and len(words) != 0:\n return False\n if len(words) == 0 and len(s) != 0:\n return False\n if len(s) == 0 and len(words) == 0:\n retu... | 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: easy to understand with helper function | expressive-words | 0 | 1 | # Code\n```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n # edge cases\n if len(s) == 0 and len(words) != 0:\n return False\n if len(words) == 0 and len(s) != 0:\n return False\n if len(s) == 0 and len(words) == 0:\n retu... | 1 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
Python 3 || 9 lines, groupby, w/ explanation || T/M: 97% / 17% | expressive-words | 0 | 1 | Here\'s the plan:\n\nFor the string `s` and for each string `word` in `words`, we use `groupby` to construct two tuples per string. The first lists the distinct characters in the string, and second lists the multiplicty of each character in the string.\n\nI think, knowing that, one can figure out the code below. \'The ... | 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 3 || 9 lines, groupby, w/ explanation || T/M: 97% / 17% | expressive-words | 0 | 1 | Here\'s the plan:\n\nFor the string `s` and for each string `word` in `words`, we use `groupby` to construct two tuples per string. The first lists the distinct characters in the string, and second lists the multiplicty of each character in the string.\n\nI think, knowing that, one can figure out the code below. \'The ... | 4 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
Python Solution Without Using Any Special Library With Detailed Explanation | expressive-words | 0 | 1 | In this solution first, I use the process function to get the characters that appear without repeating in the string and also their frequencies. For example for the string "helloo", chars=[\'h\',\'e\',\'l\',\'o\'] and counts=[1,1,2,2]. \nIf any of the strings in the words list has the same characters as S, then it wort... | 19 | 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 Solution Without Using Any Special Library With Detailed Explanation | expressive-words | 0 | 1 | In this solution first, I use the process function to get the characters that appear without repeating in the string and also their frequencies. For example for the string "helloo", chars=[\'h\',\'e\',\'l\',\'o\'] and counts=[1,1,2,2]. \nIf any of the strings in the words list has the same characters as S, then it wort... | 19 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
Python3 48ms with itertools | expressive-words | 0 | 1 | ```\nfrom typing import List\nfrom itertools import groupby\n\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n """\n split original string into groups\n eg. hello -> ["h", "e", "ll", "o"]\n\n Apply this same grouping to every query string, and then compare ... | 2 | 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 48ms with itertools | expressive-words | 0 | 1 | ```\nfrom typing import List\nfrom itertools import groupby\n\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n """\n split original string into groups\n eg. hello -> ["h", "e", "ll", "o"]\n\n Apply this same grouping to every query string, and then compare ... | 2 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
python 3 || simple solution || O(n)/O(1) | expressive-words | 0 | 1 | ```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n def groupWord(word):\n size = 1\n for i in range(1, len(word)):\n if word[i] == word[i - 1]:\n size += 1\n else:\n yield word[i - 1], s... | 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 || simple solution || O(n)/O(1) | expressive-words | 0 | 1 | ```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n def groupWord(word):\n size = 1\n for i in range(1, len(word)):\n if word[i] == word[i - 1]:\n size += 1\n else:\n yield word[i - 1], s... | 1 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
Problem description is horrible... but here's simple python solution | expressive-words | 0 | 1 | hope this helps for someone who is also struggling with the description\n```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n \n def summarize(word):\n res = []\n \n n = len(word)\n i, j = 0, 0\n while i<=j and j <= ... | 3 | 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 |
Problem description is horrible... but here's simple python solution | expressive-words | 0 | 1 | hope this helps for someone who is also struggling with the description\n```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n \n def summarize(word):\n res = []\n \n n = len(word)\n i, j = 0, 0\n while i<=j and j <= ... | 3 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
python most basic | expressive-words | 0 | 1 | ```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n def check(x):\n i = j = 0\n while i < len(s) and j < len(x):\n counts = 1\n while i<len(s)-1 and s[i]==s[i+1]:\n i+=1\n counts+=1\n ... | 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 most basic | expressive-words | 0 | 1 | ```\nclass Solution:\n def expressiveWords(self, s: str, words: List[str]) -> int:\n def check(x):\n i = j = 0\n while i < len(s) and j < len(x):\n counts = 1\n while i<len(s)-1 and s[i]==s[i+1]:\n i+=1\n counts+=1\n ... | 4 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
809: Beats 97.96%, Solution with step by step explanation | expressive-words | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ndef check(stretchy: str, word: str) -> bool:\n```\n\nInside the main function, a helper function named check is defined. It takes two strings, stretchy and word, and returns a boolean indicating if word can be stretched... | 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 |
809: Beats 97.96%, Solution with step by step explanation | expressive-words | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ndef check(stretchy: str, word: str) -> bool:\n```\n\nInside the main function, a helper function named check is defined. It takes two strings, stretchy and word, and returns a boolean indicating if word can be stretched... | 0 | You are given an `n x n` binary matrix `grid`. You are allowed to change **at most one** `0` to be `1`.
Return _the size of the largest **island** in_ `grid` _after applying this operation_.
An **island** is a 4-directionally connected group of `1`s.
**Example 1:**
**Input:** grid = \[\[1,0\],\[0,1\]\]
**Output:** ... | null |
Solution | chalkboard-xor-game | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool xorGame(vector<int>& nums) {\n int xo = 0;\n for (int i: nums) xo ^= i;\n return xo == 0 || nums.size() % 2 == 0;\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def xorGame(self, nums: List[int]) -> bool:\n xor = 0\n for i in nums... | 1 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
Solution | chalkboard-xor-game | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool xorGame(vector<int>& nums) {\n int xo = 0;\n for (int i: nums) xo ^= i;\n return xo == 0 || nums.size() % 2 == 0;\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def xorGame(self, nums: List[int]) -> bool:\n xor = 0\n for i in nums... | 1 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
The Clever Math Solution Explained - Allows an O(1) Solution if N%2==0 | chalkboard-xor-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nI have no intuition for deriving this :(\n\n# Approach\n\nThere are three cases:\n1. the xor of all the numbers is 0 => Alice wins because the problem says so\n2. the xor is nonzero, and `len(nums)` is even\n3. the xor is nonzero, and `... | 1 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
The Clever Math Solution Explained - Allows an O(1) Solution if N%2==0 | chalkboard-xor-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nI have no intuition for deriving this :(\n\n# Approach\n\nThere are three cases:\n1. the xor of all the numbers is 0 => Alice wins because the problem says so\n2. the xor is nonzero, and `len(nums)` is even\n3. the xor is nonzero, and `... | 1 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
810: Beats 93.00%, Solution with step by step explanation | chalkboard-xor-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nxor_sum = 0\n```\n\nHere, we initialize a variable xor_sum that will hold the XOR of all the numbers in the list. The XOR operation is a bitwise operation where bits that are the same result in 0, and bits that are diff... | 0 | You are given an array of integers `nums` represents the numbers written on a chalkboard.
Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become `0`, then that player loses. The bitwi... | null |
810: Beats 93.00%, Solution with step by step explanation | chalkboard-xor-game | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nxor_sum = 0\n```\n\nHere, we initialize a variable xor_sum that will hold the XOR of all the numbers in the list. The XOR operation is a bitwise operation where bits that are the same result in 0, and bits that are diff... | 0 | Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`.
* For example, calling `countUniqueChars(s)` if `s = "LEETCODE "` then `"L "`, `"T "`, `"C "`, `"O "`, `"D "` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.
Given a ... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.