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
Python3||Beats 95.1% || Easy beginner solution
largest-number-at-least-twice-of-others
0
1
![image.png](https://assets.leetcode.com/users/images/4e46ce2a-f10a-4a3e-ac7c-45eb2d14e1f4_1677867186.9021106.png)\n\n# Code\n```\nclass Solution:\n def dominantIndex(self, nums: List[int]) -> int:\n nums1 = sorted(nums)\n if nums1[len(nums1)-1]>= 2*nums1[len(nums1)-2]:\n for i in range(len(...
4
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python 1-liner. Explained
shortest-completing-word
0
1
# Approach\nNormalize plate as `p`:\n1. lowercase\n2. filter out all non-letters\n3. build a Counter for easy "contain all" tests\n\nFind the minimum word by length considering only ones that matches criteria (p is lower or equalt Counter(word)).\n\nTo combine both steps into one line laverage:\n1. walrus operator (aka...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python 1-liner. Explained
shortest-completing-word
0
1
# Approach\nNormalize plate as `p`:\n1. lowercase\n2. filter out all non-letters\n3. build a Counter for easy "contain all" tests\n\nFind the minimum word by length considering only ones that matches criteria (p is lower or equalt Counter(word)).\n\nTo combine both steps into one line laverage:\n1. walrus operator (aka...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
748: Solution with step by step explanation
shortest-completing-word
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nlicense_chars = [char.lower() for char in licensePlate if char.isalpha()]\n```\nUsing a list comprehension, we loop through each character in the license Plate.\nWe only consider the character if it\'s an alphabet using...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
748: Solution with step by step explanation
shortest-completing-word
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nlicense_chars = [char.lower() for char in licensePlate if char.isalpha()]\n```\nUsing a list comprehension, we loop through each character in the license Plate.\nWe only consider the character if it\'s an alphabet using...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python Solution || Beginner Friendly
shortest-completing-word
0
1
# Code\n```\nclass Solution:\n def wordCheck(self, lis: List[str],word: str)-> int:\n temp=list(lis)\n for i in word:\n if i in temp:\n temp.remove(i)\n if len(temp)==0:\n return 1\n return 0\n \n def shortestCompletingWord(se...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python Solution || Beginner Friendly
shortest-completing-word
0
1
# Code\n```\nclass Solution:\n def wordCheck(self, lis: List[str],word: str)-> int:\n temp=list(lis)\n for i in word:\n if i in temp:\n temp.remove(i)\n if len(temp)==0:\n return 1\n return 0\n \n def shortestCompletingWord(se...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python | Easy Solution✅
shortest-completing-word
0
1
```\ndef shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:\n # removing digit and space from licensePlate\n licensePlate = \'\'.join([i.lower() for i in licensePlate if i.isalpha()])\n # sorting words array based on length of item\n words = sorted(words, key=len)\n ...
12
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python | Easy Solution✅
shortest-completing-word
0
1
```\ndef shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:\n # removing digit and space from licensePlate\n licensePlate = \'\'.join([i.lower() for i in licensePlate if i.isalpha()])\n # sorting words array based on length of item\n words = sorted(words, key=len)\n ...
12
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python Elegant & Short | Two lines | Counter
shortest-completing-word
0
1
\tfrom collections import Counter\n\n\n\tclass Solution:\n\t\t"""\n\t\tTime: O(m*max(n,k))\n\t\tMemory: O(n+k)\n\n\t\tn - length of letters in license_plate\n\t\tm - length of words\n\t\tk - length of each word in words\n\t\t"""\n\n\t\tdef shortestCompletingWord(self, license_plate: str, words: List[str]) -> str:\n\t...
11
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python Elegant & Short | Two lines | Counter
shortest-completing-word
0
1
\tfrom collections import Counter\n\n\n\tclass Solution:\n\t\t"""\n\t\tTime: O(m*max(n,k))\n\t\tMemory: O(n+k)\n\n\t\tn - length of letters in license_plate\n\t\tm - length of words\n\t\tk - length of each word in words\n\t\t"""\n\n\t\tdef shortestCompletingWord(self, license_plate: str, words: List[str]) -> str:\n\t...
11
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python3 very simple Explanation
shortest-completing-word
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n- find all hidden alphabets in licensePlate and convert them to lowercase\n- now iterate through words array\n- if all alphabets from licensePlate in word and length of current word is minimum then save index of word\n- flag is used to check if any of...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python3 very simple Explanation
shortest-completing-word
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n- find all hidden alphabets in licensePlate and convert them to lowercase\n- now iterate through words array\n- if all alphabets from licensePlate in word and length of current word is minimum then save index of word\n- flag is used to check if any of...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Solution
shortest-completing-word
1
1
```C++ []\nclass Solution {\npublic:\n string shortestCompletingWord(string licensePlate, vector<string>& words) {\n int m = licensePlate.length();\n int n = words.size();\n\n int hashSize = 26;\n int i = 0;\n int *hash1 = new int[hashSize];\n int *hash2 = new int[hashSize];...
2
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Solution
shortest-completing-word
1
1
```C++ []\nclass Solution {\npublic:\n string shortestCompletingWord(string licensePlate, vector<string>& words) {\n int m = licensePlate.length();\n int n = words.size();\n\n int hashSize = 26;\n int i = 0;\n int *hash1 = new int[hashSize];\n int *hash2 = new int[hashSize];...
2
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
shortest-completing-word
shortest-completing-word
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 a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
shortest-completing-word
shortest-completing-word
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
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python3 Simple solution without Dictionary
shortest-completing-word
0
1
```\nclass Solution:\n def shortestCompletingWord(self, P: str, words: List[str]) -> str:\n alphs=""\n res="" \n for p in P:\n if p.isalpha():\n alphs+=p.lower()\n for word in words: \n if all(alphs.count(alphs[i])...
4
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Python3 Simple solution without Dictionary
shortest-completing-word
0
1
```\nclass Solution:\n def shortestCompletingWord(self, P: str, words: List[str]) -> str:\n alphs=""\n res="" \n for p in P:\n if p.isalpha():\n alphs+=p.lower()\n for word in words: \n if all(alphs.count(alphs[i])...
4
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
[Python] One-liner, The Best
shortest-completing-word
0
1
# Complexity\n- Time complexity: $$O(n|\\Sigma|)$$ for input length $$n$$ and word alphabet $$\\Sigma$$\n- Space complexity: $$O(|\\Sigma|)$$\n\nThe Best!\n\n# Code\n```py\ndef shortestCompletingWord(self, license: str, words: list[str]) -> str:\n return min((w for lic in [Counter(c.lower() for c in license if c.isa...
1
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
[Python] One-liner, The Best
shortest-completing-word
0
1
# Complexity\n- Time complexity: $$O(n|\\Sigma|)$$ for input length $$n$$ and word alphabet $$\\Sigma$$\n- Space complexity: $$O(|\\Sigma|)$$\n\nThe Best!\n\n# Code\n```py\ndef shortestCompletingWord(self, license: str, words: list[str]) -> str:\n return min((w for lic in [Counter(c.lower() for c in license if c.isa...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Simple & Clean
shortest-completing-word
0
1
\n\n# Code\n```\nclass Solution:\n def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:\n characters = "".join([char for char in licensePlate if char.isalpha()]).lower()\n result = \'\'\n\n for word in words:\n isValid = len(result) == 0 or len(word) < len(result)\n\n if(isV...
0
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
Simple & Clean
shortest-completing-word
0
1
\n\n# Code\n```\nclass Solution:\n def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:\n characters = "".join([char for char in licensePlate if char.isalpha()]).lower()\n result = \'\'\n\n for word in words:\n isValid = len(result) == 0 or len(word) < len(result)\n\n if(isV...
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
traverse words and compare
shortest-completing-word
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGet alphas from licencePlate and match with words list.\ncheck for duplicates and match\nget the minimum length.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGet alphas from licencePlate and match with words list....
0
Given a string `licensePlate` and an array of strings `words`, find the **shortest completing** word in `words`. A **completing** word is a word that **contains all the letters** in `licensePlate`. **Ignore numbers and spaces** in `licensePlate`, and treat letters as **case insensitive**. If a letter appears more than...
Scan through the array to find the unique largest element `m`, keeping track of it's index `maxIndex`. Scan through the array again. If we find some `x != m` with `m < 2*x`, we should return `-1`. Otherwise, we should return `maxIndex`.
traverse words and compare
shortest-completing-word
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGet alphas from licencePlate and match with words list.\ncheck for duplicates and match\nget the minimum length.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGet alphas from licencePlate and match with words list....
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Solution
contain-virus
1
1
```C++ []\nint mp[2652], *zp[2652], zc[2652], step, mk;\n\nint dfs(int *p) {\n *p = 2;\n int res = 0;\n for (int *nb : {p-step, p-1, p+1, p+step}) {\n int v = *nb;\n if (v == mk || v > 1) continue;\n if (v <= 0) *nb = mk, ++res; else res += dfs(nb);\n }\n return res;\n}\nint dfs2(int...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
749: Solution with step by step explanation
contain-virus
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nm, n = len(mat), len(mat[0])\nDIRECTIONS = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n```\nm and n capture the dimensions of the matrix.\nDIRECTIONS is a list of four possible moves from a cell: up, down, left, and right.\n\n`...
2
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Pyhon Easy Solution || DFS || Time O(m*n*max(m , n)) || Faster
contain-virus
0
1
```\n class Solution:\n def containVirus(self, mat: List[List[int]]) -> int:\n m,n = len(mat),len(mat[0])\n\n def dfs(i,j,visited,nextInfected): # return no. of walls require to quarantined dfs area\n if 0<=i<m and 0<=j<n and (i,j) not in visited:\n if mat[i][j]==2: # Already...
8
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
[Python3] brute-force
contain-virus
0
1
\n```\nclass Solution:\n def containVirus(self, isInfected: List[List[int]]) -> int:\n m, n = len(isInfected), len(isInfected[0])\n ans = 0 \n while True: \n regions = []\n fronts = []\n walls = []\n seen = set()\n for i in range(m): \n ...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Another Explanation, Python3, O(R*C*max(R,C)) time and O(R*C) space, Faster than 95%
contain-virus
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThis is a pretty hard problem, there\'s a lot going on. There are a lot of considerations to make. But the idea is that the problem asks us to wall of clusters according to what happens each day/night, and explains the rules. So we simu...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Using BFS
contain-virus
0
1
# Code\n```\nclass Solution:\n def containVirus(self, isInfected: List[List[int]]) -> int:\n dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n m, n = len(isInfected), len(isInfected[0])\n ans = 0\n while True:\n # 1. BFS\n # neighbors stores every to-spread/to-put-wall a...
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Easiest Solution
contain-virus
1
1
\n\n# Code\n``` java []\nclass Solution {\n \n private static final int[][] DIR = new int[][]{\n {1, 0}, {-1, 0}, {0, 1}, {0, -1}\n };\n \n public int containVirus(int[][] isInfected) {\n int m = isInfected.length, n = isInfected[0].length;\n int ans = 0;\n \n while( tr...
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Detailed and clear solution
contain-virus
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 virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python Short and Easy to Understand Solution
contain-virus
0
1
\n```\nclass Solution:\n def containVirus(self, isInfected: List[List[int]]) -> int:\n walls_needed = 0\n\n # identify infected and uninfected cells\n infected = [(i, j) for i in range(len(isInfected)) for j in range(len(isInfected[0])) if isInfected[i][j] == 1]\n uninfected = [(i, j) for...
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
[Python] DFS and simulation. explained
contain-virus
0
1
* (1) DFS to find all the infected regions\n* (2) Pick the region that will infect the largetest number of cells in the next day, and build a wall around it\n* (3) Update the infected regions\nthe cells that are in the wall is updated with "controlled" state (i.e., value 2)\nthe cells that is on the boundary of uncontr...
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
Python Object Oriented Solution - Easy to understand
contain-virus
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is Python version:\nFor explanation please refer [CPP Version](https://leetcode.com/problems/contain-virus/solutions/847507/cpp-dfs-solution-explained/)\nCredit goes to: [rai02](https://leetcode.com/rai02/)\n\n# Approach\n<!-- Descri...
0
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
BFS: A Step-by-Step Guide with Comments.
contain-virus
0
1
# Template for BFS\n```\n1. bfs = deque() # to store the node first meets the requirements\n2. while bfs: # there are still nodes to visit\n3. node = bfs.popleft()\n4. # Mark as visited\n5. # Expand the node in all four directions\n6. if not visited and within the valid range:\n7. # update and d...
1
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as an `m x n` binary grid `isInfected`, where `isInfected[i][j] == 0` represents uninfected cells, and `isInfected[i][j] == 1` represents cells contaminated with the virus. A wall (and only one wall...
Count only the letters (possibly converted to lowercase) of each word. If a word is shorter and the count of each letter is at least the count of that letter in the licensePlate, it is the best answer we've seen yet.
752: Beats 98.86%, Solution with step by step explanation
open-the-lock
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ndef neighbors(node):\n for i in range(4):\n x = int(node[i])\n for d in (-1, 1): \n y = (x + d) % 10\n yield node[:i] + str(y) + node[i+1:]\n```\nThe neighbors function generates ...
1
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: `'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'`. The wheels can rotate freely and wrap around: for example we can turn `'9'` to be `'0'`, or `'0'` to be `'9'`. Each move consists of turning one wheel one slot. The lock initially starts...
Convert the ip addresses to and from (long) integers. You want to know what is the most addresses you can put in this block starting from the "start" ip, up to n. It is the smallest between the lowest bit of start and the highest bit of n. Then, repeat this process with a new start and n.
752: Beats 98.86%, Solution with step by step explanation
open-the-lock
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ndef neighbors(node):\n for i in range(4):\n x = int(node[i])\n for d in (-1, 1): \n y = (x + d) % 10\n yield node[:i] + str(y) + node[i+1:]\n```\nThe neighbors function generates ...
1
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
SUCCICNT EXPLANATION, BFS
open-the-lock
0
1
```\nLet\'s say target = \'7120\'\n\nAs the target starts with \'7\', obviously we have to go by \n \'9000\' --> \'8000\' --> \'7000\' --> \'7100\' --> \'7110\' --> \'7120\' --> \'7120\'\nBut what if all of those string are already in "deadends"? Then maybe the shortest way from\n\'1000\' .....\n\nSo we\'ve to check ...
1
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: `'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'`. The wheels can rotate freely and wrap around: for example we can turn `'9'` to be `'0'`, or `'0'` to be `'9'`. Each move consists of turning one wheel one slot. The lock initially starts...
Convert the ip addresses to and from (long) integers. You want to know what is the most addresses you can put in this block starting from the "start" ip, up to n. It is the smallest between the lowest bit of start and the highest bit of n. Then, repeat this process with a new start and n.
SUCCICNT EXPLANATION, BFS
open-the-lock
0
1
```\nLet\'s say target = \'7120\'\n\nAs the target starts with \'7\', obviously we have to go by \n \'9000\' --> \'8000\' --> \'7000\' --> \'7100\' --> \'7110\' --> \'7120\' --> \'7120\'\nBut what if all of those string are already in "deadends"? Then maybe the shortest way from\n\'1000\' .....\n\nSo we\'ve to check ...
1
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
Simple BFS solution
open-the-lock
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: `'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'`. The wheels can rotate freely and wrap around: for example we can turn `'9'` to be `'0'`, or `'0'` to be `'9'`. Each move consists of turning one wheel one slot. The lock initially starts...
Convert the ip addresses to and from (long) integers. You want to know what is the most addresses you can put in this block starting from the "start" ip, up to n. It is the smallest between the lowest bit of start and the highest bit of n. Then, repeat this process with a new start and n.
Simple BFS solution
open-the-lock
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
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
BFS
open-the-lock
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. -->\nWe start by defining the openLock function, which takes in two parameters: deadends (a list of dead-end combinations) and target (the target combination to unlock the ...
1
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: `'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'`. The wheels can rotate freely and wrap around: for example we can turn `'9'` to be `'0'`, or `'0'` to be `'9'`. Each move consists of turning one wheel one slot. The lock initially starts...
Convert the ip addresses to and from (long) integers. You want to know what is the most addresses you can put in this block starting from the "start" ip, up to n. It is the smallest between the lowest bit of start and the highest bit of n. Then, repeat this process with a new start and n.
BFS
open-the-lock
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. -->\nWe start by defining the openLock function, which takes in two parameters: deadends (a list of dead-end combinations) and target (the target combination to unlock the ...
1
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
753: Solution with step by step explanation
cracking-the-safe
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nif n == 1:\n return \'\'.join(map(str, range(k)))\n```\n\nIf n is 1, we simply return a string made of all numbers from 0 to k-1. This is because for n=1, every single digit is a possible password. We use map to conv...
2
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
753: Solution with step by step explanation
cracking-the-safe
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\nif n == 1:\n return \'\'.join(map(str, range(k)))\n```\n\nIf n is 1, we simply return a string made of all numbers from 0 to k-1. This is because for n=1, every single digit is a possible password. We use map to conv...
2
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
Solution
cracking-the-safe
1
1
```C++ []\nclass Solution {\npublic:\n using VT = pair<vector<string>,unordered_map<string,int>>;\n VT gNodes(int n, int k) {\n if (n==1) return VT({""},{{"",0}});\n VT ns;\n auto prev = gNodes(n-1,k).first;\n for (auto s: prev) {\n s += " ";\n for (int i=0; i<k; ...
2
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
Solution
cracking-the-safe
1
1
```C++ []\nclass Solution {\npublic:\n using VT = pair<vector<string>,unordered_map<string,int>>;\n VT gNodes(int n, int k) {\n if (n==1) return VT({""},{{"",0}});\n VT ns;\n auto prev = gNodes(n-1,k).first;\n for (auto s: prev) {\n s += " ";\n for (int i=0; i<k; ...
2
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
Python Eulerian Path (Eulerian Trail) Solution
cracking-the-safe
0
1
# Intuition\nThe original problem can be formulated as finding the Eulerian Path in a finite connected graph. If you are not familiar with the concept of the Eulerian Path, please read [this Wikipedia page](https://en.wikipedia.org/wiki/Eulerian_path).\n\nLet\'s say the length of the correct password is `n`, and the po...
5
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
Python Eulerian Path (Eulerian Trail) Solution
cracking-the-safe
0
1
# Intuition\nThe original problem can be formulated as finding the Eulerian Path in a finite connected graph. If you are not familiar with the concept of the Eulerian Path, please read [this Wikipedia page](https://en.wikipedia.org/wiki/Eulerian_path).\n\nLet\'s say the length of the correct password is `n`, and the po...
5
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
✅Python 3 - fast (95%+) - explained 😃 - de Bruijn sequence
cracking-the-safe
0
1
![image.png](https://assets.leetcode.com/users/images/0865974f-c981-497d-9936-701f3f44a38e_1673439994.1812327.png)\n\n\n# Intuition\nIt is very intuitive that two **adjacent passwords** should **differ by first and last digits**\n- example for `n = 3`, `k = 2`:\n```\nwe have 8 (2 ** 3) different passwords\n\nsolution: ...
2
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
✅Python 3 - fast (95%+) - explained 😃 - de Bruijn sequence
cracking-the-safe
0
1
![image.png](https://assets.leetcode.com/users/images/0865974f-c981-497d-9936-701f3f44a38e_1673439994.1812327.png)\n\n\n# Intuition\nIt is very intuitive that two **adjacent passwords** should **differ by first and last digits**\n- example for `n = 3`, `k = 2`:\n```\nwe have 8 (2 ** 3) different passwords\n\nsolution: ...
2
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
Not too complicated
cracking-the-safe
0
1
# Intuition\n\n\n# Approach\nFirstly create a directed graph where verticies are all k**n possible passwords, and there is an edge a-b if an end of a vertex "a" is a start of vertex "b" (a[1:] == b[:-1]). Then You just need to get a tour: a noncyclic way through all verticies. Starting with "0" * n vertex, append only ...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
Not too complicated
cracking-the-safe
0
1
# Intuition\n\n\n# Approach\nFirstly create a directed graph where verticies are all k**n possible passwords, and there is an edge a-b if an end of a vertex "a" is a start of vertex "b" (a[1:] == b[:-1]). Then You just need to get a tour: a noncyclic way through all verticies. Starting with "0" * n vertex, append only ...
0
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
hamilton path --> eulerian path
cracking-the-safe
0
1
It\'s easy to generate all the possible passwords of length n using k digits. At first glance, this problem seems like an eulerian path finding problem. However, if we think of each password as a node in the graph, soon we can see it\'s not about finding eulerian path (going through each edge once) but about finding ha...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
hamilton path --> eulerian path
cracking-the-safe
0
1
It\'s easy to generate all the possible passwords of length n using k digits. At first glance, this problem seems like an eulerian path finding problem. However, if we think of each password as a node in the graph, soon we can see it\'s not about finding eulerian path (going through each edge once) but about finding ha...
0
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
easy solution from iu7-11b bmstu to Kvasnikov V2 with recursion
cracking-the-safe
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
easy solution from iu7-11b bmstu to Kvasnikov V2 with recursion
cracking-the-safe
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 standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
easy solution from iu7-11b bmstu to Kvasnikov
cracking-the-safe
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
easy solution from iu7-11b bmstu to Kvasnikov
cracking-the-safe
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 standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
Easiest Hard Problem Logic Explained
cracking-the-safe
0
1
# Intuition\nFind the string which follow this property strictly:-\nEvery following string must contain n-1 digits from previous string\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nBacktrack to find the only solution maintaining the property as discussed can have any permutation a...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
Easiest Hard Problem Logic Explained
cracking-the-safe
0
1
# Intuition\nFind the string which follow this property strictly:-\nEvery following string must contain n-1 digits from previous string\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nBacktrack to find the only solution maintaining the property as discussed can have any permutation a...
0
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
DFS | Making password and checking if already exists
cracking-the-safe
0
1
\n# Code\n```\nclass Solution:\n def crackSafe(self, n: int, k: int) -> str:\n # the requirement is to have minimum length substring which contains all possible passwords of length n using [0,k-1] digits \n\n # Approach is simple , start with a possible password and in next step take last n-1 digits an...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
DFS | Making password and checking if already exists
cracking-the-safe
0
1
\n# Code\n```\nclass Solution:\n def crackSafe(self, n: int, k: int) -> str:\n # the requirement is to have minimum length substring which contains all possible passwords of length n using [0,k-1] digits \n\n # Approach is simple , start with a possible password and in next step take last n-1 digits an...
0
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
Very short DFS without backtracking in Python, faster than 94%
cracking-the-safe
0
1
# Complexity\n- Time complexity: $$O(n^k)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^k)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def crackSafe(self, n: int, k: int) -> str:\n visited = set()\n mod = 10 ** n\n ...
0
There is a safe protected by a password. The password is a sequence of `n` digits where each digit can be in the range `[0, k - 1]`. The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the **most recent** `n` **digits** that were entered each time you type a digit. * For ex...
We can think of this problem as a shortest path problem on a graph: there are `10000` nodes (strings `'0000'` to `'9999'`), and there is an edge between two nodes if they differ in one digit, that digit differs by 1 (wrapping around, so `'0'` and `'9'` differ by 1), and if *both* nodes are not in `deadends`.
Very short DFS without backtracking in Python, faster than 94%
cracking-the-safe
0
1
# Complexity\n- Time complexity: $$O(n^k)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^k)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def crackSafe(self, n: int, k: int) -> str:\n visited = set()\n mod = 10 ** n\n ...
0
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "post...
Solution
reach-a-number
1
1
```C++ []\nclass Solution {\n public:\n int reachNumber(int target) {\n const int newTarget = abs(target);\n int ans = 0;\n int pos = 0;\n\n while (pos < newTarget)\n pos += ++ans;\n while ((pos - newTarget) & 1)\n pos += ++ans;\n\n return ans;\n }\n};\n```\n\n```Python3 []\nclass Solution...
1
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "po...
754: Solution with step by step explanation
reach-a-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ntarget = abs(target)\n```\nThe direction to the target (left or right) doesn\'t really matter because moving to a positive target t would take the same number of moves as moving to its negative -t due to the symmetric n...
1
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "po...
✅ O(1) Walkthrough with 3 solutions + Intuition (Python)
reach-a-number
0
1
# 1. Brute Force Approach\n\n> At each step, there are only 2 choices: Go right or go left\n\n## Intuition\n1. At each step, there are only 2 choices: Go right or go left\n\t1. For instance, at our 1st step starting from 0, we can either go to -1 or 1\n2. We can form a state space, where each node represents the total ...
10
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "po...
"Python" easy explanation blackboard
reach-a-number
0
1
**The approach is explained in the image**\n*Simple and easy python3 solution*\nNote:- The minimum steps to target is same as the minimum steps to abs(target).\n![image](https://assets.leetcode.com/users/images/2e414173-ae03-4068-b015-ecb071c10458_1609177270.1487808.png)\n\n```\nclass Solution:\n def reachNumber(sel...
24
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "po...
Python 3 Binary Search/Math Explained
reach-a-number
0
1
```\nclass Solution:\n def reachNumber(self, target: int) -> int:\n """\n This program uses math and binary search to determine\n the minimum number of steps needed to reach the given\n target value (target).\n\n :param target: target number to reach\n :type target: int\n ...
9
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "po...
O(1) math solution beats 100% with detailed explanation
reach-a-number
1
1
# Intuition\n\n<details>\n\nFirst, any negative target will require the same number of moves as its positive counterpart. You simply need to flip the signs in the sum for the movements. As such, you can make the target positive for the purposes of this function.\n- For example, $7$ requires $1 + 2 + 3 - 4 + 5$ and $-7$...
0
You are standing at position `0` on an infinite number line. There is a destination at position `target`. You can make some number of moves `numMoves` so that: * On each move, you can either go left or right. * During the `ith` move (starting from `i == 1` to `i == numMoves`), you take `i` steps in the chosen dir...
We can think of this problem as the problem of finding an Euler path (a path visiting every edge exactly once) on the following graph: there are $$k^{n-1}$$ nodes with each node having $$k$$ edges. It turns out this graph always has an Eulerian circuit (path starting where it ends.) We should visit each node in "po...
Python Elegant & Short | Backtracking | No TLE
pyramid-transition-matrix
0
1
# Complexity\n- Time complexity: $$O(2^n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n + m)$$, where $$n$$ - length of ```bottom``` and $$m$$ - ```allowed``` size\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def pyramidTransition(s...
2
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
Solution
pyramid-transition-matrix
1
1
```C++ []\nclass Solution {\n unordered_set<string> invalid;\n bool solve(string bottom, int i, unordered_map<string,string> mp, string z)\n {\n int n=bottom.size();\n int m=z.size();\n if(n<2)\n return true;\n if(invalid.count(bottom))\n return false;\n if(m==n...
1
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
756: Solution with step by step explanation
pyramid-transition-matrix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\ntransition = {}\nfor triplet in allowed:\n transition.setdefault(triplet[:2], []).append(triplet[2])\n```\nFor each triplet in the allowed list, we take its first two characters (which represent the bottom blocks) an...
1
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
[Python 3] BFS and DFS
pyramid-transition-matrix
0
1
BFS solution: gives TLE\n\n\tclass Solution:\n\t\t\tdef get_states(self,s,i,new,allowed):\n\t\t\t\tif i==len(s):\n\t\t\t\t\tself.res.append(\'\'.join(new))\n\t\t\t\t\treturn \n\t\t\t\tfor x in allowed[s[i-1]+s[i]]:\n\t\t\t\t\tnew.append(x)\n\t\t\t\t\tself.get_states(s,i+1,new,allowed)\n\t\t\t\t\tnew.pop()\n\n\t\t\tdef ...
4
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
Backtracking/Python
pyramid-transition-matrix
0
1
This is a classic backtracking problem with two important base conditions:\n1) if length of the bottom layer becomes 1, then you were able to make a pyramid\n2) if you reached the end of the bottom array, then make the top layer as current bottom layer\n```\nclass Solution:\n def pyramidTransition(self, bottom: str,...
0
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
Easy Back tracking with no bit manipulation
pyramid-transition-matrix
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n`dfs` solution not worked for me (TLE). \n`dp way` for example, `dp[i][bottom_blocks][upper_blocks]` also not worked. (TLE)\n\nI found out that all i need to know is possibilty of generating pyramid with `allowed`, no need to find all c...
0
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
Python, straightforward DFS with heuristic pruning
pyramid-transition-matrix
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n\nClassical DFS may got TLE.\n\nTwo heuristic pruning strategies are added to reduce the running time.\n(1) We check whether this block can make a potential base with the previous block.\n(2) We check whether this block is the "left" block of some bas...
0
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
Python || Recursion || Memoization
pyramid-transition-matrix
0
1
```\nclass Solution:\n def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool:\n allowed=set(allowed)\n letters=[\'A\',\'B\',\'C\',\'D\',\'E\',\'F\']\n @lru_cache(None)\n def solve(bottom):\n if len(bottom)==1: return True\n tops=[""]\n n=len...
1
You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains **one less block** than the row beneath it and is centered on top. To make the pyramid aesthetically pleasing, there are only specific **triangular patterns** that are allowed. A tria...
null
Solution
set-intersection-size-at-least-two
1
1
```C++ []\nclass Solution {\npublic:\n int intersectionSizeTwo(vector<vector<int>>& intervals) {\n sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b) {\n return a[1] < b[1] || (a[1] == b[1] && a[0] > b[0]); \n });\n int n = intervals.size(), ans = 0, p1 = -1, ...
1
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
Solution
set-intersection-size-at-least-two
1
1
```C++ []\nclass Solution {\npublic:\n int intersectionSizeTwo(vector<vector<int>>& intervals) {\n sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b) {\n return a[1] < b[1] || (a[1] == b[1] && a[0] > b[0]); \n });\n int n = intervals.size(), ans = 0, p1 = -1, ...
1
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
757: Solution with step by step explanation
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n intervals.sort(key = lambda e: (e[1], -e[0]))\n```\n\nHere, we sort the intervals list based on two criteria:\n\nAscending order of the interval\'s end values (e[1]).\nFor intervals with the same end value, we s...
2
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
757: Solution with step by step explanation
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n```\n intervals.sort(key = lambda e: (e[1], -e[0]))\n```\n\nHere, we sort the intervals list based on two criteria:\n\nAscending order of the interval\'s end values (e[1]).\nFor intervals with the same end value, we s...
2
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
Python [Explained] | O(nlogn)
set-intersection-size-at-least-two
0
1
# Intuition :\n- This problem is similar to [Meeting rooms II](https://leetcode.com/problems/meeting-rooms-ii/) in which we have to find the minimum number of rooms required to organize all the conferences.\n- Similarly in this problem we have to find the minimum size of set that contain atleast two integers from each ...
10
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
Python [Explained] | O(nlogn)
set-intersection-size-at-least-two
0
1
# Intuition :\n- This problem is similar to [Meeting rooms II](https://leetcode.com/problems/meeting-rooms-ii/) in which we have to find the minimum number of rooms required to organize all the conferences.\n- Similarly in this problem we have to find the minimum size of set that contain atleast two integers from each ...
10
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
✅greedy || sorting || python
set-intersection-size-at-least-two
0
1
\n# Code\n```\nclass Solution:\n def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:\n arr=sorted(intervals, key = lambda x: (x[1], x[0]))\n ans=[]\n for v in arr:\n l=0\n r=len(ans)-1\n a=-1\n while(l<=r):\n if(a!=-1 and l...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
✅greedy || sorting || python
set-intersection-size-at-least-two
0
1
\n# Code\n```\nclass Solution:\n def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:\n arr=sorted(intervals, key = lambda x: (x[1], x[0]))\n ans=[]\n for v in arr:\n l=0\n r=len(ans)-1\n a=-1\n while(l<=r):\n if(a!=-1 and l...
0
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
Unbeatable Python Code for Intersection Size Accepted 100%🥰
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem seems to be about finding the minimum number of points that intersect all the given intervals. The code sorts the intervals based on their end points and then iteratively checks the intersections.\n\n# Approach\n<!-- Describe ...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
Unbeatable Python Code for Intersection Size Accepted 100%🥰
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem seems to be about finding the minimum number of points that intersect all the given intervals. The code sorts the intervals based on their end points and then iteratively checks the intersections.\n\n# Approach\n<!-- Describe ...
0
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
91 in runtime and 100 in memory , unique solution using Greedy
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
91 in runtime and 100 in memory , unique solution using Greedy
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
done :)
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
done :)
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
Python (Simple Maths)
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
Python (Simple Maths)
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
O(nlogn) solution
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thought is that we need to find a way to maximize the number of points that are included in the intersection of all intervals. We can do this by selecting the intervals that have the most overlap with other intervals, and then se...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null
O(nlogn) solution
set-intersection-size-at-least-two
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thought is that we need to find a way to maximize the number of points that are included in the intersection of all intervals. We can do this by selecting the intervals that have the most overlap with other intervals, and then se...
0
We are given a list `schedule` of employees, which represents the working time for each employee. Each employee has a list of non-overlapping `Intervals`, and these intervals are in sorted order. Return the list of finite intervals representing **common, positive-length free time** for _all_ employees, also in sorted...
null
Python3 🐍 concise solution beats 99%
set-intersection-size-at-least-two
0
1
# Code\n```\nclass Solution:\n def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:\n ans = []\n for x, y in sorted(intervals, key=lambda x: (x[1], -x[0])): \n if not ans or ans[-2] < x: \n if ans and x <= ans[-1]: ans.append(y)\n else: ans.extend([...
0
You are given a 2D integer array `intervals` where `intervals[i] = [starti, endi]` represents all the integers from `starti` to `endi` inclusively. A **containing set** is an array `nums` where each interval from `intervals` has **at least two** integers in `nums`. * For example, if `intervals = [[1,3], [3,7], [8,9...
null