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 |
|---|---|---|---|---|---|---|---|
[Java/Python 3] 3 One liners + one w/o lib w/ analysis. | defanging-an-ip-address | 1 | 1 | ```java\n public String defangIPaddr(String address) {\n return address.replace(".", "[.]");\n }\n public String defangIPaddr(String address) {\n return String.join("[.]", address.split("\\\\."));\n }\n public String defangIPaddr(String address) {\n return address.replaceAll("\\\\.",... | 196 | Given a valid (IPv4) IP `address`, return a defanged version of that IP address.
A _defanged IP address_ replaces every period `". "` with `"[.] "`.
**Example 1:**
**Input:** address = "1.1.1.1"
**Output:** "1\[.\]1\[.\]1\[.\]1"
**Example 2:**
**Input:** address = "255.100.50.0"
**Output:** "255\[.\]100\[.\]50\[.\... | Let's find for every user separately the websites he visited. Consider all possible 3-sequences, find the number of distinct users who visited each of them. How to check if some user visited some 3-sequence ? Store for every user all the 3-sequence he visited. |
Simple Python Code with if else statement | defanging-an-ip-address | 0 | 1 | # Approach\nsimple approch\n\n# Code\n```\nclass Solution:\n def defangIPaddr(self, address: str) -> str:\n s = \'\'\n for i in address:\n if i == \'.\': s+=\'[.]\'\n else: s+=i\n return s\n``` | 2 | Given a valid (IPv4) IP `address`, return a defanged version of that IP address.
A _defanged IP address_ replaces every period `". "` with `"[.] "`.
**Example 1:**
**Input:** address = "1.1.1.1"
**Output:** "1\[.\]1\[.\]1\[.\]1"
**Example 2:**
**Input:** address = "255.100.50.0"
**Output:** "255\[.\]100\[.\]50\[.\... | Let's find for every user separately the websites he visited. Consider all possible 3-sequences, find the number of distinct users who visited each of them. How to check if some user visited some 3-sequence ? Store for every user all the 3-sequence he visited. |
using .replace | defanging-an-ip-address | 0 | 1 | \n# Code\n```\nclass Solution:\n def defangIPaddr(self, address: str) -> str:\n return address.replace(".","[.]")\n``` | 2 | Given a valid (IPv4) IP `address`, return a defanged version of that IP address.
A _defanged IP address_ replaces every period `". "` with `"[.] "`.
**Example 1:**
**Input:** address = "1.1.1.1"
**Output:** "1\[.\]1\[.\]1\[.\]1"
**Example 2:**
**Input:** address = "255.100.50.0"
**Output:** "255\[.\]100\[.\]50\[.\... | Let's find for every user separately the websites he visited. Consider all possible 3-sequences, find the number of distinct users who visited each of them. How to check if some user visited some 3-sequence ? Store for every user all the 3-sequence he visited. |
Python 3 || 5 lines, prefix sum || T/M: 100% / 24% | corporate-flight-bookings | 0 | 1 | ```\nclass Solution:\n def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:\n\n arr = [0]*(n+1)\n for lv, ar, seats in bookings:\n arr[lv-1]+= seats\n arr[ar]-= seats\n\n return list(accumulate(arr[:-1]))\n```\n[https://leetcode.com/problems/corpora... | 6 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Python 3 || 5 lines, prefix sum || T/M: 100% / 24% | corporate-flight-bookings | 0 | 1 | ```\nclass Solution:\n def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:\n\n arr = [0]*(n+1)\n for lv, ar, seats in bookings:\n arr[lv-1]+= seats\n arr[ar]-= seats\n\n return list(accumulate(arr[:-1]))\n```\n[https://leetcode.com/problems/corpora... | 6 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
[Python 3] - Different Arrays data structure | corporate-flight-bookings | 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)$$ --... | 5 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
[Python 3] - Different Arrays data structure | corporate-flight-bookings | 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)$$ --... | 5 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Solution using Range Addition | corporate-flight-bookings | 0 | 1 | # Intuition\nThis is the same problem as range addition. For any booking, we essentially add a number of seats to the specified range. \n\n# Approach\nThere are `n` days in total. So we can create an array with length `n`, representing the difference array (ith entry represents the difference of ith and the (i-1)th ent... | 2 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Solution using Range Addition | corporate-flight-bookings | 0 | 1 | # Intuition\nThis is the same problem as range addition. For any booking, we essentially add a number of seats to the specified range. \n\n# Approach\nThere are `n` days in total. So we can create an array with length `n`, representing the difference array (ith entry represents the difference of ith and the (i-1)th ent... | 2 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Easy Python O(n) using accumulate | corporate-flight-bookings | 0 | 1 | ```\nclass Solution:\n def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:\n res = [0]*n\n for first, last, seat in bookings:\n res[first - 1] += seat\n if last < n:\n res[last] -= seat\n return accumulate(res)\n``` | 4 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Easy Python O(n) using accumulate | corporate-flight-bookings | 0 | 1 | ```\nclass Solution:\n def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:\n res = [0]*n\n for first, last, seat in bookings:\n res[first - 1] += seat\n if last < n:\n res[last] -= seat\n return accumulate(res)\n``` | 4 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Python O(n) solution | Top 98% Speed | 9 Lines of Code | corporate-flight-bookings | 0 | 1 | **Python O(n) solution | Top 98% Speed | 9 Lines of Code**\n\n"Cummulative Sum" Algorithm Optmized for Maximum speed.\n\n```\nclass Solution:\n def corpFlightBookings(self, bookings, n: int):\n ans = [0]*(n+1)\n for i,j,k in bookings:\n ans[i-1] += k\n ans[j] -= k\n ans.p... | 8 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Python O(n) solution | Top 98% Speed | 9 Lines of Code | corporate-flight-bookings | 0 | 1 | **Python O(n) solution | Top 98% Speed | 9 Lines of Code**\n\n"Cummulative Sum" Algorithm Optmized for Maximum speed.\n\n```\nclass Solution:\n def corpFlightBookings(self, bookings, n: int):\n ans = [0]*(n+1)\n for i,j,k in bookings:\n ans[i-1] += k\n ans[j] -= k\n ans.p... | 8 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
PYTHON SOL | O( M + N ) | EXPLAINED WELL | FAST | ARRAYS | | corporate-flight-bookings | 0 | 1 | # Runtime: 896 ms, faster than 94.45% of Python3 online submissions for Corporate Flight Bookings .Memory Usage: 28.6 MB, less than 13.12% of Python3 online submissions for Corporate Flight Bookings.\n\n\n\n\n# EXPLANATION\n\n```\nRemember whenever we want the total for each place and we need to fill via the\nintervals... | 1 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
PYTHON SOL | O( M + N ) | EXPLAINED WELL | FAST | ARRAYS | | corporate-flight-bookings | 0 | 1 | # Runtime: 896 ms, faster than 94.45% of Python3 online submissions for Corporate Flight Bookings .Memory Usage: 28.6 MB, less than 13.12% of Python3 online submissions for Corporate Flight Bookings.\n\n\n\n\n# EXPLANATION\n\n```\nRemember whenever we want the total for each place and we need to fill via the\nintervals... | 1 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
[Python3] Good enough | corporate-flight-bookings | 0 | 1 | ``` Python3 []\nclass Solution:\n def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:\n total = [0]*n\n\n for x in bookings:\n total[x[0]-1] += x[2]\n if x[1]<n:\n total[x[1]] -= x[2]\n \n for i in range(1,n):\n tot... | 0 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
[Python3] Good enough | corporate-flight-bookings | 0 | 1 | ``` Python3 []\nclass Solution:\n def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:\n total = [0]*n\n\n for x in bookings:\n total[x[0]-1] += x[2]\n if x[1]<n:\n total[x[1]] -= x[2]\n \n for i in range(1,n):\n tot... | 0 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
1109, difference | corporate-flight-bookings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCalculate the difference between adjacent elements.\n\n# Complexity\n- Time complexity: n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n`... | 0 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
1109, difference | corporate-flight-bookings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCalculate the difference between adjacent elements.\n\n# Complexity\n- Time complexity: n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n`... | 0 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Fast🚀 & Easy🍼 to understand | corporate-flight-bookings | 0 | 1 | ### My solutions are usually POV answers, what that means is reading it as if you wrote it will make more sense and familiarity.\n###### Do upvote, If you liked it \u2B06\uFE0F\n# Intuition\nOkay, so I thought of many ways and could not understand how to solve the problem, so thought of reading the solutions and then t... | 0 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Fast🚀 & Easy🍼 to understand | corporate-flight-bookings | 0 | 1 | ### My solutions are usually POV answers, what that means is reading it as if you wrote it will make more sense and familiarity.\n###### Do upvote, If you liked it \u2B06\uFE0F\n# Intuition\nOkay, so I thought of many ways and could not understand how to solve the problem, so thought of reading the solutions and then t... | 0 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Easy Python O(N) | corporate-flight-bookings | 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 are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Easy Python O(N) | corporate-flight-bookings | 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 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Python Corporate Flight Bookings - beats 96% | corporate-flight-bookings | 0 | 1 | \n# Intuition\n- The code processes a list of flight booking records, each containing the starting flight, ending flight, and the number of bookings.\n- It aims to calculate the total number of bookings for each flight within the range of flights from 1 to `n`.\n\n# Approach\n- The code initializes a list `result` of s... | 0 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Python Corporate Flight Bookings - beats 96% | corporate-flight-bookings | 0 | 1 | \n# Intuition\n- The code processes a list of flight booking records, each containing the starting flight, ending flight, and the number of bookings.\n- It aims to calculate the total number of bookings for each flight within the range of flights from 1 to `n`.\n\n# Approach\n- The code initializes a list `result` of s... | 0 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Python easy cummulative sum approach O(n) | corporate-flight-bookings | 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. -->\n1. Initialize an array answer of length (n + 1) to represent the number of seats reserved for each flight. We add one extra element to the array for convenience.\n\n2.... | 0 | There are `n` flights that are labeled from `1` to `n`.
You are given an array of flight bookings `bookings`, where `bookings[i] = [firsti, lasti, seatsi]` represents a booking for flights `firsti` through `lasti` (**inclusive**) with `seatsi` seats reserved for **each flight** in the range.
Return _an array_ `answer... | null |
Python easy cummulative sum approach O(n) | corporate-flight-bookings | 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. -->\n1. Initialize an array answer of length (n + 1) to represent the number of seats reserved for each flight. We add one extra element to the array for convenience.\n\n2.... | 0 | Design a **Skiplist** without using any built-in libraries.
A **skiplist** is a data structure that takes `O(log(n))` time to add, erase and search. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be comparatively short and the idea behind Skiplists ... | null |
Commented Python solution DFS (recursive) | delete-nodes-and-return-forest | 0 | 1 | # Code\n```\n\nclass Solution:\n def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:\n # really good explanation: https://leetcode.com/problems/delete-nodes-and-return-forest/solutions/656106/python-recursive-clean-explained-in-details-with-tips-10-lines-fast/\n res = ... | 3 | Given the `root` of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in `to_delete`, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
**Example 1:**
**Input:** root = ... | null |
Commented Python solution DFS (recursive) | delete-nodes-and-return-forest | 0 | 1 | # Code\n```\n\nclass Solution:\n def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:\n # really good explanation: https://leetcode.com/problems/delete-nodes-and-return-forest/solutions/656106/python-recursive-clean-explained-in-details-with-tips-10-lines-fast/\n res = ... | 3 | Given an array of integers `arr`, return `true` _if the number of occurrences of each value in the array is **unique** or_ `false` _otherwise_.
**Example 1:**
**Input:** arr = \[1,2,2,1,1,3\]
**Output:** true
**Explanation:** The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of oc... | null |
[Python] BFS Solution | delete-nodes-and-return-forest | 0 | 1 | ```class Solution:\n def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n queue = collections.deque([(root, False)])\n res = []\n deleteSet = set(to_delete)\n \n while queue:\n node, hasParent = queue.popleft()\n # new Root found\n ... | 61 | Given the `root` of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in `to_delete`, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
**Example 1:**
**Input:** root = ... | null |
[Python] BFS Solution | delete-nodes-and-return-forest | 0 | 1 | ```class Solution:\n def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:\n queue = collections.deque([(root, False)])\n res = []\n deleteSet = set(to_delete)\n \n while queue:\n node, hasParent = queue.popleft()\n # new Root found\n ... | 61 | Given an array of integers `arr`, return `true` _if the number of occurrences of each value in the array is **unique** or_ `false` _otherwise_.
**Example 1:**
**Input:** arr = \[1,2,2,1,1,3\]
**Output:** true
**Explanation:** The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of oc... | null |
Simple logic || Two cases, check every node || 99% | delete-nodes-and-return-forest | 1 | 1 | # Intuition\n\nSuppose the current node, say $$node$$, you\'re checking is not present in the $$to$$_$$delete$$ array. You leave $$node$$ untouched and check it\'s $$left$$ and $$right$$ children.\n\nIf $$node$$ is present in $$to$$_$$delete$$ array, we need to tell it\'s parent to not point to this node. We also need ... | 3 | Given the `root` of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in `to_delete`, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
**Example 1:**
**Input:** root = ... | null |
Simple logic || Two cases, check every node || 99% | delete-nodes-and-return-forest | 1 | 1 | # Intuition\n\nSuppose the current node, say $$node$$, you\'re checking is not present in the $$to$$_$$delete$$ array. You leave $$node$$ untouched and check it\'s $$left$$ and $$right$$ children.\n\nIf $$node$$ is present in $$to$$_$$delete$$ array, we need to tell it\'s parent to not point to this node. We also need ... | 3 | Given an array of integers `arr`, return `true` _if the number of occurrences of each value in the array is **unique** or_ `false` _otherwise_.
**Example 1:**
**Input:** arr = \[1,2,2,1,1,3\]
**Output:** true
**Explanation:** The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of oc... | null |
Python || 98.06% Faster || Greedy Approach || O(N) Solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | ```\nclass Solution:\n def maxDepthAfterSplit(self, seq: str) -> List[int]:\n ans=[]\n prev=1\n for i in seq:\n if i==\'(\':\n if prev==0:\n ans.append(1)\n else:\n ans.append(0)\n else:\n an... | 2 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Python || 98.06% Faster || Greedy Approach || O(N) Solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | ```\nclass Solution:\n def maxDepthAfterSplit(self, seq: str) -> List[int]:\n ans=[]\n prev=1\n for i in seq:\n if i==\'(\':\n if prev==0:\n ans.append(1)\n else:\n ans.append(0)\n else:\n an... | 2 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Python || Easy || Explained || O(n) Solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | ```\nclass Solution:\n def maxDepthAfterSplit(self, seq: str) -> List[int]:\n m,c,n=0,0,len(seq)\n for i in range(n):\n if seq[i]==\'(\':\n c+=1\n m=max(c,m) # Here m is the maximium depth of the VPS\n elif seq[i]==\')\': \n c-=1\n ... | 1 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Python || Easy || Explained || O(n) Solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | ```\nclass Solution:\n def maxDepthAfterSplit(self, seq: str) -> List[int]:\n m,c,n=0,0,len(seq)\n for i in range(n):\n if seq[i]==\'(\':\n c+=1\n m=max(c,m) # Here m is the maximium depth of the VPS\n elif seq[i]==\')\': \n c-=1\n ... | 1 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Python | 90% Faster | Clear Explanation | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | \n\n\nI was very confused by this problem, but after looking at some of the testcases, what we have to return is if the depth is odd, we have to return 0, and if it is even, we have to return 1, or I think ... | 1 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Python | 90% Faster | Clear Explanation | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | \n\n\nI was very confused by this problem, but after looking at some of the testcases, what we have to return is if the depth is odd, we have to return 0, and if it is even, we have to return 1, or I think ... | 1 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Python - O(n) | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAssign ( alternatively to A and B. Assign ) to whatever the last ( was assigned to.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity he... | 0 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Python - O(n) | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAssign ( alternatively to A and B. Assign ) to whatever the last ( was assigned to.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity he... | 0 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Beat 100% Python Solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo minimize the max of two split parenthesis, we need to make two parts\' depth as same as possible (ideally even split). Thus, we could first calculate the depth of each parenthesis. \n\nExample: depths of \'(())()\' will be [1, 2, 2, 1,... | 0 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Beat 100% Python Solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo minimize the max of two split parenthesis, we need to make two parts\' depth as same as possible (ideally even split). Thus, we could first calculate the depth of each parenthesis. \n\nExample: depths of \'(())()\' will be [1, 2, 2, 1,... | 0 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Python Solution - Maximum Nesting Depth of Two Valid Parentheses Strings | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | \n\n# Intuition\n- The code aims to split the given valid parentheses sequence into two disjoint subsequences while maximizing the depth of each subsequence.\n- It assigns parentheses to either of the two subsequences in such a way that the depth of both subsequences is as balanced as possible.\n\n# Approach\n- The cod... | 0 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Python Solution - Maximum Nesting Depth of Two Valid Parentheses Strings | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | \n\n# Intuition\n- The code aims to split the given valid parentheses sequence into two disjoint subsequences while maximizing the depth of each subsequence.\n- It assigns parentheses to either of the two subsequences in such a way that the depth of both subsequences is as balanced as possible.\n\n# Approach\n- The cod... | 0 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Simple Alternating class solution Python3 | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n We assign each VPS to either A or B via a class parameter. The intuition is nuanced but to find the disjoint subsequence of VPS with minimum depth for max(depth(A), depth(B)), then essentially all we need to do is find minimum depth for ... | 0 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Simple Alternating class solution Python3 | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n We assign each VPS to either A or B via a class parameter. The intuition is nuanced but to find the disjoint subsequence of VPS with minimum depth for max(depth(A), depth(B)), then essentially all we need to do is find minimum depth for ... | 0 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Run time O(n). | maximum-nesting-depth-of-two-valid-parentheses-strings | 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:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nfrom collections import deque\nclass Solution:\n def maxDepthAfterSplit(self, ... | 0 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Run time O(n). | maximum-nesting-depth-of-two-valid-parentheses-strings | 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:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nfrom collections import deque\nclass Solution:\n def maxDepthAfterSplit(self, ... | 0 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Python greedy solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\nFirst we store the current depth of `A` and `B` in `level_A` and `level_B` respectively.\n\nThe current depth is defined to be the number of unclosed brackets in the string. For example\n```\nA = ()()() ; depth = 0\nA = ( ; depth = 1\nA = (( ; depth = 2\nA = (() ; depth = 1\n```\n- When encount... | 0 | A string is a _valid parentheses string_ (denoted VPS) if and only if it consists of `"( "` and `") "` characters only, and:
* It is the empty string, or
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are VPS's, or
* It can be written as `(A)`, where `A` is a VPS.
We can similarly de... | Without loss of generality, there is a triangle that uses adjacent vertices A[0] and A[N-1] (where N = A.length). Depending on your choice K of it, this breaks down the triangulation into two subproblems A[1:K] and A[K+1:N-1]. |
Python greedy solution | maximum-nesting-depth-of-two-valid-parentheses-strings | 0 | 1 | # Intuition\nFirst we store the current depth of `A` and `B` in `level_A` and `level_B` respectively.\n\nThe current depth is defined to be the number of unclosed brackets in the string. For example\n```\nA = ()()() ; depth = 0\nA = ( ; depth = 1\nA = (( ; depth = 2\nA = (() ; depth = 1\n```\n- When encount... | 0 | You are given two strings `s` and `t` of the same length and an integer `maxCost`.
You want to change `s` to `t`. Changing the `ith` character of `s` to `ith` character of `t` costs `|s[i] - t[i]|` (i.e., the absolute difference between the ASCII values of the characters).
Return _the maximum length of a substring of... | null |
Python3 Solution using 5 primitives (lock, semaphore, condition, event, barrier) | print-in-order | 0 | 1 | # Approach \\#1. Lock\n- `RLock` is not suitable in this case. \n\n<iframe src="https://leetcode.com/playground/mgRcT2DB/shared" frameBorder="0" width="600" height="400"></iframe>\n\n# Approach \\#2. Semaphore\n- `Semaphore(1)` is equivalent to `Lock()`\n<iframe src="https://leetcode.com/playground/c4bkiTBr/shared" fra... | 12 | There are `n` items each belonging to zero or one of `m` groups where `group[i]` is the group that the `i`\-th item belongs to and it's equal to `-1` if the `i`\-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.
Return a sorted list of the items such that... | null |
Python 3 | 56ms | Lock vs Event | print-in-order | 0 | 1 | The fastest solution for the problem is to use Lock mechanism. See the following code with comments:\n```python3\nimport threading\n\nclass Foo:\n def __init__(self):\n\t\t# create lock to control sequence between first and second functions\n self.lock1 = threading.Lock()\n\t\tself.lock1.acquire()\n\t\t\n\t\t... | 34 | There are `n` items each belonging to zero or one of `m` groups where `group[i]` is the group that the `i`\-th item belongs to and it's equal to `-1` if the `i`\-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.
Return a sorted list of the items such that... | null |
2-Lines Python Solution || 75% Faster || Memory less than 95% | print-in-order | 0 | 1 | ```\nclass Foo:\n is_first_executed=False\n is_second_executed=False\n def __init__(self):\n pass\n\n def first(self, printFirst):\n printFirst()\n self.is_first_executed=True\n\n def second(self, printSecond):\n while not self.is_first_executed: continue \n printSe... | 3 | There are `n` items each belonging to zero or one of `m` groups where `group[i]` is the group that the `i`\-th item belongs to and it's equal to `-1` if the `i`\-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.
Return a sorted list of the items such that... | null |
Python with one Barrier | print-foobar-alternately | 0 | 1 | # Intuition\nOne Barrier should be enough to synch each iteration\n\n# Approach\nUse single barrier to sync AFTER foo and BEFORE the bar in each iteration on both threads.\n\nIf you like it, plese up-vote. Thank you!\n\n\n# Code\n```\nfrom threading import Event, Barrier\n\nclass FooBar:\n def __init__(self, n):\n ... | 2 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Python 3 | 6 Approaches | Explanation | print-foobar-alternately | 0 | 1 | ## Before you proceed\n- **Intuition**: Make sure the executing order using `Lock` or similar mechanism.\n- Technically 8 approaches since the first two can be rewrote using `Semaphore`. \n\n## Approach \\#1. Lock/Semaphore only\n```python\nfrom threading import Lock\nclass FooBar:\n def __init__(self, n):\n ... | 6 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
🔥 All Language in Leetcode Solution with Mutex | print-foobar-alternately | 1 | 1 | ***C++***\n```\n#include<semaphore.h>\n\nclass FooBar {\nprivate:\n int n;\n sem_t F, B;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_init(&F, 0, 0);\n sem_init(&B, 0, 1);\n }\n\n void foo(function<void()> printFoo) {\n for (int i = 0; i < n; i++) {\n sem_wait(... | 2 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
python3: 40ms 86.64% Faster 100% Less memory, using threading.Lock | print-foobar-alternately | 0 | 1 | ```\nfrom threading import Lock\nclass FooBar:\n def __init__(self, n):\n self.n = n\n self.foo_lock = Lock()\n self.bar_lock = Lock()\n self.bar_lock.acquire()\n\n\n def foo(self, printFoo: \'Callable[[], None]\') -> None:\n for i in range(self.n):\n self.foo_lock.ac... | 14 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Python with two Events | print-foobar-alternately | 0 | 1 | # Intuition\nTwo events: "ready for foo" and "ready for bar". \n\n# Approach\nCreate two events. Just set and clear each in proper moment.\nSee the comments in the code.\n\nIf you like it, plese up-vote. Thank you!\n\n\n# Code\n```\nfrom threading import Event, Barrier\n\nclass FooBar:\n def __init__(self, n):\n ... | 2 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Print FooBar Alternately, Python Solution | print-foobar-alternately | 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 two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Solution based on threading.Condition() | print-foobar-alternately | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThere are many solutions using two locks, which works fine for this solution. But two locks usually are risky, and can be a typical root cause for deadlock situation (though in this question, with proper variable initialization deadlock w... | 0 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Python3: Canonical Single Lock Solution | print-foobar-alternately | 0 | 1 | # Intuition\n\nMost solutions have two `Lock` or `Event` objects or similar.\n\nFortunately there\'s an easier way.\n\nWe know that\n* foo has to be printed before bar\n* for each foo, we print a bar\n\nSo if\n* foo and bar have been printed the same number of times, it\'s time to print foo\n* if foo has been printed 1... | 0 | Given two integer arrays `arr1` and `arr2`, return the minimum number of operations (possibly zero) needed to make `arr1` strictly increasing.
In one operation, you can choose two indices `0 <= i < arr1.length` and `0 <= j < arr2.length` and do the assignment `arr1[i] = arr2[j]`.
If there is no way to make `arr1` str... | null |
Python solution and explanation (with semaphores and barrier) | building-h2o | 0 | 1 | This solution uses `Semaphore` and `Barrier`. It is simple to understand, and performs well.\n\n## Semantics\n* a `Semaphore` -- trying to acquire it, is possible if there are `tokens` left. Otherwise the thread that tried is asked to wait until a different thread returns the tokens it was using.\n* a `Barrier` -- if... | 25 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
Simple Python3 Solution Using Two Locks | building-h2o | 0 | 1 | # Description\nThe description of this function can be very confusing - but essentially what it says is that there\'s one thread that calls `hydrogen()` 2n times and there\'s another thread that calls `oxygen()` n times. We need to implement the two functions such that the output is n times "HHO". \n\n# Approach\nSince... | 0 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
Python3: Two Solutions, Including Explicit Matching | building-h2o | 0 | 1 | # Intuition\n\n## First Solution: Explicit Matching\n\nI misunderstood the problem and took a long time to solve it - I thought that the first two threads that called `hydrogen()` had to be matched with the first thread that called `oxygen()`, etc.\n\nSo my thought process was:\n* to release a molecule, the prior molec... | 0 | Given an array of integers, return the maximum sum for a **non-empty** subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maxim... | null |
99.47% Python Solution (Custom lambda) | relative-sort-array | 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 two arrays `arr1` and `arr2`, the elements of `arr2` are distinct, and all elements in `arr2` are also in `arr1`.
Sort the elements of `arr1` such that the relative ordering of items in `arr1` are the same as in `arr2`. Elements that do not appear in `arr2` should be placed at the end of `arr1` in **ascending** ... | Binary search for the length of the answer. (If there's an answer of length 10, then there are answers of length 9, 8, 7, ...) To check whether an answer of length K exists, we can use Rabin-Karp 's algorithm. |
99.47% Python Solution (Custom lambda) | relative-sort-array | 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 | We have `n` chips, where the position of the `ith` chip is `position[i]`.
We need to move all the chips to **the same position**. In one step, we can change the position of the `ith` chip from `position[i]` to:
* `position[i] + 2` or `position[i] - 2` with `cost = 0`.
* `position[i] + 1` or `position[i] - 1` with... | Using a hashmap, we can map the values of arr2 to their position in arr2. After, we can use a custom sorting function. |
Python HashMap Fast Solution - Step by Step Explanation | relative-sort-array | 0 | 1 | ```\nclass Solution:\n def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:\n #Start with creating an array/list to store answer\n ans = []\n \n #Create a hashmap and store all the elements as key and their frequency as value\n mapD = {}\n \n fo... | 1 | Given two arrays `arr1` and `arr2`, the elements of `arr2` are distinct, and all elements in `arr2` are also in `arr1`.
Sort the elements of `arr1` such that the relative ordering of items in `arr1` are the same as in `arr2`. Elements that do not appear in `arr2` should be placed at the end of `arr1` in **ascending** ... | Binary search for the length of the answer. (If there's an answer of length 10, then there are answers of length 9, 8, 7, ...) To check whether an answer of length K exists, we can use Rabin-Karp 's algorithm. |
Python HashMap Fast Solution - Step by Step Explanation | relative-sort-array | 0 | 1 | ```\nclass Solution:\n def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:\n #Start with creating an array/list to store answer\n ans = []\n \n #Create a hashmap and store all the elements as key and their frequency as value\n mapD = {}\n \n fo... | 1 | We have `n` chips, where the position of the `ith` chip is `position[i]`.
We need to move all the chips to **the same position**. In one step, we can change the position of the `ith` chip from `position[i]` to:
* `position[i] + 2` or `position[i] - 2` with `cost = 0`.
* `position[i] + 1` or `position[i] - 1` with... | Using a hashmap, we can map the values of arr2 to their position in arr2. After, we can use a custom sorting function. |
Python3 solution with explanation. Fast | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first thing that comes to mind for this problem is to use a depth-first search (DFS) approach. The idea is to traverse the tree and find the deepest leaves and the lowest common ancestor (LCA) of those leaves.\n# Approach\n<!-- Descri... | 2 | Given the `root` of a binary tree, return _the lowest common ancestor of its deepest leaves_.
Recall that:
* The node of a binary tree is a leaf if and only if it has no children
* The depth of the root of the tree is `0`. if the depth of a node is `d`, the depth of each of its children is `d + 1`.
* The lowest... | Can be the problem divided in parts, so solving each part and sum their solutions it should return the answer? Yes, you only need to divide the problem in finger jumps. In each finger jump you need to move your finger from one character to another, you need to know its index. Map each character to it's index. Use a has... |
Python3 solution with explanation. Fast | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe first thing that comes to mind for this problem is to use a depth-first search (DFS) approach. The idea is to traverse the tree and find the deepest leaves and the lowest common ancestor (LCA) of those leaves.\n# Approach\n<!-- Descri... | 2 | Given an integer array `arr` and an integer `difference`, return the length of the longest subsequence in `arr` which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals `difference`.
A **subsequence** is a sequence that can be derived from `arr` by deleting some or n... | Do a postorder traversal. Then, if both subtrees contain a deepest leaf, you can mark this node as the answer (so far). The final node marked will be the correct answer. |
Python Straightforward 2-Pass Solution | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | # Intuition\nFind the deepest leaves, push them up one level at a time until they meet. The first node where they meet is the LCA. This is based on the fact that the deepest leaves are on the same level (depth).\n\n# Approach\nBFS to find deepest leaves, and keep a dict to record each node\'s parent in order to push no... | 1 | Given the `root` of a binary tree, return _the lowest common ancestor of its deepest leaves_.
Recall that:
* The node of a binary tree is a leaf if and only if it has no children
* The depth of the root of the tree is `0`. if the depth of a node is `d`, the depth of each of its children is `d + 1`.
* The lowest... | Can be the problem divided in parts, so solving each part and sum their solutions it should return the answer? Yes, you only need to divide the problem in finger jumps. In each finger jump you need to move your finger from one character to another, you need to know its index. Map each character to it's index. Use a has... |
Python Straightforward 2-Pass Solution | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | # Intuition\nFind the deepest leaves, push them up one level at a time until they meet. The first node where they meet is the LCA. This is based on the fact that the deepest leaves are on the same level (depth).\n\n# Approach\nBFS to find deepest leaves, and keep a dict to record each node\'s parent in order to push no... | 1 | Given an integer array `arr` and an integer `difference`, return the length of the longest subsequence in `arr` which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals `difference`.
A **subsequence** is a sequence that can be derived from `arr` by deleting some or n... | Do a postorder traversal. Then, if both subtrees contain a deepest leaf, you can mark this node as the answer (so far). The final node marked will be the correct answer. |
Python easy to understand and read | DFS | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | My main intention is to find the height at every node and check if left subtree height is greater than right and vice versa. If height of left subtree is greater then i go furthur into the left subtree and same logic for right subtree as well. There will be a point where height of left subtree == height of right subtre... | 6 | Given the `root` of a binary tree, return _the lowest common ancestor of its deepest leaves_.
Recall that:
* The node of a binary tree is a leaf if and only if it has no children
* The depth of the root of the tree is `0`. if the depth of a node is `d`, the depth of each of its children is `d + 1`.
* The lowest... | Can be the problem divided in parts, so solving each part and sum their solutions it should return the answer? Yes, you only need to divide the problem in finger jumps. In each finger jump you need to move your finger from one character to another, you need to know its index. Map each character to it's index. Use a has... |
Python easy to understand and read | DFS | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | My main intention is to find the height at every node and check if left subtree height is greater than right and vice versa. If height of left subtree is greater then i go furthur into the left subtree and same logic for right subtree as well. There will be a point where height of left subtree == height of right subtre... | 6 | Given an integer array `arr` and an integer `difference`, return the length of the longest subsequence in `arr` which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals `difference`.
A **subsequence** is a sequence that can be derived from `arr` by deleting some or n... | Do a postorder traversal. Then, if both subtrees contain a deepest leaf, you can mark this node as the answer (so far). The final node marked will be the correct answer. |
python post order solution | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | ```\nclass Solution:\n def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode:\n def post_order(root):\n if not root:\n return 0, None\n d1, n1 = post_order(root.left)\n d2, n2 = post_order(root.right)\n if d1 == d2:\n return d1+1, root... | 12 | Given the `root` of a binary tree, return _the lowest common ancestor of its deepest leaves_.
Recall that:
* The node of a binary tree is a leaf if and only if it has no children
* The depth of the root of the tree is `0`. if the depth of a node is `d`, the depth of each of its children is `d + 1`.
* The lowest... | Can be the problem divided in parts, so solving each part and sum their solutions it should return the answer? Yes, you only need to divide the problem in finger jumps. In each finger jump you need to move your finger from one character to another, you need to know its index. Map each character to it's index. Use a has... |
python post order solution | lowest-common-ancestor-of-deepest-leaves | 0 | 1 | ```\nclass Solution:\n def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode:\n def post_order(root):\n if not root:\n return 0, None\n d1, n1 = post_order(root.left)\n d2, n2 = post_order(root.right)\n if d1 == d2:\n return d1+1, root... | 12 | Given an integer array `arr` and an integer `difference`, return the length of the longest subsequence in `arr` which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals `difference`.
A **subsequence** is a sequence that can be derived from `arr` by deleting some or n... | Do a postorder traversal. Then, if both subtrees contain a deepest leaf, you can mark this node as the answer (so far). The final node marked will be the correct answer. |
✔ Python3 Solution | O(n) | longest-well-performing-interval | 0 | 1 | `Time Complexity` : `O(n)`\n`Space Complexity` : `O(n)`\n```\nclass Solution:\n def longestWPI(self, A):\n curr, ans, D = 0, 0, {}\n for e, i in enumerate(map(lambda x: (-1, 1)[x > 8], A)):\n curr += i\n D[curr] = D.get(curr, e)\n ans = e + 1 if curr > 0 else max(ans, e... | 1 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
✔ Python3 Solution | O(n) | longest-well-performing-interval | 0 | 1 | `Time Complexity` : `O(n)`\n`Space Complexity` : `O(n)`\n```\nclass Solution:\n def longestWPI(self, A):\n curr, ans, D = 0, 0, {}\n for e, i in enumerate(map(lambda x: (-1, 1)[x > 8], A)):\n curr += i\n D[curr] = D.get(curr, e)\n ans = e + 1 if curr > 0 else max(ans, e... | 1 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Simple Python Solution | longest-well-performing-interval | 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 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
Simple Python Solution | longest-well-performing-interval | 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 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
PYTHON | AS INTERVIEWER WANTS |EXPLAINED WITH PICTURE | FAST | HASHMAP + PREFIX_SUM | | longest-well-performing-interval | 0 | 1 | # EXPLANATION\n\n**The idea is shown in the picture:**\n\n\n```\nSearching the j for every i can cost us O(n*n) which will cause TLE \nSo we need to use hashmap here \n\nNow let us say sum is -1 \nSo if we get a... | 5 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
PYTHON | AS INTERVIEWER WANTS |EXPLAINED WITH PICTURE | FAST | HASHMAP + PREFIX_SUM | | longest-well-performing-interval | 0 | 1 | # EXPLANATION\n\n**The idea is shown in the picture:**\n\n\n```\nSearching the j for every i can cost us O(n*n) which will cause TLE \nSo we need to use hashmap here \n\nNow let us say sum is -1 \nSo if we get a... | 5 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
📌📌 For-Beginners || Well-Explained || 97% faster || Easy-to-understand 🐍 | longest-well-performing-interval | 0 | 1 | ## IDEA:\n* Firstly create a prefix array (here \'dummy\') which represents number of tired day till specific index.\n* When it is tired day increased by one and similarly decrease 1 for normal day.\n\n**After creating prefix array:**\n* Now go through once and if you are finding positive number then directly update re... | 4 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
📌📌 For-Beginners || Well-Explained || 97% faster || Easy-to-understand 🐍 | longest-well-performing-interval | 0 | 1 | ## IDEA:\n* Firstly create a prefix array (here \'dummy\') which represents number of tired day till specific index.\n* When it is tired day increased by one and similarly decrease 1 for normal day.\n\n**After creating prefix array:**\n* Now go through once and if you are finding positive number then directly update re... | 4 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
10 lines, commented. | longest-well-performing-interval | 0 | 1 | # Intuition\nuse prefix sum with their indices for quick compute of subarray ends at current iterating index. \n\n# Code\n```\nclass Solution:\n def longestWPI(self, hours: List[int]) -> int:\n hours = [1 if h > 8 else -1 for h in hours]\n summ = 0 \n # keeps negative prefix sum\'s earliest inde... | 0 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
10 lines, commented. | longest-well-performing-interval | 0 | 1 | # Intuition\nuse prefix sum with their indices for quick compute of subarray ends at current iterating index. \n\n# Code\n```\nclass Solution:\n def longestWPI(self, hours: List[int]) -> int:\n hours = [1 if h > 8 else -1 for h in hours]\n summ = 0 \n # keeps negative prefix sum\'s earliest inde... | 0 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Python3: Yet Another Explanation of the O(N) Solution | longest-well-performing-interval | 0 | 1 | # Intuition\n\nWe want intervals where there are more >8 hour days than <= 8 hour days.\n\nThey call this a "well performing interval." But by that definition, my entire time working in finance was well performing... and yet I got laid off. So, uh, yeah. Checkmate, atheists?\n\n(yes, I\'m salty!)\n\nANYWAY. That means ... | 0 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
Python3: Yet Another Explanation of the O(N) Solution | longest-well-performing-interval | 0 | 1 | # Intuition\n\nWe want intervals where there are more >8 hour days than <= 8 hour days.\n\nThey call this a "well performing interval." But by that definition, my entire time working in finance was well performing... and yet I got laid off. So, uh, yeah. Checkmate, atheists?\n\n(yes, I\'m salty!)\n\nANYWAY. That means ... | 0 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Two approaches | longest-well-performing-interval | 0 | 1 | This problem can be solved with either a prefix sum array, or a monotonic stack.\n\n### Prefix sum approach\n\n``` python\nclass Solution:\n def longestWPI(self, hours: List[int]) -> int:\n score = res = 0\n table = {}\n\n for i, h in enumerate(hours):\n score = score + 1 if h > 8 els... | 0 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
Two approaches | longest-well-performing-interval | 0 | 1 | This problem can be solved with either a prefix sum array, or a monotonic stack.\n\n### Prefix sum approach\n\n``` python\nclass Solution:\n def longestWPI(self, hours: List[int]) -> int:\n score = res = 0\n table = {}\n\n for i, h in enumerate(hours):\n score = score + 1 if h > 8 els... | 0 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Longest well-performing interval | longest-well-performing-interval | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe should look at cumulative good days: cumulative += (1 if hour > 8 else -1) \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCalculate cumulative good days from t0. If current cumulative > 0 then longest period =... | 0 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
Longest well-performing interval | longest-well-performing-interval | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe should look at cumulative good days: cumulative += (1 if hour > 8 else -1) \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCalculate cumulative good days from t0. If current cumulative > 0 then longest period =... | 0 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Python O(N) | Debunked all ill-explained solutions | One Post that beats all | True explanation | longest-well-performing-interval | 0 | 1 | # Intuition & Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Use prefix sum approach first to calculate running net sum of number of tiring and non-tiring days. Use +1 for tiring days and -1 for non-tiring days. \n example - [6,6,9,1,11,8] : [-1, -2, -1, -2, -1, 0]\n\n2. From the... | 0 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
Python O(N) | Debunked all ill-explained solutions | One Post that beats all | True explanation | longest-well-performing-interval | 0 | 1 | # Intuition & Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Use prefix sum approach first to calculate running net sum of number of tiring and non-tiring days. Use +1 for tiring days and -1 for non-tiring days. \n example - [6,6,9,1,11,8] : [-1, -2, -1, -2, -1, 0]\n\n2. From the... | 0 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Detailed explanations for beginners like me | longest-well-performing-interval | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nbefore this problem you can first solve leetcode 560.\nthan we can change the number that strictly greater than 8 into 1 else into -1. what we need to slove is **finding the longest subarray that the sum strictly greater than 0**.\n\n- wh... | 0 | We are given `hours`, a list of the number of hours worked per day for a given employee.
A day is considered to be a _tiring day_ if and only if the number of hours worked is (strictly) greater than `8`.
A _well-performing interval_ is an interval of days for which the number of tiring days is strictly larger than th... | Model the problem as a graph problem. Add an edge from one character to another if you need to convert between them. What if one character needs to be converted into more than one character? There would be no solution. Thus, every node can have at most one outgoing edge. How to process a linked list? How to process a c... |
Detailed explanations for beginners like me | longest-well-performing-interval | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nbefore this problem you can first solve leetcode 560.\nthan we can change the number that strictly greater than 8 into 1 else into -1. what we need to slove is **finding the longest subarray that the sum strictly greater than 0**.\n\n- wh... | 0 | In a gold mine `grid` of size `m x n`, each cell in this mine has an integer representing the amount of gold in that cell, `0` if it is empty.
Return the maximum amount of gold you can collect under the conditions:
* Every time you are located in a cell you will collect all the gold in that cell.
* From your posi... | Make a new array A of +1/-1s corresponding to if hours[i] is > 8 or not. The goal is to find the longest subarray with positive sum. Using prefix sums (PrefixSum[i+1] = A[0] + A[1] + ... + A[i]), you need to find for each j, the smallest i < j with PrefixSum[i] + 1 == PrefixSum[j]. |
Python Hard | smallest-sufficient-team | 0 | 1 | ```\nclass Solution:\n def smallestSufficientTeam(self, req_skills: List[str], people: List[List[str]]) -> List[int]:\n\n d = defaultdict(int)\n N = len(req_skills)\n P = len(people)\n\n for i, skill in enumerate(req_skills):\n d[skill] = 1 << i\n\n people_skills = []\n\... | 1 | In a project, you have a list of required skills `req_skills`, and a list of people. The `ith` person `people[i]` contains a list of skills that the person has.
Consider a sufficient team: a set of people such that for every required skill in `req_skills`, there is at least one person in the team who has that skill. W... | What if you think of a tree hierarchy for the files?. A path is a node in the tree. Use a hash table to store the valid paths along with their values. |
Python Hard | smallest-sufficient-team | 0 | 1 | ```\nclass Solution:\n def smallestSufficientTeam(self, req_skills: List[str], people: List[List[str]]) -> List[int]:\n\n d = defaultdict(int)\n N = len(req_skills)\n P = len(people)\n\n for i, skill in enumerate(req_skills):\n d[skill] = 1 << i\n\n people_skills = []\n\... | 1 | Given an integer `n`, your task is to count how many strings of length `n` can be formed under the following rules:
* Each character is a lower case vowel (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`)
* Each vowel `'a'` may only be followed by an `'e'`.
* Each vowel `'e'` may only be followed by an `'a'` or an `'i'`.
* ... | Do a bitmask DP. For each person, for each set of skills, we can update our understanding of a minimum set of people needed to perform this set of skills. |
Python short and clean. DP. Bitmask. Functional programming. | smallest-sufficient-team | 0 | 1 | # Approach\nTL;DR, Similar to [Editorial solution Approach 2](https://leetcode.com/problems/smallest-sufficient-team/editorial/) but written functionally.\n\n# Complexity\n- Time complexity: $$O(2^k \\cdot n \\cdot log(n))$$\n (Note: $$log(n)$$ is for `int.bit_count()`)\n\n- Space complexity: $$O(2^k)$$\n\nwhere,\n`n i... | 1 | In a project, you have a list of required skills `req_skills`, and a list of people. The `ith` person `people[i]` contains a list of skills that the person has.
Consider a sufficient team: a set of people such that for every required skill in `req_skills`, there is at least one person in the team who has that skill. W... | What if you think of a tree hierarchy for the files?. A path is a node in the tree. Use a hash table to store the valid paths along with their values. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.