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 |
|---|---|---|---|---|---|---|---|
brute force | average-waiting-time | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
Simple Solution, similar to Merge Interval | average-waiting-time | 0 | 1 | \n```\nclass Solution:\n def averageWaitingTime(self, customers: List[List[int]]) -> float:\n \n total = (customers[0][0]+ customers[0][1]) - customers[0][0]\n\n res=[[customers[0][0], customers[0][0]+ customers[0][1]]]\n\n for i in customers[1:]:\n out = res[-1]\n s... | 0 | 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 ... |
Simple Solution, similar to Merge Interval | average-waiting-time | 0 | 1 | \n```\nclass Solution:\n def averageWaitingTime(self, customers: List[List[int]]) -> float:\n \n total = (customers[0][0]+ customers[0][1]) - customers[0][0]\n\n res=[[customers[0][0], customers[0][0]+ customers[0][1]]]\n\n for i in customers[1:]:\n out = res[-1]\n s... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
Easy to understand Python3 solution | average-waiting-time | 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 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 to understand Python3 solution | average-waiting-time | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
Python Simple Interval Based Logic | average-waiting-time | 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 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 Simple Interval Based Logic | average-waiting-time | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
Simple and clear python3 solution | 1 pass | average-waiting-time | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` python3 []\nclass Solution:\n def averageWaitingTime(self, customers: List[List[int]]) -> float:\n n = len(cus... | 0 | 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 ... |
Simple and clear python3 solution | 1 pass | average-waiting-time | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` python3 []\nclass Solution:\n def averageWaitingTime(self, customers: List[List[int]]) -> float:\n n = len(cus... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
Python very easy and intiutive | average-waiting-time | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution:\n def averageWaitingTime(self, customers: List[List[int]]) -> f... | 0 | 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 very easy and intiutive | average-waiting-time | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution:\n def averageWaitingTime(self, customers: List[List[int]]) -> f... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
Python - 5 lines, simple O(n) greedy | average-waiting-time | 0 | 1 | # Intuition\nSince they are already sorted, for every customer it is enough to find maximum of last time reached and arrival plus time for processing, wait time is diference between those.\nThis si kind of greedy simulation.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\n... | 0 | 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 - 5 lines, simple O(n) greedy | average-waiting-time | 0 | 1 | # Intuition\nSince they are already sorted, for every customer it is enough to find maximum of last time reached and arrival plus time for processing, wait time is diference between those.\nThis si kind of greedy simulation.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\n... | 0 | Given a **(0-indexed)** integer array `nums` and two integers `low` and `high`, return _the number of **nice pairs**_.
A **nice pair** is a pair `(i, j)` where `0 <= i < j < nums.length` and `low <= (nums[i] XOR nums[j]) <= high`.
**Example 1:**
**Input:** nums = \[1,4,2,7\], low = 2, high = 6
**Output:** 6
**Explan... | Iterate on the customers, maintaining the time the chef will finish the previous orders. If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. Update the running time by the time when the chef starts preparing + p... |
python 3 || clean || easy approach | maximum-binary-string-after-change | 0 | 1 | The trick here is that the answer string will contain only **one 0** and that zero will be at the **cth position** (where **c** is the **no of zeroes** in the initial string).The **rest** of the string will be filled with **1s.**\n```\nlass Solution:\n def maximumBinaryString(self, s: str) -> str:\n #count of... | 6 | You are given a binary string `binary` consisting of only `0`'s or `1`'s. You can apply each of the following operations any number of times:
* Operation 1: If the number contains the substring `"00 "`, you can replace it with `"10 "`.
* For example, `"00010 " -> "10010` "
* Operation 2: If the number contai... | null |
Final Recursive Form | Commented and Explained | maximum-binary-string-after-change | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn the intuition, I\'m stepping through the start of the problem and the outline of the example as a recursive backtrack. Backtracking is not used here, but serves to inform the thinking process and outline some things worth finding in pu... | 0 | You are given a binary string `binary` consisting of only `0`'s or `1`'s. You can apply each of the following operations any number of times:
* Operation 1: If the number contains the substring `"00 "`, you can replace it with `"10 "`.
* For example, `"00010 " -> "10010` "
* Operation 2: If the number contai... | null |
beautiful greedy | maximum-binary-string-after-change | 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 binary string `binary` consisting of only `0`'s or `1`'s. You can apply each of the following operations any number of times:
* Operation 1: If the number contains the substring `"00 "`, you can replace it with `"10 "`.
* For example, `"00010 " -> "10010` "
* Operation 2: If the number contai... | null |
Magic solution | maximum-binary-string-after-change | 0 | 1 | I don\'t remember how I figured out this pattern\n\n# Code\n```\nclass Solution:\n def maximumBinaryString(self, binary: str) -> str:\n idx = binary.find(\'01\')\n if idx == -1:\n return \'1\' * (len(binary) - 1) + binary[-1]\n n = idx + binary[idx:].count(\'0\')\n return \'1\'... | 0 | You are given a binary string `binary` consisting of only `0`'s or `1`'s. You can apply each of the following operations any number of times:
* Operation 1: If the number contains the substring `"00 "`, you can replace it with `"10 "`.
* For example, `"00010 " -> "10010` "
* Operation 2: If the number contai... | null |
Simple Python Greedy Solution | maximum-binary-string-after-change | 0 | 1 | ```\nclass Solution:\n def maximumBinaryString(self, binary: str) -> str:\n arr = []\n stack = []\n\n for i in binary:\n stack.append(i)\n \n if stack[0] == \'1\':\n arr.append(\'1\')\n stack.pop()\n elif stack[0] == \'0\'... | 0 | You are given a binary string `binary` consisting of only `0`'s or `1`'s. You can apply each of the following operations any number of times:
* Operation 1: If the number contains the substring `"00 "`, you can replace it with `"10 "`.
* For example, `"00010 " -> "10010` "
* Operation 2: If the number contai... | null |
Python | Clean Code | maximum-binary-string-after-change | 0 | 1 | # Code\n```\nclass Solution:\n def maximumBinaryString(self, binary: str) -> str:\n i = binary.find(\'0\')\n binary = list(binary)\n if i == 0:\n binary.sort()\n else:\n binary[i:] = sorted(binary[i:])\n while i + 1 < len(binary) and binary[i+1] == \'0\':\n ... | 0 | You are given a binary string `binary` consisting of only `0`'s or `1`'s. You can apply each of the following operations any number of times:
* Operation 1: If the number contains the substring `"00 "`, you can replace it with `"10 "`.
* For example, `"00010 " -> "10010` "
* Operation 2: If the number contai... | null |
69th solution lol , beats 99% in runtime | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given an integer array, `nums`, and an integer `k`. `nums` comprises of only `0`'s and `1`'s. In one move, you can choose two **adjacent** indices and swap their values.
Return _the **minimum** number of moves required so that_ `nums` _has_ `k` _**consecutive**_ `1`_'s_.
**Example 1:**
**Input:** nums = \[1,... | Sort the boxes in ascending order, try to process the box with the smallest height first. |
69th solution lol , beats 99% in runtime | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | You are given a string `word` that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, `"a123bc34d8ef34 "` will become `" 123 34 8 34 "`. Notice that you are left with some integers that are separated by at least one space: `"123 "`, `"34 "`, `"8 "`, ... | Choose k 1s and determine how many steps are required to move them into 1 group. Maintain a sliding window of k 1s, and maintain the steps required to group them. When you slide the window across, should you move the group to the right? Once you move the group to the right, it will never need to slide to the left again... |
python 3 | O(n) time | O(1) space | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | ```\nclass Solution:\n def minMoves(self, nums: List[int], k: int) -> int:\n if k == 1:\n return 0\n ones = 0\n half = (k + 1) >> 1\n leftSum = rightSum = 0\n for i, num in enumerate(nums):\n if not num:\n continue\n ones += 1\n ... | 0 | You are given an integer array, `nums`, and an integer `k`. `nums` comprises of only `0`'s and `1`'s. In one move, you can choose two **adjacent** indices and swap their values.
Return _the **minimum** number of moves required so that_ `nums` _has_ `k` _**consecutive**_ `1`_'s_.
**Example 1:**
**Input:** nums = \[1,... | Sort the boxes in ascending order, try to process the box with the smallest height first. |
python 3 | O(n) time | O(1) space | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | ```\nclass Solution:\n def minMoves(self, nums: List[int], k: int) -> int:\n if k == 1:\n return 0\n ones = 0\n half = (k + 1) >> 1\n leftSum = rightSum = 0\n for i, num in enumerate(nums):\n if not num:\n continue\n ones += 1\n ... | 0 | You are given a string `word` that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, `"a123bc34d8ef34 "` will become `" 123 34 8 34 "`. Notice that you are left with some integers that are separated by at least one space: `"123 "`, `"34 "`, `"8 "`, ... | Choose k 1s and determine how many steps are required to move them into 1 group. Maintain a sliding window of k 1s, and maintain the steps required to group them. When you slide the window across, should you move the group to the right? Once you move the group to the right, it will never need to slide to the left again... |
ez sol | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minMoves(self, nums: List[int], k: int) -> int:\n if k==1:\n return 0\n n=len(nums)\n ind=[i for i in range(len(nums)) if nums[i]==1]\n pref=[0]*n\n for i in range(n):\n ... | 0 | You are given an integer array, `nums`, and an integer `k`. `nums` comprises of only `0`'s and `1`'s. In one move, you can choose two **adjacent** indices and swap their values.
Return _the **minimum** number of moves required so that_ `nums` _has_ `k` _**consecutive**_ `1`_'s_.
**Example 1:**
**Input:** nums = \[1,... | Sort the boxes in ascending order, try to process the box with the smallest height first. |
ez sol | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minMoves(self, nums: List[int], k: int) -> int:\n if k==1:\n return 0\n n=len(nums)\n ind=[i for i in range(len(nums)) if nums[i]==1]\n pref=[0]*n\n for i in range(n):\n ... | 0 | You are given a string `word` that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, `"a123bc34d8ef34 "` will become `" 123 34 8 34 "`. Notice that you are left with some integers that are separated by at least one space: `"123 "`, `"34 "`, `"8 "`, ... | Choose k 1s and determine how many steps are required to move them into 1 group. Maintain a sliding window of k 1s, and maintain the steps required to group them. When you slide the window across, should you move the group to the right? Once you move the group to the right, it will never need to slide to the left again... |
[Python3] 1-pass O(N) | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | **Algo**\nThis implementation is similar to the popular threads in Discuss. But here we built the locations of 1s on-the-fly. \n\n**Implementation** - simulation \n```\nclass Solution:\n def minMoves(self, nums: List[int], k: int) -> int:\n ii = val = 0 \n ans = inf\n loc = [] # location of 1s\n... | 3 | You are given an integer array, `nums`, and an integer `k`. `nums` comprises of only `0`'s and `1`'s. In one move, you can choose two **adjacent** indices and swap their values.
Return _the **minimum** number of moves required so that_ `nums` _has_ `k` _**consecutive**_ `1`_'s_.
**Example 1:**
**Input:** nums = \[1,... | Sort the boxes in ascending order, try to process the box with the smallest height first. |
[Python3] 1-pass O(N) | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | **Algo**\nThis implementation is similar to the popular threads in Discuss. But here we built the locations of 1s on-the-fly. \n\n**Implementation** - simulation \n```\nclass Solution:\n def minMoves(self, nums: List[int], k: int) -> int:\n ii = val = 0 \n ans = inf\n loc = [] # location of 1s\n... | 3 | You are given a string `word` that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, `"a123bc34d8ef34 "` will become `" 123 34 8 34 "`. Notice that you are left with some integers that are separated by at least one space: `"123 "`, `"34 "`, `"8 "`, ... | Choose k 1s and determine how many steps are required to move them into 1 group. Maintain a sliding window of k 1s, and maintain the steps required to group them. When you slide the window across, should you move the group to the right? Once you move the group to the right, it will never need to slide to the left again... |
Simple O(n) python solution | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- Find all the ones and keep their indices as a list.\n- The total swaps for the first k indices can be considered as the number of swaps (mid_steps) to move all indices to its mid point (mid=k//2) and then move back to cons... | 0 | You are given an integer array, `nums`, and an integer `k`. `nums` comprises of only `0`'s and `1`'s. In one move, you can choose two **adjacent** indices and swap their values.
Return _the **minimum** number of moves required so that_ `nums` _has_ `k` _**consecutive**_ `1`_'s_.
**Example 1:**
**Input:** nums = \[1,... | Sort the boxes in ascending order, try to process the box with the smallest height first. |
Simple O(n) python solution | minimum-adjacent-swaps-for-k-consecutive-ones | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- Find all the ones and keep their indices as a list.\n- The total swaps for the first k indices can be considered as the number of swaps (mid_steps) to move all indices to its mid point (mid=k//2) and then move back to cons... | 0 | You are given a string `word` that consists of digits and lowercase English letters.
You will replace every non-digit character with a space. For example, `"a123bc34d8ef34 "` will become `" 123 34 8 34 "`. Notice that you are left with some integers that are separated by at least one space: `"123 "`, `"34 "`, `"8 "`, ... | Choose k 1s and determine how many steps are required to move them into 1 group. Maintain a sliding window of k 1s, and maintain the steps required to group them. When you slide the window across, should you move the group to the right? Once you move the group to the right, it will never need to slide to the left again... |
β
[PYTHON] Easy solution: zip() [Beats 96.52%] | determine-if-string-halves-are-alike | 0 | 1 | # Code\n```\nclass Solution:\n def halvesAreAlike(self, s: str) -> bool:\n vowels = [\'a\', \'e\', \'i\', \'o\', \'u\', \'A\', \'E\', \'I\', \'O\', \'U\']\n s_len = int(len(s) / 2)\n counter_a = counter_b = 0\n # parallel iteration by first and second part of "s":\n for char_a, cha... | 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] Easy solution: zip() [Beats 96.52%] | determine-if-string-halves-are-alike | 0 | 1 | # Code\n```\nclass Solution:\n def halvesAreAlike(self, s: str) -> bool:\n vowels = [\'a\', \'e\', \'i\', \'o\', \'u\', \'A\', \'E\', \'I\', \'O\', \'U\']\n s_len = int(len(s) / 2)\n counter_a = counter_b = 0\n # parallel iteration by first and second part of "s":\n for char_a, cha... | 1 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
Beats 91.73% || Determine if string halves are alike | determine-if-string-halves-are-alike | 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. |
Beats 91.73% || Determine if string halves are alike | determine-if-string-halves-are-alike | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
MOST EASY SOLUTION you can think of | determine-if-string-halves-are-alike | 0 | 1 | ```\nclass Solution:\n def halvesAreAlike(self, s: str) -> bool:\n n = len(s)\n a = s[:n//2]\n b = s[n//2:]\n a_vowels=[]\n b_vowels=[]\n for i in a:\n if i in (\'a\',\'e\',\'i\',\'o\',\'u\',\'A\',\'E\',\'I\',\'O\',\'U\'):\n a_vowels.append(i)\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. |
MOST EASY SOLUTION you can think of | determine-if-string-halves-are-alike | 0 | 1 | ```\nclass Solution:\n def halvesAreAlike(self, s: str) -> bool:\n n = len(s)\n a = s[:n//2]\n b = s[n//2:]\n a_vowels=[]\n b_vowels=[]\n for i in a:\n if i in (\'a\',\'e\',\'i\',\'o\',\'u\',\'A\',\'E\',\'I\',\'O\',\'U\'):\n a_vowels.append(i)\n ... | 1 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
Python3 Solution | Easy to Understand | determine-if-string-halves-are-alike | 0 | 1 | Approach:\n1. Devided the string s int strings a and b\n2. count the number of vowels in a and b\n3. if the count of vowels in a is equel to the count vowels in b return True otherwise False\n# Smash that UPVOTE button! \uD83D\uDE0B\n\n```\ndef halvesAreAlike(self, s: str) -> bool:\n\tn = len(s)\n\ta = s[:n//2]\n\tb =... | 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. |
Python3 Solution | Easy to Understand | determine-if-string-halves-are-alike | 0 | 1 | Approach:\n1. Devided the string s int strings a and b\n2. count the number of vowels in a and b\n3. if the count of vowels in a is equel to the count vowels in b return True otherwise False\n# Smash that UPVOTE button! \uD83D\uDE0B\n\n```\ndef halvesAreAlike(self, s: str) -> bool:\n\tn = len(s)\n\ta = s[:n//2]\n\tb =... | 1 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
βοΈ Python Fastest Solution using Two Pointers | 99% Faster π₯ | determine-if-string-halves-are-alike | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nclass Solution:\n def halvesAreAlike(self, s: str) -> bool:\n \n vowels = [\'a\', \'e\', \'i\', \'o\', \'u\', \'A\', \'E\', \'I\', \'O\', \'U\']\n \n right = len(s)-1\n left = 0\n \n a_vow... | 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 Fastest Solution using Two Pointers | 99% Faster π₯ | determine-if-string-halves-are-alike | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nclass Solution:\n def halvesAreAlike(self, s: str) -> bool:\n \n vowels = [\'a\', \'e\', \'i\', \'o\', \'u\', \'A\', \'E\', \'I\', \'O\', \'U\']\n \n right = len(s)-1\n left = 0\n \n a_vow... | 1 | There are `n` friends that are playing a game. The friends are sitting in a circle and are numbered from `1` to `n` in **clockwise order**. More formally, moving clockwise from the `ith` friend brings you to the `(i+1)th` friend for `1 <= i < n`, and moving clockwise from the `nth` friend brings you to the `1st` friend... | Create a function that checks if a character is a vowel, either uppercase or lowercase. |
Python ACCEPTED solution with detailed explanation. | maximum-number-of-eaten-apples | 0 | 1 | Explanation:\nYou should keep track of the available apples on daily basis and consume ones which will rot earlier. We can use a heap ```h``` which will store lists. Each list in the heap will contain the data related to the batch of apples of a day. The first element in the list will tell us till how many days the ap... | 15 | 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 ACCEPTED solution with detailed explanation. | maximum-number-of-eaten-apples | 0 | 1 | Explanation:\nYou should keep track of the available apples on daily basis and consume ones which will rot earlier. We can use a heap ```h``` which will store lists. Each list in the heap will contain the data related to the batch of apples of a day. The first element in the list will tell us till how many days the ap... | 15 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
[Java/Python] Greedy Solution | maximum-number-of-eaten-apples | 1 | 1 | Honestly, when I worked on this one, I spent a lot of time to determine some minor details and jumped between the code and description frequently to make sure I am on the right track.. \n\n**To reach the max result, we need to choose the earliest rotten apples to eat.** So We will use a Priority Queue to simulate this ... | 9 | 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 |
[Java/Python] Greedy Solution | maximum-number-of-eaten-apples | 1 | 1 | Honestly, when I worked on this one, I spent a lot of time to determine some minor details and jumped between the code and description frequently to make sure I am on the right track.. \n\n**To reach the max result, we need to choose the earliest rotten apples to eat.** So We will use a Priority Queue to simulate this ... | 9 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Python Heap Solution Beats 100% with Detailed Explanation | maximum-number-of-eaten-apples | 0 | 1 | If this is given to you personally, what would you do?\nI would eat the apples that are going to rot first.\n\nSo let\'s do that. We need to know how many apples have rotten today so that we throw them away and if we have some apples left, eat one from the lot which is going to rot the earliest. \n\nThis means we need ... | 6 | 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 Heap Solution Beats 100% with Detailed Explanation | maximum-number-of-eaten-apples | 0 | 1 | If this is given to you personally, what would you do?\nI would eat the apples that are going to rot first.\n\nSo let\'s do that. We need to know how many apples have rotten today so that we throw them away and if we have some apples left, eat one from the lot which is going to rot the earliest. \n\nThis means we need ... | 6 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Easy python solution using heap (priority queue) | maximum-number-of-eaten-apples | 0 | 1 | # Intuition\ni have defined `popFromHeap` that will pop from the heap based on the limit and return the `day`, `num` and new `heap` after poping element. \n\nin the `eatenApples` for each day use `popFromHeap` to find if there is any fresh apples left to eat or not. if its there then increment the counter. finally once... | 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 |
Easy python solution using heap (priority queue) | maximum-number-of-eaten-apples | 0 | 1 | # Intuition\ni have defined `popFromHeap` that will pop from the heap based on the limit and return the `day`, `num` and new `heap` after poping element. \n\nin the `eatenApples` for each day use `popFromHeap` to find if there is any fresh apples left to eat or not. if its there then increment the counter. finally once... | 0 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Python heap | maximum-number-of-eaten-apples | 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 |
Python heap | maximum-number-of-eaten-apples | 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 **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Easy to understand Python heap solution | maximum-number-of-eaten-apples | 0 | 1 | # Code\n```\nclass Solution:\n def eatenApples(self, apples: List[int], days: List[int]) -> int:\n total = 0\n plan = []\n i = 0\n while i < len(apples) or plan:\n if i < len(apples):\n apple = apples[i]\n heapq.heappush(plan, (i + days[i], apple))... | 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 |
Easy to understand Python heap solution | maximum-number-of-eaten-apples | 0 | 1 | # Code\n```\nclass Solution:\n def eatenApples(self, apples: List[int], days: List[int]) -> int:\n total = 0\n plan = []\n i = 0\n while i < len(apples) or plan:\n if i < len(apples):\n apple = apples[i]\n heapq.heappush(plan, (i + days[i], apple))... | 0 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Python heap solution beats 91.67% | maximum-number-of-eaten-apples | 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 |
Python heap solution beats 91.67% | maximum-number-of-eaten-apples | 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 **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Python 3, using heapq | maximum-number-of-eaten-apples | 0 | 1 | # Intuition\nAlways eat the oldest apple in stock, provided that they have not expired.\n\n# Approach\nWe use a min heap, sorted by expiry date. When stocking new apples, push to the heap. When eating apples, pop from the heap.\n\n# Complexity\n- Time complexity:\n$$O(n\\log(n))$$\n\n- Space complexity:\n$$O(n)$$\n\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 |
Python 3, using heapq | maximum-number-of-eaten-apples | 0 | 1 | # Intuition\nAlways eat the oldest apple in stock, provided that they have not expired.\n\n# Approach\nWe use a min heap, sorted by expiry date. When stocking new apples, push to the heap. When eating apples, pop from the heap.\n\n# Complexity\n- Time complexity:\n$$O(n\\log(n))$$\n\n- Space complexity:\n$$O(n)$$\n\n# ... | 0 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
1705. Maximum Number of Eaten Apples_[Python 3] Easy-to-understand solution | maximum-number-of-eaten-apples | 0 | 1 | # Code\n```\nimport heapq\nclass Solution:\n def eatenApples(self, apples: List[int], days: List[int]) -> int:\n res, i = 0, 0 # res is for count eatten apple, i is used to indicate the current day\n heap = []\n while i < len(days) or heap: # \u201Cor heap\u201D represents the remaining apples a... | 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 |
1705. Maximum Number of Eaten Apples_[Python 3] Easy-to-understand solution | maximum-number-of-eaten-apples | 0 | 1 | # Code\n```\nimport heapq\nclass Solution:\n def eatenApples(self, apples: List[int], days: List[int]) -> int:\n res, i = 0, 0 # res is for count eatten apple, i is used to indicate the current day\n heap = []\n while i < len(days) or heap: # \u201Cor heap\u201D represents the remaining apples a... | 0 | There is a **3 lane road** of length `n` that consists of `n + 1` **points** labeled from `0` to `n`. A frog **starts** at point `0` in the **second** lane and wants to jump to point `n`. However, there could be obstacles along the way.
You are given an array `obstacles` of length `n + 1` where each `obstacles[i]` (**... | It's optimal to finish the apples that will rot first before those that will rot last You need a structure to keep the apples sorted by their finish time |
Simple matrix solution with Python3 | where-will-the-ball-fall | 0 | 1 | # Intuition\nHere we have matrix `grid`.\nOur goal is to put **at each column** a ball and check, if it\'s **feasible** to let a ball to reach **the final row** (that\'s falling over the board).\n\n# Approach\nAt this point we only need to check for pattern `1 => 1` or `-1 <= -1`, this leads the ball to fall over the b... | 1 | You have a 2-D `grid` of size `m x n` representing a box, and you have `n` balls. The box is open on the top and bottom sides.
Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
* A board that redirects the ball to the right spans the top... | Connect each pair of points with a weighted edge, the weight being the manhattan distance between those points. The problem is now the cost of minimum spanning tree in graph with above edges. |
Simple matrix solution with Python3 | where-will-the-ball-fall | 0 | 1 | # Intuition\nHere we have matrix `grid`.\nOur goal is to put **at each column** a ball and check, if it\'s **feasible** to let a ball to reach **the final row** (that\'s falling over the board).\n\n# Approach\nAt this point we only need to check for pattern `1 => 1` or `-1 <= -1`, this leads the ball to fall over the b... | 1 | Given a string `s`. Return all the words vertically in the same order in which they appear in `s`.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.
**Exam... | Use DFS. Traverse the path of the ball downwards until you reach the bottom or get stuck. |
[Python3] Easy approach! Simulate each ball's trajectory | where-will-the-ball-fall | 0 | 1 | My approach to solving this problem is by simulating the trajectory of each ball.\nAnd if the path is not valid, then the inner loop break, otherwise we update the answer with `res[j] = pos` in the else clause after `for` syntax.\n\n``` \nclass Solution:\n def findBall(self, grid: List[List[int]]) -> List[int]:\n ... | 1 | You have a 2-D `grid` of size `m x n` representing a box, and you have `n` balls. The box is open on the top and bottom sides.
Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
* A board that redirects the ball to the right spans the top... | Connect each pair of points with a weighted edge, the weight being the manhattan distance between those points. The problem is now the cost of minimum spanning tree in graph with above edges. |
[Python3] Easy approach! Simulate each ball's trajectory | where-will-the-ball-fall | 0 | 1 | My approach to solving this problem is by simulating the trajectory of each ball.\nAnd if the path is not valid, then the inner loop break, otherwise we update the answer with `res[j] = pos` in the else clause after `for` syntax.\n\n``` \nclass Solution:\n def findBall(self, grid: List[List[int]]) -> List[int]:\n ... | 1 | Given a string `s`. Return all the words vertically in the same order in which they appear in `s`.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.
**Exam... | Use DFS. Traverse the path of the ball downwards until you reach the bottom or get stuck. |
βοΈ Python Solution with Explanation and Diagram | 97% Faster π₯ | where-will-the-ball-fall | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nclass Solution:\n def findBall(self, grid: List[List[int]]) -> List[int]:\n r_len = len(grid)\n c_len = len(grid[0])\n output = list(range(c_len))\n \n for r in range(r_len):\n for i in range... | 1 | You have a 2-D `grid` of size `m x n` representing a box, and you have `n` balls. The box is open on the top and bottom sides.
Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
* A board that redirects the ball to the right spans the top... | Connect each pair of points with a weighted edge, the weight being the manhattan distance between those points. The problem is now the cost of minimum spanning tree in graph with above edges. |
βοΈ Python Solution with Explanation and Diagram | 97% Faster π₯ | where-will-the-ball-fall | 0 | 1 | **\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n```\nclass Solution:\n def findBall(self, grid: List[List[int]]) -> List[int]:\n r_len = len(grid)\n c_len = len(grid[0])\n output = list(range(c_len))\n \n for r in range(r_len):\n for i in range... | 1 | Given a string `s`. Return all the words vertically in the same order in which they appear in `s`.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.
**Exam... | Use DFS. Traverse the path of the ball downwards until you reach the bottom or get stuck. |
SIMPLE SOLUTION USING DFS | where-will-the-ball-fall | 0 | 1 | ```\nclass Solution:\n def dfs(self,x,y,m,n,grid,visited):\n if visited[x][y]!=None:\n return visited[x][y]\n if y+grid[x][y]<0 or y+grid[x][y]>=n or (grid[x][y]+grid[x][y+grid[x][y]]==0):\n visited[x][y]=-1\n return -1\n visited[x][y]=y+grid[x][y]\n if x+... | 1 | You have a 2-D `grid` of size `m x n` representing a box, and you have `n` balls. The box is open on the top and bottom sides.
Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
* A board that redirects the ball to the right spans the top... | Connect each pair of points with a weighted edge, the weight being the manhattan distance between those points. The problem is now the cost of minimum spanning tree in graph with above edges. |
SIMPLE SOLUTION USING DFS | where-will-the-ball-fall | 0 | 1 | ```\nclass Solution:\n def dfs(self,x,y,m,n,grid,visited):\n if visited[x][y]!=None:\n return visited[x][y]\n if y+grid[x][y]<0 or y+grid[x][y]>=n or (grid[x][y]+grid[x][y+grid[x][y]]==0):\n visited[x][y]=-1\n return -1\n visited[x][y]=y+grid[x][y]\n if x+... | 1 | Given a string `s`. Return all the words vertically in the same order in which they appear in `s`.
Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed).
Each word would be put on only one column and that in one column there will be only one word.
**Exam... | Use DFS. Traverse the path of the ball downwards until you reach the bottom or get stuck. |
Python || 99.52% Faster || Trie || Easy | maximum-xor-with-an-element-from-array | 0 | 1 | ```\nclass Trie:\n def __init__(self):\n self.root={}\n self.m=0\n \n def insert(self,word):\n node=self.root\n for ch in word:\n if ch not in node:\n node[ch]={}\n node=node[ch]\n\n def compare(self,word,x):\n node=self.root\n t... | 2 | You are given an array `nums` consisting of non-negative integers. You are also given a `queries` array, where `queries[i] = [xi, mi]`.
The answer to the `ith` query is the maximum bitwise `XOR` value of `xi` and any element of `nums` that does not exceed `mi`. In other words, the answer is `max(nums[j] XOR xi)` for a... | Suppose the first digit you need is 'd'. How can you determine if it's possible to get that digit there? Consider swapping adjacent characters to maintain relative ordering. |
Python || 99.52% Faster || Trie || Easy | maximum-xor-with-an-element-from-array | 0 | 1 | ```\nclass Trie:\n def __init__(self):\n self.root={}\n self.m=0\n \n def insert(self,word):\n node=self.root\n for ch in word:\n if ch not in node:\n node[ch]={}\n node=node[ch]\n\n def compare(self,word,x):\n node=self.root\n t... | 2 | An experiment is being conducted in a lab. To ensure accuracy, there are **two** sensors collecting data simultaneously. You are given two arrays `sensor1` and `sensor2`, where `sensor1[i]` and `sensor2[i]` are the `ith` data points collected by the two sensors.
However, this type of sensor has a chance of being defec... | In problems involving bitwise operations, we often think on the bits level. In this problem, we can think that to maximize the result of an xor operation, we need to maximize the most significant bit, then the next one, and so on. If there's some number in the array that is less than m and whose the most significant bi... |
[Python3] trie | maximum-xor-with-an-element-from-array | 0 | 1 | **Algo**\nThis problem is similar to [421. Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) which can be solved efficiently via a trie. \n\n**Implementation** (100%)\n```\nclass Solution:\n def maximizeXor(self, nums: List[int], queries: List[List[int]]) ... | 19 | You are given an array `nums` consisting of non-negative integers. You are also given a `queries` array, where `queries[i] = [xi, mi]`.
The answer to the `ith` query is the maximum bitwise `XOR` value of `xi` and any element of `nums` that does not exceed `mi`. In other words, the answer is `max(nums[j] XOR xi)` for a... | Suppose the first digit you need is 'd'. How can you determine if it's possible to get that digit there? Consider swapping adjacent characters to maintain relative ordering. |
[Python3] trie | maximum-xor-with-an-element-from-array | 0 | 1 | **Algo**\nThis problem is similar to [421. Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) which can be solved efficiently via a trie. \n\n**Implementation** (100%)\n```\nclass Solution:\n def maximizeXor(self, nums: List[int], queries: List[List[int]]) ... | 19 | An experiment is being conducted in a lab. To ensure accuracy, there are **two** sensors collecting data simultaneously. You are given two arrays `sensor1` and `sensor2`, where `sensor1[i]` and `sensor2[i]` are the `ith` data points collected by the two sensors.
However, this type of sensor has a chance of being defec... | In problems involving bitwise operations, we often think on the bits level. In this problem, we can think that to maximize the result of an xor operation, we need to maximize the most significant bit, then the next one, and so on. If there's some number in the array that is less than m and whose the most significant bi... |
Python | Trie | Offline Queries | maximum-xor-with-an-element-from-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 | You are given an array `nums` consisting of non-negative integers. You are also given a `queries` array, where `queries[i] = [xi, mi]`.
The answer to the `ith` query is the maximum bitwise `XOR` value of `xi` and any element of `nums` that does not exceed `mi`. In other words, the answer is `max(nums[j] XOR xi)` for a... | Suppose the first digit you need is 'd'. How can you determine if it's possible to get that digit there? Consider swapping adjacent characters to maintain relative ordering. |
Python | Trie | Offline Queries | maximum-xor-with-an-element-from-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 | An experiment is being conducted in a lab. To ensure accuracy, there are **two** sensors collecting data simultaneously. You are given two arrays `sensor1` and `sensor2`, where `sensor1[i]` and `sensor2[i]` are the `ith` data points collected by the two sensors.
However, this type of sensor has a chance of being defec... | In problems involving bitwise operations, we often think on the bits level. In this problem, we can think that to maximize the result of an xor operation, we need to maximize the most significant bit, then the next one, and so on. If there's some number in the array that is less than m and whose the most significant bi... |
simpler solution beat 80% of python users | maximum-units-on-a-truck | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(key=lambda x:x[1],reverse = True)\n\n p=0\n for i, j in boxTypes:\n if i<truckSize:\n p+=i*j\n truckSize-=i\n else:\n ... | 1 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
simpler solution beat 80% of python users | maximum-units-on-a-truck | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(key=lambda x:x[1],reverse = True)\n\n p=0\n for i, j in boxTypes:\n if i<truckSize:\n p+=i*j\n truckSize-=i\n else:\n ... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Greedy Approach | Sorted boxTypes | Beginner friendly | maximum-units-on-a-truck | 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 assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
Greedy Approach | Sorted boxTypes | Beginner friendly | maximum-units-on-a-truck | 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 **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Python β
β
β
|| Faster than 81.43% || Memory Beats 84.9% | maximum-units-on-a-truck | 0 | 1 | # Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(reverse=True, key=lambda x:x[1])\n units = 0\n boxesUsed = 0\n for box in boxTypes:\n if boxesUsed == truckSize: return units\n if boxesUsed + box... | 1 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
Python β
β
β
|| Faster than 81.43% || Memory Beats 84.9% | maximum-units-on-a-truck | 0 | 1 | # Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(reverse=True, key=lambda x:x[1])\n units = 0\n boxesUsed = 0\n for box in boxTypes:\n if boxesUsed == truckSize: return units\n if boxesUsed + box... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
trash solution hoping for coaching | maximum-units-on-a-truck | 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 assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
trash solution hoping for coaching | maximum-units-on-a-truck | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
easy Python3 code | maximum-units-on-a-truck | 0 | 1 | # Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n box=0 \n boxTypes.sort(key=lambda x:-x[1])\n for i in range(len(boxTypes)):\n if truckSize>=boxTypes[i][0]:\n truckSize -= boxTypes[i][0]\n b... | 1 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
easy Python3 code | maximum-units-on-a-truck | 0 | 1 | # Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n box=0 \n boxTypes.sort(key=lambda x:-x[1])\n for i in range(len(boxTypes)):\n if truckSize>=boxTypes[i][0]:\n truckSize -= boxTypes[i][0]\n b... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Simple Solution in Python | Beats 84.28% | maximum-units-on-a-truck | 0 | 1 | \n# Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(key = lambda boxTypes: boxTypes[1], reverse=True)\n res = cur_size = 0\n for box, pack in boxTypes:\n if cur_size == truckSize:\n break\n ... | 1 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
Simple Solution in Python | Beats 84.28% | maximum-units-on-a-truck | 0 | 1 | \n# Code\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(key = lambda boxTypes: boxTypes[1], reverse=True)\n res = cur_size = 0\n for box, pack in boxTypes:\n if cur_size == truckSize:\n break\n ... | 1 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Easiest solution in python3 | maximum-units-on-a-truck | 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)$$ --... | 3 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
Easiest solution in python3 | maximum-units-on-a-truck | 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)$$ --... | 3 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Python 3, Faster than 90%, easy to understand | maximum-units-on-a-truck | 0 | 1 | **Explanation:**\n\nI sorted boxTypes in a way that I would go from maximum units per box to minimum.\nThan i simply just add (units * box) to sum_ while truckSize > 0.\n\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n sum_ = 0\n\t\t#sorting boxTypes by the ... | 3 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
Python 3, Faster than 90%, easy to understand | maximum-units-on-a-truck | 0 | 1 | **Explanation:**\n\nI sorted boxTypes in a way that I would go from maximum units per box to minimum.\nThan i simply just add (units * box) to sum_ while truckSize > 0.\n\n```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n sum_ = 0\n\t\t#sorting boxTypes by the ... | 3 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
[Python] Simple solution | maximum-units-on-a-truck | 0 | 1 | ```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(key=lambda x:x[1],reverse=1)\n s=0\n for i,j in boxTypes:\n i=min(i,truckSize)\n s+=i*j\n truckSize-=i\n if truckSize==0:\n ... | 31 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
[Python] Simple solution | maximum-units-on-a-truck | 0 | 1 | ```\nclass Solution:\n def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n boxTypes.sort(key=lambda x:x[1],reverse=1)\n s=0\n for i,j in boxTypes:\n i=min(i,truckSize)\n s+=i*j\n truckSize-=i\n if truckSize==0:\n ... | 31 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
Python(Simple Solution) | Faster : 96.89 | Time : O(nlogn) | maximum-units-on-a-truck | 0 | 1 | \tclass Solution:\n\t\tdef maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n\t\t\tboxTypes = sorted(boxTypes, key = lambda x : x[1], reverse = True)\n\t\t\toutput = 0\n\t\t\tfor no, units in boxTypes:\n\t\t\t\tif truckSize > no:\n\t\t\t\t\ttruckSize -= no\n\t\t\t\t\toutput += (no * units)\n\t\t\t\... | 9 | You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
* `numberOfBoxesi` is the number of boxes of type `i`.
* `numberOfUnitsPerBoxi` is the number of units in each box of the type `i`.
You are also given... | To speed up the next available server search, keep track of the available servers in a sorted structure such as an ordered set. To determine if a server is available, keep track of the end times for each task in a heap and add the server to the available set once the soonest task ending time is less than or equal to th... |
Python(Simple Solution) | Faster : 96.89 | Time : O(nlogn) | maximum-units-on-a-truck | 0 | 1 | \tclass Solution:\n\t\tdef maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:\n\t\t\tboxTypes = sorted(boxTypes, key = lambda x : x[1], reverse = True)\n\t\t\toutput = 0\n\t\t\tfor no, units in boxTypes:\n\t\t\t\tif truckSize > no:\n\t\t\t\t\ttruckSize -= no\n\t\t\t\t\toutput += (no * units)\n\t\t\t\... | 9 | You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
1. Find a non-negative integer `k < 2maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the `it... | If we have space for at least one box, it's always optimal to put the box with the most units. Sort the box types with the number of units per box non-increasingly. Iterate on the box types and take from each type as many as you can. |
[Python3] Math + Hash Table - Simple | count-good-meals | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N * 22)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity... | 4 | A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.
You can pick **any** two different foods to make a good meal.
Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the `iββββββthββββ`ββββ item of fo... | Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied. |
[Python3] Math + Hash Table - Simple | count-good-meals | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N * 22)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity... | 4 | You are given a string `s` (**0-indexed**)ββββββ. You are asked to perform the following operation on `s`ββββββ until you get a sorted string:
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` f... | Note that the number of powers of 2 is at most 21 so this turns the problem to a classic find the number of pairs that sum to a certain value but for 21 values You need to use something fasters than the NlogN approach since there is already the log of iterating over the powers so one idea is two pointers |
[Python3] frequency table | count-good-meals | 0 | 1 | **Algo**\nCompute frequency table of `deliciousness` from which you can check for complement of each `2**k` given a number (since there are only < 30 such power of 2 to consider).\n\n**Implementation**\n```\nclass Solution:\n def countPairs(self, deliciousness: List[int]) -> int:\n ans = 0\n freq = def... | 44 | A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.
You can pick **any** two different foods to make a good meal.
Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the `iββββββthββββ`ββββ item of fo... | Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied. |
[Python3] frequency table | count-good-meals | 0 | 1 | **Algo**\nCompute frequency table of `deliciousness` from which you can check for complement of each `2**k` given a number (since there are only < 30 such power of 2 to consider).\n\n**Implementation**\n```\nclass Solution:\n def countPairs(self, deliciousness: List[int]) -> int:\n ans = 0\n freq = def... | 44 | You are given a string `s` (**0-indexed**)ββββββ. You are asked to perform the following operation on `s`ββββββ until you get a sorted string:
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` f... | Note that the number of powers of 2 is at most 21 so this turns the problem to a classic find the number of pairs that sum to a certain value but for 21 values You need to use something fasters than the NlogN approach since there is already the log of iterating over the powers so one idea is two pointers |
O(N) Python Solution | count-good-meals | 0 | 1 | # Intuition\nWe know we can solve this in O(n^2). But can we make it efficient? It turns out, yes we can. \n\n# Approach\nWe know that power functions grow very fast.\n- If we look at the constraints, we can see that the largest possible number is 2^20. what this tells us is that, there is exactly 20 numbers in the ran... | 1 | A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.
You can pick **any** two different foods to make a good meal.
Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the `iββββββthββββ`ββββ item of fo... | Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied. |
O(N) Python Solution | count-good-meals | 0 | 1 | # Intuition\nWe know we can solve this in O(n^2). But can we make it efficient? It turns out, yes we can. \n\n# Approach\nWe know that power functions grow very fast.\n- If we look at the constraints, we can see that the largest possible number is 2^20. what this tells us is that, there is exactly 20 numbers in the ran... | 1 | You are given a string `s` (**0-indexed**)ββββββ. You are asked to perform the following operation on `s`ββββββ until you get a sorted string:
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` f... | Note that the number of powers of 2 is at most 21 so this turns the problem to a classic find the number of pairs that sum to a certain value but for 21 values You need to use something fasters than the NlogN approach since there is already the log of iterating over the powers so one idea is two pointers |
[python3] O(N) | 100% | 100% | easy to understand | count-good-meals | 0 | 1 | ```\nclass Solution:\n """\n dp -> key: i value: amount of deliciousness == i\n for d in 2**0, 2**1, ..., 2**21\n why 2**21: max of deliciousness is 2*20, sum of 2 max is 2**21\n O(22*N)\n """\n def countPairs(self, ds: List[int]) -> int:\n from collections import defaultdict\n dp = d... | 5 | A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.
You can pick **any** two different foods to make a good meal.
Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the `iββββββthββββ`ββββ item of fo... | Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied. |
[python3] O(N) | 100% | 100% | easy to understand | count-good-meals | 0 | 1 | ```\nclass Solution:\n """\n dp -> key: i value: amount of deliciousness == i\n for d in 2**0, 2**1, ..., 2**21\n why 2**21: max of deliciousness is 2*20, sum of 2 max is 2**21\n O(22*N)\n """\n def countPairs(self, ds: List[int]) -> int:\n from collections import defaultdict\n dp = d... | 5 | You are given a string `s` (**0-indexed**)ββββββ. You are asked to perform the following operation on `s`ββββββ until you get a sorted string:
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` f... | Note that the number of powers of 2 is at most 21 so this turns the problem to a classic find the number of pairs that sum to a certain value but for 21 values You need to use something fasters than the NlogN approach since there is already the log of iterating over the powers so one idea is two pointers |
Clean Code || O(N) | count-good-meals | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(22N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g.... | 2 | A **good meal** is a meal that contains **exactly two different food items** with a sum of deliciousness equal to a power of two.
You can pick **any** two different foods to make a good meal.
Given an array of integers `deliciousness` where `deliciousness[i]` is the deliciousness of the `iββββββthββββ`ββββ item of fo... | Find the smallest rowSum or colSum, and let it be x. Place that number in the grid, and subtract x from rowSum and colSum. Continue until all the sums are satisfied. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.