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 |
|---|---|---|---|---|---|---|---|
Unpredicted Logic Python | substrings-of-size-three-with-distinct-characters | 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 binary string `binary`. A **subsequence** of `binary` is considered **good** if it is **not empty** and has **no leading zeros** (with the exception of `"0 "`).
Find the number of **unique good subsequences** of `binary`.
* For example, if `binary = "001 "`, then all the **good** subsequences are `[... | Try using a set to find out the number of distinct characters in a substring. |
Python 3 Simple and linear time. | substrings-of-size-three-with-distinct-characters | 0 | 1 | Iterative over string and increase the counter if all characters of length 3 string are different.\n\n```\n def countGoodSubstrings(self, s: str) -> int:\n ans=0\n for i in range(len(s)-2):\n if len(set(s[i:i+3]))==3:\n ans+=1\n return ans\n``` | 32 | A string is **good** if there are no repeated characters.
Given a string `s`, return _the number of **good substrings** of length **three** in_ `s`.
Note that if there are multiple occurrences of the same substring, every occurrence should be counted.
A **substring** is a contiguous sequence of characters... | Set each water cell to be 0. The height of each cell is limited by its closest water cell. Perform a multi-source BFS with all the water cells as sources. |
Python 3 Simple and linear time. | substrings-of-size-three-with-distinct-characters | 0 | 1 | Iterative over string and increase the counter if all characters of length 3 string are different.\n\n```\n def countGoodSubstrings(self, s: str) -> int:\n ans=0\n for i in range(len(s)-2):\n if len(set(s[i:i+3]))==3:\n ans+=1\n return ans\n``` | 32 | You are given a binary string `binary`. A **subsequence** of `binary` is considered **good** if it is **not empty** and has **no leading zeros** (with the exception of `"0 "`).
Find the number of **unique good subsequences** of `binary`.
* For example, if `binary = "001 "`, then all the **good** subsequences are `[... | Try using a set to find out the number of distinct characters in a substring. |
Python || Sliding Window || 95.01% Faster || 5 Lines | substrings-of-size-three-with-distinct-characters | 0 | 1 | ```\nclass Solution:\n def countGoodSubstrings(self, s: str) -> int:\n c,n=0,len(s)\n for i in range(n-2):\n t=set(s[i:i+3])\n if len(t)==3:\n c+=1\n return c\n``` | 4 | A string is **good** if there are no repeated characters.
Given a string `s`, return _the number of **good substrings** of length **three** in_ `s`.
Note that if there are multiple occurrences of the same substring, every occurrence should be counted.
A **substring** is a contiguous sequence of characters... | Set each water cell to be 0. The height of each cell is limited by its closest water cell. Perform a multi-source BFS with all the water cells as sources. |
Python || Sliding Window || 95.01% Faster || 5 Lines | substrings-of-size-three-with-distinct-characters | 0 | 1 | ```\nclass Solution:\n def countGoodSubstrings(self, s: str) -> int:\n c,n=0,len(s)\n for i in range(n-2):\n t=set(s[i:i+3])\n if len(t)==3:\n c+=1\n return c\n``` | 4 | You are given a binary string `binary`. A **subsequence** of `binary` is considered **good** if it is **not empty** and has **no leading zeros** (with the exception of `"0 "`).
Find the number of **unique good subsequences** of `binary`.
* For example, if `binary = "001 "`, then all the **good** subsequences are `[... | Try using a set to find out the number of distinct characters in a substring. |
Python3 Solution | O(N) Time Complexity using sliding window | substrings-of-size-three-with-distinct-characters | 0 | 1 | Approach:\nSlidding Window Technique:\n1. At each ith position slice the string from i to i + 3 and count the letters\n2. If there is a substring with all elements with count one, then the substring will be counted else the substring will be skipped\n3. Then finally return the number of substrings that are good in the ... | 1 | A string is **good** if there are no repeated characters.
Given a string `s`, return _the number of **good substrings** of length **three** in_ `s`.
Note that if there are multiple occurrences of the same substring, every occurrence should be counted.
A **substring** is a contiguous sequence of characters... | Set each water cell to be 0. The height of each cell is limited by its closest water cell. Perform a multi-source BFS with all the water cells as sources. |
Python3 Solution | O(N) Time Complexity using sliding window | substrings-of-size-three-with-distinct-characters | 0 | 1 | Approach:\nSlidding Window Technique:\n1. At each ith position slice the string from i to i + 3 and count the letters\n2. If there is a substring with all elements with count one, then the substring will be counted else the substring will be skipped\n3. Then finally return the number of substrings that are good in the ... | 1 | You are given a binary string `binary`. A **subsequence** of `binary` is considered **good** if it is **not empty** and has **no leading zeros** (with the exception of `"0 "`).
Find the number of **unique good subsequences** of `binary`.
* For example, if `binary = "001 "`, then all the **good** subsequences are `[... | Try using a set to find out the number of distinct characters in a substring. |
sort & greedy vs Frequency &2 pointers||71 ms Beats 100% | minimize-maximum-pair-sum-in-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere are 2 different approaches. \nOne is using sort & greedy; using python 1 line & C++ to implement.\nOther is using frequency & 2 pointers, C & C++ solutions which are really fast & beat 100%.\n# Approach\n<!-- Describe your approach t... | 11 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
Python [beats 99.59%]sort, split list in half, zip lower half and reversed of upper half, return max | minimize-maximum-pair-sum-in-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI believe many other posts has covered the general method of how to solve this. the ideas is to pair up the smallest number with the largest, 2nd smallest with 2nd largest and so on.\n\n# Approach\n<!-- Describe your approach to solving t... | 7 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
faster than 92.7 percent of python solutions in one line | minimize-maximum-pair-sum-in-array | 0 | 1 | # Intuition\n\nThe intuition behind this solution is to pair the largest and smallest elements in the sorted array. By doing so, we aim to maximize the sum of pairs.\n\n# Approach\nhis code uses the zip function to pair the smallest and largest numbers in the sorted list of nums. The [::-1] slice reverses the second ha... | 4 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
【Video】Give me u minutes - How we think about a solution | minimize-maximum-pair-sum-in-array | 1 | 1 | # Intuition\nSort input array.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/DzzjWJdhNhI\n\n\u25A0 Timeline of the video\n\n`0:04` Explain how we can solve Minimize Maximum Pair Sum in Array\n`2:21` Check with other pair combinations\n`4:12` Let\'s see another example!\n`5:37` Find Common process of the two examples\n... | 26 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
Python3 Solution | minimize-maximum-pair-sum-in-array | 0 | 1 | \n```\nclass Solution:\n def minPairSum(self, nums: List[int]) -> int:\n nums.sort()\n n=len(nums)\n ans=0\n for idx in range(n):\n ans=max(ans,nums[idx]+nums[n-idx-1])\n return ans \n``` | 3 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
✅☑[C++/Java/Python/JavaScript] || Easy Solution || EXPLAINED🔥 | minimize-maximum-pair-sum-in-array | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. **Function Purpose:**\n\n - `minPairSum` function finds the maximum sum obtained by pairing elements from a given vector of integers.\n1. **Sorting:**\n\n - The input vector nums is sorted in non-decreasing order using `so... | 2 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
Python (Simple Two Pointers) | minimize-maximum-pair-sum-in-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)$$ --... | 2 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
Harmonious Pairing: Unveiling the Summit of Minimized Pairs! 🌈✨ | minimize-maximum-pair-sum-in-array | 0 | 1 | # Intuition\nTo minimize the maximum pair sum, a possible approach is to pair the smallest numbers with the largest numbers. By doing so, the maximum sum would be minimized, as adding smaller numbers to larger ones typically results in smaller total sums.\n\n# Approach\n1. Sort the given array of numbers.\n2. Use two p... | 13 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
OneLine Python3 | minimize-maximum-pair-sum-in-array | 0 | 1 | \n\n# Approach\nThis is my Python solution with one line\n\n# Complexity\n- Time complexity:\nO(n*log(n))\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def minPairSum(self, nums: List[int]) -> int:\n return max([l + r for l, r in zip(sorted(nums)[:len(nums)//2], sorted(nums)[-1:len(nums)//2-1... | 1 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
😮1-Liner.py🤯 | minimize-maximum-pair-sum-in-array | 0 | 1 | # Intuition\n- something like sorting can hrlp\n\n# Approach\n- 2 -Pointer\n\n# Complexity\n- Time complexity:\n $$O(n)$$ \n\n- Space complexity:\n $$O(1)$$ \n\n# Code\n```py\nclass Solution:\n def minPairSum(self, nums: List[int]) -> int:\n return nums.sort() or max((nums[i]+nums[-(i+1)]) for i in range(l... | 6 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
Python one line | minimize-maximum-pair-sum-in-array | 0 | 1 | ```python []\nclass Solution:\n def minPairSum(self, nums: List[int]) -> int:\n return (x := sorted(nums)) and max(x[-i - 1] + x[i] for i in range(len(x) >> 1))\n\n``` | 7 | The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
* For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
Given an array `nums` of **even** length `n`, pai... | null |
🐍 {python} || 100% faster || well-explained || Simple approach | get-biggest-three-rhombus-sums-in-a-grid | 0 | 1 | Don\'t leave the idea of question and if you want more explaination or any example please feel free to ask !!\n## IDEA :\n\uD83D\uDC49 Firstly we will determine all the four vertices of rhombus.\n\uD83D\uDC49 Then we will pass that rhombus to calc function to calculate the score(perimeter).\n\uD83D\uDC49 "expand" flag... | 6 | You are given an `m x n` integer matrix `grid`.
A **rhombus sum** is the sum of the elements that form **the** **border** of a regular rhombus shape in `grid`. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus ... | Brute force and check if it is possible for a sorted array to start from each position. |
Python - prefixes sums and heap | get-biggest-three-rhombus-sums-in-a-grid | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe basically do lots of addition, so let\'s do some of it ahead of time\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCreate prefix sums of the diagonals. Keep track of best sums in a heap. For each point in the ... | 0 | You are given an `m x n` integer matrix `grid`.
A **rhombus sum** is the sum of the elements that form **the** **border** of a regular rhombus shape in `grid`. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus ... | Brute force and check if it is possible for a sorted array to start from each position. |
Prefix sum, Python 3, stupid code | get-biggest-three-rhombus-sums-in-a-grid | 0 | 1 | # Intuition\nBrute force.\n\n# Approach\nThis is a space-wasteful implementation, but coding-friendly. See the code below.\n\n# Code\n```\nimport heapq\nclass Solution:\n def getBiggestThree(self, grid: List[List[int]]) -> List[int]:\n ROW, COL = len(grid), len(grid[0])\n # prefix sums in two diagonal ... | 0 | You are given an `m x n` integer matrix `grid`.
A **rhombus sum** is the sum of the elements that form **the** **border** of a regular rhombus shape in `grid`. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus ... | Brute force and check if it is possible for a sorted array to start from each position. |
Python3, clean and commented code, Using Prefix Sum | get-biggest-three-rhombus-sums-in-a-grid | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMain idea to have a prefix sum in both diagonal directions (left and rigth) for full Grid. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, we will prepare prefix sum arrays for both diagonal directions(left... | 0 | You are given an `m x n` integer matrix `grid`.
A **rhombus sum** is the sum of the elements that form **the** **border** of a regular rhombus shape in `grid`. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus ... | Brute force and check if it is possible for a sorted array to start from each position. |
Bit Mask | minimum-xor-sum-of-two-arrays | 1 | 1 | We have a tight constraint here: n <= 14. Thus, we can just try all combinations.\n\nFor each position `i` in the first array, we\'ll try all elements in the second array that haven\'t been chosen before. We can use a bit mask to represent the chosen elements. \n\nTo avoid re-computing the same subproblem, we memoise t... | 109 | You are given two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Minimum XOR Sum of Two Arrays | minimum-xor-sum-of-two-arrays | 0 | 1 | You are given two integer arrays `nums1` and `nums2` of length n.\n\nThe XOR sum of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1]) (0-indexed)`.\n\nFor example, the XOR sum of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2... | 0 | You are given two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Polynomial Complexity C++ and Python Faster than 100% Minimum-cost Max-flow problem | minimum-xor-sum-of-two-arrays | 0 | 1 | Source connects to all points in num1 and sink connects to all points in num2 with flow is 1 and weight is 0. This problem is changed into a minimum cost max flow problem. \n\nPolynomial Complexity $O(VElog(V))$, which is $O(n^3log(n))$\n# Code\n# Python\n```\nINF = 10**10\n\nclass Edge:\n def __init__(self, from_ve... | 0 | You are given two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Python | Bit masking | DP | minimum-xor-sum-of-two-arrays | 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 two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Top Down Recursive | Commented and Explained | minimum-xor-sum-of-two-arrays | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> \nSince we only have 14 for the size of the nums, we can do an exhaustive comparison of 14 states to process these overall. This is similar in nature to a backtracking approach, but where instead we simply enumerate all possible states of ... | 0 | You are given two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Two fast and very short solutions in Python: DP vs Hungarian | minimum-xor-sum-of-two-arrays | 0 | 1 | # Solution 1: Dynamic Programming with Bitmask\n<!-- Describe your approach to solving the problem. -->\nThis problem can be solved as the classic traveling salesperson problem (TSP), which can be solved in $$O(N^2 2^N)$$ in dynamic programming, faster than the brute-force $$O(N!)$$ backtracking for large $$N$$.\n\nThi... | 0 | You are given two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Python (Simple DP + Bitmask) | minimum-xor-sum-of-two-arrays | 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 two integer arrays `nums1` and `nums2` of length `n`.
The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).
* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) +... | It's optimal to always remove one stone from the biggest 2 piles Note that the limits are small enough for simulation |
Easy and Clean Python Solution ✔ | check-if-word-equals-summation-of-two-words | 0 | 1 | Python3 Solution\n\n# Code\n```\nclass Solution:\n def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:\n x=[\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\',\'k\',\'l\',\'m\',\'n\',\'o\',\'p\',\'q\',\'r\',\'s\',\'t\',\'u\',\'v\',\'w\',\'x\',\'y\',\'z\']\n a=""\n ... | 1 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Frightening One Liner Python | check-if-word-equals-summation-of-two-words | 0 | 1 | \n\n# Approach\nI\'ve just done useless and hard to read one liner\n\nUpvote if you want to see worse one\n\n**Check it out!!**\n\n*(It\'s just for fun)* \'-\'\n# Code\n```\nclass Solution:\n def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:\n return sum([{\'a\':0, \'b\':1, \'c\'... | 3 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python simple and clean solution | check-if-word-equals-summation-of-two-words | 0 | 1 | **Python :**\n\n```\ndef isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:\n\tfirstNum, secondNum, targetNum = "", "", ""\n\n\tfor char in firstWord:\n\t\tfirstNum += str(ord(char) - ord(\'a\'))\n\n\tfor char in secondWord:\n\t\tsecondNum += str(ord(char) - ord(\'a\'))\n\n\tfor char in targetW... | 11 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Minus 49 | check-if-word-equals-summation-of-two-words | 0 | 1 | Replace letters with corresponding numbers, and then use a standard string-to-number conversion.\n\n> `\'a\' - \'0\' == 49`.\n\n**C++**\n```cpp\nbool isSumEqual(string first, string second, string target) {\n auto op = [](string &s) { for(auto &ch : s) ch -= 49; return s; };\n return stoi(op(first)) + stoi(op(sec... | 20 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python Simple Solution | check-if-word-equals-summation-of-two-words | 0 | 1 | \n```\ndef isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:\n numeric_total = lambda s: int(\'\'.join([str(ord(letter) - ord(\'a\')) for letter in s]))\n return numeric_total(firstWord) + numeric_total(secondWord) == numeric_total(targetWord)\n```\n\nAlternatively, the first line can be... | 22 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Check if word equals summation of two words | check-if-word-equals-summation-of-two-words | 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 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python3 | Time: O(n) | Space: O(1) | Clear. Explanation! | check-if-word-equals-summation-of-two-words | 0 | 1 | # **Solution**\nCreate a dictionary `mapping` that translates a letter into a number.\n\nThe `decoding` function converts a string to a number.\n\n## **How *lambda* works**\nWe use the `lambda` keyword to declare an anonymous function, which is why we refer to them as "lambda functions". An anonymous function refers to... | 10 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Easy Python Solution | check-if-word-equals-summation-of-two-words | 0 | 1 | ```\ndef isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:\n s1=self.helper(firstWord) \n s2=self.helper(secondWord)\n s3=self.helper(targetWord)\n return s1+s2==s3\n \n def helper(self, word):\n s=0\n for i in range(len(word)):\n ... | 2 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python solution Faster than >97% of submissions | check-if-word-equals-summation-of-two-words | 0 | 1 | \n\t\tdictm = {\'a\': \'0\', \'b\': \'1\', \'c\': \'2\', \'d\': \'3\', \'e\': \'4\', \'f\': \'5\', \'g\': \'6\', \'h\': \'7\', \'i\': \'8\', \'j\': \'9\'}\n\n s1 = \'\'.join([dictm[i] for i in firstWord])\n s2 = \'\'.join([dictm[i] for i in secondWord])\n s3 = \'\'.join([dictm[i] for i in targetWor... | 6 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python3 easy understanding (ord) solution beat 90.30% 25ms | check-if-word-equals-summation-of-two-words | 0 | 1 | \n\n# Complexity\n- Time complexity: O(n) n is the total length of the three input strings.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n) n is the total length of the three input strings.\n<!-- Add your space compl exity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n d... | 2 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python 3 Simple Solution in O(n) | check-if-word-equals-summation-of-two-words | 0 | 1 | **Approach:**\n\nConsider three strings ans, ans1 and ans2 which stores the corresponding values of characters of firstWord, secondWord and targetWord respectively. After converting the strings to integer, just check the difference of ans and ans1 is equal to ans2 or not.\n\n```\ndef isSumEqual(self, firstWord: str, se... | 5 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python || simple using ascii conversion | check-if-word-equals-summation-of-two-words | 0 | 1 | We will convert each letter to its ascii form and them subtract it from ascii value of "a". \nWe store all the values in a list and then concatinate it to form an integer.\nwe will then check if the required condition holds true.\n\nNote: This only works because we know we s[i]<="j" so **(ascii value of s[i] - acii val... | 4 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Check if Word Equals Summation of Two Words | check-if-word-equals-summation-of-two-words | 0 | 1 | ```\nclass Solution:\n def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:\n wordList = [firstWord, secondWord, targetWord]\n sumList = []\n for word in wordList:\n sumList.append(self.calcSum(word))\n return sumList[0]+sumList[1]==sumList[2]\n de... | 1 | The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).
The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an inte... | Build the result character by character. At each step, you choose a character from one of the two strings. If the next character of the first string is larger than that of the second string, or vice versa, it's optimal to use the larger one. If both are equal, think of a criteria that lets you decide which string to co... |
Python Elegant & Short | Maximize or Minimize | maximum-value-after-insertion | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n def maxValue(self, n: str, x: int) -> str:\n x = str(x)\n\n if n.startswith(\'-\'):\n return \'-\' + self.minimize(n[1:], x)\n\n return self.maximize(n, x)\n\n @staticmethod\n ... | 2 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
[Python 3] Find insertion position || 93ms | maximum-value-after-insertion | 0 | 1 | ```python3 []\nclass Solution:\n def maxValue(self, n: str, x: int) -> str:\n isNegative, x, L, i = n[0] == \'-\', str(x), len(n), 0\n \n while i < L:\n if not isNegative and x > n[i]: break\n elif isNegative and x < n[i]: break\n i += 1\n \n return n[:i] ... | 2 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
Python3 Easy understanding | maximum-value-after-insertion | 0 | 1 | \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, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def maxValue(self, n: str, x: int) -> str:\n if n[0] != "-":\n for i in range(len(n)... | 3 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
Python 3 || 12 lines, w/examples || T/M: 100% / 61% | maximum-value-after-insertion | 0 | 1 | ```\nclass Solution:\n def maxValue(self, n: str, x: int) -> str:\n # Example: n = "673", x = 6\n x = str(x) # n = "73", x = "6" \n \n if n[0].isdigit(): # ch\n for ch in n: ... | 4 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
75% TC and 98% SC easy python solution | maximum-value-after-insertion | 0 | 1 | ```\ndef maxValue(self, n: str, x: int) -> str:\n\tif(n[0] == "-"):\n\t\ti = 1\n\t\twhile(i < len(n) and int(n[i]) <= x):\n\t\t\ti += 1\n\telse:\n\t\ti = 0\n\t\twhile(i < len(n) and int(n[i]) >= x):\n\t\t\ti += 1\n\treturn n[:i]+str(x)+n[i:]\n``` | 5 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
Python || simple O(N) iteration | maximum-value-after-insertion | 0 | 1 | If the number is greater than 0 we will iterate from index 0 if the target is greater than the current element we will add it before that.\n\nElse if the number is less than 0 we will iterate from index 0 if target is less than the current we will replace add it there.\n\nWe will also keep a flag to check if we have pl... | 5 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
Python solution | maximum-value-after-insertion | 0 | 1 | ```\nclass Solution:\n def maxValue(self, n: str, x: int) -> str:\n\n for i in range(len(n)):\n if n[i] == "-":\n continue\n elif "-" in n:\n if int(n[i]) > x:\n return n[:i] + str(x) + n[i:]\n else:\n if int(n[i]... | 0 | You are given a very large integer `n`, represented as a string, and an integer digit `x`. The digits in `n` and the digit `x` are in the **inclusive** range `[1, 9]`, and `n` may represent a **negative** number.
You want to **maximize** `n`**'s numerical value** by inserting `x` anywhere in the decimal represen... | The naive solution is to check all possible subsequences. This works in O(2^n). Divide the array into two parts of nearly is equal size. Consider all subsets of one part and make a list of all possible subset sums and sort this list. Consider all subsets of the other part, and for each one, let its sum = x, do binary s... |
🔥 [Python3] Two Heap, beats 98% 🔥 | process-tasks-using-servers | 0 | 1 | ```\nclass Solution:\n def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:\n res, unavailable, time = [], [], 0\n available = [(weight, id) for id, weight in enumerate(servers)]\n heapify(available)\n\n for task in tasks:\n while unavailable and unavailabl... | 8 | You are given two **0-indexed** integer arrays `servers` and `tasks` of lengths `n` and `m` respectively. `servers[i]` is the **weight** of the `ith` server, and `tasks[j]` is the **time needed** to process the `jth` task **in seconds**.
Tasks are assigned to the servers using a **task ... | null |
🐍 {Python} || Heap || O(n+mlogn) || easy and well-explained | process-tasks-using-servers | 0 | 1 | ## Idea :\nUsing two heap one to store the available server for any task and other to store the servers busy in completing the task.\n\'\'\'\n\t\n\tclass Solution:\n def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:\n \n # sort the servers in order of weight, keeping index \n ... | 6 | You are given two **0-indexed** integer arrays `servers` and `tasks` of lengths `n` and `m` respectively. `servers[i]` is the **weight** of the `ith` server, and `tasks[j]` is the **time needed** to process the `jth` task **in seconds**.
Tasks are assigned to the servers using a **task ... | null |
Python Two heaps one queue | process-tasks-using-servers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimilar to 1834 Single-Threaded CPU, but instead of just one server, we have multiple servers. Therefore the code structure are similar instead we use while inside the outter while \n\n\n# Approach\n<!-- Describe your approach to solving ... | 0 | You are given two **0-indexed** integer arrays `servers` and `tasks` of lengths `n` and `m` respectively. `servers[i]` is the **weight** of the `ith` server, and `tasks[j]` is the **time needed** to process the `jth` task **in seconds**.
Tasks are assigned to the servers using a **task ... | null |
O(mlogn) time | O(n) space | solution explained | process-tasks-using-servers | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe are given m number of tasks with processing time and n number of servers with weights. We have to schedule these tasks and return which task will be assigned to which server.\n\nFor each task arriving at time t:\n- release all busy ser... | 0 | You are given two **0-indexed** integer arrays `servers` and `tasks` of lengths `n` and `m` respectively. `servers[i]` is the **weight** of the `ith` server, and `tasks[j]` is the **time needed** to process the `jth` task **in seconds**.
Tasks are assigned to the servers using a **task ... | null |
Using minheap, the solution can be solved in O(nlogm). | process-tasks-using-servers | 0 | 1 | # Intuition\nWe create two heaps for servers, available and unavailable.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlogm)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def assignTasks(self, servers: List[int], tasks: List[int]) -> Lis... | 0 | You are given two **0-indexed** integer arrays `servers` and `tasks` of lengths `n` and `m` respectively. `servers[i]` is the **weight** of the `ith` server, and `tasks[j]` is the **time needed** to process the `jth` task **in seconds**.
Tasks are assigned to the servers using a **task ... | null |
(Warning: No Explanation) [Python] DP + Time to Distance Equivalent Trick | minimum-skips-to-arrive-at-meeting-on-time | 0 | 1 | # Learning\nObserve the **skip & noskip** variable in **Dp** and how it is implemented with dinstance equivant of time to not get into precision error troubles. \nI have provided this solution just because i found my equivalent distance calculate little different than others and it\'s correct.\n**Your task is to check ... | 1 | You are given an integer `hoursBefore`, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through `n` roads. The road lengths are given as an integer array `dist` of length `n`, where `dist[i]` describes the length of the `ith` road in **kilometers**. In addition, you... | Get the LCA of p and q. The answer is the sum of distances between p-LCA and q-LCA |
Memoization in Python within 10 lines | minimum-skips-to-arrive-at-meeting-on-time | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nDynamic programming in the reversed order of the roads. A state in the search space can be defined by `(i, skip)` where `i` is the ith road and `skip` is the remaining number of skips can be applied.\nAt each state, there are three cases to consider: ... | 0 | You are given an integer `hoursBefore`, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through `n` roads. The road lengths are given as an integer array `dist` of length `n`, where `dist[i]` describes the length of the `ith` road in **kilometers**. In addition, you... | Get the LCA of p and q. The answer is the sum of distances between p-LCA and q-LCA |
[Python] Floating Point Sucks! use distance instead | minimum-skips-to-arrive-at-meeting-on-time | 0 | 1 | \n# Code\n```\nclass Solution:\n def minSkips(self, dist: List[int], sp: int, hb: int) -> int:\n n = len(dist)\n dp = [[0]*(n+1) for i in range(n)] # dp[i][k] - min distance upto i with k sips\n dp[0][0] = math.ceil(dist[0]/sp)*sp\n for k in range(1,n+1): dp[0][k] = dist[0]\n for i... | 0 | You are given an integer `hoursBefore`, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through `n` roads. The road lengths are given as an integer array `dist` of length `n`, where `dist[i]` describes the length of the `ith` road in **kilometers**. In addition, you... | Get the LCA of p and q. The answer is the sum of distances between p-LCA and q-LCA |
Python (Simple DP) | minimum-skips-to-arrive-at-meeting-on-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 | You are given an integer `hoursBefore`, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through `n` roads. The road lengths are given as an integer array `dist` of length `n`, where `dist[i]` describes the length of the `ith` road in **kilometers**. In addition, you... | Get the LCA of p and q. The answer is the sum of distances between p-LCA and q-LCA |
Python Bottom-up DP: O(n^2) time & space | 63% time, 78% space | minimum-skips-to-arrive-at-meeting-on-time | 0 | 1 | ```python []\nclass Solution:\n def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:\n n = len(dist)\n # Handle float accuracy error\n eps = 1e-9\n\n dp = [[sys.maxsize] * n for _ in range(n)]\n dp[0][0] = dist[0] / speed\n\n for i in range(1, n):\n ... | 0 | You are given an integer `hoursBefore`, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through `n` roads. The road lengths are given as an integer array `dist` of length `n`, where `dist[i]` describes the length of the `ith` road in **kilometers**. In addition, you... | Get the LCA of p and q. The answer is the sum of distances between p-LCA and q-LCA |
[Python3] top-down dp | minimum-skips-to-arrive-at-meeting-on-time | 0 | 1 | \n```\nclass Solution:\n def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int:\n if sum(dist)/speed > hoursBefore: return -1 # impossible \n \n @cache\n def fn(i, k): \n """Return min time (in distance) of traveling first i roads with k skips."""\n ... | 3 | You are given an integer `hoursBefore`, the number of hours you have to travel to your meeting. To arrive at your meeting, you have to travel through `n` roads. The road lengths are given as an integer array `dist` of length `n`, where `dist[i]` describes the length of the `ith` road in **kilometers**. In addition, you... | Get the LCA of p and q. The answer is the sum of distances between p-LCA and q-LCA |
Recursive, Iterative, Generic | egg-drop-with-2-eggs-and-n-floors | 1 | 1 | It may take you a while to come up with an efficient math-based solution. So, for an interview, I would start with a simple recursion - at least you will have something. It can help you see a pattern, and it will be easiser to develop an intuition for an improved solution. \n\nIf you follow-up with an improved solution... | 226 | You are given **two identical** eggs and you have access to a building with `n` floors labeled from `1` to `n`.
You know that there exists a floor `f` where `0 <= f <= n` such that any egg dropped at a floor **higher** than `f` will **break**, and any egg dropped **at or below** floor `f` will **not break**.
In each ... | Think about how the final string will look like. It will either start with a '0' and be like '010101010..' or with a '1' and be like '10101010..' Try both ways, and check for each way, the number of changes needed to reach it from the given string. The answer is the minimum of both ways. |
[Python3] rotate matrix | determine-whether-matrix-can-be-obtained-by-rotation | 0 | 1 | \n```\nclass Solution:\n def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n for _ in range(4): \n if mat == target: return True\n mat = [list(x) for x in zip(*mat[::-1])]\n return False \n``` | 93 | Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is possible to make_ `mat` _equal to_ `target` _by **rotating**_ `mat` _in **90-degree increments**, or_ `false` _otherwise._
**Example 1:**
**Input:** mat = \[\[0,1\],\[1,0\]\], target = \[\[1,0\],\[0,1\]\]
**Output:** true
**Explanation:** W... | Let's change the question if we know the maximum size of a bag what is the minimum number of bags you can make note that as the maximum size increases the minimum number of bags decreases so we can binary search the maximum size |
Python - Very detailed explanation of one liner solution (for python beginners) | determine-whether-matrix-can-be-obtained-by-rotation | 0 | 1 | Lets take an example - `mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]`\n\nFirst rotate 90 degree::\n```\n mat = [list(x) for x in zip(*mat[::-1])]\n```\nStart processing above statement from right to left.\n\n**Step 1->** Reverse ---> `mat[::-1] --> [ [1,1,1], [0,1,0], [0,0,0] ]`\n**Step 2->** U... | 56 | Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is possible to make_ `mat` _equal to_ `target` _by **rotating**_ `mat` _in **90-degree increments**, or_ `false` _otherwise._
**Example 1:**
**Input:** mat = \[\[0,1\],\[1,0\]\], target = \[\[1,0\],\[0,1\]\]
**Output:** true
**Explanation:** W... | Let's change the question if we know the maximum size of a bag what is the minimum number of bags you can make note that as the maximum size increases the minimum number of bags decreases so we can binary search the maximum size |
Comparing indices without rotating the matrix || Python | determine-whether-matrix-can-be-obtained-by-rotation | 0 | 1 | # Complexity\n- Time complexity: O(n2)\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```\nclass Solution:\n def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n n = len(mat)\n ... | 3 | Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is possible to make_ `mat` _equal to_ `target` _by **rotating**_ `mat` _in **90-degree increments**, or_ `false` _otherwise._
**Example 1:**
**Input:** mat = \[\[0,1\],\[1,0\]\], target = \[\[1,0\],\[0,1\]\]
**Output:** true
**Explanation:** W... | Let's change the question if we know the maximum size of a bag what is the minimum number of bags you can make note that as the maximum size increases the minimum number of bags decreases so we can binary search the maximum size |
Python rotate function | determine-whether-matrix-can-be-obtained-by-rotation | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nHow clockwise rotation works?\n\nGiven a matrix \n```\n[[1, 2, 3], \n [4, 5, 6], \n [7, 8, 9]]\n```\n, it will become \n\n```\n[[7, 4, 1], \n [8, 5, 2], \n [9, 6, 3]]\n```\n\nif rotated 90 degree clockwise.\n\nAs we can see that **the f... | 4 | Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is possible to make_ `mat` _equal to_ `target` _by **rotating**_ `mat` _in **90-degree increments**, or_ `false` _otherwise._
**Example 1:**
**Input:** mat = \[\[0,1\],\[1,0\]\], target = \[\[1,0\],\[0,1\]\]
**Output:** true
**Explanation:** W... | Let's change the question if we know the maximum size of a bag what is the minimum number of bags you can make note that as the maximum size increases the minimum number of bags decreases so we can binary search the maximum size |
Fast and easy | determine-whether-matrix-can-be-obtained-by-rotation | 0 | 1 | ```\nclass Solution:\n def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:\n \n def rotate(matrix):\n l,r=0,len(matrix)-1\n\n while l< r:\n for i in range(r-l):\n top,bottom=l,r\n temp=matrix[top][l+i]... | 0 | Given two `n x n` binary matrices `mat` and `target`, return `true` _if it is possible to make_ `mat` _equal to_ `target` _by **rotating**_ `mat` _in **90-degree increments**, or_ `false` _otherwise._
**Example 1:**
**Input:** mat = \[\[0,1\],\[1,0\]\], target = \[\[1,0\],\[0,1\]\]
**Output:** true
**Explanation:** W... | Let's change the question if we know the maximum size of a bag what is the minimum number of bags you can make note that as the maximum size increases the minimum number of bags decreases so we can binary search the maximum size |
Python3 Solution | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | \n```\nclass Solution:\n def reductionOperations(self, nums: List[int]) -> int:\n n=len(nums)\n nums.sort(reverse=True)\n ans=0\n for i in range(n-1):\n if nums[i]>nums[i+1]:\n ans+=i+1\n return ans \n``` | 12 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
[Python] One line Python Solution, better than O(n logn) Time | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse Counter(nums) to make nums into a hash table, then sort it by the number, which would also reduce the complexity.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe could make the nums into Counter and sort int... | 7 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
✅☑[C++/Java/Python/JavaScript] || Easiest Approach || EXPLAINED🔥 | reduction-operations-to-make-the-array-elements-equal | 1 | 1 | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. **Sorting:**\n\n - The code sorts the input vector nums in non-decreasing order using `sort()`.\n1. **Iterating Through the Sorted Vector:**\n\n - It iterates through the sorted vector and counts the number of unique eleme... | 8 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
Easy Sorting Solution with Dry Run! 🌟 | reduction-operations-to-make-the-array-elements-equal | 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. -->\nsort the array, count distinct values, and track operations to reduce each element\neg - 1 3 3 5 5\ncount shows the number of distinct elements between the lowest and ... | 3 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
Efficiently Equalizing Array Elements 😊: Strategy and Complexity Analysis | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | # Intuition\nGiven an array, we aim to find the number of operations needed to make all elements equal. To do so, we need to strategically reduce larger elements to the smallest element in the array.\n\n# Approach\n1. Count the frequencies of each number: Create a map or dictionary to store the frequencies of each elem... | 7 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
Easy Python Solution || Beats 99% in Runtime | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | # Beats 99% in runtime\n\n\n\n# Intuition\nIf all the elements in the array are to be made equal just by reduction of values, then all elements can only be equal if they are equal to the smallest element.\n... | 2 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
✅ [C++/Python] Clean & Simple Solutions w/ Explanation | Sorting | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | # Intuition\nThe goal is to make all elements in the array equal by following a set of operations. The operations involve finding the largest value, determining the next largest value smaller than the current largest, and reducing the corresponding element to the next largest value. The task is to calculate the total n... | 1 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
Easy Python Solution 🐍 | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | # Code\n```\nclass Solution:\n def reductionOperations(self, nums: List[int]) -> int:\n x=sorted(list(set(nums)))\n j=0\n freq={}\n for i in x:\n freq[i]=j\n j+=1\n ans=0\n for i in nums: ans+=freq[i]\n return ans\n```\n***Hope it helps...!!*** \... | 1 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
[Python] Dynamic Frequency with unique elements | Easy to understand | Approach & Code | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | #### Approach:\n\n1. Made a counter to keep in check the frequency of all elements\n2. Made an array of all unique elements & dropped the last element after dropping it as it\'s our target element\n3. Iterate through the array of `uniqueElements`, and keep adding the frequency of each element in `answer`.\n4. During th... | 1 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
This question doesn't test your... | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis question does\'nt test your programming skills.\nIt tests your problem solving skills. \nThat is why I liked this.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTake a pen paper and you will find that at e... | 1 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
EASY PYTHON SOLUTION | reduction-operations-to-make-the-array-elements-equal | 0 | 1 | \n# Approach\nCount the occurrence of each number and store it in dictionary. Sort the keys as the index of keys will increase its steps to minimum value will increase by one \n\n# Complexity\n- Time complexity: O(N^2)\n\n- Space complexity: O(N)\n\n# Code\n```\nclass Solution:\n def reductionOperations(self, nums: ... | 1 | Given an integer array `nums`, your goal is to make all elements in `nums` equal. To complete one operation, follow these steps:
1. Find the **largest** value in `nums`. Let its index be `i` (**0-indexed**) and its value be `largest`. If there are multiple elements with the largest value, pick the smallest `i`.
2. F... | Consider a trio with nodes u, v, and w. The degree of the trio is just degree(u) + degree(v) + degree(w) - 6. The -6 comes from subtracting the edges u-v, u-w, and v-w, which are counted twice each in the vertex degree calculation. To get the trios (u,v,w), you can iterate on u, then iterate on each w,v such that w and... |
Simple Python3 Solution | 99.9% | Sliding Window | Simple And Elegant With Explanation. | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to find the minimum number of flips needed to make a given binary string have alternating characters. The solution aims to achieve this by considering two possible alternate patterns: one starting with "0" and the other sta... | 1 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
✔ Python3 Solution | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | # Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n def minFlips(self, s):\n n = len(s)\n e, o = (n + 1) // 2, n // 2\n x = s[::2].count(\'1\') - s[1::2].count(\'1\')\n ans = min(e - x, o + x)\n if n&1 == 0: return ans\n ... | 4 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
[Python] Simple DP (beats 99.52%) | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | * **Type-1** operation doesn\'t count. It will only benefit when the length of s is **odd** (will allow one-time two adjacent positions with same value, e.g. moving the first \'1\' of \'110\' to the end, it will become valid \'101\'.\n* We track 4 DP variables to count the # of Type-2 operations:\n\t* **start_1**: fina... | 8 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
Python || easy sliding window based solutiion | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | * so first of all basic idea is that we add s at the back because for every possible s that is a n length piece\n* we try to see what no changes it req for that we make it 2n length now we make to dummty strings s1 and s2 as only two alternative are available .... one starts with one 10101 and another starts with 01... | 6 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
Python | Sliding Window | Clean code | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | # Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n- \n# Code\n```\nclass Solution:\n def minFlips(self, s: str) -> int:\n N = len(s)\n s *= 2\n \n valid1 = []\n valid2 = []\n for i in range(len(s)):\n valid1.append("0" if i % 2 == 0 else "1")\n ... | 0 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
1888. Minimum Number of Flips to Make the Binary String Alternating | minimum-number-of-flips-to-make-the-binary-string-alternating | 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 `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
O(n) time | O(1) space | solution explained | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf the string length is: \n- even, the rotate (type-I) operation will not reduce the number of flips (type-II operation)\n- odd, the rotate operation may reduce the flips. So, we need to perform both operations\n\nFirst, find the flips. I... | 0 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
Python Medium Sliding Window | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | ```\nclass Solution:\n def minFlips(self, s: str) -> int:\n s += s\n arr = [int(char) for char in s]\n\n\n def calc(arr):\n N = len(arr) // 2\n\n prefSum = [0]\n ans = float("inf")\n\n for i in range(len(arr)):\n if i % 2 == 0:\n ... | 0 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
linear time | minimum-number-of-flips-to-make-the-binary-string-alternating | 0 | 1 | \n# Approach\nsliding window\n# Complexity\n- Time complexity:\nO(N)\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution:\n def minFlips(self, s: str) -> int:\n n = len(s)\n s = s+s\n alt1,alt2 = "",""\n\n for i in range(len(s)):\n alt1 += "0" if i % 2 else "1"\n ... | 0 | You are given a binary string `s`. You are allowed to perform two types of operations on the string in any sequence:
* **Type-1: Remove** the character at the start of the string `s` and **append** it to the end of the string.
* **Type-2: Pick** any character in `s` and **flip** its value, i.e., if its value is `'... | Iterate through each point, and keep track of the current point with the smallest Manhattan distance from your current location. |
[Python3] prefix sum & binary search | minimum-space-wasted-from-packaging | 0 | 1 | \n```\nclass Solution:\n def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:\n packages.sort()\n prefix = [0]\n for x in packages: prefix.append(prefix[-1] + x)\n \n ans = inf \n for box in boxes: \n box.sort()\n if packages[-1... | 5 | You have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
Python | Binary search | minimum-space-wasted-from-packaging | 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 have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
Solution | minimum-space-wasted-from-packaging | 0 | 1 | \n# Code\n```\nfrom typing import List\nfrom math import inf\nfrom itertools import accumulate\nfrom bisect import bisect_right\n\nclass Solution:\n def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:\n MOD = 10**9 + 7\n packages.sort()\n prefix_sum = [0] + list(accumul... | 0 | You have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
Line Sweep w/Early Stopping | Commented and Explained | minimum-space-wasted-from-packaging | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFrom the problem description and examples, we can notice that \n- packages and boxes in their lists are not in order \n- packages can contain duplicates \n- any box list without max package in it is not feasible and can be ignored\n\nFrom... | 0 | You have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
python 3 | line sweep | 100% faster | minimum-space-wasted-from-packaging | 0 | 1 | # Complexity\n- Time complexity:\n$O(n + h + \\sum_{i=0}^{m-1}b_i\\log (b_i))$\n\n- Space complexity:\n$O(h + \\max(b_0, b_1,..., b_{m-1}))$\n\n# Code\n```\nclass Solution:\n def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:\n n, s, high = len(packages), sum(packages), max(packages... | 0 | You have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
Python (Simple Binary Search) | minimum-space-wasted-from-packaging | 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 have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
Binary search (right) + prefix sum | minimum-space-wasted-from-packaging | 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 have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
Easiest Solution | minimum-space-wasted-from-packaging | 0 | 1 | \n```\nclass Solution:\n def minWastedSpace(self, P: List[int], B: List[List[int]]) -> int:\n maxp, maxb, n, ans = max(P), max(max(x) for x in B), len(P), math.inf\n if maxb < maxp:\n return -1\n P.sort()\n presum = [0] + list(itertools.accumulate(P))\n for box in B:\n ... | 0 | You have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
[Python 3] Binary Search + Prefix Sum (1576 ms) | minimum-space-wasted-from-packaging | 0 | 1 | ```\nclass Solution:\n def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:\n # prefix sum to save time\n acc = [0] + [*accumulate(packages)]\n packages.sort()\n\n ans = float(\'inf\')\n for box in boxes:\n tmp = 0\n # deal with smalle... | 2 | You have `n` packages that you are trying to place in boxes, **one package in each box**. There are `m` suppliers that each produce boxes of **different sizes** (with infinite supply). A package can be placed in a box if the size of the package is **less than or equal to** the size of the box.
The package sizes are gi... | Let's note that the maximum power of 3 you'll use in your soln is 3^16 The number can not be represented as a sum of powers of 3 if it's ternary presentation has a 2 in it |
7 lines of code beats 99.52% | check-if-all-the-integers-in-a-range-are-covered | 0 | 1 | # Code\n```\nclass Solution:\n def isCovered(self, matrix: List[List[int]], l: int, r: int) -> bool:\n mat = []\n \n for start, end in matrix:\n mat.extend(list(range(start, end + 1)))\n \n for i in range(l, r + 1):\n if i not in mat:\n return F... | 1 | You are given a 2D integer array `ranges` and two integers `left` and `right`. Each `ranges[i] = [starti, endi]` represents an **inclusive** interval between `starti` and `endi`.
Return `true` _if each integer in the inclusive range_ `[left, right]` _is covered by **at least one** interval in_ `ranges`. Return `false`... | Think about dynamic programming Define an array dp[nums.length][2], where dp[i][0] is the max subarray sum including nums[i] and without squaring any element. dp[i][1] is the max subarray sum including nums[i] and having only one element squared. |
Python3 | Solved Using Sorting And Prefix Sum O(nlogn) Runtime and O(n) Space! | check-if-all-the-integers-in-a-range-are-covered | 0 | 1 | ```\nclass Solution:\n #Let n = len(ranges)!\n #Time-Complexity: O(nlogn + n * 1 + n * 1) - > O(nlogn)\n #Space-Complexity: O(n), if there are n intervals side by side non overlapping!\n def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n #Approach: I will iterate through e... | 0 | You are given a 2D integer array `ranges` and two integers `left` and `right`. Each `ranges[i] = [starti, endi]` represents an **inclusive** interval between `starti` and `endi`.
Return `true` _if each integer in the inclusive range_ `[left, right]` _is covered by **at least one** interval in_ `ranges`. Return `false`... | Think about dynamic programming Define an array dp[nums.length][2], where dp[i][0] is the max subarray sum including nums[i] and without squaring any element. dp[i][1] is the max subarray sum including nums[i] and having only one element squared. |
Python simple solution | check-if-all-the-integers-in-a-range-are-covered | 0 | 1 | ```\nclass Solution:\n def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n ans = 0\n for i in range(left, right+1):\n for x,y in ranges:\n if i in [x for x in range(x,y+1)]:\n ans += 1\n break\n return ans... | 2 | You are given a 2D integer array `ranges` and two integers `left` and `right`. Each `ranges[i] = [starti, endi]` represents an **inclusive** interval between `starti` and `endi`.
Return `true` _if each integer in the inclusive range_ `[left, right]` _is covered by **at least one** interval in_ `ranges`. Return `false`... | Think about dynamic programming Define an array dp[nums.length][2], where dp[i][0] is the max subarray sum including nums[i] and without squaring any element. dp[i][1] is the max subarray sum including nums[i] and having only one element squared. |
✅✅✅ Python3 - Faster than 99.63% ✅✅✅ | check-if-all-the-integers-in-a-range-are-covered | 0 | 1 | \n\n```\nclass Solution:\n def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n cross_off_list = [i for i in range(left, right+1)]\n for rnge in ranges:\n f... | 1 | You are given a 2D integer array `ranges` and two integers `left` and `right`. Each `ranges[i] = [starti, endi]` represents an **inclusive** interval between `starti` and `endi`.
Return `true` _if each integer in the inclusive range_ `[left, right]` _is covered by **at least one** interval in_ `ranges`. Return `false`... | Think about dynamic programming Define an array dp[nums.length][2], where dp[i][0] is the max subarray sum including nums[i] and without squaring any element. dp[i][1] is the max subarray sum including nums[i] and having only one element squared. |
99.99% | python | clean | | check-if-all-the-integers-in-a-range-are-covered | 0 | 1 | ```\nclass Solution:\n def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n \n \n t=[0]*(60)\n \n for i in ranges:\n \n t[i[0]]+=1\n t[i[1]+1]-=1\n \n for i in range(1,len(t)):\n t[i] += t[i-1]\... | 6 | You are given a 2D integer array `ranges` and two integers `left` and `right`. Each `ranges[i] = [starti, endi]` represents an **inclusive** interval between `starti` and `endi`.
Return `true` _if each integer in the inclusive range_ `[left, right]` _is covered by **at least one** interval in_ `ranges`. Return `false`... | Think about dynamic programming Define an array dp[nums.length][2], where dp[i][0] is the max subarray sum including nums[i] and without squaring any element. dp[i][1] is the max subarray sum including nums[i] and having only one element squared. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.