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 |
|---|---|---|---|---|---|---|---|
Clear Explanation with Easy Recursive DFS Solution | count-all-possible-routes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTry all paths from the start until you run out of fuel. From all the possible paths, keep a count of which paths end on the finishing location from the start. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThere ... | 1 | You are given an array of **distinct** positive integers locations where `locations[i]` represents the position of city `i`. You are also given integers `start`, `finish` and `fuel` representing the starting city, ending city, and the initial amount of fuel you have, respectively.
At each step, if you are at city `i`,... | Sort the arrays, then compute the maximum difference between two consecutive elements for horizontal cuts and vertical cuts. The answer is the product of these maximum values in horizontal cuts and vertical cuts. |
Clear Explanation with Easy Recursive DFS Solution | count-all-possible-routes | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTry all paths from the start until you run out of fuel. From all the possible paths, keep a count of which paths end on the finishing location from the start. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThere ... | 1 | Given an integer `n`, return _the **decimal value** of the binary string formed by concatenating the binary representations of_ `1` _to_ `n` _in order, **modulo**_ `109 + 7`.
**Example 1:**
**Input:** n = 1
**Output:** 1
**Explanation: ** "1 " in binary corresponds to the decimal value 1.
**Example 2:**
**Input:**... | Use dynamic programming to solve this problem with each state defined by the city index and fuel left. Since the array contains distinct integers fuel will always be spent in each move and so there can be no cycles. |
python 3 - top down dp | count-all-possible-routes | 0 | 1 | Pay attention on 2 questions: "how to cache" and "time complexity". Evaluate by reading the constraints.\n\n# Intuition\n2 state variables: where you are, and fuel remaining. Sum up all states is ok.\n\nOptimization: if abs(loca[start] - loca[finish]) > fuel -> This means you cannot reach "finish" with the shortest pat... | 1 | You are given an array of **distinct** positive integers locations where `locations[i]` represents the position of city `i`. You are also given integers `start`, `finish` and `fuel` representing the starting city, ending city, and the initial amount of fuel you have, respectively.
At each step, if you are at city `i`,... | Sort the arrays, then compute the maximum difference between two consecutive elements for horizontal cuts and vertical cuts. The answer is the product of these maximum values in horizontal cuts and vertical cuts. |
python 3 - top down dp | count-all-possible-routes | 0 | 1 | Pay attention on 2 questions: "how to cache" and "time complexity". Evaluate by reading the constraints.\n\n# Intuition\n2 state variables: where you are, and fuel remaining. Sum up all states is ok.\n\nOptimization: if abs(loca[start] - loca[finish]) > fuel -> This means you cannot reach "finish" with the shortest pat... | 1 | Given an integer `n`, return _the **decimal value** of the binary string formed by concatenating the binary representations of_ `1` _to_ `n` _in order, **modulo**_ `109 + 7`.
**Example 1:**
**Input:** n = 1
**Output:** 1
**Explanation: ** "1 " in binary corresponds to the decimal value 1.
**Example 2:**
**Input:**... | Use dynamic programming to solve this problem with each state defined by the city index and fuel left. Since the array contains distinct integers fuel will always be spent in each move and so there can be no cycles. |
SIMPLE PYTHON SOLUTION USING DP | count-all-possible-routes | 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 array of **distinct** positive integers locations where `locations[i]` represents the position of city `i`. You are also given integers `start`, `finish` and `fuel` representing the starting city, ending city, and the initial amount of fuel you have, respectively.
At each step, if you are at city `i`,... | Sort the arrays, then compute the maximum difference between two consecutive elements for horizontal cuts and vertical cuts. The answer is the product of these maximum values in horizontal cuts and vertical cuts. |
SIMPLE PYTHON SOLUTION USING DP | count-all-possible-routes | 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 integer `n`, return _the **decimal value** of the binary string formed by concatenating the binary representations of_ `1` _to_ `n` _in order, **modulo**_ `109 + 7`.
**Example 1:**
**Input:** n = 1
**Output:** 1
**Explanation: ** "1 " in binary corresponds to the decimal value 1.
**Example 2:**
**Input:**... | Use dynamic programming to solve this problem with each state defined by the city index and fuel left. Since the array contains distinct integers fuel will always be spent in each move and so there can be no cycles. |
Python DP | count-all-possible-routes | 0 | 1 | Try all possible routes with DP\n```python []\nclass Solution:\n def countRoutes(self, locations: List[int], start: int, finish: int, fuel: int) -> int:\n @cache\n def dp(i, fuel):\n cnt = int(i == finish)\n for j in range(len(locations)):\n cost = abs(locations[i] ... | 1 | You are given an array of **distinct** positive integers locations where `locations[i]` represents the position of city `i`. You are also given integers `start`, `finish` and `fuel` representing the starting city, ending city, and the initial amount of fuel you have, respectively.
At each step, if you are at city `i`,... | Sort the arrays, then compute the maximum difference between two consecutive elements for horizontal cuts and vertical cuts. The answer is the product of these maximum values in horizontal cuts and vertical cuts. |
Python DP | count-all-possible-routes | 0 | 1 | Try all possible routes with DP\n```python []\nclass Solution:\n def countRoutes(self, locations: List[int], start: int, finish: int, fuel: int) -> int:\n @cache\n def dp(i, fuel):\n cnt = int(i == finish)\n for j in range(len(locations)):\n cost = abs(locations[i] ... | 1 | Given an integer `n`, return _the **decimal value** of the binary string formed by concatenating the binary representations of_ `1` _to_ `n` _in order, **modulo**_ `109 + 7`.
**Example 1:**
**Input:** n = 1
**Output:** 1
**Explanation: ** "1 " in binary corresponds to the decimal value 1.
**Example 2:**
**Input:**... | Use dynamic programming to solve this problem with each state defined by the city index and fuel left. Since the array contains distinct integers fuel will always be spent in each move and so there can be no cycles. |
Easy Solution for beginners || Beats 95%+✨💫 || 🧛✌️ | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Intuition\nIt seems that you want to replace the question mark (\'?\') in the input string \'s\' with lowercase English alphabets (\'a\' to \'z\') in such a way that adjacent characters are not the same. The code uses a dictionary to keep track of the positions of the question marks and then iterates through the stri... | 1 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Easy Solution for beginners || Beats 95%+✨💫 || 🧛✌️ | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Intuition\nIt seems that you want to replace the question mark (\'?\') in the input string \'s\' with lowercase English alphabets (\'a\' to \'z\') in such a way that adjacent characters are not the same. The code uses a dictionary to keep track of the positions of the question marks and then iterates through the stri... | 1 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python random choice solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI came up with this random choice solution. \nHowever, I realized that we only need three letters when I checked out the other solutions. LOL\nAnyway, the upper lengh of the string is 100. \nSo I guess there is no big difference in time c... | 2 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python random choice solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI came up with this random choice solution. \nHowever, I realized that we only need three letters when I checked out the other solutions. LOL\nAnyway, the upper lengh of the string is 100. \nSo I guess there is no big difference in time c... | 2 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
[Python3] one of three letters | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | \n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list(s)\n for i in range(len(s)):\n if s[i] == "?": \n for c in "abc": \n if (i == 0 or s[i-1] != c) and (i+1 == len(s) or s[i+1] != c): \n s[i] = c\n ... | 70 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
[Python3] one of three letters | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | \n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list(s)\n for i in range(len(s)):\n if s[i] == "?": \n for c in "abc": \n if (i == 0 or s[i-1] != c) and (i+1 == len(s) or s[i+1] != c): \n s[i] = c\n ... | 70 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python Easy Solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n new=s\n for i in range(len(s)):\n if new[i]=="?":\n if len(new)==1:\n return "p"\n if i==0:\n lis=[new[i+1]]\n if i==len(s)-1:\n ... | 1 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python Easy Solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n new=s\n for i in range(len(s)):\n if new[i]=="?":\n if len(new)==1:\n return "p"\n if i==0:\n lis=[new[i+1]]\n if i==len(s)-1:\n ... | 1 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python | Easy Solution✅ | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\u2705\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if s =="?":\n return "a"\n if len(s) == 1:\n return s\n \n s_list = [x for x in s]\n for index, letter in enumerate(s_list):\n replace_char = {"a","b","c"} \n ... | 6 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python | Easy Solution✅ | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\u2705\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if s =="?":\n return "a"\n if len(s) == 1:\n return s\n \n s_list = [x for x in s]\n for index, letter in enumerate(s_list):\n replace_char = {"a","b","c"} \n ... | 6 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python simple solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | **Python :**\n\n```\ndef modifyString(self, s: str) -> str:\n\ts = list(s)\n\n\tfor i in range(len(s)):\n\t\tif s[i] == \'?\':\n\t\t\tfor c in "abc":\n\t\t\t\tif (i == 0 or s[i - 1] != c) and (i + 1 == len(s) or s[i + 1] != c):\n\t\t\t\t\ts[i] = c\n\t\t\t\t\tbreak\n\n\treturn "".join(s)\n```\n\n**Like it ? please upvot... | 12 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python simple solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | **Python :**\n\n```\ndef modifyString(self, s: str) -> str:\n\ts = list(s)\n\n\tfor i in range(len(s)):\n\t\tif s[i] == \'?\':\n\t\t\tfor c in "abc":\n\t\t\t\tif (i == 0 or s[i - 1] != c) and (i + 1 == len(s) or s[i + 1] != c):\n\t\t\t\t\ts[i] = c\n\t\t\t\t\tbreak\n\n\treturn "".join(s)\n```\n\n**Like it ? please upvot... | 12 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Simple Python solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | **Idea:**\nFor every `?` in the string, recplace it with either `a, b, or c`, as there are only two sides for \neach letter therefor there is no need for other letters in the alphabet to recplace it in order to receive a valid string.\n(The spaces added to the beginning and end of the string are in order to skip checki... | 3 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Simple Python solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | **Idea:**\nFor every `?` in the string, recplace it with either `a, b, or c`, as there are only two sides for \neach letter therefor there is no need for other letters in the alphabet to recplace it in order to receive a valid string.\n(The spaces added to the beginning and end of the string are in order to skip checki... | 3 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python 3 Solution for Beginners ( 3 letter soln) | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if len(s)==1: #if input contains only a \'?\'\n if s[0]==\'?\':\n return \'a\'\n s=list(s)\n for i in range(len(s)):\n if s[i]==\'?\':\n for c in \'abc\':\n if ... | 7 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python 3 Solution for Beginners ( 3 letter soln) | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if len(s)==1: #if input contains only a \'?\'\n if s[0]==\'?\':\n return \'a\'\n s=list(s)\n for i in range(len(s)):\n if s[i]==\'?\':\n for c in \'abc\':\n if ... | 7 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s: list[str] = list(s)\n alpha: list[str] = list("abcdefghijklmnopqqrstuvwxyz?")\n if len(s) == 1 and s[0] == "?": return "a"\n\n for i in range(len(s)):\n if s[i] == "?":\n if i == 0:\n ... | 0 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python solution | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s: list[str] = list(s)\n alpha: list[str] = list("abcdefghijklmnopqqrstuvwxyz?")\n if len(s) == 1 and s[0] == "?": return "a"\n\n for i in range(len(s)):\n if s[i] == "?":\n if i == 0:\n ... | 0 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Simple solutions | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n # bruteforce solutions\n # 36ms\n # Beats 79.02% of users with Python3\n avail = {\n \'a\': \'a\', \'b\': \'b\', \'c\': \'c\', \'d\': \'d\', \'e\': \'e\', \n \'f\': \'f\', \'g\': \'g\', \'h\': \'... | 0 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Simple solutions | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n # bruteforce solutions\n # 36ms\n # Beats 79.02% of users with Python3\n avail = {\n \'a\': \'a\', \'b\': \'b\', \'c\': \'c\', \'d\': \'d\', \'e\': \'e\', \n \'f\': \'f\', \'g\': \'g\', \'h\': \'... | 0 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python3 using range() and filter() | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n l = list(s)\n i = range(ord(\'a\'), ord(\'z\'))\n for c in range(len(l)):\n if l[c] == \'?\':\n prev = l[c-1] if c != 0 else \'a\'\n nxt = l[c+1] if c != len(l)-1 else \'z\'\n ... | 0 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
Python3 using range() and filter() | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n l = list(s)\n i = range(ord(\'a\'), ord(\'z\'))\n for c in range(len(l)):\n if l[c] == \'?\':\n prev = l[c-1] if c != 0 else \'a\'\n nxt = l[c+1] if c != len(l)-1 else \'z\'\n ... | 0 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
EASY SOLUTION FOR BEGINNERS | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | \n# Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if(len(s)==1):\n return "a"\n if("?" in s):\n i = s.index("?")\n else:\n return s\n al = "abcdefghijklmnopqrstuvwxyz"\n con = s.count("?")\n print(con)\n for j i... | 0 | Given a string `s` containing only lowercase English letters and the `'?'` character, convert **all** the `'?'` characters into lowercase letters such that the final string does not contain any **consecutive repeating** characters. You **cannot** modify the non `'?'` characters.
It is **guaranteed** that there are no ... | Treat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge. |
EASY SOLUTION FOR BEGINNERS | replace-all-s-to-avoid-consecutive-repeating-characters | 0 | 1 | \n# Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if(len(s)==1):\n return "a"\n if("?" in s):\n i = s.index("?")\n else:\n return s\n al = "abcdefghijklmnopqrstuvwxyz"\n con = s.count("?")\n print(con)\n for j i... | 0 | Given a string `s`, return _the number of **distinct** substrings of_ `s`.
A **substring** of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
**Example 1:**
**Input:** s = "aabbaba "
**Output:** 21
**E... | Processing string from left to right, whenever you get a ‘?’, check left character and right character, and select a character not equal to either of them Do take care to compare with replaced occurrence of ‘?’ when checking the left character. |
Python3 Two Pointer Solution | O(1) Space, O(n*m) Time with Indepth Explanation | number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere are two types of triples we\'re looking for: \n\n Type 1: Triplet (i, j, k) if nums1[i]^2 == nums2[j] * nums2[k] \n Type 2: Triplet (i, j, k) if nums2[i]^2 == nums1[j] * nums1[k]\n\nNotice the symmetry. It\'s saying we must lo... | 0 | Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:
* Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
* Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j... | Check how many ways you can distribute the balls between the boxes. Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). The probability of ... |
Python3 solution || easy to understand || beginner's friendly | number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers | 0 | 1 | # Code\n```\nclass Solution:\n def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n Map1 = Counter([n * n for n in nums1])\n Map2 = Counter([n * n for n in nums2])\n\n res = 0\n for i in range(len(nums1) - 1):\n for j in range(i + 1, len(nums1)):\n ... | 0 | Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:
* Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
* Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j... | Check how many ways you can distribute the balls between the boxes. Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). The probability of ... |
Easy understand python solution | number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers | 0 | 1 | # Code\n```\nclass Solution:\n def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n Count1 = Counter(nums1)\n Count2 = Counter(nums2)\n res = 0\n for i in Count1:\n db = Count2.copy()\n while db:\n item = db.popitem()\n Q,... | 0 | Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:
* Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
* Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j... | Check how many ways you can distribute the balls between the boxes. Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). The probability of ... |
Brute Force Slowish | number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers | 0 | 1 | # Code\n```\nclass Solution:\n def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n def count_triplets(nums1: List[int], nums2: List[int]) -> int:\n count = 0\n product_map = defaultdict(int)\n\n for i in range(len(nums1)):\n for j in range(i + 1,... | 0 | Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:
* Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
* Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j... | Check how many ways you can distribute the balls between the boxes. Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). The probability of ... |
[LC-1577-M | Python3] A Plain Solution | number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers | 0 | 1 | Use `collections.Counter` and iterations to count.\n\n```python3 []\nclass Solution:\n def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n sq_cnter1 = Counter([num**2 for num in nums1])\n sq_cnter2 = Counter([num**2 for num in nums2])\n\n def count(nums, sq_cnter):\n n... | 0 | Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:
* Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
* Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j... | Check how many ways you can distribute the balls between the boxes. Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). The probability of ... |
Python3 solution. | number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition here is to find all the triplets that satisfy the given condition. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can use a two-pointer technique to solve this problem. We can iterate through each ... | 0 | Given two arrays of integers `nums1` and `nums2`, return the number of triplets formed (type 1 and type 2) under the following rules:
* Type 1: Triplet (i, j, k) if `nums1[i]2 == nums2[j] * nums2[k]` where `0 <= i < nums1.length` and `0 <= j < k < nums2.length`.
* Type 2: Triplet (i, j, k) if `nums2[i]2 == nums1[j... | Check how many ways you can distribute the balls between the boxes. Consider that one way you will use (x1, x2, x3, ..., xk) where xi is the number of balls from colour i. The probability of achieving this way randomly is ( (ball1 C x1) * (ball2 C x2) * (ball3 C x3) * ... * (ballk C xk)) / (2n C n). The probability of ... |
Python linear solution in O(N) Time complexity and O(1) Space complexity | minimum-time-to-make-rope-colorful | 0 | 1 | ```\nclass Solution:\n def minCost(self, colors: str, neededTime: List[int]) -> int:\n st,ct,total=0,1,0\n sm=neededTime[0]\n mx=neededTime[0]\n for i in range(1,len(colors)):\n if colors[i]==colors[i-1]:\n ct+=1\n sm+=neededTime[i]\n ... | 5 | Alice has `n` balloons arranged on a rope. You are given a **0-indexed** string `colors` where `colors[i]` is the color of the `ith` balloon.
Alice wants the rope to be **colorful**. She does not want **two consecutive balloons** to be of the same color, so she asks Bob for help. Bob can remove some balloons from the ... | null |
Python linear solution in O(N) Time complexity and O(1) Space complexity | minimum-time-to-make-rope-colorful | 0 | 1 | ```\nclass Solution:\n def minCost(self, colors: str, neededTime: List[int]) -> int:\n st,ct,total=0,1,0\n sm=neededTime[0]\n mx=neededTime[0]\n for i in range(1,len(colors)):\n if colors[i]==colors[i-1]:\n ct+=1\n sm+=neededTime[i]\n ... | 5 | The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed i... | Maintain the running sum and max value for repeated letters. |
Python 3 || 1578. Minimum Time to Make Rope Colorful | minimum-time-to-make-rope-colorful | 0 | 1 | I\'m not real proud of this one... T/M: 76%/16%\n```\nclass Solution:\n def minCost(self, colors: str, neededTime: List[int]) -> int:\n\t\n arr = list(zip(colors,neededTime))\n\t\t\n groupTime = [list(zip(*g))[1] for k, g in groupby(arr, lambda x: x[0])]\n\t\t\n return sum(neededTime) - sum(max(... | 3 | Alice has `n` balloons arranged on a rope. You are given a **0-indexed** string `colors` where `colors[i]` is the color of the `ith` balloon.
Alice wants the rope to be **colorful**. She does not want **two consecutive balloons** to be of the same color, so she asks Bob for help. Bob can remove some balloons from the ... | null |
Python 3 || 1578. Minimum Time to Make Rope Colorful | minimum-time-to-make-rope-colorful | 0 | 1 | I\'m not real proud of this one... T/M: 76%/16%\n```\nclass Solution:\n def minCost(self, colors: str, neededTime: List[int]) -> int:\n\t\n arr = list(zip(colors,neededTime))\n\t\t\n groupTime = [list(zip(*g))[1] for k, g in groupby(arr, lambda x: x[0])]\n\t\t\n return sum(neededTime) - sum(max(... | 3 | The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed i... | Maintain the running sum and max value for repeated letters. |
Simplest Python UF solution | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | \n# Code\n```\nclass DSU():\n def __init__(self, n):\n self.par = list(range(n))\n self.rank = [1] * n\n self.size = 1\n \n def find(self, u):\n if u != self.par[u]:\n self.par[u] = self.find(self.par[u])\n return self.par[u]\n \n def union(self, u, v):\n ... | 2 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
Simplest Python UF solution | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | \n# Code\n```\nclass DSU():\n def __init__(self, n):\n self.par = list(range(n))\n self.rank = [1] * n\n self.size = 1\n \n def find(self, u):\n if u != self.par[u]:\n self.par[u] = self.find(self.par[u])\n return self.par[u]\n \n def union(self, u, v):\n ... | 2 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
python 3 - DSU + union by rank + path compression | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | # Intuition\nAs long as you know DSU, this question is quite straghtforward.\n\nProcess edges in 2 parts:\n- Join all type 3 edges and then type 1 edges. This denotes all Alice edges. And edges with the same parent can be removed.\n- Same process will be done by using type 2 edges instead of type 1.\n\nCheck if all nod... | 2 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
python 3 - DSU + union by rank + path compression | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | # Intuition\nAs long as you know DSU, this question is quite straghtforward.\n\nProcess edges in 2 parts:\n- Join all type 3 edges and then type 1 edges. This denotes all Alice edges. And edges with the same parent can be removed.\n- Same process will be done by using type 2 edges instead of type 1.\n\nCheck if all nod... | 2 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
Python3 Solution | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | \n\n```\nclass Solution:\n\n def dfs(self,vis,li,com,i):\n if(vis[i]):\n return 0\n vis[i]=com\n ans=1\n for j in li[i]:\n ans+=self.dfs(vis,li,com,j)\n return ans\n\n def maxNumEdgesToRemove(self, n: int, e: List[List[int]]) -> int:\n ans=0\n ... | 2 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
Python3 Solution | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | \n\n```\nclass Solution:\n\n def dfs(self,vis,li,com,i):\n if(vis[i]):\n return 0\n vis[i]=com\n ans=1\n for j in li[i]:\n ans+=self.dfs(vis,li,com,j)\n return ans\n\n def maxNumEdgesToRemove(self, n: int, e: List[List[int]]) -> int:\n ans=0\n ... | 2 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
Python short and clean. DSU (Disjoint-Set-Union). Functional programming. | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | # Approach\nTL;DR, Similar to [Editorial solution](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/editorial/).\n\n# Complexity\n- Time complexity: $$O(e)$$\n\n- Space complexity: $$O(n)$$\n\nwhere,\n`n is number of nodes`,\n`e is number of edges`.\n\n# Code\n```python\nclass So... | 3 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
Python short and clean. DSU (Disjoint-Set-Union). Functional programming. | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | # Approach\nTL;DR, Similar to [Editorial solution](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/editorial/).\n\n# Complexity\n- Time complexity: $$O(e)$$\n\n- Space complexity: $$O(n)$$\n\nwhere,\n`n is number of nodes`,\n`e is number of edges`.\n\n# Code\n```python\nclass So... | 3 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
Beating 98.69% Python Easiest Solution | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | \n\n# Code\n```\nclass Solution:\n\n def dfs(self,vis,li,com,i):\n if(vis[i]):\n return 0\n vis[i]=com\n ans=1\n for j in li[i]:\n ans+=self.dfs(vis,li,c... | 1 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
Beating 98.69% Python Easiest Solution | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | \n\n# Code\n```\nclass Solution:\n\n def dfs(self,vis,li,com,i):\n if(vis[i]):\n return 0\n vis[i]=com\n ans=1\n for j in li[i]:\n ans+=self.dfs(vis,li,c... | 1 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
Image Explanation🏆- [Easiest & Complete Intuition] - C++/Java/Python | remove-max-number-of-edges-to-keep-graph-fully-traversable | 1 | 1 | # Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`Remove Max Number of Edges to Keep Graph Fully Traversable` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n - Link in LeetCode Profile\n`Remove Max Number of Edges to Keep Graph Fully Traversable` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n. Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
Kruskal MST | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | # Intuition\nMinimum spanning tree with Kruskal algorithm (disjoint set), with some hairs.\n\n# Approach\nFirst attemp to use all type-3 edges (both) (phase 1), then use type-1 (alice-only) and type-2 (bob-only) separatedly (phase 2). Until the tree is built. Register the total edge used.\n\nProof of correctness:\n\n0.... | 1 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
Kruskal MST | remove-max-number-of-edges-to-keep-graph-fully-traversable | 0 | 1 | # Intuition\nMinimum spanning tree with Kruskal algorithm (disjoint set), with some hairs.\n\n# Approach\nFirst attemp to use all type-3 edges (both) (phase 1), then use type-1 (alice-only) and type-2 (bob-only) separatedly (phase 2). Until the tree is built. Register the total edge used.\n\nProof of correctness:\n\n0.... | 1 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
✔💯 DAY 395 | DSU | 100% 0ms | [PYTHON/JAVA/C++] | EXPLAINED | Approach 🆙🆙🆙 | remove-max-number-of-edges-to-keep-graph-fully-traversable | 1 | 1 | # Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n\n\n# Intuition \n##### \u2022\tWe have a graph with N nodes and bidirectional edges of three types: Type 1 (Alice only), Type ... | 26 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
✔💯 DAY 395 | DSU | 100% 0ms | [PYTHON/JAVA/C++] | EXPLAINED | Approach 🆙🆙🆙 | remove-max-number-of-edges-to-keep-graph-fully-traversable | 1 | 1 | # Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n\n\n# Intuition \n##### \u2022\tWe have a graph with N nodes and bidirectional edges of three types: Type 1 (Alice only), Type ... | 26 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
[Python3] - Union Find - Easy to understand | remove-max-number-of-edges-to-keep-graph-fully-traversable | 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)$$ --... | 6 | Alice and Bob have an undirected graph of `n` nodes and three types of edges:
* Type 1: Can be traversed by Alice only.
* Type 2: Can be traversed by Bob only.
* Type 3: Can be traversed by both Alice and Bob.
Given an array `edges` where `edges[i] = [typei, ui, vi]` represents a bidirectional edge of type `typ... | null |
[Python3] - Union Find - Easy to understand | remove-max-number-of-edges-to-keep-graph-fully-traversable | 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)$$ --... | 6 | There is a restaurant with a single chef. You are given an array `customers`, where `customers[i] = [arrivali, timei]:`
* `arrivali` is the arrival time of the `ith` customer. The arrival times are sorted in **non-decreasing** order.
* `timei` is the time needed to prepare the order of the `ith` customer.
When a ... | Build the network instead of removing extra edges. Suppose you have the final graph (after removing extra edges). Consider the subgraph with only the edges that Alice can traverse. What structure does this subgraph have? How many edges are there? Use disjoint set union data structure for both Alice and Bob. Always use ... |
🚀🚀🚀EASY SOLUTiON EVER !!! || Java || C++ || Python || JS || C# || Ruby || Go 🚀🚀🚀 by PRODONiK | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\nThe problem aims to find the number of special positions in a given matrix. A position is considered special if the element at that position is 1, and there is no other 1 in the same row and the same column.\n\n# Approach\nThe `numSpecial` method iterates over each row in the matrix. For each element with ... | 16 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
🚀🚀🚀EASY SOLUTiON EVER !!! || Java || C++ || Python || JS || C# || Ruby || Go 🚀🚀🚀 by PRODONiK | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\nThe problem aims to find the number of special positions in a given matrix. A position is considered special if the element at that position is 1, and there is no other 1 in the same row and the same column.\n\n# Approach\nThe `numSpecial` method iterates over each row in the matrix. For each element with ... | 16 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
【Video】Give me 5 minutes - How we think about a solution | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\nCheck row and column direction.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/qTAh4HGfUHk\n\n\u25A0 Timeline of the video\n\n`0:05` Explain algorithm of the first step\n`1:06` Explain algorithm of the second step\n`2:36` Coding\n`4:17` Time Complexity and Space Complexity\n`4:55` Step by step algorithm of... | 56 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
【Video】Give me 5 minutes - How we think about a solution | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\nCheck row and column direction.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/qTAh4HGfUHk\n\n\u25A0 Timeline of the video\n\n`0:05` Explain algorithm of the first step\n`1:06` Explain algorithm of the second step\n`2:36` Coding\n`4:17` Time Complexity and Space Complexity\n`4:55` Step by step algorithm of... | 56 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥 | special-positions-in-a-binary-matrix | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Brute Force)***\n1. It iterates through each element of the matrix, checking if it equals 1.\n1. For each element that equals 1, it checks the entire row and column to see if there are any other \'1\'s apart fr... | 38 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
✅☑[C++/Java/Python/JavaScript] || 2 Approaches || EXPLAINED🔥 | special-positions-in-a-binary-matrix | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Brute Force)***\n1. It iterates through each element of the matrix, checking if it equals 1.\n1. For each element that equals 1, it checks the entire row and column to see if there are any other \'1\'s apart fr... | 38 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
C++/Python one pass||0 ms Beats 100% | special-positions-in-a-binary-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck which rows & columns have exactly one 1!\n\nThe revised C++ code simplifies the loop. The unnecessary copying for column vector is omitted which becomes a fast code, runs in 0 ms and beats 100%!\n\nPython code is in the same manner ... | 9 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
C++/Python one pass||0 ms Beats 100% | special-positions-in-a-binary-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck which rows & columns have exactly one 1!\n\nThe revised C++ code simplifies the loop. The unnecessary copying for column vector is omitted which becomes a fast code, runs in 0 ms and beats 100%!\n\nPython code is in the same manner ... | 9 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
🚀✅🚀 100% beats Users by TC || Java, C#, Python, Rust, C++, C, Ruby, Go|| 🔥🔥 | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\nThe problem seems to be asking for the number of special positions in a binary matrix. A position is considered special if it contains a 1 and the sum of elements in its row and column is 1.\n\n# Approach\nThe provided code iterates through each element of the matrix and checks if it is 1. If it is, it cal... | 9 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
🚀✅🚀 100% beats Users by TC || Java, C#, Python, Rust, C++, C, Ruby, Go|| 🔥🔥 | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\nThe problem seems to be asking for the number of special positions in a binary matrix. A position is considered special if it contains a 1 and the sum of elements in its row and column is 1.\n\n# Approach\nThe provided code iterates through each element of the matrix and checks if it is 1. If it is, it cal... | 9 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
💯Faster✅💯 Lesser✅2 Methods🔥Brute Force🔥Hashing🔥Python🐍Java☕C++✅C📈 | special-positions-in-a-binary-matrix | 1 | 1 | # \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 2 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe problem involves finding the number of special positions in a binary matrix. A specia... | 23 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
💯Faster✅💯 Lesser✅2 Methods🔥Brute Force🔥Hashing🔥Python🐍Java☕C++✅C📈 | special-positions-in-a-binary-matrix | 1 | 1 | # \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 2 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe problem involves finding the number of special positions in a binary matrix. A specia... | 23 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
⭐✅|| STORING EACH ROW AND COL SUM || WELL EXPLAINED (HINDI) || BEATS 100% || ✅⭐ | special-positions-in-a-binary-matrix | 1 | 1 | \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# ALGORITHM\n<!-- Describe your approach to solving the problem. -->\n1)**Matrix ke dimensions nikalo**:\nInput matrix mat ki rows (n) aur columns (m) ka pata lagayein.\n\n2)**Variables ko initialize karein**:\nans naamak ek variable ko 0 se initi... | 5 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
⭐✅|| STORING EACH ROW AND COL SUM || WELL EXPLAINED (HINDI) || BEATS 100% || ✅⭐ | special-positions-in-a-binary-matrix | 1 | 1 | \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# ALGORITHM\n<!-- Describe your approach to solving the problem. -->\n1)**Matrix ke dimensions nikalo**:\nInput matrix mat ki rows (n) aur columns (m) ka pata lagayein.\n\n2)**Variables ko initialize karein**:\nans naamak ek variable ko 0 se initi... | 5 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
[We💕Simple] One Liner! (Beat 100%) | special-positions-in-a-binary-matrix | 0 | 1 | ## Python3\n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n return sum(\n sum(mat[i][col] for i in range(len(mat))) == 1\n for col in [\n row.index(1)\n for row in mat\n if sum(row) == 1\n ]\n )\n\... | 3 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
[We💕Simple] One Liner! (Beat 100%) | special-positions-in-a-binary-matrix | 0 | 1 | ## Python3\n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n return sum(\n sum(mat[i][col] for i in range(len(mat))) == 1\n for col in [\n row.index(1)\n for row in mat\n if sum(row) == 1\n ]\n )\n\... | 3 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
🚀🚀 Beats 95% | Easy To Understand | Fully Explained 😎💖 | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\n- We want to find special positions in a binary matrix.\n- A position is special if the element at that position is 1 and all other elements in its row and column are 0.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Iterate through the matrix to find the positions w... | 6 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
🚀🚀 Beats 95% | Easy To Understand | Fully Explained 😎💖 | special-positions-in-a-binary-matrix | 1 | 1 | # Intuition\n- We want to find special positions in a binary matrix.\n- A position is special if the element at that position is 1 and all other elements in its row and column are 0.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Iterate through the matrix to find the positions w... | 6 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
Python3 Solution | special-positions-in-a-binary-matrix | 0 | 1 | \n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n n=len(mat)\n m=len(mat[0])\n ans=0\n transposeMat=list(zip(*mat))\n for i in range(n):\n for j in range(m):\n if mat[i][j]==1 and sum(mat[i])==1 and sum(transposeMat[j])==1:\n ... | 2 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
Python3 Solution | special-positions-in-a-binary-matrix | 0 | 1 | \n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n n=len(mat)\n m=len(mat[0])\n ans=0\n transposeMat=list(zip(*mat))\n for i in range(n):\n for j in range(m):\n if mat[i][j]==1 and sum(mat[i])==1 and sum(transposeMat[j])==1:\n ... | 2 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
✅ One Line Solution | special-positions-in-a-binary-matrix | 0 | 1 | (Disclaimer: these are not examples to follow in a real project - they are written for fun and training mostly)\n# Code #1\nTime complexity: $$O(m*n)$$. Space complexity: $$O(1)$$.\n```\nclass Solution:\n def numSpecial(self, M: List[List[int]]) -> int:\n return sum(sum(rr[j] for rr in M)==1 for r in M if sum... | 4 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
✅ One Line Solution | special-positions-in-a-binary-matrix | 0 | 1 | (Disclaimer: these are not examples to follow in a real project - they are written for fun and training mostly)\n# Code #1\nTime complexity: $$O(m*n)$$. Space complexity: $$O(1)$$.\n```\nclass Solution:\n def numSpecial(self, M: List[List[int]]) -> int:\n return sum(sum(rr[j] for rr in M)==1 for r in M if sum... | 4 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
Easy Solution || Python | special-positions-in-a-binary-matrix | 0 | 1 | # Intuition\nFirst iterating through the matrix gives us the position of ones and we calculate if there are any ones in the respective row or column.\nIn the 2nd iteration we check if the ones are in special position.\n\n\n\n# Complexity\n- Time complexity:\nO(m*n)\n\n- Space complexity:\nO(m+n)\n\n# Code\n```\nclass S... | 1 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
Easy Solution || Python | special-positions-in-a-binary-matrix | 0 | 1 | # Intuition\nFirst iterating through the matrix gives us the position of ones and we calculate if there are any ones in the respective row or column.\nIn the 2nd iteration we check if the ones are in special position.\n\n\n\n# Complexity\n- Time complexity:\nO(m*n)\n\n- Space complexity:\nO(m+n)\n\n# Code\n```\nclass S... | 1 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
Simple Solution To count Special Cases | special-positions-in-a-binary-matrix | 0 | 1 | # Code\n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n answer = 0\n for row in range(len(mat)):\n for column in range(len(mat[row])):\n vertical = sum([i[column] for i in mat])\n horizontal = sum([horizontal for horizontal in mat[row... | 1 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
Simple Solution To count Special Cases | special-positions-in-a-binary-matrix | 0 | 1 | # Code\n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n answer = 0\n for row in range(len(mat)):\n for column in range(len(mat[row])):\n vertical = sum([i[column] for i in mat])\n horizontal = sum([horizontal for horizontal in mat[row... | 1 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
Easy solution python || store and access | special-positions-in-a-binary-matrix | 0 | 1 | \n# Complexity\n- Time complexity:O(n*m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(m+n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n row=[0]*len(mat)\n col=[0]*len(mat[... | 1 | Given an `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
Easy solution python || store and access | special-positions-in-a-binary-matrix | 0 | 1 | \n# Complexity\n- Time complexity:O(n*m)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(m+n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def numSpecial(self, mat: List[List[int]]) -> int:\n row=[0]*len(mat)\n col=[0]*len(mat[... | 1 | You are given a string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
Solution using sum | special-positions-in-a-binary-matrix | 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 `m x n` binary matrix `mat`, return _the number of special positions in_ `mat`_._
A position `(i, j)` is called **special** if `mat[i][j] == 1` and all other elements in row `i` and column `j` are `0` (rows and columns are **0-indexed**).
**Example 1:**
**Input:** mat = \[\[1,0,0\],\[0,0,1\],\[1,0,0\]\]
**O... | Use two stack one for back history and one for forward history and simulate the functions. Can you do faster by using different data structure ? |
Solution using sum | special-positions-in-a-binary-matrix | 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 string `s` of even length. Split this string into two halves of equal lengths, and let `a` be the first half and `b` be the second half.
Two strings are **alike** if they have the same number of vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`, `'A'`, `'E'`, `'I'`, `'O'`, `'U'`). Notice that `s` contains uppe... | Keep track of 1s in each row and in each column. Then while iterating over matrix, if the current position is 1 and current row as well as current column contains exactly one occurrence of 1. |
Python 3 || 4 lines, w/ explanation || T/M: 95% / 71% | count-unhappy-friends | 0 | 1 | Here\'s how the code works:\n\nWe initialize a defaultdict`sc`, which will store the preferred friends for each pair `u` and friend `v` in the pairs.\n\nWe iterate [u, v] in the pairs list. For each pair in`pairs`, we assign the preferred friends before`u`and `v` to `sc[u]` and `sc[v]` respectively. The preferred frien... | 3 | You are given a list of `preferences` for `n` friends, where `n` is always **even**.
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denot... | Use Dynamic programming. Define dp[i][j][k] as the minimum cost where we have k neighborhoods in the first i houses and the i-th house is painted with the color j. |
Python 3 || 4 lines, w/ explanation || T/M: 95% / 71% | count-unhappy-friends | 0 | 1 | Here\'s how the code works:\n\nWe initialize a defaultdict`sc`, which will store the preferred friends for each pair `u` and friend `v` in the pairs.\n\nWe iterate [u, v] in the pairs list. For each pair in`pairs`, we assign the preferred friends before`u`and `v` to `sc[u]` and `sc[v]` respectively. The preferred frien... | 3 | There is a special kind of apple tree that grows apples every day for `n` days. On the `ith` day, the tree grows `apples[i]` apples that will rot after `days[i]` days, that is on day `i + days[i]` the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by ... | Create a matrix “rank” where rank[i][j] holds how highly friend ‘i' views ‘j’. This allows for O(1) comparisons between people |
[Python] Make you happy with clean and concise solution | count-unhappy-friends | 0 | 1 | The problem description is not very clear. Please find below clean, concise and self explanatory code. \nTime Complexity: O(n^2). Space Complexity: O(n^2)\nIncluding comments in the code for better understanding. Kindly upvote if you like the solution.\n\n\t\t#Map to get pair mapping\n pairMap = defaultdict(int)... | 18 | You are given a list of `preferences` for `n` friends, where `n` is always **even**.
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denot... | Use Dynamic programming. Define dp[i][j][k] as the minimum cost where we have k neighborhoods in the first i houses and the i-th house is painted with the color j. |
[Python] Make you happy with clean and concise solution | count-unhappy-friends | 0 | 1 | The problem description is not very clear. Please find below clean, concise and self explanatory code. \nTime Complexity: O(n^2). Space Complexity: O(n^2)\nIncluding comments in the code for better understanding. Kindly upvote if you like the solution.\n\n\t\t#Map to get pair mapping\n pairMap = defaultdict(int)... | 18 | There is a special kind of apple tree that grows apples every day for `n` days. On the `ith` day, the tree grows `apples[i]` apples that will rot after `days[i]` days, that is on day `i + days[i]` the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by ... | Create a matrix “rank” where rank[i][j] holds how highly friend ‘i' views ‘j’. This allows for O(1) comparisons between people |
Python | EASY | Explained |99% fast | count-unhappy-friends | 0 | 1 | As per the explanation we got to understand that we first have to make somthing that holds people with their priorities and whats better than a DICT to do so . \nSo we can say as per the first example friend have a preferece:\n0 : 1,2,3\n1 : 3,2,0\n2 : 3,1,0\n3 : 1,2,0\n\nAnd given pairs are = [[0, 1], [2, 3]]\n0 is h... | 3 | You are given a list of `preferences` for `n` friends, where `n` is always **even**.
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denot... | Use Dynamic programming. Define dp[i][j][k] as the minimum cost where we have k neighborhoods in the first i houses and the i-th house is painted with the color j. |
Python | EASY | Explained |99% fast | count-unhappy-friends | 0 | 1 | As per the explanation we got to understand that we first have to make somthing that holds people with their priorities and whats better than a DICT to do so . \nSo we can say as per the first example friend have a preferece:\n0 : 1,2,3\n1 : 3,2,0\n2 : 3,1,0\n3 : 1,2,0\n\nAnd given pairs are = [[0, 1], [2, 3]]\n0 is h... | 3 | There is a special kind of apple tree that grows apples every day for `n` days. On the `ith` day, the tree grows `apples[i]` apples that will rot after `days[i]` days, that is on day `i + days[i]` the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by ... | Create a matrix “rank” where rank[i][j] holds how highly friend ‘i' views ‘j’. This allows for O(1) comparisons between people |
Beat 99% with clear logic | count-unhappy-friends | 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 list of `preferences` for `n` friends, where `n` is always **even**.
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denot... | Use Dynamic programming. Define dp[i][j][k] as the minimum cost where we have k neighborhoods in the first i houses and the i-th house is painted with the color j. |
Beat 99% with clear logic | count-unhappy-friends | 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 | There is a special kind of apple tree that grows apples every day for `n` days. On the `ith` day, the tree grows `apples[i]` apples that will rot after `days[i]` days, that is on day `i + days[i]` the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by ... | Create a matrix “rank” where rank[i][j] holds how highly friend ‘i' views ‘j’. This allows for O(1) comparisons between people |
python3 + simulation | count-unhappy-friends | 0 | 1 | # Code\n```\nclass Solution:\n def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:\n d = {}\n p = {}\n for idx,e in enumerate(preferences):\n d[idx] = e\n c = set()\n for x,y in pairs:\n p[x] = y\n p[y] = x... | 0 | You are given a list of `preferences` for `n` friends, where `n` is always **even**.
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denot... | Use Dynamic programming. Define dp[i][j][k] as the minimum cost where we have k neighborhoods in the first i houses and the i-th house is painted with the color j. |
python3 + simulation | count-unhappy-friends | 0 | 1 | # Code\n```\nclass Solution:\n def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:\n d = {}\n p = {}\n for idx,e in enumerate(preferences):\n d[idx] = e\n c = set()\n for x,y in pairs:\n p[x] = y\n p[y] = x... | 0 | There is a special kind of apple tree that grows apples every day for `n` days. On the `ith` day, the tree grows `apples[i]` apples that will rot after `days[i]` days, that is on day `i + days[i]` the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by ... | Create a matrix “rank” where rank[i][j] holds how highly friend ‘i' views ‘j’. This allows for O(1) comparisons between people |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.