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
Using O(sqrt N) for division and N for nums
four-divisors
0
1
upvote if you like\n# Code\n```\nclass Solution:\n def sumFourDivisors(self, nums: List[int]) -> int:\n def divisors(n):\n result = set()\n for i in range(1, int(n**0.5)+1):\n if n % i == 0:\n result.add(i)\n result.add(n//i)\n ...
0
Given an integer array `nums`, return _the sum of divisors of the integers in that array that have exactly four divisors_. If there is no such integer in the array, return `0`. **Example 1:** **Input:** nums = \[21,4,7\] **Output:** 32 **Explanation:** 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2...
null
Using O(sqrt N) for division and N for nums
four-divisors
0
1
upvote if you like\n# Code\n```\nclass Solution:\n def sumFourDivisors(self, nums: List[int]) -> int:\n def divisors(n):\n result = set()\n for i in range(1, int(n**0.5)+1):\n if n % i == 0:\n result.add(i)\n result.add(n//i)\n ...
0
Given a `m x n` binary matrix `mat`. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing `1` to `0` and `0` to `1`). A pair of cells are called neighbors if they share one edge. Return the _minimum number of steps_ required to convert `mat` to a zero matrix...
Find the divisors of each element in the array. You only need to loop to the square root of a number to find its divisors.
Python3 prime Beats 99.70%
four-divisors
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given an integer array `nums`, return _the sum of divisors of the integers in that array that have exactly four divisors_. If there is no such integer in the array, return `0`. **Example 1:** **Input:** nums = \[21,4,7\] **Output:** 32 **Explanation:** 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2...
null
Python3 prime Beats 99.70%
four-divisors
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given a `m x n` binary matrix `mat`. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing `1` to `0` and `0` to `1`). A pair of cells are called neighbors if they share one edge. Return the _minimum number of steps_ required to convert `mat` to a zero matrix...
Find the divisors of each element in the array. You only need to loop to the square root of a number to find its divisors.
by PRODONiK (C++, Java, C#, Python, Ruby)
four-divisors
1
1
# Intuition\n The problem requires finding the sum of four-divisor integers in an array. A four-divisor integer is an integer with exactly four divisors. Our intuition is to iterate through the array, count divisors for each number, and calculate the sum of divisors if the count is exactly 4.\n\n# Approach\n Our approa...
0
Given an integer array `nums`, return _the sum of divisors of the integers in that array that have exactly four divisors_. If there is no such integer in the array, return `0`. **Example 1:** **Input:** nums = \[21,4,7\] **Output:** 32 **Explanation:** 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2...
null
by PRODONiK (C++, Java, C#, Python, Ruby)
four-divisors
1
1
# Intuition\n The problem requires finding the sum of four-divisor integers in an array. A four-divisor integer is an integer with exactly four divisors. Our intuition is to iterate through the array, count divisors for each number, and calculate the sum of divisors if the count is exactly 4.\n\n# Approach\n Our approa...
0
Given a `m x n` binary matrix `mat`. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing `1` to `0` and `0` to `1`). A pair of cells are called neighbors if they share one edge. Return the _minimum number of steps_ required to convert `mat` to a zero matrix...
Find the divisors of each element in the array. You only need to loop to the square root of a number to find its divisors.
Python3 Clean Solution
four-divisors
0
1
\n\n# Code\n```\nclass Solution:\n def sumFourDivisors(self, nums: List[int]) -> int:\n \n \n @cache\n def f(x):\n c=sm=0\n for i in range(1,isqrt(x)+1):\n if x%i!=0:\n continue\n if i*i==x:\n c+=1\n...
0
Given an integer array `nums`, return _the sum of divisors of the integers in that array that have exactly four divisors_. If there is no such integer in the array, return `0`. **Example 1:** **Input:** nums = \[21,4,7\] **Output:** 32 **Explanation:** 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2...
null
Python3 Clean Solution
four-divisors
0
1
\n\n# Code\n```\nclass Solution:\n def sumFourDivisors(self, nums: List[int]) -> int:\n \n \n @cache\n def f(x):\n c=sm=0\n for i in range(1,isqrt(x)+1):\n if x%i!=0:\n continue\n if i*i==x:\n c+=1\n...
0
Given a `m x n` binary matrix `mat`. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing `1` to `0` and `0` to `1`). A pair of cells are called neighbors if they share one edge. Return the _minimum number of steps_ required to convert `mat` to a zero matrix...
Find the divisors of each element in the array. You only need to loop to the square root of a number to find its divisors.
[100% Faster] I don't think anyone has done this yet 😂
check-if-there-is-a-valid-path-in-a-grid
0
1
# Intuition\nJust do as the question says.\n\n# Complexity\n- Time complexity:\n$$O(N)$$ *since each cell is visited at most once*\n\n- Space complexity:\n$$O(N)$$ *for queue*\n\n# Code\n```\nclass Solution:\n def hasValidPath(self, grid: List[List[int]]) -> bool:\n m,n = len(grid),len(grid[0])\n paths...
1
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
[100% Faster] I don't think anyone has done this yet 😂
check-if-there-is-a-valid-path-in-a-grid
0
1
# Intuition\nJust do as the question says.\n\n# Complexity\n- Time complexity:\n$$O(N)$$ *since each cell is visited at most once*\n\n- Space complexity:\n$$O(N)$$ *for queue*\n\n# Code\n```\nclass Solution:\n def hasValidPath(self, grid: List[List[int]]) -> bool:\n m,n = len(grid),len(grid[0])\n paths...
1
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Python Solution with dfs and Untion Find
check-if-there-is-a-valid-path-in-a-grid
0
1
\n**Title: Valid Path in a Grid - Union-Find Solution\n**\nDescription:\n\nThe problem asks us to determine whether there exists a valid path in a given grid, where each cell represents a street with specific connections. We are not allowed to change any streets, and the valid path should start from the upper-left cell...
2
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
Python Solution with dfs and Untion Find
check-if-there-is-a-valid-path-in-a-grid
0
1
\n**Title: Valid Path in a Grid - Union-Find Solution\n**\nDescription:\n\nThe problem asks us to determine whether there exists a valid path in a given grid, where each cell represents a street with specific connections. We are not allowed to change any streets, and the valid path should start from the upper-left cell...
2
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
[Python] SUPER EASY Idea: just walk the maze based on the rule
check-if-there-is-a-valid-path-in-a-grid
0
1
0 means going right, 1 means upwards, 2 means downwards, 3 means going left. \n\nAt cell with type 1, you can either enter it with right direction and leave it keeping the same direction, or enter it with left and leave for the left cell. So the rule for cell \'1\' is `{0: 0, 3: 3}`, and we can do the same transition r...
9
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
[Python] SUPER EASY Idea: just walk the maze based on the rule
check-if-there-is-a-valid-path-in-a-grid
0
1
0 means going right, 1 means upwards, 2 means downwards, 3 means going left. \n\nAt cell with type 1, you can either enter it with right direction and leave it keeping the same direction, or enter it with left and leave for the left cell. So the rule for cell \'1\' is `{0: 0, 3: 3}`, and we can do the same transition r...
9
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Ugly DFS Solution
check-if-there-is-a-valid-path-in-a-grid
0
1
# Intuition\nUgly DFS Solution\n\n# Approach\nUse previous coordinates and current coordinate to determine if the current road is reachable from the previous road.\n\nSome example scenarios to consider: \n- Coordinate (0, 0) cannot be of street type 5\n- If the current cell is street type 3, the previous cell must have...
0
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
Ugly DFS Solution
check-if-there-is-a-valid-path-in-a-grid
0
1
# Intuition\nUgly DFS Solution\n\n# Approach\nUse previous coordinates and current coordinate to determine if the current road is reachable from the previous road.\n\nSome example scenarios to consider: \n- Coordinate (0, 0) cannot be of street type 5\n- If the current cell is street type 3, the previous cell must have...
0
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Simple DFS solution
check-if-there-is-a-valid-path-in-a-grid
0
1
```\nclass Solution:\n def hasValidPath(self, grid: List[List[int]]) -> bool:\n step = {(0, 1):{1, 3, 5}, (0, -1):{1, 4, 6}, (1, 0):{2, 5, 6}, (-1, 0):{2, 3, 4}}\n direct = {1:[(0,-1), (0,1)], 2:[(-1,0), (1,0)], 3:[(0,-1), (1,0)], 4:[(0,1), (1,0)], 5:[(0,-1), (-1,0)], 6:[(0,1), (-1,0)]}\n n, m =...
0
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
Simple DFS solution
check-if-there-is-a-valid-path-in-a-grid
0
1
```\nclass Solution:\n def hasValidPath(self, grid: List[List[int]]) -> bool:\n step = {(0, 1):{1, 3, 5}, (0, -1):{1, 4, 6}, (1, 0):{2, 5, 6}, (-1, 0):{2, 3, 4}}\n direct = {1:[(0,-1), (0,1)], 2:[(-1,0), (1,0)], 3:[(0,-1), (1,0)], 4:[(0,1), (1,0)], 5:[(0,-1), (-1,0)], 6:[(0,1), (-1,0)]}\n n, m =...
0
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Easy Python DFS
check-if-there-is-a-valid-path-in-a-grid
0
1
\n# Code\n```\nclass Solution:\n def dfs(self,grid,i,j):\n z=grid[i][j]\n grid[i][j]=-1\n \n \n \n if z==1:\n q=i\n w=j+1\n if q>=0 and w>=0 and q<len(grid) and w<len(grid[0]) and grid[q][w] in [1,3,5]:\n self.dfs(grid,q,w)\n ...
0
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
Easy Python DFS
check-if-there-is-a-valid-path-in-a-grid
0
1
\n# Code\n```\nclass Solution:\n def dfs(self,grid,i,j):\n z=grid[i][j]\n grid[i][j]=-1\n \n \n \n if z==1:\n q=i\n w=j+1\n if q>=0 and w>=0 and q<len(grid) and w<len(grid[0]) and grid[q][w] in [1,3,5]:\n self.dfs(grid,q,w)\n ...
0
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Python solution beated 99%
check-if-there-is-a-valid-path-in-a-grid
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- O(N*M)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here...
0
You are given an `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
Python solution beated 99%
check-if-there-is-a-valid-path-in-a-grid
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- O(N*M)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here...
0
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Python | DFS Solution
check-if-there-is-a-valid-path-in-a-grid
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 `m x n` `grid`. Each cell of `grid` represents a street. The street of `grid[i][j]` can be: * `1` which means a street connecting the left cell and the right cell. * `2` which means a street connecting the upper cell and the lower cell. * `3` which means a street connecting the left cell and the...
Use hashset to store all elements. Loop again to count all valid elements.
Python | DFS Solution
check-if-there-is-a-valid-path-in-a-grid
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given a `date` string in the form `Day Month Year`, where: * `Day` is in the set `{ "1st ", "2nd ", "3rd ", "4th ", ..., "30th ", "31st "}`. * `Month` is in the set `{ "Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec "}`. * `Year` is in the range `[1900, 2100]`. Conve...
Start DFS from the node (0, 0) and follow the path till you stop. When you reach a cell and cannot move anymore check that this cell is (m - 1, n - 1) or not.
Dynamic programming solution
longest-happy-prefix
0
1
# Intuition\nThe idea is to compute prefix function for each substring in a given \nstring, storing the result in a dp array of size = s.length()\n\n# Approach\n1. The base case is a substring of length 1, s[0], and its prefix function\n= 0, i. e. an empty string. dp[0] = 0;\n2. Starting from i = 1 to (s.length() - 1),...
1
You are given the array `nums` consisting of `n` positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of `n * (n + 1) / 2` numbers. _Return the sum of the numbers from index_ `left` _to index_ `right` (**indexed ...
Use Longest Prefix Suffix (KMP-table) or String Hashing.
Dynamic programming solution
longest-happy-prefix
0
1
# Intuition\nThe idea is to compute prefix function for each substring in a given \nstring, storing the result in a dp array of size = s.length()\n\n# Approach\n1. The base case is a substring of length 1, s[0], and its prefix function\n= 0, i. e. an empty string. dp[0] = 0;\n2. Starting from i = 1 to (s.length() - 1),...
1
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: Note that the integers in the lists may be returned in any order.
For each integer in nums1, check if it exists in nums2. Do the same for each integer in nums2.
easy-solution👁️ | well-explained✅ | O(M+N) | Microsoft🔥
longest-happy-prefix
0
1
# Please upvote if it is helpful ^_^\n***6Companies30days #ReviseWithArsh Challenge 2023\nDay2\nQ12. Longest Happy Prefix***\n\n**Approach:** *KMP Approach, DP*\n\n![12.longest-happy-prefix.jpg](https://assets.leetcode.com/users/images/368329ab-8eb6-4ddb-8c57-fc07336b759d_1672917737.342274.jpeg)\n\n**Complexity:** *O(M...
4
You are given the array `nums` consisting of `n` positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of `n * (n + 1) / 2` numbers. _Return the sum of the numbers from index_ `left` _to index_ `right` (**indexed ...
Use Longest Prefix Suffix (KMP-table) or String Hashing.
easy-solution👁️ | well-explained✅ | O(M+N) | Microsoft🔥
longest-happy-prefix
0
1
# Please upvote if it is helpful ^_^\n***6Companies30days #ReviseWithArsh Challenge 2023\nDay2\nQ12. Longest Happy Prefix***\n\n**Approach:** *KMP Approach, DP*\n\n![12.longest-happy-prefix.jpg](https://assets.leetcode.com/users/images/368329ab-8eb6-4ddb-8c57-fc07336b759d_1672917737.342274.jpeg)\n\n**Complexity:** *O(M...
4
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: Note that the integers in the lists may be returned in any order.
For each integer in nums1, check if it exists in nums2. Do the same for each integer in nums2.
longest-happy-prefix
longest-happy-prefix
0
1
# Code\n```\nclass Solution:\n def longestPrefix(self, s: str) -> str:\n b = len(s)\n t = 0\n p = ""\n for i in range(len(s)-1):\n if s[:i+1]==s[b-1-i:]:\n if i+1>t:\n p = s[:i+1]\n t= i+1 \n return p\n\n\...
1
You are given the array `nums` consisting of `n` positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of `n * (n + 1) / 2` numbers. _Return the sum of the numbers from index_ `left` _to index_ `right` (**indexed ...
Use Longest Prefix Suffix (KMP-table) or String Hashing.
longest-happy-prefix
longest-happy-prefix
0
1
# Code\n```\nclass Solution:\n def longestPrefix(self, s: str) -> str:\n b = len(s)\n t = 0\n p = ""\n for i in range(len(s)-1):\n if s[:i+1]==s[b-1-i:]:\n if i+1>t:\n p = s[:i+1]\n t= i+1 \n return p\n\n\...
1
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: Note that the integers in the lists may be returned in any order.
For each integer in nums1, check if it exists in nums2. Do the same for each integer in nums2.
KMP approach
longest-happy-prefix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nKMP approach\n\n# Approach\nCompute LPS for the string and take the last index to find the starting point of the prefix and print till end since it is always a suffix.\n\n# Complexity\n- Time complexity: O(M)\n\n- Space complexity: O(M)\n...
0
You are given the array `nums` consisting of `n` positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of `n * (n + 1) / 2` numbers. _Return the sum of the numbers from index_ `left` _to index_ `right` (**indexed ...
Use Longest Prefix Suffix (KMP-table) or String Hashing.
KMP approach
longest-happy-prefix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nKMP approach\n\n# Approach\nCompute LPS for the string and take the last index to find the starting point of the prefix and print till end since it is always a suffix.\n\n# Complexity\n- Time complexity: O(M)\n\n- Space complexity: O(M)\n...
0
Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where: Note that the integers in the lists may be returned in any order.
For each integer in nums1, check if it exists in nums2. Do the same for each integer in nums2.
Python Easy to understand code Beats 100% Memory , and Runtime .
find-lucky-integer-in-an-array
0
1
# Intuition\nThe code appears to be trying to find a "lucky" integer in a given list, where a "lucky" integer is defined as an integer that appears exactly as many times as its value in the list.\n\n# Approach\nThe code uses a brute-force approach to find the lucky integer. It iterates through the input list `arr`, cou...
3
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
Python Easy to understand code Beats 100% Memory , and Runtime .
find-lucky-integer-in-an-array
0
1
# Intuition\nThe code appears to be trying to find a "lucky" integer in a given list, where a "lucky" integer is defined as an integer that appears exactly as many times as its value in the list.\n\n# Approach\nThe code uses a brute-force approach to find the lucky integer. It iterates through the input list `arr`, cou...
3
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
python3 code
find-lucky-integer-in-an-array
0
1
\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def findLucky(self, arr: List[int]) -> int:\n l=[]\n for i in arr:\n x = arr.count(i)\n if(x==i):\n l.append(i)\n else:\n l.append(-...
1
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
python3 code
find-lucky-integer-in-an-array
0
1
\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution:\n def findLucky(self, arr: List[int]) -> int:\n l=[]\n for i in arr:\n x = arr.count(i)\n if(x==i):\n l.append(i)\n else:\n l.append(-...
1
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
Python code to return the largest lucky integer in the array. (TC & SC: O(n))
find-lucky-integer-in-an-array
0
1
\n\n# Approach\nExplanation of the approach used in the code:\n\nInitialize two variables, x and d. x will be used to store the largest lucky number found, and d is a dictionary that will be used to count the occurrences of each element in the input array arr.\n\nIterate through the elements of the arr list using a for...
1
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
Python code to return the largest lucky integer in the array. (TC & SC: O(n))
find-lucky-integer-in-an-array
0
1
\n\n# Approach\nExplanation of the approach used in the code:\n\nInitialize two variables, x and d. x will be used to store the largest lucky number found, and d is a dictionary that will be used to count the occurrences of each element in the input array arr.\n\nIterate through the elements of the arr list using a for...
1
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
Find lucky integer in an array
find-lucky-integer-in-an-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)$$ --...
1
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
Find lucky integer in an array
find-lucky-integer-in-an-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)$$ --...
1
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
Python | Easy Solution✅
find-lucky-integer-in-an-array
0
1
```\ndef findLucky(self, arr: List[int]) -> int:\n large = -1\n unique = set(arr)\n for item in unique:\n count = arr.count(item)\n if item == count:\n large = max(large, item) \n return large\n```
4
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
Python | Easy Solution✅
find-lucky-integer-in-an-array
0
1
```\ndef findLucky(self, arr: List[int]) -> int:\n large = -1\n unique = set(arr)\n for item in unique:\n count = arr.count(item)\n if item == count:\n large = max(large, item) \n return large\n```
4
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
simple solution using dictionary in python
find-lucky-integer-in-an-array
0
1
\n```\nclass Solution:\n def findLucky(self, arr: List[int]) -> int:\n dic={}\n l=[]\n for i in arr:\n if i not in dic:\n dic[i]=1\n else:\n dic[i]+=1\n for i in dic:\n if i==dic[i]:\n l.append(i)\n if(le...
2
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
simple solution using dictionary in python
find-lucky-integer-in-an-array
0
1
\n```\nclass Solution:\n def findLucky(self, arr: List[int]) -> int:\n dic={}\n l=[]\n for i in arr:\n if i not in dic:\n dic[i]=1\n else:\n dic[i]+=1\n for i in dic:\n if i==dic[i]:\n l.append(i)\n if(le...
2
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
Easy Python Solution
find-lucky-integer-in-an-array
1
1
```\nclass Solution:\n def findLucky(self, arr: List[int]) -> int:\n a=Counter(arr)\n l=[]\n for i in arr:\n if i==a[i]:\n l.append(i)\n if l:\n return max(l)\n return -1\n```
1
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
Easy Python Solution
find-lucky-integer-in-an-array
1
1
```\nclass Solution:\n def findLucky(self, arr: List[int]) -> int:\n a=Counter(arr)\n l=[]\n for i in arr:\n if i==a[i]:\n l.append(i)\n if l:\n return max(l)\n return -1\n```
1
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
JAVA|| BEATS 98.94% || 3 LINE CODE || HASHMAP
find-lucky-integer-in-an-array
1
1
**PLEASE UPVOTE THIS IF YOU LIKE IT**\n# Approach\nFinding out the frequency of each element using HashMap,then if the frequency matches the value then print that. but, since we have to return the largest so we will traverse from the last end;\n\n\n# Code\n```\nclass Solution {\n public int findLucky(int[] arr) {\n ...
5
Given an array of integers `arr`, a **lucky integer** is an integer that has a frequency in the array equal to its value. Return _the largest **lucky integer** in the array_. If there is no **lucky integer** return `-1`. **Example 1:** **Input:** arr = \[2,2,3,4\] **Output:** 2 **Explanation:** The only lucky number...
null
JAVA|| BEATS 98.94% || 3 LINE CODE || HASHMAP
find-lucky-integer-in-an-array
1
1
**PLEASE UPVOTE THIS IF YOU LIKE IT**\n# Approach\nFinding out the frequency of each element using HashMap,then if the frequency matches the value then print that. but, since we have to return the largest so we will traverse from the last end;\n\n\n# Code\n```\nclass Solution {\n public int findLucky(int[] arr) {\n ...
5
Alice and Bob take turns playing a game, with Alice starting first. Initially, there are `n` stones in a pile. On each player's turn, that player makes a _move_ consisting of removing **any** non-zero **square number** of stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positi...
Count the frequency of each integer in the array. Get all lucky numbers and return the largest of them.
Easiest Code Ever - Python - Top 97% Speed - O( n log n ) Combinatorial
count-number-of-teams
0
1
**Easiest Code Ever - Python - Top 97% Speed - O( n log n ) Combinatorial**\n\nThe code below simply loops through the array taking an element "x" as the pivot. Using this pivot, we count separately the number of values:\n1. **Lower** than "x" to the **left** (lo_L)\n2. **Higher** than "x" to the **left** (hi_L)\n1. **...
69
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Python | clean DP solution O(n^2) 92 % fast
count-number-of-teams
0
1
```\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\n n = len(rating)\n \n up = [0] * n\n down = [0] * n\n \n teams = 0\n \n for i in range(n-1, -1, -1):\n for j in range(i+1, n):\n if rating[i] < rating[j]:\n ...
40
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Python, O(N^2)) Time, O(N) Space
count-number-of-teams
0
1
# Intuition\nIf we know how many elements are less/greater than `rating[j]` in $$[0;j)$$ and greater/less than `rating[j]` in $$(j;n)$$ we can multiply the two numbers to get the number of triples.\n\n# Approach\nWe will keep two sorted lists: one for elements $$[0;j)$$ and one for $$(j;n)$$.\nWhile scanning from left ...
1
There are `n` soldiers standing in a line. Each soldier is assigned a **unique** `rating` value. You have to form a team of 3 soldiers amongst them under the following rules: * Choose 3 soldiers with index (`i`, `j`, `k`) with rating (`rating[i]`, `rating[j]`, `rating[k]`). * A team is valid if: (`rating[i] < rat...
To walk from point A to point B there will be an optimal strategy to walk ? Advance in diagonal as possible then after that go in straight line. Repeat the process until visiting all the points.
Python3 Solution
design-underground-system
0
1
\n```\nclass UndergroundSystem:\n\n def __init__(self):\n self.i=defaultdict(tuple)\n self.o=defaultdict(list)\n\n def checkIn(self, id: int, stationName: str, t: int) -> None:\n self.i[id]=(t,stationName)\n\n def checkOut(self, id: int, stationName: str, t: int) -> None:\n starttim...
2
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Python3 Solution
design-underground-system
0
1
\n```\nclass UndergroundSystem:\n\n def __init__(self):\n self.i=defaultdict(tuple)\n self.o=defaultdict(list)\n\n def checkIn(self, id: int, stationName: str, t: int) -> None:\n self.i[id]=(t,stationName)\n\n def checkOut(self, id: int, stationName: str, t: int) -> None:\n starttim...
2
Given an array of integers `nums`, return _the number of **good pairs**_. A pair `(i, j)` is called _good_ if `nums[i] == nums[j]` and `i` < `j`. **Example 1:** **Input:** nums = \[1,2,3,1,1,3\] **Output:** 4 **Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. **Example 2:** **Input:** nu...
Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations.
Beating 86.12% Python Easiest Solution
design-underground-system
0
1
![image.png](https://assets.leetcode.com/users/images/49ba5212-262e-4a10-8981-4239531f8a66_1685529317.5107667.png)\n\n\n\n# Code\n```\nclass UndergroundSystem:\n\n def __init__(self):\n self.i=defaultdict(tuple)\n self.o=defaultdict(list)\n\n def checkIn(self, id: int, stationName: str, t: int) -> N...
1
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Beating 86.12% Python Easiest Solution
design-underground-system
0
1
![image.png](https://assets.leetcode.com/users/images/49ba5212-262e-4a10-8981-4239531f8a66_1685529317.5107667.png)\n\n\n\n# Code\n```\nclass UndergroundSystem:\n\n def __init__(self):\n self.i=defaultdict(tuple)\n self.o=defaultdict(list)\n\n def checkIn(self, id: int, stationName: str, t: int) -> N...
1
Given an array of integers `nums`, return _the number of **good pairs**_. A pair `(i, j)` is called _good_ if `nums[i] == nums[j]` and `i` < `j`. **Example 1:** **Input:** nums = \[1,2,3,1,1,3\] **Output:** 4 **Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. **Example 2:** **Input:** nu...
Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations.
Python short and clean.
design-underground-system
0
1
# Complexity\n- Time complexity: $$O(1)$$, for each method.\n\n- Space complexity: $$O(1)$$, for each method.\n\n# Code\n```python\nclass UndergroundSystem:\n\n def __init__(self):\n self.check_ins = {} # station_in => t_in\n self.trips = defaultdict(lambda: (0, 0)) # (station_in, station_out) => (tota...
2
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Python short and clean.
design-underground-system
0
1
# Complexity\n- Time complexity: $$O(1)$$, for each method.\n\n- Space complexity: $$O(1)$$, for each method.\n\n# Code\n```python\nclass UndergroundSystem:\n\n def __init__(self):\n self.check_ins = {} # station_in => t_in\n self.trips = defaultdict(lambda: (0, 0)) # (station_in, station_out) => (tota...
2
Given an array of integers `nums`, return _the number of **good pairs**_. A pair `(i, j)` is called _good_ if `nums[i] == nums[j]` and `i` < `j`. **Example 1:** **Input:** nums = \[1,2,3,1,1,3\] **Output:** 4 **Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. **Example 2:** **Input:** nu...
Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations.
EASY | BEGINNER FRIENDLY EXPLANATION | SPACE OPTIMISED |C++ |PYTHON3
design-underground-system
0
1
# ADVICE\nREAD APPROACH AND TRY DOING YOURSELF .THEN SEE CODE AND UPVOTE \uD83D\uDE42.\n# Approach\nWHAT WE WANT?\nAverage Travelling time b/w two stations .\nOne person{id} is contributing some Travelling time for a particular \n{start station , end station}.\nWe will store its contribution in map .\n```\n //id Sta...
5
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
EASY | BEGINNER FRIENDLY EXPLANATION | SPACE OPTIMISED |C++ |PYTHON3
design-underground-system
0
1
# ADVICE\nREAD APPROACH AND TRY DOING YOURSELF .THEN SEE CODE AND UPVOTE \uD83D\uDE42.\n# Approach\nWHAT WE WANT?\nAverage Travelling time b/w two stations .\nOne person{id} is contributing some Travelling time for a particular \n{start station , end station}.\nWe will store its contribution in map .\n```\n //id Sta...
5
Given an array of integers `nums`, return _the number of **good pairs**_. A pair `(i, j)` is called _good_ if `nums[i] == nums[j]` and `i` < `j`. **Example 1:** **Input:** nums = \[1,2,3,1,1,3\] **Output:** 4 **Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. **Example 2:** **Input:** nu...
Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations.
Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand
design-underground-system
1
1
**!! BIG ANNOUNCEMENT !!**\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies related to computer science and data science to my current Subscribers. This is only for first 10,000 Subscribers. **DON\'T FORGET** to Subscribe\n\n# Search \u...
32
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand
design-underground-system
1
1
**!! BIG ANNOUNCEMENT !!**\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies related to computer science and data science to my current Subscribers. This is only for first 10,000 Subscribers. **DON\'T FORGET** to Subscribe\n\n# Search \u...
32
Given an array of integers `nums`, return _the number of **good pairs**_. A pair `(i, j)` is called _good_ if `nums[i] == nums[j]` and `i` < `j`. **Example 1:** **Input:** nums = \[1,2,3,1,1,3\] **Output:** 4 **Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. **Example 2:** **Input:** nu...
Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations.
Easiest way || By using Map
design-underground-system
1
1
```\n//Please do upvote, if you like my solution :)\n//and free to ask if have any query :)\nclass dataa{\npublic:\n string from;\n string to;\n int in;\n int out;\n};\nclass UndergroundSystem {\npublic:\n map<pair<string,string>,vector<int>> loc;\n map<int,dataa> store;\n\t\n UndergroundSystem() {...
26
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. Implement the `UndergroundSystem` class: * `void checkIn(int id, string stationName, int t)` * A customer w...
Store number of computer in each row and column. Count all servers that are not isolated.
Easiest way || By using Map
design-underground-system
1
1
```\n//Please do upvote, if you like my solution :)\n//and free to ask if have any query :)\nclass dataa{\npublic:\n string from;\n string to;\n int in;\n int out;\n};\nclass UndergroundSystem {\npublic:\n map<pair<string,string>,vector<int>> loc;\n map<int,dataa> store;\n\t\n UndergroundSystem() {...
26
Given an array of integers `nums`, return _the number of **good pairs**_. A pair `(i, j)` is called _good_ if `nums[i] == nums[j]` and `i` < `j`. **Example 1:** **Input:** nums = \[1,2,3,1,1,3\] **Output:** 4 **Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. **Example 2:** **Input:** nu...
Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations.
Faster and less memory than all other solutions
find-all-good-strings
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem requires very, very careful counting.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe problem can be decomposed into two: counting all valid strings less than s1, then all valid strings less than s...
1
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Faster and less memory than all other solutions
find-all-good-strings
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem requires very, very careful counting.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe problem can be decomposed into two: counting all valid strings less than s1, then all valid strings less than s...
1
Given a binary string `s`, return _the number of substrings with all characters_ `1`_'s_. Since the answer may be too large, return it modulo `109 + 7`. **Example 1:** **Input:** s = "0110111 " **Output:** 9 **Explanation:** There are 9 substring in total with only 1's characters. "1 " -> 5 times. "11 " -> 3 times...
Use DP with 4 states (pos: Int, posEvil: Int, equalToS1: Bool, equalToS2: Bool) which compute the number of valid strings of size "pos" where the maximum common suffix with string "evil" has size "posEvil". When "equalToS1" is "true", the current valid string is equal to "S1" otherwise it is greater. In a similar way w...
beats 100 and 91 % in runtime and memory , Python3 code
find-all-good-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
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
beats 100 and 91 % in runtime and memory , Python3 code
find-all-good-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
Given a binary string `s`, return _the number of substrings with all characters_ `1`_'s_. Since the answer may be too large, return it modulo `109 + 7`. **Example 1:** **Input:** s = "0110111 " **Output:** 9 **Explanation:** There are 9 substring in total with only 1's characters. "1 " -> 5 times. "11 " -> 3 times...
Use DP with 4 states (pos: Int, posEvil: Int, equalToS1: Bool, equalToS2: Bool) which compute the number of valid strings of size "pos" where the maximum common suffix with string "evil" has size "posEvil". When "equalToS1" is "true", the current valid string is equal to "S1" otherwise it is greater. In a similar way w...
Python DP Solution | Using KMP Algorithm | Faster than 77%
find-all-good-strings
0
1
\n\n# Code\n```python []\nclass Solution:\n def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:\n chars, lenEvil = string.ascii_lowercase, len(evil)\n lps, left = [0] * lenEvil, 0\n for right in range(1, lenEvil):\n while left and evil[left] != evil[right]:\n ...
0
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
Python DP Solution | Using KMP Algorithm | Faster than 77%
find-all-good-strings
0
1
\n\n# Code\n```python []\nclass Solution:\n def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:\n chars, lenEvil = string.ascii_lowercase, len(evil)\n lps, left = [0] * lenEvil, 0\n for right in range(1, lenEvil):\n while left and evil[left] != evil[right]:\n ...
0
Given a binary string `s`, return _the number of substrings with all characters_ `1`_'s_. Since the answer may be too large, return it modulo `109 + 7`. **Example 1:** **Input:** s = "0110111 " **Output:** 9 **Explanation:** There are 9 substring in total with only 1's characters. "1 " -> 5 times. "11 " -> 3 times...
Use DP with 4 states (pos: Int, posEvil: Int, equalToS1: Bool, equalToS2: Bool) which compute the number of valid strings of size "pos" where the maximum common suffix with string "evil" has size "posEvil". When "equalToS1" is "true", the current valid string is equal to "S1" otherwise it is greater. In a similar way w...
[Python3] dp & kmp ... finally
find-all-good-strings
0
1
\n```\nclass Solution:\n def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:\n lps = [0]\n k = 0 \n for i in range(1, len(evil)): \n while k and evil[k] != evil[i]: k = lps[k-1]\n if evil[k] == evil[i]: k += 1\n lps.append(k)\n \n ...
4
Given the strings `s1` and `s2` of size `n` and the string `evil`, return _the number of **good** strings_. A **good** string has size `n`, it is alphabetically greater than or equal to `s1`, it is alphabetically smaller than or equal to `s2`, and it does not contain the string `evil` as a substring. Since the answer ...
Brute force is a good choice because length of the string is ≤ 1000. Binary search the answer. Use Trie data structure to store the best three matching. Traverse the Trie.
[Python3] dp & kmp ... finally
find-all-good-strings
0
1
\n```\nclass Solution:\n def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:\n lps = [0]\n k = 0 \n for i in range(1, len(evil)): \n while k and evil[k] != evil[i]: k = lps[k-1]\n if evil[k] == evil[i]: k += 1\n lps.append(k)\n \n ...
4
Given a binary string `s`, return _the number of substrings with all characters_ `1`_'s_. Since the answer may be too large, return it modulo `109 + 7`. **Example 1:** **Input:** s = "0110111 " **Output:** 9 **Explanation:** There are 9 substring in total with only 1's characters. "1 " -> 5 times. "11 " -> 3 times...
Use DP with 4 states (pos: Int, posEvil: Int, equalToS1: Bool, equalToS2: Bool) which compute the number of valid strings of size "pos" where the maximum common suffix with string "evil" has size "posEvil". When "equalToS1" is "true", the current valid string is equal to "S1" otherwise it is greater. In a similar way w...
Python3 - Easy Solution Explained
count-largest-group
0
1
```\nclass Solution:\n def countLargestGroup(self, n: int) -> int:\n\n # This method provides the sum of the digits of a number\n def sumDigits(number: int) -> int:\n\n return sum([int(d) for d in str(number)])\n \n # We initializate a dictionary to group the number by sumDigit...
1
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Python DP O(N) 99%/100%
count-largest-group
0
1
The idea here is that we can save on calculating sums of digits for bigger numbers if we start from `1` and keep the results in `dp[]`. \n\n```\nclass Solution:\n\n def countLargestGroup(self, n: int) -> int:\n dp = {0: 0}\n counts = [0] * (4 * 9)\n for i in range(1, n + 1):\n quotien...
52
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Python 3 two solutions - beautiful and fast :)
count-largest-group
0
1
**Solution 1**\nThis one is short and beatiful :)\n\n```python\ndef countLargestGroup(self, n: int) -> int:\n d = {}\n for i in range(1, n + 1):\n t = sum(map(int, list(str(i))))\n d[t] = d.get(t, 0) + 1\n return sum(1 for i in d.values() if i >= max(d.values()))\n```\n\n**Solution 2**\nThis one ...
14
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
100% beat easy
count-largest-group
0
1
\n# Approach\nMy system for calculating sum of digits works at $$O(n)$$ time complexity versus $$O(log(n))$$ time complexity in the case where you calculating sum of digits at the every iteration.\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(log(n))$$ \n\n# Code\n```\nclass Solution:\n d...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Solution for beginners
count-largest-group
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 `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
easy dictionary/ array approach
count-largest-group
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nfirst we create a dictionary with keys having all possible sum, now it\'s time to assign values to these keys. since we need to return a group maintain an array in place of values.\n\n# Approach\n<!-- Describe your approach to solving the...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Python solution
count-largest-group
0
1
```\nclass Solution:\n def countLargestGroup(self, n: int) -> int:\n hash_map, count = {}, 0\n\n for number in range(1, n + 1):\n num = sum([int(nu) for nu in str(number)])\n hash_map[num] = hash_map.get(num, 0) + 1\n\n _max = max(hash_map.values())\n\n return sum(1 ...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Python Very Simple Solution!!
count-largest-group
0
1
\n# Code\n```\nclass Solution:\n def countLargestGroup(self, n: int) -> int:\n\n hashmap: dict = {}\n numbers: range = range(1, n + 1)\n for number in numbers:\n _sum: int = sum(map(int, f"{number}"))\n hashmap[_sum] = hashmap.get(_sum, 0) + 1\n\n maximum: int = max(hashmap.valu...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
PYTHON SIMPLE WITH EXPLANATION
count-largest-group
0
1
# Code\n```\nclass Solution:\n def countLargestGroup(self, n: int) -> int:\n # FUNCTION TO CALCULATE SUM OF DIGITS\n def getSum(n): \n return sum(int(digit) for digit in str(n))\n # CREATE A HASHMAP\n count = {}\n for i in range(1, n + 1):\n k = getSum(i)\n ...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Rust & Python Solution
count-largest-group
0
1
# Code\n```python []\nclass Solution:\n def countLargestGroup(self, n: int) -> int:\n if n == 1:\n return 1\n\n def calculate_total(n: int) -> int:\n total = 0\n for num in str(n):\n total += int(num)\n return total\n\n map = {}\n\n ...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Python3 Counter
count-largest-group
0
1
\n\n# Code\n```\nclass Solution:\n def countLargestGroup(self, n: int) -> int:\n \n \n c1=Counter()\n \n for i in range(1,n+1):\n sm=sum(int(ch) for ch in str(i))\n c1[sm]+=1\n \n c2=Counter(c1.values())\n \n return c2[max(c2)]\n ...
0
You are given an integer `n`. Each number from `1` to `n` is grouped according to the sum of its digits. Return _the number of groups that have the largest size_. **Example 1:** **Input:** n = 13 **Output:** 4 **Explanation:** There are 9 groups in total, they are grouped according sum of its digits of numbers from...
null
Clear Python 3 solution faster than 91% with explanation
construct-k-palindrome-strings
0
1
```\nfrom collections import Counter\n\nclass Solution:\n def canConstruct(self, s: str, k: int) -> bool:\n if k > len(s):\n return False\n h = Counter(s)\n countOdd = 0\n for value in h.values():\n if value % 2:\n countOdd += 1\n if countOdd > ...
34
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Clear Python 3 solution faster than 91% with explanation
construct-k-palindrome-strings
0
1
```\nfrom collections import Counter\n\nclass Solution:\n def canConstruct(self, s: str, k: int) -> bool:\n if k > len(s):\n return False\n h = Counter(s)\n countOdd = 0\n for value in h.values():\n if value % 2:\n countOdd += 1\n if countOdd > ...
34
A sequence of numbers is called an **arithmetic progression** if the difference between any two consecutive elements is the same. Given an array of numbers `arr`, return `true` _if the array can be rearranged to form an **arithmetic progression**. Otherwise, return_ `false`. **Example 1:** **Input:** arr = \[3,5,1\]...
If the s.length < k we cannot construct k strings from s and answer is false. If the number of characters that have odd counts is > k then the minimum number of palindrome strings we can construct is > k and answer is false. Otherwise you can construct exactly k palindrome strings and answer is true (why ?).
✔ [C++ / Python3] Solution | XOR
construct-k-palindrome-strings
0
1
# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Python3\n```\nclass Solution:\n def canConstruct(self, S, K):\n return bin(reduce(operator.xor, map(lambda x: 1 << (ord(x) - 97), S))).count(\'1\') <= K <= len(S)\n```\n\n# C++\n```\nclass Solution {\npublic:\n bool canConstruct(s...
2
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
✔ [C++ / Python3] Solution | XOR
construct-k-palindrome-strings
0
1
# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Python3\n```\nclass Solution:\n def canConstruct(self, S, K):\n return bin(reduce(operator.xor, map(lambda x: 1 << (ord(x) - 97), S))).count(\'1\') <= K <= len(S)\n```\n\n# C++\n```\nclass Solution {\npublic:\n bool canConstruct(s...
2
A sequence of numbers is called an **arithmetic progression** if the difference between any two consecutive elements is the same. Given an array of numbers `arr`, return `true` _if the array can be rearranged to form an **arithmetic progression**. Otherwise, return_ `false`. **Example 1:** **Input:** arr = \[3,5,1\]...
If the s.length < k we cannot construct k strings from s and answer is false. If the number of characters that have odd counts is > k then the minimum number of palindrome strings we can construct is > k and answer is false. Otherwise you can construct exactly k palindrome strings and answer is true (why ?).
Python: Using Hashmap
construct-k-palindrome-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
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python: Using Hashmap
construct-k-palindrome-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
A sequence of numbers is called an **arithmetic progression** if the difference between any two consecutive elements is the same. Given an array of numbers `arr`, return `true` _if the array can be rearranged to form an **arithmetic progression**. Otherwise, return_ `false`. **Example 1:** **Input:** arr = \[3,5,1\]...
If the s.length < k we cannot construct k strings from s and answer is false. If the number of characters that have odd counts is > k then the minimum number of palindrome strings we can construct is > k and answer is false. Otherwise you can construct exactly k palindrome strings and answer is true (why ?).
Python3 Simple Solution
construct-k-palindrome-strings
0
1
\n\n# Code\n```\nclass Solution:\n def canConstruct(self, s: str, x: int) -> bool:\n \n \n freq=Counter(s)\n odd=0\n \n for k,v in freq.items():\n if v%2:\n odd+=1\n \n if odd>x:\n return False\n \n ret...
0
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Python3 Simple Solution
construct-k-palindrome-strings
0
1
\n\n# Code\n```\nclass Solution:\n def canConstruct(self, s: str, x: int) -> bool:\n \n \n freq=Counter(s)\n odd=0\n \n for k,v in freq.items():\n if v%2:\n odd+=1\n \n if odd>x:\n return False\n \n ret...
0
A sequence of numbers is called an **arithmetic progression** if the difference between any two consecutive elements is the same. Given an array of numbers `arr`, return `true` _if the array can be rearranged to form an **arithmetic progression**. Otherwise, return_ `false`. **Example 1:** **Input:** arr = \[3,5,1\]...
If the s.length < k we cannot construct k strings from s and answer is false. If the number of characters that have odd counts is > k then the minimum number of palindrome strings we can construct is > k and answer is false. Otherwise you can construct exactly k palindrome strings and answer is true (why ?).
Easy Python Solution
construct-k-palindrome-strings
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nif a string has more than one letter whose frequency is odd then that string cannot form palindrome, hence keeping this in mind, we keep track of each letters frequency, if it is odd we increment num by 1 for every odd frequ...
0
Given a string `s` and an integer `k`, return `true` _if you can use all the characters in_ `s` _to construct_ `k` _palindrome strings or_ `false` _otherwise_. **Example 1:** **Input:** s = "annabelle ", k = 2 **Output:** true **Explanation:** You can construct two palindromes using all characters in s. Some possibl...
It's straightforward to check if A or B won or not, check for each row/column/diag if all the three are the same. Then if no one wins, the game is a draw iff the board is full, i.e. moves.length = 9 otherwise is pending.
Easy Python Solution
construct-k-palindrome-strings
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nif a string has more than one letter whose frequency is odd then that string cannot form palindrome, hence keeping this in mind, we keep track of each letters frequency, if it is odd we increment num by 1 for every odd frequ...
0
A sequence of numbers is called an **arithmetic progression** if the difference between any two consecutive elements is the same. Given an array of numbers `arr`, return `true` _if the array can be rearranged to form an **arithmetic progression**. Otherwise, return_ `false`. **Example 1:** **Input:** arr = \[3,5,1\]...
If the s.length < k we cannot construct k strings from s and answer is false. If the number of characters that have odd counts is > k then the minimum number of palindrome strings we can construct is > k and answer is false. Otherwise you can construct exactly k palindrome strings and answer is true (why ?).
Python - Easy geometry
circle-and-rectangle-overlapping
0
1
First we have to find the nearest point on the rectangle to the center of the circle. Calculate the distance between the center of the circle with the nearest x and y coordinates. We can check the intersection by using pythagorean theorem. A point lies inside the circle if distance < radius. A point lies on the circle ...
2
You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
Python, translate square by center of circle, find closest point to origin, pythagoras theorem
circle-and-rectangle-overlapping
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 circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
check all cases
circle-and-rectangle-overlapping
0
1
# Code\n```\nclass Solution:\n def checkOverlap(self, radius: int, xCenter: int, yCenter: int, x1: int, y1: int, x2: int, y2: int) -> bool:\n R2 = radius ** 2\n # some point on the 4 sides of the rec is inside the circle\n if R2 >= (x1 - xCenter) ** 2:\n y = yCenter + (R2 - (x1 - xCen...
0
You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
Python Beats 80% Circle and Square Intersect
circle-and-rectangle-overlapping
0
1
```\nimport itertools\nclass Solution:\n def in_rect(corners: tuple[int, int, int, int], point: tuple[int, int]) -> bool:\n x1, y1, x2, y2 = corners\n x, y = point\n # Has to be included in both dimensions\n return x1 <= x and x <= x2 and y1 <= y and y <= y2\n def in_semi(semi: tuple[t...
0
You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
Simple maths solution
circle-and-rectangle-overlapping
0
1
# Intuition\nSample point from rectangle and check if in circle.\n\n# Approach\nFind a point $(x, y)$ such that $x_1 <= x <= y_2$ and $y_1 <= y <= y_2$ (i.e. in rectangle) which is closest to the circle origin.\nObviously, if $x_c$ lies within the range $[x_1, x_2]$ then $x = x_c$. If $x_c < x_1$, take $x=x_1$, convers...
0
You are given a circle represented as `(radius, xCenter, yCenter)` and an axis-aligned rectangle represented as `(x1, y1, x2, y2)`, where `(x1, y1)` are the coordinates of the bottom-left corner, and `(x2, y2)` are the coordinates of the top-right corner of the rectangle. Return `true` _if the circle and rectangle are...
Can we have an answer if the number of tomatoes is odd ? If we have answer will be there multiple answers or just one answer ? Let us define number of jumbo burgers as X and number of small burgers as Y We have to find an x and y in this equation 1. 4X + 2Y = tomato 2. X + Y = cheese
Python O(nlogn) solution for reference
reducing-dishes
0
1
```\nclass Solution:\n def maxSatisfaction(self, sat: List[int]) -> int:\n sat = sorted(sat)\n \n sm, res , idx , nend = 0,0,1,-1\n for i in range(0,len(sat)):\n if(sat[i] >= 0 ):\n sm += sat[i]\n res += sat[i]*idx \n idx += 1\n ...
2
A chef has collected data on the `satisfaction` level of his `n` dishes. Chef can cook any dish in 1 unit of time. **Like-time coefficient** of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. `time[i] * satisfaction[i]`. Return _the maximum sum...
Create an additive table that counts the sum of elements of submatrix with the superior corner at (0,0). Loop over all subsquares in O(n^3) and check if the sum make the whole array to be ones, if it checks then add 1 to the answer.
Python O(nlogn) solution for reference
reducing-dishes
0
1
```\nclass Solution:\n def maxSatisfaction(self, sat: List[int]) -> int:\n sat = sorted(sat)\n \n sm, res , idx , nend = 0,0,1,-1\n for i in range(0,len(sat)):\n if(sat[i] >= 0 ):\n sm += sat[i]\n res += sat[i]*idx \n idx += 1\n ...
2
We have a wooden plank of the length `n` **units**. Some ants are walking on the plank, each ant moves with a speed of **1 unit per second**. Some of the ants move to the **left**, the other move to the **right**. When two ants moving in two **different** directions meet at some point, they change their directions and...
Use dynamic programming to find the optimal solution by saving the previous best like-time coefficient and its corresponding element sum. If adding the current element to the previous best like-time coefficient and its corresponding element sum would increase the best like-time coefficient, then go ahead and add it. Ot...
[Python3] DP + Greedy approach + explanation
reducing-dishes
0
1
# Intuition\nWe need to maximaze the satisfaction usign formulae:\n```\nsum((i+1) * dish[i])\n```\nwith some of the dishes reduced. \nWe can have negative satisfaction dishes, which reduce the overall satisfaction, apart from they add time so that further positive-satisfaction dishes contribute more satisfaction. \n\nA...
1
A chef has collected data on the `satisfaction` level of his `n` dishes. Chef can cook any dish in 1 unit of time. **Like-time coefficient** of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. `time[i] * satisfaction[i]`. Return _the maximum sum...
Create an additive table that counts the sum of elements of submatrix with the superior corner at (0,0). Loop over all subsquares in O(n^3) and check if the sum make the whole array to be ones, if it checks then add 1 to the answer.