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 |
|---|---|---|---|---|---|---|---|
⏳ [VIDEO] O(m log k) Maximizing Computer Run Time with Binary Search | maximum-running-time-of-n-computers | 1 | 1 | # Intuition\nWhen I first read this problem, I realized that it\'s not just about using each battery to its full capacity before moving on to the next. The goal here is to maximize the running time of all computers, and a simple linear approach would not achieve that. The challenge here lies in striking a balance betwe... | 31 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Python3 Solution | maximum-running-time-of-n-computers | 0 | 1 | \n```\nclass Solution:\n def maxRunTime(self, n: int, batteries: List[int]) -> int:\n left=0\n right=sum(batteries)//n+1\n def check(time):\n return sum(min(time,b) for b in batteries)>=n*time\n\n while left<=right:\n mid=(left+right)//2\n if check(mid):\n... | 6 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Binary_Search.py | maximum-running-time-of-n-computers | 0 | 1 | # Code\n```\nclass Solution:\n def maxRunTime(self, n: int, b: List[int]) -> int:\n def check(n,mid):\n return sum(mid if i>mid else i for i in b)/n>=mid\n i,j=0,10**20\n while i<j:\n mid=(i+j)//2\n if check(n,mid):\n i=mid+1\n else:\n ... | 3 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Beats 100% || Clear and concise explanation || No Binary Search No heap || | maximum-running-time-of-n-computers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTry to reach a state where all computers have same runtime by interchanging batteries, or it is not possible to Reach such state because of certain bottleneck batteries in your store.\n\n# Approach\n<!-- Describe your approach to solving ... | 1 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
python 3 - binary search, greedy | maximum-running-time-of-n-computers | 0 | 1 | # Intuition\nObviously you need to try different time t. This part requires binary search. Nothing special. For each binary search iteration, you need to check "if a certain t can be achieved". This is the hardest part.\n\nReverse sort the array first. Sum up bat[n:]. If sum > 0, you can rearrange this sum to any batte... | 1 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Beats 100% in runtime and 98% in memory | maximum-running-time-of-n-computers | 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 have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Python solution. Array sorting. Beats 100% | maximum-running-time-of-n-computers | 0 | 1 | \n# Complexity\n- Time complexity:\n$$O(m\u22C5log\u2061m)$$\n\n- Space complexity:\n$$O(m)$$\n\n\n# Code\n```\nclass Solution:\n def maxRunTime(self, n: int, batteries: List[int]) -> int:\n\n ... | 1 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
JavaScript && Python [Clean, Short and Simple solution] (With Explanation) | maximum-running-time-of-n-computers | 0 | 1 | # JavaScript\n```js\nvar maxRunTime = function(n, batteries) {\n batteries.sort((a, b) => a - b); // Sort\n let total = batteries.reduce((a, b) => a + b, 0); // Sum of all\n\n // If last battery is very high\n // (So that if remaining battery is shared by remaining computers still less than last battery)\n ... | 1 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
100% time complexity, 85% space complexity. m log(m) sorting solution. | maximum-running-time-of-n-computers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nYou want to allocate batteries in the most efficient way possible to get the maximum usage. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe key concept is that by using the n largest batteries, you can ensure th... | 3 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Python | 6 Lines of Code | Easy to Understand | Hard Problem | Maximum Running Time of N Computers | maximum-running-time-of-n-computers | 0 | 1 | # Python | 6 Lines of Code | Easy to Understand | Hard Problem | 2141. Maximum Running Time of N Computers\n```\nclass Solution:\n def maxRunTime(self, n: int, batteries: List[int]) -> int:\n batteries.sort()\n total = sum(batteries)\n while batteries[-1] > total//n:\n n -= 1\n ... | 16 | You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the `ith` battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
Initially, you can insert **at most one batter... | Could you generate all the different numbers comprised of only digit1 and digit2 with the constraints? Going from least to greatest, check if the number you generated is greater than k and a multiple of k. |
Simple python solution | minimum-cost-of-buying-candies-with-discount | 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 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
Beats 100% | 0ms | Greedy 🚀🔥 | minimum-cost-of-buying-candies-with-discount | 1 | 1 | # Intuition\n\n\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 com... | 2 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
4 lines. Beats 99% runtime | minimum-cost-of-buying-candies-with-discount | 0 | 1 | # Intuition\nFor the max value, we have to pay for it.\nFor the second max value, we still have to pay for it.\nFor the third max value, we can get it free one as bonus.\nAnd continuely do this for the rest.\n\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g... | 1 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
Python | 100% Faster | Easy Solution✅ | minimum-cost-of-buying-candies-with-discount | 0 | 1 | # Code\u2705\n```\nclass Solution:\n def minimumCost(self, cost: List[int]) -> int:\n cost = sorted(cost,reverse=True)\n i =0 \n output = 0\n while i<len(cost):\n output += cost[i] if i == len(cost)-1 else cost[i] + cost[i+1]\n i +=3\n return output\n\n\n```\n... | 6 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
One-liner | minimum-cost-of-buying-candies-with-discount | 0 | 1 | Sort cost descending, and then sum, skipping every third cost.\n\n**Python 3**\n```python\nclass Solution:\n minimumCost = lambda self, cost: sum(c for i, c in enumerate(sorted(cost, reverse=True)) if i % 3 != 2)\n```\n\n**C++**\nSame logic in C++.\n```cpp\nint minimumCost(vector<int>& cost) {\n int res = 0;\n ... | 17 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
Greedy solution in Python, beats 100% (36ms) | minimum-cost-of-buying-candies-with-discount | 0 | 1 | Your goal is: skip the costly items as many as possible.\n\nTo do so, you choose the two largest items in the list, and then skip the next largest item. Repeat this process greedily.\n\nAlgorithm is to sort the item in an descending order, and take the first 2 items and move the index by 3, and repeat this until you re... | 6 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
[Python] Simple solution | 100% faster | O(N logN) Time | O(1) Space | minimum-cost-of-buying-candies-with-discount | 0 | 1 | Understanding:\nReverse sort the array of costs. Now the first two will be highest costs which can help us get the third one for free and so on. \n\nAlgorithm:\n- Reverse sort the array\n- Initialize index i at 0, resultant cost as 0 and N as length of array cost\n- Add the cost of 2 candies to result, i.e., the cost a... | 10 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
[Python3] Sort + One Pass + Greedy | O(n) Time | O(1) Space | minimum-cost-of-buying-candies-with-discount | 0 | 1 | Below is the code, please let me know if you have any questions!\n```\nclass Solution:\n def minimumCost(self, cost: List[int]) -> int:\n cost.sort(reverse=True)\n bought = res = 0\n for p in cost:\n if bought < 2:\n res += p\n bought += 1\n el... | 2 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
[Python3] greedy 1-line | minimum-cost-of-buying-candies-with-discount | 0 | 1 | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/8fc8469e823a229654fc858172dfa9383d805e39) for solutions of biweekly 70. \n\n**Intuition**\nSort the candies in descending order and group them by every 3 candies. Among each group, add the cost of largest 2 to the final answer. \n\n```\nclass Solut... | 5 | A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
* For example, if there... | Could you keep track of the minimum element visited while traversing? We have a potential candidate for the answer if the prefix min is lesser than nums[i]. |
Right - Left | count-the-hidden-sequences | 0 | 1 | Assuming that the starting point is zero, we determine the range of the hidden sequence ([left, right]).\n\nIf this range is smaller than [lower, upper] - we can form a valid sequence. The difference between those two ranges tells us how many valid sequences we can form: `upper - lower - (right - left) + 1`.\n\n**Pytho... | 5 | You are given a **0-indexed** array of `n` integers `differences`, which describes the **differences** between each pair of **consecutive** integers of a **hidden** sequence of length `(n + 1)`. More formally, call the hidden sequence `hidden`, then we have that `differences[i] = hidden[i + 1] - hidden[i]`.
You are fu... | There are n choices for when the first robot moves to the second row. Can we use prefix sums to help solve this problem? |
✔️ [Python3] FLOWING CONSTRAINT ♪♪ ヽ(ˇ∀ˇ )ゞ, Explained | count-the-hidden-sequences | 0 | 1 | **UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nThe idea is to calculate constraints for every element of the hidden sequence starting from the beginning. The last constraint will be equal to the range that the last element of the sequence can take. Considering th... | 7 | You are given a **0-indexed** array of `n` integers `differences`, which describes the **differences** between each pair of **consecutive** integers of a **hidden** sequence of length `(n + 1)`. More formally, call the hidden sequence `hidden`, then we have that `differences[i] = hidden[i + 1] - hidden[i]`.
You are fu... | There are n choices for when the first robot moves to the second row. Can we use prefix sums to help solve this problem? |
[Python3] compare range | count-the-hidden-sequences | 0 | 1 | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/8fc8469e823a229654fc858172dfa9383d805e39) for solutions of biweekly 70. \n\n**Intuition**\nCompute the range of variation from `differences`. Compuare this range with allowed range `upper - lower`. If it is within this range, then a solutions exist... | 2 | You are given a **0-indexed** array of `n` integers `differences`, which describes the **differences** between each pair of **consecutive** integers of a **hidden** sequence of length `(n + 1)`. More formally, call the hidden sequence `hidden`, then we have that `differences[i] = hidden[i + 1] - hidden[i]`.
You are fu... | There are n choices for when the first robot moves to the second row. Can we use prefix sums to help solve this problem? |
✔️ Python3 Solution | Heap | k-highest-ranked-items-within-a-price-range | 0 | 1 | # Complexity\n- Time complexity: $$O(n*m)$$\n- Space complexity: $$O(n*m)$$\n\n# Code\n```\nclass Solution:\n def highestRankedKItems(self, grid, pricing, start, k):\n\n res = []\n m, n = len(grid), len(grid[0])\n low, high = pricing\n row, col = start\n heap = deque([(0, row, col)... | 1 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
[Python 3] Dijkstra Algorithm - Easy to Understand | k-highest-ranked-items-within-a-price-range | 0 | 1 | # Intuition\n- First we can recognize that we should find the shortest path from start to all the cells -> Dijkstra\n- then we after we have the shortest path from the start cell to all other cells, we may sort it and take k highest one with value in range pricing (we can do this better utilize the properites in dijkst... | 3 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
[Java/Python 3] BFS using PriorityQueue/heap w/ brief explanation and analysis. | k-highest-ranked-items-within-a-price-range | 1 | 1 | **Q & A**\n\nQ1: Why do you have `ans.size() < k` in the `while` condition?\nA1: The `PriorityQueue/heap` is sorted according to the rank specified in the problem. Therefore, the `ans` always holds highest ranked items within the given price range, and once it reaches the size of `k`, the loop does NOT need to iterate ... | 16 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
Dijkstra Algorithm, and a list of similar questions | k-highest-ranked-items-within-a-price-range | 0 | 1 | If you are new to Dijkstra, see my other posts with graph explanations. https://leetcode.com/problems/the-maze-ii/discuss/1377876/improved-dijkstra-based-on-the-most-voted-solution/1208402\n\nFor such matrix-like problem, can always think about coverting it to a graph problem: each cell represents a node in graph and i... | 6 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
Python || Clean and Simple solution using BFS and Priority Queue | k-highest-ranked-items-within-a-price-range | 0 | 1 | ```python\nfrom collections import deque\nfrom heapq import heappush, heappushpop\n\nMOVES = (\n (1, 0),\n (-1, 0),\n (0, 1),\n (0, -1),\n)\nWALL = 0\n\n\nclass Solution:\n def highestRankedKItems(self, M: list[list[int]], P: list[int],\n start: list[int], k: int) -> list[list[... | 0 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
Quite Ordinary Approach | k-highest-ranked-items-within-a-price-range | 0 | 1 | # Complexity\n- Time complexity: $$O(n*log(n))$$, n - number of elements in the grid.\n\n- Space complexity: $$O(n)$$.\n\n# Code\n```\nclass Solution:\n def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:\n m, n = len(grid), len(grid[0])\n ... | 0 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
Python BFSb | k-highest-ranked-items-within-a-price-range | 0 | 1 | # Code\n```\nclass Solution:\n def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:\n if not grid:\n return []\n \n m, n = len(grid), len(grid[0])\n d = [[0,1], [0,-1], [1,0], [-1,0]]\n h = []\n que... | 0 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
Level wise BFS for each cell #Python3 || 86% beats | k-highest-ranked-items-within-a-price-range | 0 | 1 | # Intuition\nSince we want to return k highest ranked items, and for determining rank we need to use (level, price, row, col). The shortest the distance better. Because of this I decided to use BFS to track the shortest distance starting from start point. So we will explore neighbors level by level and add them to the ... | 0 | You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
* `0` represents a wall that you cannot pass through.
* `1` represents an empty cell that you can freely move to and from.
* All other positive int... | Check all possible placements for the word. There is a limited number of places where a word can start. |
【Video】Give me 7 minutes - How we think about a solution | number-of-ways-to-divide-a-long-corridor | 1 | 1 | # Intuition\nConsider two seats as a pair.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/DF9NXNW0G6E\n\n\u25A0 Timeline of the video\n\n`0:05` Return 0\n`0:38` Think about a case where we can put dividers\n`4:44` Coding\n`7:18` Time Complexity and Space Complexity\n`7:33` Summary of the algorithm with my solution code... | 15 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
multiply plant counts | number-of-ways-to-divide-a-long-corridor | 0 | 1 | One possible solution can use the fact that the ans is the multiplication of - no of consecutive plants between seats (when numbered 1 indexed ) n and n +1 ( where n = 2,4,6 ... )\n\nS P S **P** S P S **P** S P S\n\nThe two bold Ps above has 2 possible solutions, ans would be 2*2 = 4\n\n\n```\nclass Solution:\n def ... | 4 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
✅☑[C++/Java/Python/JavaScript] || 5 Approaches || DP || EXPLAINED🔥 | number-of-ways-to-divide-a-long-corridor | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Top Dowm DP)***\n1. **count function:**\n\n - This function is responsible for counting the number of valid ways to divide the corridor string into sections with exactly two \'S\'s.\n - It uses dynamic pr... | 4 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
✔️🚀Beats 99% | 102ms | Dynamic Programming Bottom Up Approach with Space Optimisation | number-of-ways-to-divide-a-long-corridor | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe can say that to compute three values at index indexindexindex, we need only three values at index index+1index + 1index+1. Hence, we can save only three variables, namely zero, one, and two that represent the values at the most recent ... | 15 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
Easy Full Explanation 💯 || Beginner Friendly🔥🔥|| 100% Fast | number-of-ways-to-divide-a-long-corridor | 1 | 1 | \n\n# Intuition\n- Iterate through the corridor, counting consecutive seats and the number of divisions between them.\n- For each section of consecutive seats, calculate ... | 3 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
Easy Python Solution|| O(N) with O(1) Space complexity|| :) | number-of-ways-to-divide-a-long-corridor | 0 | 1 | # Generalizing the problem\n\n\n## - If Seats < 2,then\nNo division possible\n\n---\n\n\n## - If Seats == 2,then\nonly 1 division possible\n\n---\n\n\n## - If Seats are odd,then\nNo division is possible since the question requires each division to have exactly 2 seats\n\n---\n\n\n## - If Seats >2,then\n#### Explainatio... | 2 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
Count only needed plants. O(n) (beats 100% time) | number-of-ways-to-divide-a-long-corridor | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1) We don\'t need to consider plants before first seat and after last seat\n2) We can only place dividers between odd and even numbered seats (0-indexed)\n3) Special cases:\n Odd number of seats -> return 0\n No seats -> return 0\n#... | 2 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
Python3 Solution | number-of-ways-to-divide-a-long-corridor | 0 | 1 | \n```\nclass Solution:\n def numberOfWays(self, corridor: str) -> int:\n mod=10**9+7\n a,b,c=1,0,0\n for ch in corridor:\n if ch=="S":\n a,b,c=0,a+c,b\n else:\n a,b,c=a+c,b,c\n return c%mod \n``` | 2 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
🥇 C++ | PYTHON | JAVA || O( N ) EXPLAINED || ; ] ✅ | number-of-ways-to-divide-a-long-corridor | 1 | 1 | \n# Solution 1\n\n- We need to place a dividor after 2 seats.\n- When a dividor is placed between any 2 seats : \n- * `Number of ways to do so = (Number of plants in between seats + 1)`\n\nSo we iterate through the String\n1. After every two seats : Count the number of plants till next seat\n2. Number of ways to put di... | 42 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
Python 7lines / Rust a little more. Linear solution, constant time with explanation | number-of-ways-to-divide-a-long-corridor | 0 | 1 | # Intuition\n\nFirst of all it is clear that if the number of seats you have is odd or there are no seats, you can\'t really create any divisions and the answer is zero. So lets focus on another situation and lets change seat to 1 and plant to 0 (as it is just easy for me to distinguish between them). Also it is clear ... | 1 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
✅ One Line Solution | number-of-ways-to-divide-a-long-corridor | 0 | 1 | # Code #1 - Oneliner\n```\nclass Solution:\n def numberOfWays(self, c: str) -> int:\n return prod(len(m.group(1))+1 for m in re.finditer(r\'S(P*)S\', c[c.find(\'S\')+1:]))%(10**9+7) if (s:=c.count(\'S\')) and s%2 == 0 else 0\n```\n\n# Code #1.1 - Oneliner Unwrapped\n```\nclass Solution:\n def numberOfWays(... | 4 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
Sliding Window | O(1) Space | number-of-ways-to-divide-a-long-corridor | 0 | 1 | Simple Sliding Window approach\n# C++\n```\nint numberOfWays(string corridor) {\n long mod = 1e9 + 7;\n int ans = 1;\n int seats = 0;\n for (int l = 0, r = 0; r < corridor.size(); r++) {\n if (corridor[r] == \'P\') continue;\n seats++;\n if (seats < 2) continue;\n if (seats % 2 =... | 1 | Along a long library corridor, there is a line of seats and decorative plants. You are given a **0-indexed** string `corridor` of length `n` consisting of letters `'S'` and `'P'` where each `'S'` represents a seat and each `'P'` represents a plant.
One room divider has **already** been installed to the left of index `... | The number of operators in the equation is less. Could you find the right answer then generate all possible answers using different orders of operations? Divide the equation into blocks separated by the operators, and use memoization on the results of blocks for optimization. Use set and the max limit of the answer for... |
simple.py | count-elements-with-strictly-smaller-and-greater-elements | 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 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Simple python method | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | \n\n```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n min_num = min(nums)\n max_num = max(nums)\n count = 0\n for i in nums:\n if i > min_num and i < max_num:\n count += 1\n return count\n \n``` | 0 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
[C++] [Python] Brute Force with Optimized Approach || Too Easy || Fully Explained | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nSuppose we have the following input vector:\n```\nvector<int> nums = {3, 1, 7, 4, 2, 6};\n```\n\nNow, let\'s go through the function step by step:\n\n1. Initialize two boolean variables, `mx` (for "maximum") and `mn` (for "minimum"), to false.\n2. Ini... | 3 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
easiest 7 liner code in python | count-elements-with-strictly-smaller-and-greater-elements | 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 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Very very easy code in just 3 lines using Python | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | ```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n res = 0\n mn = min(nums)\n mx = max(nums)\n for i in nums:\n if i > mn and i < mx:\n res += 1\n return res\n``` | 13 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Python Min and Max solution | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | **Python :**\n\n```\ndef countElements(self, nums: List[int]) -> int:\n\tmin_ = min(nums)\n\tmax_ = max(nums)\n\n\treturn sum(1 for i in nums if min_ < i < max_)\n```\n\n**Like it ? please upvote !** | 8 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Simple Python one-liner | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | \n# Code\n```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n return len(nums) - nums.count(max(nums)) - nums.count(min(nums)) if max(nums) != min(nums) else 0\n``` | 3 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Easy python solution || 45 ms Beats 97.88% | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n from collections import Counter\n if len(Counter(nums))<=1:\n return 0\n nums=sorted(nums)\n return len(nums)-nums.count(nums[0])-nums.count(nums[-1])\n``` | 2 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Python Easy Solution | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | # Code\n```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n d=dict()\n cnt=0\n for i in nums:\n if i in d:\n d[i]+=1\n else:\n d[i]=1\n nums=list(set(nums))\n nums.sort()\n if len(nums)>=3:\n ... | 1 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Very easy [Python] | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | ```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n min_=min(nums)\n max_=max(nums)\n c=0\n for i in nums:\n if min_<i<max_:\n c+=1\n return c\n``` | 1 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Python Solution Using Sorting and Counter | count-elements-with-strictly-smaller-and-greater-elements | 0 | 1 | ```\nclass Solution:\n def countElements(self, nums: List[int]) -> int:\n nums.sort()\n freq_table = Counter(nums)\n arr = list(freq_table.keys())\n arr.sort()\n ans = len(nums)\n ans -= freq_table[arr[0]]\n ans -= freq_table[arr[-1]]\n return max(ans,0)\n``` | 3 | Given an integer array `nums`, return _the number of elements that have **both** a strictly smaller and a strictly greater element appear in_ `nums`.
**Example 1:**
**Input:** nums = \[11,7,2,15\]
**Output:** 2
**Explanation:** The element 7 has the element 2 strictly smaller than it and the element 11 strictly great... | Can we sort the arrays to help solve the problem? Can we greedily match each student to a seat? The smallest positioned student will go to the smallest positioned chair, and then the next smallest positioned student will go to the next smallest positioned chair, and so on. |
Simplest Beginner friendly Python solution | rearrange-array-elements-by-sign | 0 | 1 | \n# Code\n```\nclass Solution:\n def rearrangeArray(self, nums: List[int]) -> List[int]:\n p, n = [x for x in nums if x > 0], [x for x in nums if x < 0]\n a = []\n for i in range(len(p)):\n a.append(p[i])\n a.append(n[i])\n return a \n``` | 1 | You are given a **0-indexed** integer array `nums` of **even** length consisting of an **equal** number of positive and negative integers.
You should **rearrange** the elements of `nums` such that the modified array follows the given conditions:
1. Every **consecutive pair** of integers have **opposite signs**.
2. ... | Does the number of moves a player can make depend on what the other player does? No How many moves can Alice make if colors == "AAAAAA" If a group of n consecutive pieces has the same color, the player can take n - 2 of those pieces if n is greater than or equal to 3 |
2 lists | rearrange-array-elements-by-sign | 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 **0-indexed** integer array `nums` of **even** length consisting of an **equal** number of positive and negative integers.
You should **rearrange** the elements of `nums` such that the modified array follows the given conditions:
1. Every **consecutive pair** of integers have **opposite signs**.
2. ... | Does the number of moves a player can make depend on what the other player does? No How many moves can Alice make if colors == "AAAAAA" If a group of n consecutive pieces has the same color, the player can take n - 2 of those pieces if n is greater than or equal to 3 |
# 3 Down to Up stage Solution | rearrange-array-elements-by-sign | 0 | 1 | @LEE BOSS\n After Solving qustions i watch @Lee submittion...Everytime i got something new and i tried to implement this in my code gives me a lot of confidence. Thanks @Lee Sir\n\n\n \'\'\' 1st ONE\n pos=[po for po in nums if po > 0]\n neg=[ne for ne in nums if ne < 0]\n res=[]\n ... | 2 | You are given a **0-indexed** integer array `nums` of **even** length consisting of an **equal** number of positive and negative integers.
You should **rearrange** the elements of `nums` such that the modified array follows the given conditions:
1. Every **consecutive pair** of integers have **opposite signs**.
2. ... | Does the number of moves a player can make depend on what the other player does? No How many moves can Alice make if colors == "AAAAAA" If a group of n consecutive pieces has the same color, the player can take n - 2 of those pieces if n is greater than or equal to 3 |
Python Easy Solution || Hash Table | find-all-lonely-numbers-in-the-array | 0 | 1 | # Code\n```\nclass Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n dic={}\n res=[]\n for i in nums:\n if i in dic:\n dic[i]+=1\n else:\n dic[i]=1\n for i in nums:\n if dic[i]==1:\n if (i-1 not ... | 1 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
Python Elegant & Short | Hash Map | Counter | find-all-lonely-numbers-in-the-array | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n count = Counter(nums)\n return [num for num, cnt in count.items() if cnt == 1 and count[num - 1] == count[num + 1] == 0]\n``` | 1 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
Easy Python Solution | find-all-lonely-numbers-in-the-array | 0 | 1 | # Intuition\nThe code counts the frequency of integers in the input list using the collections.Counter() function. It then checks if an integer occurs only once in the input list and does not have adjacent integers. The integers satisfying these conditions are added to the output list.\n\n# Approach\nThe approach taken... | 1 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
Counter | find-all-lonely-numbers-in-the-array | 0 | 1 | **Python 3**\n```python\nclass Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n m = Counter(nums)\n return [n for n in nums if m[n] == 1 and m[n - 1] + m[n + 1] == 0]\n```\n\n**C++**\n```cpp\nvector<int> findLonely(vector<int>& nums) {\n unordered_map<int, int> m;\n vector<int> r... | 49 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
Python Dictionary | find-all-lonely-numbers-in-the-array | 0 | 1 | ```\nclass Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n dict1=dict()\n l=[]\n for i in nums:\n if(i in dict1.keys()):\n dict1[i]=-1\n else: \n dict1[i]=1\n dict1[i-1]=-1\n dict1[i+1]=-1 \n ... | 2 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
Python Solution Using Sorting & Counter | find-all-lonely-numbers-in-the-array | 0 | 1 | ```\nclass Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n ans = []\n freq_count = Counter(nums)\n nums.sort()\n n = len(nums)\n for i in range(n):\n val = nums[i]\n if freq_count[val] == 1 and (i == 0 or nums[i-1] != val-1) and (i == n-1 or ... | 2 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
[Python] Easy to undestand hash map | find-all-lonely-numbers-in-the-array | 0 | 1 | ```\nclass Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n if not nums:\n return []\n count = {}\n for num in nums:\n if num in count:\n count[num] += 1\n else:\n count[num] = 1\n \n result = []\n ... | 1 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
C++ & Python | Easy to understand | O(n) | Simple, Clean, Explained | find-all-lonely-numbers-in-the-array | 0 | 1 | ### Approach:\nHashmaps are the most convenient way since we are dealing with frequencies of more than one element. The definition of a lonely number `x` requires us to check:\n1. the presence of `x - 1` and `x + 1` -> frequency of `x - 1` and `x + 1` = 0\n2. whether frequency of `x` in the array to be **exactly 1**.\... | 2 | You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1)` appear in the array.
Return _**all** lonely numbers in_ `nums`. You may return the answer in **any order**.
**Example 1:**
**Input:** nums = \[10,6,5,8\]
**Output:**... | Can we split this problem into four cases depending on the sign of the numbers? Can we binary search the value? |
Bitmasking Video Solution | maximum-good-people-based-on-statements | 0 | 1 | For the full video, you can check out this [Youtube video]( https://youtu.be/v7sr-1Zbh6k) \n\nSince `n<=15` we can brute force our solution and generate all possibilities of good / bad people of our group.\nThen for each of those arrangement we will verify if there are no contradictions. \nIf there are no contradiction... | 4 | There are two types of persons:
* The **good person**: The person who always tells the truth.
* The **bad person**: The person who might tell the truth and might lie.
You are given a **0-indexed** 2D integer array `statements` of size `n x n` that represents the statements made by `n` people about each other. Mor... | What method can you use to find the shortest time taken for a message from a data server to reach the master server? How can you use this value and the server's patience value to determine the time at which the server sends its last message? What is the time when the last message sent from a server gets back to the ser... |
Python 3 | Itertools & Bitmask | Explanation | maximum-good-people-based-on-statements | 0 | 1 | ### Explanation\n- The idea is super simple: Test out possibilities and take the one has most `good` persons\n\t- There are at most `2^15 = 32768` possibilities\n\t- The `n*n` matrix `statements` will have at most `15 * 15 = 225` cells\n\t- Total will be `2 ^ 15 * 15 * 15 = 7372800 ~= 7 * 10^3 * 10^3`\n\t\t- The genera... | 2 | There are two types of persons:
* The **good person**: The person who always tells the truth.
* The **bad person**: The person who might tell the truth and might lie.
You are given a **0-indexed** 2D integer array `statements` of size `n x n` that represents the statements made by `n` people about each other. Mor... | What method can you use to find the shortest time taken for a message from a data server to reach the master server? How can you use this value and the server's patience value to determine the time at which the server sends its last message? What is the time when the last message sent from a server gets back to the ser... |
Bitmask Iteration | Python | maximum-good-people-based-on-statements | 0 | 1 | \n# Code\n```\nfrom typing import List\n\n\nclass Solution:\n\tdef maximumGood(self, statements: List[List[int]]) -> int:\n\t\tn = len(statements)\n\n\t\tdef correct(mask):\n\t\t\tfor p in range(n):\n\t\t\t\tfor q in range(n):\n\t\t\t\t\tif not (\n\t\t\t\t\t\t\tstatements[p][q] == 2 or\n\t\t\t\t\t\t\t(mask >> p & 1) ==... | 0 | There are two types of persons:
* The **good person**: The person who always tells the truth.
* The **bad person**: The person who might tell the truth and might lie.
You are given a **0-indexed** 2D integer array `statements` of size `n x n` that represents the statements made by `n` people about each other. Mor... | What method can you use to find the shortest time taken for a message from a data server to reach the master server? How can you use this value and the server's patience value to determine the time at which the server sends its last message? What is the time when the last message sent from a server gets back to the ser... |
Top python Solution | keep-multiplying-found-values-by-two | 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)$$ --... | 2 | You are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.
You then do the following steps:
1. If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).
2. Otherwise, **stop** the proces... | Find the smallest substring you need to consider at a time. Try delaying a move as long as possible. |
Python solution simple faster than 80% less memory than 99% | keep-multiplying-found-values-by-two | 0 | 1 | \tclass Solution:\n\n\t def findFinalValue(self, nums: List[int], original: int) -> int:\n\t\n while original in nums:\n\t\t\n original *= 2\n\t\t\t\n return original\n\t\t\n\t\t\n | 2 | You are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.
You then do the following steps:
1. If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).
2. Otherwise, **stop** the proces... | Find the smallest substring you need to consider at a time. Try delaying a move as long as possible. |
very easy cod uzbeksila | keep-multiplying-found-values-by-two | 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 of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.
You then do the following steps:
1. If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).
2. Otherwise, **stop** the proces... | Find the smallest substring you need to consider at a time. Try delaying a move as long as possible. |
[Python 3] Prefix Sum + Hash Table - Easy to Understand | all-divisions-with-the-highest-score-of-a-binary-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: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here... | 2 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
Prefix and Suffix arrays + HashMap | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nusing prefix, suffix arrays and storing running count of 0\'s and 1\'s\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing prefix,suffix arrays by storing running count of 1\'s and 0\'s\nand then using a hashmap t... | 2 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
Python | O(n) step by step solution | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | Starting with `i=0`, the left array will be empty, so the division score is the number of ones in `nums`, which we can easily get by summing its elements. Let\'s initialize `max_score` to be this score as well:\n```python\nscore = max_score = sum(nums)\n```\nWe may create a `highest_scores` list which will hold the ind... | 1 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
Python || Easy || O(n) Solution || Prefix Sum | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | ```\nclass Solution:\n def maxScoreIndices(self, nums: List[int]) -> List[int]:\n l,r=0,sum(nums)\n n=len(nums)\n ans=[0]\n m=r\n for i in range(n):\n if nums[i]==0:\n l+=1\n else:\n r-=1\n s=l+r\n if s==m:\n... | 3 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
[Python3] scan | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/badfbf601ede2f43ba191b567d6f5c7e0d046286) for solutions of weekly 278. \n\n```\nclass Solution:\n def maxScoreIndices(self, nums: List[int]) -> List[int]:\n ans = [0]\n cand = most = nums.count(1)\n for i, x in enumerate(num... | 2 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
Python 3 Solutions | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | # Approach 1:\nCounted the zeros from left(NumsLeft) and ones from the right(NumsRight).\nFound the total sum for each index i and if it is the maximum sum, appended the index in a list.\n# Solution 1:\n```\nclass Solution:\n def maxScoreIndices(self, nums: List[int]) -> List[int]:\n\t\tn=len(nums)\n\t\tzero=[0]*(n+... | 2 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
python | O(n) | Beats 100.00% | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\n- we first count the number of zeros and ones\n- initalize an array to store the score \n- add the value of ones at first\n- create another variable to keep count of the zeros that are to the left\n- iterate starting from index 1 to len(nums)+1\n- w... | 0 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
95%, 95%, 7 Line O(n) | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | # Code\n```\nclass Solution:\n def maxScoreIndices(self, nums: List[int]) -> List[int]:\n mx = nums.count(1); cnt = [0]; cur = mx\n for i, num in enumerate(nums):\n if num == 0: cur += 1\n else: cur -=1\n if cur > mx: mx = cur; cnt = [i+1]\n elif cur == mx: c... | 0 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
Easy to understand Python3 solution | all-divisions-with-the-highest-score-of-a-binary-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 a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
A clear O(n) solution | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | # Intuition\nIf we know the score for division at index $$i$$, then calculating the score for $$i + 1$$ is easy.\n\nWhen we move from $$i$$ to $$i + 1$$ state, we remove $$nums[i]$$ from $$nums_{right}$$ and then add it to $$nums_{left}$$.\n\n- If $$nums[i]$$ is $$0$$, then it is a gain for $$nums_{left}$$. $$nums_{rig... | 0 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
Python || Clean and Simple | all-divisions-with-the-highest-score-of-a-binary-array | 0 | 1 | ```\nclass Solution:\n def maxScoreIndices(self, A: list[int]) -> list[int]:\n output, s = [], -1\n\n for i, score in enumerate(self.generate_scores(A)):\n if s < score:\n output, s = [i], score\n elif s == score:\n output.append(i)\n\n return ... | 0 | You are given a **0-indexed** binary array `nums` of length `n`. `nums` can be divided at index `i` (where `0 <= i <= n)` into two arrays (possibly empty) `numsleft` and `numsright`:
* `numsleft` has all the elements of `nums` between index `0` and `i - 1` **(inclusive)**, while `numsright` has all the elements of n... | What should the sum of the n rolls be? Could you generate an array of size n such that each element is between 1 and 6? |
[Python3] rolling hash | find-substring-with-given-hash-value | 0 | 1 | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/badfbf601ede2f43ba191b567d6f5c7e0d046286) for solutions of weekly 278. \n\n```\nclass Solution:\n def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:\n pp = pow(power, k-1, modulo)\n hs = ii = 0 \n... | 2 | The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:
* `hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m`.
Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.... | There are limited outcomes given the current sum and the stones remaining. Can we greedily simulate starting with taking a stone with remainder 1 or 2 divided by 3? |
Rolling Hash Back to Front in python3 | find-substring-with-given-hash-value | 0 | 1 | ```\nclass Solution:\n def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:\n ords = [ord(l) - ord(\'a\') + 1 for l in s]\n pows = [pow(power,i,modulo) for i in range(k)]\n n = len(s) \n hash0 = sum(pows[i]*ords[n-k+i] for i in range(k)) % (modul... | 0 | The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:
* `hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m`.
Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.... | There are limited outcomes given the current sum and the stones remaining. Can we greedily simulate starting with taking a stone with remainder 1 or 2 divided by 3? |
Time: O(n)O(n) Space: O(1)O(1) | find-substring-with-given-hash-value | 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 | The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:
* `hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m`.
Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.... | There are limited outcomes given the current sum and the stones remaining. Can we greedily simulate starting with taking a stone with remainder 1 or 2 divided by 3? |
[Python3] union-find | groups-of-strings | 0 | 1 | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/badfbf601ede2f43ba191b567d6f5c7e0d046286) for solutions of weekly 278. \n\n```\nclass UnionFind: \n def __init__(self, n): \n self.parent = list(range(n))\n self.rank = [1] * n \n \n def find(self, p): \n if p != s... | 6 | You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.
Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one... | Use stack. For every character to be appended, decide how many character(s) from the stack needs to get popped based on the stack length and the count of the required character. Pop the extra characters out from the stack and return the characters in the stack (reversed). |
(Python3) Union find/ Bitmask - Beats 97.83% | groups-of-strings | 0 | 1 | # Intuition\nUsing an out of range mask (`SIMILAR_MASK`) to detect when we can replace one word to get another is copied from https://leetcode.com/problems/groups-of-strings/solutions/1732959/python3-union-find/\n\n# Complexity\n- Time complexity:\n $$O(n*s*alphabetSize*Ackermann(n))$$\nwhere `n = len(words)`, `s = max... | 1 | You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.
Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one... | Use stack. For every character to be appended, decide how many character(s) from the stack needs to get popped based on the stack length and the count of the required character. Pop the extra characters out from the stack and return the characters in the stack (reversed). |
[Python] Bitmask & UnionFind | groups-of-strings | 0 | 1 | Caution: this code gets accepted, but sometimes gets TLE.\n\n\n\n\nFirst, we just need to think about the set of characters. This means that we can handle each word as a bit mask.\nFor example, \'a\' can be wr... | 3 | You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.
Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one... | Use stack. For every character to be appended, decide how many character(s) from the stack needs to get popped based on the stack length and the count of the required character. Pop the extra characters out from the stack and return the characters in the stack (reversed). |
Python3 with explanation | groups-of-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:\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 **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.
Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one... | Use stack. For every character to be appended, decide how many character(s) from the stack needs to get popped based on the stack length and the count of the required character. Pop the extra characters out from the stack and return the characters in the stack (reversed). |
[Python] AC Union Find O(N*26) | Pre-Processing | groups-of-strings | 0 | 1 | # Code\n```\nclass Solution:\n def groupStrings(self, words: List[str]) -> List[int]:\n def gm(w):\n mk =0\n for x in w:\n mk |= 1<<(ord(x)-ord(\'a\'))\n return mk\n masks = [gm(w) for w in words]\n d = dict([(w,i) for i,w in enumerate(masks)])\n ... | 0 | You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.
Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one... | Use stack. For every character to be appended, decide how many character(s) from the stack needs to get popped based on the stack length and the count of the required character. Pop the extra characters out from the stack and return the characters in the stack (reversed). |
Python (Simple DFS + Bitmask) | groups-of-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:\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 **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.
Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one... | Use stack. For every character to be appended, decide how many character(s) from the stack needs to get popped based on the stack length and the count of the required character. Pop the extra characters out from the stack and return the characters in the stack (reversed). |
Python - simple & fast with explanation - no permutation | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | Please upvote if you like my solution. Let me know in the comments if you have any suggestions to increase performance or readability. \n\n**Notice** that:\n1. We do not need to generate all possible combinations of spliting the input into two numbers. In the ideal solution, num1 and num2 will always be of equal length... | 61 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
🔥 [Python3] 2 line, simple sort and make pairs | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | ```\nclass Solution:\n def minimumSum(self, num: int) -> int:\n d = sorted(str(num))\n return int(d[0] + d[2]) + int(d[1] + d[3])\n\n``` | 14 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Easy Python3 Solution | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nConvert the integer into a sorted string. After sorting,ascending order is obtained.\nreturn sum of the lowest sum pairs after converting pairs backs to integers.\n# Code\n```\nclass Solution:\n def minimumSum(self, num: int) -> int:\n l=s... | 1 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Easy Python Solution - minSum | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | \n\n# Code\n```\nclass Solution:\n def minimumSum(self, num: int) -> int:\n l=[]\n while num != 0:\n l.append(num % 10)\n num = num // 10\n \n l.sort()\n res = (l[1] * 10 + l[2]) + (l[0] * 10 + l[3])\n return res\n``` | 5 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Python || Easy || 3Lines || Sorting || | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | ```\nclass Solution:\n def minimumSum(self, num: int) -> int:\n n=[num//1000,(num//100)%10,(num//10)%10,num%10] #This line will convert the four digit no. into array \n n.sort() #It will sort the digits in ascending order\n return (n[0]*10+n[3])+(n[1]*10+n[2]) # Combination of first and last and... | 4 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Magic Logic Marvellous Python3 | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 8 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Python || 2 Approach (Math, String) || 99.70%beats | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | \n# Code\n> # Full Math Approach\n```\nclass Solution:\n def minimumSum(self, num: int) -> int:\n n = []\n while num > 0:\n n.append(num%10)\n num = num // 10\n \n n.sort()\n\n n1 = n[0] * 10 + n[-1]\n n2 = n[1] * 10 + n[-2]\n return n1+ n2\n\n\n... | 3 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Convert into the list and then store the value.. | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | # Code\n```\nclass Solution:\n def minimumSum(self, num: int) -> int:\n nums=list(str(num))\n nums.sort()\n return int(nums[0]+nums[-1])+int(nums[1]+nums[2])\n \n``` | 5 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Simple Solution || Minimum sum of four digit number after splitting digits | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Simple and Logic code in python | minimum-sum-of-four-digit-number-after-splitting-digits | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given a **positive** integer `num` consisting of exactly four digits. Split `num` into two new integers `new1` and `new2` by using the **digits** found in `num`. **Leading zeros** are allowed in `new1` and `new2`, and **all** the digits found in `num` must be used.
* For example, given `num = 2932`, you have... | Is it possible to make two integers a and b equal if they have different remainders dividing by x? If it is possible, which number should you select to minimize the number of operations? What if the elements are sorted? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.