post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1691811/3-Methods-or-Python3
class Solution: def createTargetArray(self, nums, index): target = [] for i,value in zip(index, nums): target.insert(i,value) return target obj = Solution() print(obj.createTargetArray([0,1,2,3,4],[0,1,2,2,1]))
create-target-array-in-the-given-order
3 Methods | Python3 🐍
hritik5102
0
57
create target array in the given order
1,389
0.859
Easy
20,900
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1256771/python-solution-easy-to-understand.
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: target = [] for i in range(len(nums)): target.insert(index[i], nums[i]) return target
create-target-array-in-the-given-order
python solution - easy to understand.
jayesh_kaushik
0
71
create target array in the given order
1,389
0.859
Easy
20,901
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1006014/Python-or-92.56-or-3-Methods-to-solve
class Solution: def createTargetArray(self, nums, index): target = [0]*len(nums) target_copy = [0]*len(nums) for i,v in zip(index,nums): if target[i]: for j in range(i+1,len(target_copy)): target[j] = target_copy[j-1] target[i]=v target[i]=v target_copy = target[:] return target
create-target-array-in-the-given-order
Python | 92.56% | 3 Methods to solve
hritik5102
0
174
create target array in the given order
1,389
0.859
Easy
20,902
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1006014/Python-or-92.56-or-3-Methods-to-solve
class Solution: def createTargetArray(self, nums, index): target = [] for i in range(len(nums)): target = target[:index[i]] + [nums[i]] + target[index[i]:] return target
create-target-array-in-the-given-order
Python | 92.56% | 3 Methods to solve
hritik5102
0
174
create target array in the given order
1,389
0.859
Easy
20,903
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/1006014/Python-or-92.56-or-3-Methods-to-solve
class Solution: def createTargetArray(self, nums, index): target = [] for i,value in zip(index, nums): target.insert(i,value) return target obj = Solution() print(obj.createTargetArray([0,1,2,3,4],[0,1,2,2,1]))
create-target-array-in-the-given-order
Python | 92.56% | 3 Methods to solve
hritik5102
0
174
create target array in the given order
1,389
0.859
Easy
20,904
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/955156/Python-Linked-List-greater99.4Easiest-Approach
class Solution: def createTargetArray(self, nums, index): head = Node(nums[0]) # We always start at nums[0]. ll = LL(head) # Initialise our linked list. for i in range(1, len(index)): # We set our head node already. Skip. ll.insert(nums[i], index[i]) # Very simple insertion! return ll.toArray() # Only non-standard linked list operation. class Node: # Separate classes for neatness. def __init__(self, val=None, ref=None): self.val = val self.ref = ref # I dislike stepping on Python's "next" keyword. class LL: def __init__(self, head=Node()): self.head = head def insert(self, val, pos): if pos == 0: new_node = Node(val, self.head) self.head = new_node return curr = self.head while curr and pos-1: curr = curr.ref pos -= 1 new_node = Node(val, curr.ref) curr.ref = new_node def toArray(self): arr = [] curr = self.head while curr: arr.append(curr.val) curr = curr.ref return arr
create-target-array-in-the-given-order
Python Linked List [>99.4%][Easiest Approach]
StephenLalor
0
159
create target array in the given order
1,389
0.859
Easy
20,905
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/692454/Python-3-Create-Target-Array-in-the-Given-Order
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: result = [] for i in range(len(nums)): result.insert(index[i],nums[i]) return result
create-target-array-in-the-given-order
Python 3 Create Target Array in the Given Order
abhijeetmallick29
0
135
create target array in the given order
1,389
0.859
Easy
20,906
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/674094/Python3-solution-without-using-list.insert()
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: target = [None] * len(nums) for k in range(len(nums)): if target[index[k]] == None: target[index[k]] = nums[k] else: l = index[k] + 1 while target[l] != None: l += 1 while l != index[k]: target[l] = target[l-1] l -= 1 target[index[k]] = nums[k] return target
create-target-array-in-the-given-order
Python3 solution without using list.insert()
adb786
0
225
create target array in the given order
1,389
0.859
Easy
20,907
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/547268/Python3-brute-force
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: ans = [] for i, x in zip(index, nums): ans.insert(i, x) return ans
create-target-array-in-the-given-order
[Python3] brute-force
ye15
0
100
create target array in the given order
1,389
0.859
Easy
20,908
https://leetcode.com/problems/create-target-array-in-the-given-order/discuss/836690/Python-3-24-ms-Solution-98.62
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: result = [] for i in range(0, len(index)): # insertion is guaranteed to be valid result.insert(index[i], nums[i]) # shift then insert value return result
create-target-array-in-the-given-order
Python 3 24 ms Solution 98.62%
Skyfall2017
-1
250
create target array in the given order
1,389
0.859
Easy
20,909
https://leetcode.com/problems/four-divisors/discuss/547308/Python3-Short-Easy-Solution
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: res = 0 for num in nums: divisor = set() for i in range(1, floor(sqrt(num)) + 1): if num % i == 0: divisor.add(num//i) divisor.add(i) if len(divisor) > 4: break if len(divisor) == 4: res += sum(divisor) return res
four-divisors
[Python3] Short Easy Solution
localhostghost
22
2,500
four divisors
1,390
0.413
Medium
20,910
https://leetcode.com/problems/four-divisors/discuss/547208/Python3-use-a-helper-fn
class Solution: def sumFourDivisors(self, nums: List[int]) -> int: def fn(x): c = s = 0 for i in range(1, int(x**0.5)+1): if x % i == 0: c += 1 + (0 if x//i == i else 1) s += i + (0 if x//i == i else x//i) return s if c == 4 else 0 return sum(fn(x) for x in nums)
four-divisors
[Python3] use a helper fn
ye15
0
85
four divisors
1,390
0.413
Medium
20,911
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/635713/Python3-dfs-solution-Check-if-There-is-a-Valid-Path-in-a-Grid
class Solution: directions = [[-1, 0], [0, 1], [1, 0], [0, -1]] streetDirections = { 1: [1, 3], 2: [0, 2], 3: [2, 3], 4: [1, 2], 5: [0, 3], 6: [0, 1] } def hasValidPath(self, grid: List[List[int]]) -> bool: m, n = len(grid), len(grid[0]) def dfs(i: int, j: int, oppositeDirection: int) -> None: if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] < 0: return v = grid[i][j] sd = Solution.streetDirections[v] direction = (oppositeDirection + 2) % 4 if direction not in sd: return grid[i][j] = -v for d in sd: delta = Solution.directions[d] dfs(i+delta[0], j+delta[1], d) dfs(0, 0, 0) dfs(0, 0, 3) return grid[m-1][n-1] < 0
check-if-there-is-a-valid-path-in-a-grid
Python3 dfs solution - Check if There is a Valid Path in a Grid
r0bertz
2
391
check if there is a valid path in a grid
1,391
0.472
Medium
20,912
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/1486702/Python3-readable-solution-with-comments
class Solution: # define direction identifiers left, right, up, down = 0, 1, 2, 3 # define possible directions to move from a given street moves = { 1: {left, right}, 2: {up, down}, 3: {left, down}, 4: {down, right}, 5: {left, up}, 6: {up, right} } # defind offsets x, y offsets for each direction offsets = { left: ( 0, -1, right), # y offset, x offset, move to return to previous position right: ( 0, 1, left), up: (-1, 0, down), down: ( 1, 0, up) } def hasValidPath(self, grid: List[List[int]]) -> bool: if len(grid) == 1 and len(grid[0]) == 1: return True # on the start we can possibly move only to right and down # so there are only two possible paths which can lead as to the final position for direction in [Solution.right, Solution.down]: cur_x, cur_y = 0, 0 while 1: y_offset, x_offset, reverse_move = Solution.offsets[direction] cur_x += x_offset cur_y += y_offset # break if current road leads us to out of grid if not (0 <= cur_x < len(grid[0]) and 0 <= cur_y < len(grid)): break # break if current road leads us to incompatible road if not reverse_move in Solution.moves[grid[cur_y][cur_x]]: break # we are in the infinite loop if (cur_x, cur_y) == (0, 0): break # define next direction direction = [i for i in Solution.moves[grid[cur_y][cur_x]] if i != reverse_move][0] if (cur_x, cur_y) == (len(grid[0]) - 1, len(grid) - 1): return True return False
check-if-there-is-a-valid-path-in-a-grid
Python3 readable solution with comments
ac_h_illes
1
185
check if there is a valid path in a grid
1,391
0.472
Medium
20,913
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/547226/Python3-graph-traversal
class Solution: def hasValidPath(self, grid: List[List[int]]) -> bool: m, n = len(grid), len(grid[0]) #dimension graph = dict() for i in range(m): for j in range(n): if grid[i][j] == 1: graph[i, j] = [(i, j-1), (i, j+1)] elif grid[i][j] == 2: graph[i, j] = [(i-1, j), (i+1, j)] elif grid[i][j] == 3: graph[i, j] = [(i, j-1), (i+1, j)] elif grid[i][j] == 4: graph[i, j] = [(i+1, j), (i, j+1)] elif grid[i][j] == 5: graph[i, j] = [(i-1, j), (i, j-1)] else: graph[i, j] = [(i-1, j), (i, j+1)] #traverse graph stack = [(0, 0)] seen = set() while stack: i, j = stack.pop() if i == m-1 and j == n-1: return True seen.add((i, j)) #mark as visited for ii, jj in graph[i, j]: if 0 <= ii < m and 0 <= jj < n and (ii, jj) not in seen and (i, j) in graph[ii, jj]: stack.append((ii, jj)) return False
check-if-there-is-a-valid-path-in-a-grid
[Python3] graph traversal
ye15
1
224
check if there is a valid path in a grid
1,391
0.472
Medium
20,914
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/2009081/Python3-solution
class Solution: def hasValidPath(self, grid: List[List[int]]) -> bool: d = { # direction 'E': (0,1), 'W': (0,-1), 'N': (-1,0), 'S': (1,0), } valid_path = { 1: {'E': [1, 3, 5], 'W': [1, 4, 6]}, 2: {'N': [2, 3, 4], 'S': [2, 5, 6]}, 3: {'W': [1, 4, 6], 'S': [2, 5, 6]}, 4: {'E': [1, 3, 5], 'S': [2, 5, 6]}, 5: {'N': [2, 3, 4], 'W': [1, 4, 6]}, 6: {'N': [2, 3, 4], 'E': [1, 3, 5]}, } def dfs(i, j): if (i,j) == (m-1,n-1): return True visited.add((i,j)) for dr, valid_types in valid_path[grid[i][j]].items(): i_nxt, j_nxt = i + d[dr][0], j + d[dr][1] if (i_nxt, j_nxt) not in visited and\ 0 <= i_nxt < m and 0 <= j_nxt < n: if grid[i_nxt][j_nxt] in valid_types: if dfs(i_nxt, j_nxt): return True return False m, n = len(grid), len(grid[0]) visited = set() return dfs(0, 0)
check-if-there-is-a-valid-path-in-a-grid
Python3 solution
dalechoi
0
40
check if there is a valid path in a grid
1,391
0.472
Medium
20,915
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/547245/Python-3-BFS
class Solution: def hasValidPath(self, grid: List[List[int]]) -> bool: self.m, self.n, self.res = len(grid), len(grid[0]), False self.vis = [[0] * (self.n) for _ in range(self.m)] def inArea(x, y): return x >= 0 and x < self.m and y >= 0 and y < self.n # BFS - startPos: (startx, starty) def bfs(startx, starty): que = [[startx, starty]] dirr = [[-1, 0], [1, 0], [0, -1], [0, 1]] self.vis[startx][starty] = 1 while que: curx, cury = que.pop(0) if curx == self.m - 1 and cury == self.n - 1: self.res = True; break for i in range(4): nxtx, nxty = curx + dirr[i][0], cury + dirr[i][1] # curPos(curx, cury) -> nxtPos(nxtx, nxty), both in the grid and are connected by the road # if curDirection is ↑, then grid[curx][cury] should be 2 or 5 or 6, and grid[nxtx][nxty] should be 2 or 3 or 4 # if curDirection is ↓, then grid[curx][cury] should be 2 or 3 or 4, and grid[nxtx][nxty] should be 2 or 5 or 6 # if curDirection is ←, then grid[curx][cury] should be 1 or 3 or 5, and grid[nxtx][nxty] should be 1 or 4 or 6 # if curDirection is →, then grid[curx][cury] should be 1 or 4 or 6, and grid[nxtx][nxty] should be 1 or 3 or 5 if inArea(nxtx, nxty) and self.vis[nxtx][nxty] == 0: if (i == 0 and (grid[curx][cury] == 2 or grid[curx][cury] == 5 or grid[curx][cury] == 6) and (grid[nxtx][nxty] == 2 or grid[nxtx][nxty] == 3 or grid[nxtx][nxty] == 4)) or (i == 1 and (grid[curx][cury] == 2 or grid[curx][cury] == 3 or grid[curx][cury] == 4) and (grid[nxtx][nxty] == 2 or grid[nxtx][nxty] == 5 or grid[nxtx][nxty] == 6)) or (i == 2 and (grid[curx][cury] == 1 or grid[curx][cury] == 3 or grid[curx][cury] == 5) and (grid[nxtx][nxty] == 1 or grid[nxtx][nxty] == 4 or grid[nxtx][nxty] == 6)) or (i == 3 and (grid[curx][cury] == 1 or grid[curx][cury] == 4 or grid[curx][cury] == 6) and (grid[nxtx][nxty] == 1 or grid[nxtx][nxty] == 3 or grid[nxtx][nxty] == 5)): self.vis[nxtx][nxty] = 1 que.append([nxtx, nxty]) bfs(0, 0) return self.res
check-if-there-is-a-valid-path-in-a-grid
[Python 3] BFS
cr_496352127
0
101
check if there is a valid path in a grid
1,391
0.472
Medium
20,916
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/548292/Python-DFS-and-Bit-Manipulation-(14-lines)
class Solution(object): def hasValidPath(self, grid): streets, m, n = [0, 56, 146, 152, 176, 26, 50], 3*len(grid), 3*len(grid[0]) def get(r, c): return 1 &amp; ((0 <= r < m and 0 <= c < n) and (streets[grid[r/3][c/3]] >> 3*(r%3)+(c%3))) stack, seen = [(1,1)], {(1, 1)} while stack: (r, c) = stack.pop() if (r, c) == (m-2, n-2): return True for r, c in [(r-1, c), (r+1, c), (r, c+1), (r, c-1)]: if (r, c) not in seen and get(r, c) == 1: seen.add((r, c)) stack.append((r, c)) return False
check-if-there-is-a-valid-path-in-a-grid
[Python] DFS and Bit Manipulation (14 lines)
leetcoder289
-1
117
check if there is a valid path in a grid
1,391
0.472
Medium
20,917
https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/discuss/547415/Python-BFS-short-and-readable-with-explanation
class Solution: def hasValidPath(self, grid: List[List[int]]) -> bool: if not grid or not grid[0]: return False M, N = len(grid), len(grid[0]) left, right, up, down = (0,-1), (0,1), (-1,0), (1,0) direction = { 1: (left, right), 2: (up, down), 3: (left, down), 4: (right, down), 5: (left, up), 6: (right, up) } compatibles = { right: {1, 3, 5}, left: {1, 4, 6}, up: {2, 3, 4}, down: {2, 5, 6} } q = collections.deque([(0, 0)]) seen = {(0, 0)} while q: r, c = q.popleft() if (r, c) == (M-1, N-1): return True di_x, di_y = direction[grid[r][c]] accepted_paths = compatibles[di_x] | compatibles[di_y] for dr, dc in [di_x, di_y]: cr, cc = r+dr, c+dc if (cr, cc) not in seen and 0 <= cr < M and 0 <= cc < N and grid[cr][cc] in accepted_paths: seen.add((cr, cc)) q.append((cr, cc)) return False
check-if-there-is-a-valid-path-in-a-grid
[Python] BFS, short & readable with explanation
browndog
-1
145
check if there is a valid path in a grid
1,391
0.472
Medium
20,918
https://leetcode.com/problems/longest-happy-prefix/discuss/2814375/Dynamic-programming-solution
class Solution: def longestPrefix(self, s: str) -> str: n = [0] + [None] * (len(s) - 1) for i in range(1, len(s)): k = n[i - 1] # trying length k + 1 while (k > 0) and (s[i] != s[k]): k = n[k - 1] if s[i] == s[k]: k += 1 n[i] = k happy_border = n[-1] return s[:happy_border]
longest-happy-prefix
Dynamic programming solution
aknyazev87
1
24
longest happy prefix
1,392
0.45
Hard
20,919
https://leetcode.com/problems/longest-happy-prefix/discuss/2016882/python3-Solution-or-Z-Algorithm
class Solution: def longestPrefix(self, s: str) -> str: n = len(s) z = [0] * n l, r = 0, 0 for i in range(1, n): if i < r: z[i] = min(r - i, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] == n: return s[i:] if i + z[i] > r: l, r = i, i + z[i] return ''
longest-happy-prefix
python3 Solution | Z Algorithm
satyam2001
1
49
longest happy prefix
1,392
0.45
Hard
20,920
https://leetcode.com/problems/longest-happy-prefix/discuss/2842457/Python-KMP-algorithm-faster-than-73.93
class Solution: def longestPrefix(self, s: str) -> str: table = [0 for _ in range(len(s))] longest_prefix = 0 for j in range(1, len(s)): while longest_prefix>0 and s[longest_prefix]!=s[j]: longest_prefix = table[longest_prefix-1] if s[longest_prefix]==s[j]: longest_prefix+=1 table[j] = longest_prefix return s[:table[-1]]
longest-happy-prefix
Python KMP algorithm faster than 73.93%
amy279
0
1
longest happy prefix
1,392
0.45
Hard
20,921
https://leetcode.com/problems/longest-happy-prefix/discuss/2315799/Short-Python3-implementation-with-Rolling-Hash
class Solution: def longestPrefix(self, s: str) -> str: a, m, p = 1103515245, 2**31, 1 ans = -1 prefix_hash, suffix_hash = 0, 0 for i in range(len(s)-1): prefix_hash = (prefix_hash * a + ord(s[i])) % m suffix_hash = (suffix_hash + ord(s[-i-1]) * p) % m p = p * a % m if prefix_hash == suffix_hash and s[:i+1] == s[-i-1:]: ans = i return s[:ans+1]
longest-happy-prefix
Short Python3 implementation with Rolling Hash
metaphysicalist
0
33
longest happy prefix
1,392
0.45
Hard
20,922
https://leetcode.com/problems/longest-happy-prefix/discuss/965771/Python3-Solution
class Solution: def longestPrefix(self, s: str) -> str: ans = "" for i in range(len(s)): if(i!=len(s)-1): if(s[:i+1] == s[len(s)-i-1:]): # print(s[:i+1],s[:-i][::-1]) if len(s[:i+1])>len(ans): ans = "" ans += s[:i+1] return ans
longest-happy-prefix
Python3 Solution
swap2001
0
84
longest happy prefix
1,392
0.45
Hard
20,923
https://leetcode.com/problems/longest-happy-prefix/discuss/547251/Python-3-Straight-Forward
class Solution: def longestPrefix(self, s: str) -> str: lens, maxStr = len(s), "" for i in range(lens - 1): # Slice - Prefix and suffix comparison if s[:(i + 1)] == s[(lens - i - 1):]: # Update the result maxStr = s[:(i + 1)] return maxStr
longest-happy-prefix
[Python 3] Straight Forward
cr_496352127
0
41
longest happy prefix
1,392
0.45
Hard
20,924
https://leetcode.com/problems/longest-happy-prefix/discuss/547247/Python3-one-line
class Solution: def longestPrefix(self, s: str) -> str: return next((s[:i] for i in reversed(range(1, len(s))) if s[:i] == s[-i:]), "")
longest-happy-prefix
[Python3] one-line
ye15
0
53
longest happy prefix
1,392
0.45
Hard
20,925
https://leetcode.com/problems/longest-happy-prefix/discuss/547247/Python3-one-line
class Solution: def longestPrefix(self, s: str) -> str: MOD = 1_000_000_007 ii = -1 p = 1 prefix = suffix = 0 for i in range(len(s)-1): prefix = (26*prefix + ord(s[i]) - 97) % MOD suffix = ((ord(s[~i]) - 97)*p + suffix) % MOD if prefix == suffix: ii = i p = 26 * p % MOD return s[:ii+1]
longest-happy-prefix
[Python3] one-line
ye15
0
53
longest happy prefix
1,392
0.45
Hard
20,926
https://leetcode.com/problems/longest-happy-prefix/discuss/1810048/Python-beginner-code
class Solution: def longestPrefix(self, s: str) -> str: lps = [0]*len(s) curr = 1 pre = 0 while curr < len(s): if s[curr] == s[pre]: lps[curr] = pre + 1 curr +=1 pre+=1 else: if pre == 0: lps[curr] = 0 curr += 1 else: pre = lps[pre-1] a = '' for i in range(lps[-1]): a += s[i] return a
longest-happy-prefix
Python beginner code
Umarabdullah101
-1
57
longest happy prefix
1,392
0.45
Hard
20,927
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2103066/PYTHON-or-Simple-python-solution
class Solution: def findLucky(self, arr: List[int]) -> int: charMap = {} for i in arr: charMap[i] = 1 + charMap.get(i, 0) res = [] for i in charMap: if charMap[i] == i: res.append(i) res = sorted(res) if len(res) > 0: return res[-1] return -1
find-lucky-integer-in-an-array
PYTHON | Simple python solution
shreeruparel
1
145
find lucky integer in an array
1,394
0.636
Easy
20,928
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1891347/Simple-python-solution
class Solution: def findLucky(self, arr: List[int]) -> int: ans=[] d={} for ar in arr: if ar in d: d[ar]+=1 else: d[ar]=1 for key in d: if key ==d[key]: ans.append(key) if len(ans)==0: return -1 return max(ans)
find-lucky-integer-in-an-array
Simple python solution
Buyanjargal
1
84
find lucky integer in an array
1,394
0.636
Easy
20,929
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1831818/1-Line-Python-Solution-oror-10-Fast-oror-Memory-less-than-99
class Solution: def findLucky(self, arr: List[int]) -> int: return max([a for a in arr if arr.count(a)==a], default=-1)
find-lucky-integer-in-an-array
1-Line Python Solution || 10% Fast || Memory less than 99%
Taha-C
1
71
find lucky integer in an array
1,394
0.636
Easy
20,930
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1139094/Python-pythonic-wo-counter
class Solution: def findLucky(self, arr: List[int]) -> int: dct = {} for i in arr: dct[i] = dct.get(i, 0) + 1 return max([key for key, value in dct.items() if key == value], default=-1)
find-lucky-integer-in-an-array
[Python] pythonic, w/o counter
cruim
1
110
find lucky integer in an array
1,394
0.636
Easy
20,931
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2841216/Simple-Python-solution-using-Counter
class Solution: def findLucky(self, arr: List[int]) -> int: count = Counter(arr) ans = float("-inf") for k,v in count.items(): if k == v: ans = max(ans,k) return -1 if ans == float("-inf") else ans
find-lucky-integer-in-an-array
Simple Python solution using Counter
aruj900
0
1
find lucky integer in an array
1,394
0.636
Easy
20,932
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2790597/simple-and-easy-to-understand-(dont-even-need-explaining)
class Solution: def findLucky(self, arr: List[int]) -> int: a=[] for i in arr: if arr.count(i)==i: a.append(i) return max(a) if len(a)>0 else -1
find-lucky-integer-in-an-array
simple and easy to understand (dont even need explaining)
neetikumar42
0
7
find lucky integer in an array
1,394
0.636
Easy
20,933
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2786762/PYTHON-100-EASY-TO-UNDERSTANDSIMPLECLEAN
class Solution: def findLucky(self, arr: List[int]) -> int: luckyNumbers = [] for i in range(len(arr)): if arr[i] == arr.count(arr[i]): luckyNumbers.append(arr[i]) if len(luckyNumbers) == 0: return -1 else: return max(luckyNumbers)
find-lucky-integer-in-an-array
🔥PYTHON 100% EASY TO UNDERSTAND/SIMPLE/CLEAN🔥
YuviGill
0
4
find lucky integer in an array
1,394
0.636
Easy
20,934
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2779696/Find-Lucky-Number
class Solution: def findLucky(self, arr: List[int]) -> int: dic = {} my_list = [] for i in arr: dic[i]=dic.get(i, 0)+1 for k,v in dic.items(): if k==v: my_list.append(k) ans = max(my_list, default=-1) return ans
find-lucky-integer-in-an-array
Find Lucky Number
kashifnawab5
0
1
find lucky integer in an array
1,394
0.636
Easy
20,935
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2665338/Easy-Python-Solution-Using-Dictionary
class Solution: def findLucky(self, arr: List[int]) -> int: dic={} for i in arr: if i not in dic: dic[i]=1 else: dic[i]+=1 res=-1 for k,v in dic.items(): if v==k: res=max(v,res) return res
find-lucky-integer-in-an-array
Easy Python Solution Using Dictionary
ankitr8055
0
5
find lucky integer in an array
1,394
0.636
Easy
20,936
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2613929/Python-dictionary
class Solution: def findLucky(self, arr: List[int]) -> int: freq = dict() for i in range(len(arr)): if arr[i] not in freq: freq[arr[i]] = 1 else: freq[arr[i]] += 1 # reverse sort based on keys for key in sorted(freq.keys(), reverse=True): if key == freq[key]: return key return -1
find-lucky-integer-in-an-array
Python dictionary
aj1904
0
7
find lucky integer in an array
1,394
0.636
Easy
20,937
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2380876/simple
class Solution: def findLucky(self, arr: List[int]) -> int: d = Counter(arr) ans = -1 for key,value in d.items(): if key == value: ans = max(ans, value) return ans
find-lucky-integer-in-an-array
simple
ellie_aa
0
29
find lucky integer in an array
1,394
0.636
Easy
20,938
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2342331/Easy-python-hashmap
class Solution: def findLucky(self, arr: List[int]) -> int: max_=-1 freq = {} for i in arr: if i in freq: freq[i]+=1 else: freq[i]=1 for i in freq: if freq[i]==i: max_=max(max_,i) return max_
find-lucky-integer-in-an-array
Easy python hashmap
sunakshi132
0
45
find lucky integer in an array
1,394
0.636
Easy
20,939
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2234531/Beginner-Friendly-Explanation-oror-68ms-Faster-Than-79-oror-Python
class Solution: def findLucky(self, arr: List[int]) -> int: # Set up a Python dictionary object to store each number and its number of occurrences. int_count = {} # Build our dictionary. for i in arr: if i not in int_count: int_count.setdefault(i, 1) else: int_count[i] += 1 # Now, we search for our lucky number. If no lucky number is found, return -1. lucky_number = -1 for num in int_count.items(): # dict_items() returns an iterable list of tuples. This iterable list is of the class 'dict_items' if num[0] == num[1] and lucky_number < num[0]: lucky_number = num[0] return lucky_number
find-lucky-integer-in-an-array
Beginner Friendly Explanation || 68ms, Faster Than 79% || Python
cool-huip
0
58
find lucky integer in an array
1,394
0.636
Easy
20,940
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2047559/Python-simple-solution
class Solution: def findLucky(self, arr: List[int]) -> int: ans = [] for i in arr: if arr.count(i) == i: ans.append(i) return max(ans) if ans else -1
find-lucky-integer-in-an-array
Python simple solution
StikS32
0
64
find lucky integer in an array
1,394
0.636
Easy
20,941
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/2036766/Python3-using-counter-and-max
class Solution: def findLucky(self, arr: List[int]) -> int: frequency = Counter(arr); lucky = [] for n in frequency: if n == frequency[n]: lucky.append(frequency[n]) return max(lucky) if len(lucky) > 0 else -1
find-lucky-integer-in-an-array
[Python3] using counter and max
Shiyinq
0
42
find lucky integer in an array
1,394
0.636
Easy
20,942
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1937040/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def findLucky(self, arr: List[int]) -> int: count = Counter(arr) op = [-1] for i,n in reversed(count.items()): if i == n: op.append(i) return max(op)
find-lucky-integer-in-an-array
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
58
find lucky integer in an array
1,394
0.636
Easy
20,943
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1936339/Python-One-Line-or-Counter-or-Dictionary-or-Set
class Solution: def findLucky(self, arr): counter = {} for n in arr: counter[n] = counter.get(n, 0) + 1 lucky = {key for key, value in counter.items() if key == value} return max(lucky,default=-1)
find-lucky-integer-in-an-array
Python - One-Line | Counter | Dictionary | Set
domthedeveloper
0
60
find lucky integer in an array
1,394
0.636
Easy
20,944
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1936339/Python-One-Line-or-Counter-or-Dictionary-or-Set
class Solution: def findLucky(self, arr): counter = Counter(arr) lucky = {key for key, value in counter.items() if key == value} return max(lucky,default=-1)
find-lucky-integer-in-an-array
Python - One-Line | Counter | Dictionary | Set
domthedeveloper
0
60
find lucky integer in an array
1,394
0.636
Easy
20,945
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1936339/Python-One-Line-or-Counter-or-Dictionary-or-Set
class Solution: def findLucky(self, arr): return max({k for k,v in Counter(arr).items() if k==v}, default=-1)
find-lucky-integer-in-an-array
Python - One-Line | Counter | Dictionary | Set
domthedeveloper
0
60
find lucky integer in an array
1,394
0.636
Easy
20,946
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1896209/Python-easy-solution-for-beginners-using-sorting
class Solution: def findLucky(self, arr: List[int]) -> int: for i in sorted(set(arr), reverse=True): if arr.count(i) == i: return i return -1
find-lucky-integer-in-an-array
Python easy solution for beginners using sorting
alishak1999
0
69
find lucky integer in an array
1,394
0.636
Easy
20,947
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1862862/Python-solution
class Solution: def findLucky(self, arr: List[int]) -> int: from collections import Counter count=Counter(arr) for i in sorted(count,reverse=True): if count[i]==i: return i return -1
find-lucky-integer-in-an-array
Python solution
g0urav
0
25
find lucky integer in an array
1,394
0.636
Easy
20,948
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1862447/Python-Solution-using-Counter
class Solution: def findLucky(self, arr: List[int]) -> int: arrCount = Counter(arr) target = [key for key in arrCount if key == arrCount[key]] return max(target) if len(target) >= 1 else -1
find-lucky-integer-in-an-array
Python Solution using Counter
White_Frost1984
0
20
find lucky integer in an array
1,394
0.636
Easy
20,949
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1801230/Beginner-Friendly-or-Python
class Solution: def findLucky(self, arr: List[int]) -> int: nums =[] #Empty list for storing lucky integers for num in arr: if arr.count(num) == num: # Main condition nums.append(num) # Add to nums if len(nums) == 0: # If there is no lucky integar in array return -1 return max(nums) # Return maximum amomg all lucky numbers in nums
find-lucky-integer-in-an-array
Beginner Friendly | Python
LittleMonster23
0
59
find lucky integer in an array
1,394
0.636
Easy
20,950
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1742997/Python-dollarolution
class Solution: def findLucky(self, arr: List[int]) -> int: for i in sorted(arr,reverse = True): if arr.count(i) == i: return i return -1
find-lucky-integer-in-an-array
Python $olution
AakRay
0
47
find lucky integer in an array
1,394
0.636
Easy
20,951
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1594976/Python-3-fast-easy-solution
class Solution: def findLucky(self, arr: List[int]) -> int: lucky = -1 counter = collections.Counter(arr) for num, freq in counter.items(): if num == freq and num > lucky: lucky = num return lucky
find-lucky-integer-in-an-array
Python 3 fast, easy solution
dereky4
0
106
find lucky integer in an array
1,394
0.636
Easy
20,952
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1544461/Easy-python-solution
class Solution: def findLucky(self, arr: List[int]) -> int: num_count = defaultdict(int) for num in arr: num_count[num] += 1 lucky_num = -1 for num, count in num_count.items(): if num == count: lucky_num = max(num, lucky_num) return lucky_num
find-lucky-integer-in-an-array
Easy python solution
akshaykumar19002
0
61
find lucky integer in an array
1,394
0.636
Easy
20,953
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1422478/O(N)-greater-O(1)-Space-Complexity-oror-Easy-to-Understand
class Solution: def findLucky(self, arr: List[int]) -> int: #lucky integer is an integer which has a frequency in the array #equal to its value #Approach 1 - Time Complexity - O(N) and Space Complexity - O(N) #We will create a dictionary #and store all the elements as "keys" in the dictionary and #their frequency as values. #Upon iterating over the dictionary, we will store all the elements #in a new array "answer" where key == value, as #they will be lucky numbers. Then, return max(answer) to get output. d = {} for i in arr: if i in d: d[i] += 1 else: d[i] = 1 answer = [] for key, value in d.items(): if key == value: answer.append(key) if len(answer) != 0: return max(answer) else: return -1 #Approach 2 - Time Complexity - O(N) and Space Complexity - O(1) #Same as above #Except declaring an array "answer", we will declare a "temp" variable #which will store the keys that satisfy the condition. #To make sure that only maximum key is stored in "temp", we will #apply a parameter as shown below. d = {} for i in arr: if i in d: d[i] += 1 else: d[i] = 1 temp = 0 for key, value in d.items(): if key == value: if key > temp: temp = key if temp != 0: return temp else: return -1
find-lucky-integer-in-an-array
O(N) --> O(1) Space Complexity || Easy to Understand
aarushsharmaa
0
84
find lucky integer in an array
1,394
0.636
Easy
20,954
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1388354/Python3-Memory-Less-Than-97.54-Faster-Than-87.49
class Solution: def findLucky(self, arr: List[int]) -> int: from collections import Counter c = Counter(arr) mx = 0 for key, val in c.items(): if key == val: mx = max(mx, key) return mx if mx != 0 else -1
find-lucky-integer-in-an-array
Python3 Memory Less Than 97.54%, Faster Than 87.49%
Hejita
0
37
find lucky integer in an array
1,394
0.636
Easy
20,955
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1256813/python-easy-solution
class Solution: def findLucky(self, arr: List[int]) -> int: res = -1 dic = collections.Counter(arr) for item in dic: if item == dic[item]: if item > res: res = item return res
find-lucky-integer-in-an-array
python - easy solution
jayesh_kaushik
0
79
find lucky integer in an array
1,394
0.636
Easy
20,956
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/1004488/python-3-solution
class Solution: def findLucky(self, arr: List[int]) -> int: dic = {} high = -1 for nums in arr: if nums in dic: dic[nums] += 1 else: dic[nums] = 1 for nums in arr: if dic[nums] == nums and nums > high: high = nums return high
find-lucky-integer-in-an-array
python 3 solution
EH1
0
52
find lucky integer in an array
1,394
0.636
Easy
20,957
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/937180/Python3%3A-Using-Counter
class Solution: def findLucky(self, arr: List[int]) -> int: counter = collections.Counter(arr) largest = [] for num in counter: if num == counter[num]: largest.append(num) if largest: return max(largest) else: return -1
find-lucky-integer-in-an-array
Python3: Using Counter
MakeTeaNotWar
0
76
find lucky integer in an array
1,394
0.636
Easy
20,958
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/859651/Python3-freq-table
class Solution: def findLucky(self, arr: List[int]) -> int: freq = {} for x in arr: freq[x] = 1 + freq.get(x, 0) return max((x for x in arr if x == freq[x]), default=-1)
find-lucky-integer-in-an-array
[Python3] freq table
ye15
0
53
find lucky integer in an array
1,394
0.636
Easy
20,959
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/714519/Easy-Python-counter-and-non-counter-solution
class Solution: def findLucky(self, arr: List[int]) -> int: freq = {} maxVal = -1 for i in arr: if i in freq: freq[i] += 1 else: freq[i] = 1 for i in arr: if freq[i] == i: maxVal = max(maxVal, freq[i]) return maxVal
find-lucky-integer-in-an-array
Easy Python counter and non-counter solution
alexr243
0
84
find lucky integer in an array
1,394
0.636
Easy
20,960
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/558683/python3-80-time-and-100-space
class Solution: def findLucky(self, arr: List[int]) -> int: viva = dict() ans = -1 for x in range(len(arr)): if arr[x] in viva: viva[arr[x]] += 1 else: viva[arr[x]] = 1 for key in sorted(list(viva.keys())): if key == viva[key]: ans = key return ans
find-lucky-integer-in-an-array
python3 80% time and 100% space
codexdelta
0
52
find lucky integer in an array
1,394
0.636
Easy
20,961
https://leetcode.com/problems/find-lucky-integer-in-an-array/discuss/554840/Python-3-Array-%2B-HashTable(Counting)
class Solution: def findLucky(self, arr: List[int]) -> int: ans = [0] * 501 res = -1 for i in range(len(arr)): ans[arr[i]] += 1 for i in range(1, 501): if ans[i] == i: res = max(res, i) return res
find-lucky-integer-in-an-array
[Python 3] Array + HashTable(Counting)
cr_496352127
0
43
find lucky integer in an array
1,394
0.636
Easy
20,962
https://leetcode.com/problems/count-number-of-teams/discuss/1465532/Python-or-O(n2)-or-Slow-but-very-easy-to-understand-or-Explanation
class Solution: def numTeams(self, rating: List[int]) -> int: dp = [[1, 0, 0] for i in range(len(rating))] for i in range(1, len(rating)): for j in range(i): if rating[i] > rating[j]: dp[i][1] += dp[j][0] dp[i][2] += dp[j][1] a = sum(dp[i][2] for i in range(len(dp))) #print(a) dp = [[1, 0, 0] for i in range(len(rating))] for i in range(1, len(rating)): for j in range(i): if rating[i] < rating[j]: dp[i][1] += dp[j][0] dp[i][2] += dp[j][1] b = sum(dp[i][2] for i in range(len(dp))) return a + b
count-number-of-teams
Python | O(n^2) | Slow but very easy to understand | Explanation
detective_dp
5
595
count number of teams
1,395
0.68
Medium
20,963
https://leetcode.com/problems/count-number-of-teams/discuss/555469/Python-O(n2)-sol-by-sliding-range-w-Diagram
class Solution: def numTeams(self, rating: List[int]) -> int: r, size = rating, len( rating ) # compute statistics of sliding range left_smaller = [ sum( r[i] < r[j] for i in range(0,j) ) for j in range(size) ] right_bigger = [ sum( r[j] < r[k] for k in range(j+1, size) ) for j in range(size)] num_of_teams = 0 # j slides from 0 to ( n-1 ), and j stands for the index of middle element for j in range( 0, size): num_of_ascending_team = left_smaller[j] * right_bigger[j] num_of_descending_team = ( j - left_smaller[j] ) * ( size-1 - j - right_bigger[j] ) num_of_teams += ( num_of_ascending_team + num_of_descending_team ) return num_of_teams
count-number-of-teams
Python O(n^2) sol by sliding range [w/ Diagram]
brianchiang_tw
4
583
count number of teams
1,395
0.68
Medium
20,964
https://leetcode.com/problems/count-number-of-teams/discuss/555469/Python-O(n2)-sol-by-sliding-range-w-Diagram
class Solution: def numTeams(self, rating: List[int]) -> int: r, size = rating, len( rating ) # compute statistics of sliding range left_smaller = [ sum( r[i] < r[j] for i in range(0,j) ) for j in range(size) ] right_bigger = [ sum( r[j] < r[k] for k in range(j+1, size) ) for j in range(size)] return sum( left_smaller[j] * right_bigger[j] + ( j - left_smaller[j] ) * ( size-1 - j - right_bigger[j] ) for j in range( 0, size) )
count-number-of-teams
Python O(n^2) sol by sliding range [w/ Diagram]
brianchiang_tw
4
583
count number of teams
1,395
0.68
Medium
20,965
https://leetcode.com/problems/count-number-of-teams/discuss/1079864/Python3-dp
class Solution: def numTeams(self, rating: List[int]) -> int: ans = 0 seen = [[0]*2 for _ in rating] for i in range(len(rating)): for ii in range(i): if rating[ii] < rating[i]: ans += seen[ii][0] seen[i][0] += 1 elif rating[ii] > rating[i]: ans += seen[ii][1] seen[i][1] += 1 return ans
count-number-of-teams
[Python3] dp
ye15
1
309
count number of teams
1,395
0.68
Medium
20,966
https://leetcode.com/problems/count-number-of-teams/discuss/2794657/DP-python-solution-O(n2)
class Solution: def numTeams(self, rating: List[int]) -> int: N = len(rating) dp = [[0] * N for _ in range(3)] for i in range(N): dp[0][i] = 1 for i in range(1, 3): for j in range(i, N): for k in range(i - 1, j): if rating[j] > rating[k]: dp[i][j] += dp[i - 1][k] r2 = rating[::-1] dp2 = [[0] * N for _ in range(3)] for i in range(N): dp2[0][i] = 1 for i in range(1, 3): for j in range(i, N): for k in range(i - 1, j): if r2[j] > r2[k]: dp2[i][j] += dp2[i - 1][k] return sum(dp[-1]) + sum(dp2[-1])
count-number-of-teams
DP / python solution / O(n^2)
Lara_Craft
0
8
count number of teams
1,395
0.68
Medium
20,967
https://leetcode.com/problems/count-number-of-teams/discuss/1816368/Pyhton-or-O(n2)-or-Easy-solution
class Solution: def getValueCount(self,value,rating, isSmallerCount = False): count = 0 for t in rating: if isSmallerCount: if t > value: count += 1 else: if t < value: count += 1 return count def numTeams(self, rating: List[int]) -> int: ans = 0 for i in range(1,len(rating)): ans +=self.getValueCount(rating[i],rating[0:i], True) * self.getValueCount(rating[i],rating[i:]) ans +=self.getValueCount(rating[i],rating[0:i]) * self.getValueCount(rating[i],rating[i:], True) return ans
count-number-of-teams
Pyhton | O(n^2) | Easy solution
iamamiya
0
188
count number of teams
1,395
0.68
Medium
20,968
https://leetcode.com/problems/count-number-of-teams/discuss/1588488/Python3-O(n3)-and-O(n2)-solution
class Solution: def numTeams(self, rating: List[int]) -> int: res = 0 for i in range(len(rating) - 2): for j in range(i + 1, len(rating) - 1): for k in range(j + 1, len(rating)): if rating[i] > rating[j] and rating[j] > rating[k] or rating[i] < rating[j] and rating[j] < rating[k]: res += 1 return res class Solution: def numTeams(self, rating: List[int]) -> int: down2up = collections.defaultdict(int) up2down = collections.defaultdict(int) for i in range(len(rating) - 1): for j in range(i + 1, len(rating)): if rating[i] < rating[j]: down2up[i] += 1 else: up2down[j] += 1 res = 0 for i in range(len(rating) - 1): for j in range(i + 1, len(rating)): if rating[i] < rating[j]: res += down2up[j] else: res += up2down[i] return res
count-number-of-teams
[Python3] O(n^3) and O(n^2) solution
maosipov11
0
156
count number of teams
1,395
0.68
Medium
20,969
https://leetcode.com/problems/count-number-of-teams/discuss/695427/Python-3-C-Brute-Force
class Solution: def numTeams(self, rating: List[int]) -> int: count = 0 for i in range(len(rating)): for j in range(i+1,len(rating)): for k in range(j+1,len(rating)): if((rating[i]>rating[j] and rating[j]>rating[k]) or (rating[i]<rating[j] and rating[j]<rating[k])): count += 1 return count
count-number-of-teams
[Python 3 / C] Brute Force
abhijeetmallick29
0
95
count number of teams
1,395
0.68
Medium
20,970
https://leetcode.com/problems/count-number-of-teams/discuss/658043/python3-Brute-Force-solution-memory-100
class Solution: def numTeams(self, rating: List[int]) -> int: if len(rating) < 3: return 0 else: count = 0 for i in range(len(rating)-2): arr = [] arr.append(rating[i]) for j in range(i+1,len(rating)-1): curelmt = rating[i] if rating[j] > curelmt: # Increasing element curelmt = rating[j] arr.append(rating[j]) for k in range(j+1,len(rating)): if rating[k] > curelmt: arr.append(rating[k]) if len(arr) == 3: count += 1 arr.pop(-1) elif rating[j] < curelmt: # Decreasing element curelmt = rating[j] arr.append(rating[j]) for k in range(j+1, len(rating)): if rating[k] < curelmt: arr.append(rating[k]) if len(arr) == 3: count += 1 arr.pop(-1) arr.pop(-1) return count
count-number-of-teams
python3 Brute Force solution, memory 100%
zharfanf
0
139
count number of teams
1,395
0.68
Medium
20,971
https://leetcode.com/problems/count-number-of-teams/discuss/591389/Intuitive-recursive-approach
class Solution: def numTeams(self, rating: List[int]) -> int: teams = [0] def up(n1, n2): return n2 > n1 def down(n1, n2): return n2 < n1 def search_team(team, np=0, op=up): for i in range(np, len(rating)): nv = rating[i] if not team or op(team[-1], nv): team_copy = [] team_copy.extend(team) team_copy.append(nv) if len(team_copy) < 3: search_team(team_copy, np=i, op=op) else: teams[0] += 1 # Search team in up rating search_team([]) # Search team in down rating search_team([], op=down) return teams[0]
count-number-of-teams
Intuitive recursive approach
puremonkey2001
0
146
count number of teams
1,395
0.68
Medium
20,972
https://leetcode.com/problems/find-all-good-strings/discuss/1133347/Python3-dp-and-kmp-...-finally
class Solution: def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int: lps = [0] k = 0 for i in range(1, len(evil)): while k and evil[k] != evil[i]: k = lps[k-1] if evil[k] == evil[i]: k += 1 lps.append(k) @cache def fn(i, k, lower, upper): """Return number of good strings at position i and k prefix match.""" if k == len(evil): return 0 # boundary condition if i == n: return 1 lo = ascii_lowercase.index(s1[i]) if lower else 0 hi = ascii_lowercase.index(s2[i]) if upper else 25 ans = 0 for x in range(lo, hi+1): kk = k while kk and evil[kk] != ascii_lowercase[x]: kk = lps[kk-1] if evil[kk] == ascii_lowercase[x]: kk += 1 ans += fn(i+1, kk, lower and x == lo, upper and x == hi) return ans return fn(0, 0, True, True) % 1_000_000_007
find-all-good-strings
[Python3] dp & kmp ... finally
ye15
3
351
find all good strings
1,397
0.422
Hard
20,973
https://leetcode.com/problems/find-all-good-strings/discuss/2834604/Faster-and-less-memory-than-all-other-solutions
class Solution: def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int: p=10**9+7 ord_a=ord('a') l=len(evil) dup=[] for i in range(1, l): if evil[i:]==evil[:l-i]: dup.append(i) lend=len(dup) def count(s): tmp_ct=0 bd=1 ind=n without_s=[] for i in range(n): tmp_ct*=26 if bd: tmp_ct+=ord(s[i])-ord_a if i>=l-1 and ind>i-l and evil<s[i-l+1:i+1]: tmp_ct-=1 if i>=l: tmp_ct-=without_s[i-l] if i>=l-1: if s[i-l+1:i+1]==evil: bd=0 ind=i tmp_ct%=p tmp_with_s=0 for j in range(lend): d=dup[j] if i>=d: tmp_with_s+=without_s[i-d] if i>=d-1 and ind>i-d and evil[:d]<s[i-d+1:i+1]: tmp_with_s+=1 without_s.append(tmp_ct-tmp_with_s) return tmp_ct, bd str_ct1, bd1=count(s1) str_ct2, bd2=count(s2) return (str_ct2-str_ct1+bd2)%p
find-all-good-strings
Faster and less memory than all other solutions
mbeceanu
0
5
find all good strings
1,397
0.422
Hard
20,974
https://leetcode.com/problems/count-largest-group/discuss/660765/Python-DP-O(N)-99100
class Solution: def countLargestGroup(self, n: int) -> int: dp = {0: 0} counts = [0] * (4 * 9) for i in range(1, n + 1): quotient, reminder = divmod(i, 10) dp[i] = reminder + dp[quotient] counts[dp[i] - 1] += 1 return counts.count(max(counts))
count-largest-group
Python DP O(N) 99%/100%
xshoan
43
2,000
count largest group
1,399
0.672
Easy
20,975
https://leetcode.com/problems/count-largest-group/discuss/611971/Python-easy-to-understand
class Solution(object): def countLargestGroup(self, n): """ :type n: int :rtype: int """ res = [] for i in range(1, n+1): res.append(sum(int(x) for x in str(i))) c = collections.Counter(res) x = [i for i in c.values() if i == max(c.values())] return len(x)
count-largest-group
Python easy to understand
goodfine1210
5
533
count largest group
1,399
0.672
Easy
20,976
https://leetcode.com/problems/count-largest-group/discuss/1817885/Python-Easy-Solution
class Solution: def countLargestGroup(self, n: int) -> int: def val(): return 0 dic = defaultdict(val) for i in range(1,n+1): st = str(i) ans = 0 for j in st: ans += int(j) dic[ans] += 1 maximum = max(dic.values()) ans = 0 for i in dic.keys(): if dic[i] == maximum: ans += 1 return ans
count-largest-group
Python Easy Solution
MS1301
1
79
count largest group
1,399
0.672
Easy
20,977
https://leetcode.com/problems/count-largest-group/discuss/1195100/Python-Line-by-Line-Explanations-with-comments(-Fast-and-easy)
class Solution: def countLargestGroup(self, n: int) -> int: #Function to get sum of digits def get_sum(value): res = 0 while value: res += (value % 10) value //= 10 return res #Dictionary to group values with same sum eg - [1,10] having sum 1 #So, in d it will be stored as {1:2},i,e, 1 occuring 2 times as sum d = {} #Looping &amp; storing values in d for i in range(1,n+1): temp = get_sum(i) if temp not in d: d[temp] = 1 else: d[temp] += 1 #Finding the maximum size of group maxSize = max(d.values()) ans = 0 #Finding how many groups are there with maxSize groups for i in d: if d[i] == maxSize: ans+=1 #Returning ans return ans
count-largest-group
Python Line by Line Explanations with comments( Fast and easy)
iamkshitij77
1
213
count largest group
1,399
0.672
Easy
20,978
https://leetcode.com/problems/count-largest-group/discuss/2823584/Simple-python-solution-using-map
class Solution: def countLargestGroup(self, n: int) -> int: def sum_digits(num): sum1 = 0 while num>0: sum1+=num%10 num//=10 return sum1 map1 = {} maxi = 0 count = 0 for i in range(1, n+1): s = sum_digits(i) if s in map1: map1[s]+=1 if map1[s]>maxi: maxi = map1[s] else: map1[s]=1 if map1[s]>maxi: maxi = map1[s] for elem in map1: if map1[elem]==maxi: count+=1 print(map1) return count
count-largest-group
Simple python solution using map
Rajeev_varma008
0
4
count largest group
1,399
0.672
Easy
20,979
https://leetcode.com/problems/count-largest-group/discuss/2307609/Python3-simple-solution
class Solution: def countLargestGroup(self, n: int) -> int: arr = [] for i in range(1, n + 1): x = i t = 0 while x > 0: t = t + (x % 10) x = x // 10 arr.append(t) return len(multimode(arr))
count-largest-group
Python3 simple solution
mediocre-coder
0
35
count largest group
1,399
0.672
Easy
20,980
https://leetcode.com/problems/count-largest-group/discuss/2114233/Easy-Python-code
class Solution: def countLargestGroup(self, n: int) -> int: dic = {} for i in range(1,n+1): sum1 = 0 for num in str(i): sum1 = sum1 + int(num) if sum1 in dic: dic[sum1] += 1 else: dic[sum1] = 1 b = list(dic.values()) a = max(b) return b.count(a)
count-largest-group
Easy Python code
prernaarora221
0
94
count largest group
1,399
0.672
Easy
20,981
https://leetcode.com/problems/count-largest-group/discuss/2022156/Python-Clean-and-Simple!
class Solution: def countLargestGroup(self, n): counts = Counter() for i in range(1,n+1): digitSum = sum(int(c) for c in str(i)) counts[digitSum] += 1 maxCount = max(counts.values()) return sum(count == maxCount for count in counts.values())
count-largest-group
Python - Clean and Simple!
domthedeveloper
0
106
count largest group
1,399
0.672
Easy
20,982
https://leetcode.com/problems/count-largest-group/discuss/1875539/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def countLargestGroup(self, n: int) -> int: dict = {} output = 0 for i in range(1, n+1): count = 0 if len(str(i)) == 1: if str(i) not in dict: dict[i] = 1 else: dict[i]+=1 else: for j in str(i): count+=int(j) if count in dict: dict[count]+=1 else: dict[count]=1 for i, n in dict.items(): if n == max(dict.values()): output += 1 return output
count-largest-group
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
96
count largest group
1,399
0.672
Easy
20,983
https://leetcode.com/problems/count-largest-group/discuss/1850004/3-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-85
class Solution: def countLargestGroup(self, n: int) -> int: ans=dict.fromkeys([i for i in range(1,37)], 0) for i in range(1,n+1): s=sum([int(x) for x in str(i)]) ; ans[s]+=1 return max(Counter([y for x,y in ans.items()]).items(), key=itemgetter(0))[1]
count-largest-group
3-Lines Python Solution || 60% Faster || Memory less than 85%
Taha-C
0
86
count largest group
1,399
0.672
Easy
20,984
https://leetcode.com/problems/count-largest-group/discuss/1850004/3-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-85
class Solution: def countLargestGroup(self, n: int) -> int: ans=dict.fromkeys([i for i in range(1,37)], 0) for i in range(1,n+1): ans[sum([int(x) for x in str(i)])]+=1 return sum(1 for x in ans.values() if x==max(ans.values()))
count-largest-group
3-Lines Python Solution || 60% Faster || Memory less than 85%
Taha-C
0
86
count largest group
1,399
0.672
Easy
20,985
https://leetcode.com/problems/count-largest-group/discuss/1850004/3-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-85
class Solution: def countLargestGroup(self, n: int) -> int: ans=[] for i in range(1,n+1): ans.append(sum(int(x) for x in str(i))) C=Counter(ans).values() ; mx=max(C) return sum([1 for x in C if x==mx])
count-largest-group
3-Lines Python Solution || 60% Faster || Memory less than 85%
Taha-C
0
86
count largest group
1,399
0.672
Easy
20,986
https://leetcode.com/problems/count-largest-group/discuss/1799926/Python3-accepted-solution-(using-defaultdict)
class Solution: from collections import defaultdict def countLargestGroup(self, n: int) -> int: memo = defaultdict(list) for i in range(1,n+1): memo[sum([int(i) for i in str(i)])].append(i) li = sorted([memo[i] for i in memo.keys()],key=len) return len([j for j in li if(len(j)==len(li[-1]))])
count-largest-group
Python3 accepted solution (using defaultdict)
sreeleetcode19
0
20
count largest group
1,399
0.672
Easy
20,987
https://leetcode.com/problems/count-largest-group/discuss/1743036/Python-dollarolution-(mem-use%3A-less-than-100)
class Solution: def countLargestGroup(self, n: int) -> int: d, count, m = {}, 0, 0 for i in range(1,n+1): c, j = 0, i while j != 0: c += j%10 j //= 10 if c not in d: d[c] = 1 else: d[c] += 1 if d[c] > m: m = d[c] count = 1 elif m == d[c]: count += 1 return count
count-largest-group
Python $olution (mem use: less than 100%)
AakRay
0
71
count largest group
1,399
0.672
Easy
20,988
https://leetcode.com/problems/count-largest-group/discuss/1368137/Python3-solution
class Solution: def countLargestGroup(self, n: int) -> int: count = 0 d = {} # we need a dictionary to store the values def sum_digits(n): n_sum = 0 while n > 0: r = n % 10 n_sum += r n //= 10 return n_sum for i in range(1,n+1): my_sum = sum_digits(i) if my_sum not in d: d[my_sum] = 1 else: d[my_sum] += 1 my_values = list(d.values()) my_max = max(my_values) # we get the largest group of integers for i in my_values: if my_max == i: count += 1 # counting the largest groups return count
count-largest-group
Python3 solution
FlorinnC1
0
94
count largest group
1,399
0.672
Easy
20,989
https://leetcode.com/problems/count-largest-group/discuss/1158988/python-with-comments
class Solution: def countLargestGroup(self, n: int) -> int: # calculating sum of digits of given num def sod(num): dsum = 0 while num > 0: num, mod = divmod(num, 10) dsum += mod return dsum res = 0 # making a dictionary to store number as list # key: sum of digits(0~9), values: numbers d = collections.defaultdict(list) for i in range(1,n+1): dsum = sod(i) d[dsum].append(i) maxlen = 0 for v in d.values(): maxlen = max(maxlen, len(v)) for v in d.values(): if len(v) == maxlen: res += 1 return res
count-largest-group
python with comments
keewook2
0
119
count largest group
1,399
0.672
Easy
20,990
https://leetcode.com/problems/count-largest-group/discuss/1142188/Python-Solution-100-space-optimize
class Solution: def countLargestGroup(self, n: int) -> int: arr = [0] * (10**4+1) for x in range(1,n+1): temp = x s = 0 while temp: s+= temp%10 temp //= 10 arr[s] += 1 return arr.count(max(arr))
count-largest-group
Python Solution 100% space optimize
Sanjaychandak95
0
125
count largest group
1,399
0.672
Easy
20,991
https://leetcode.com/problems/count-largest-group/discuss/1067026/Python3-simple-solution-using-two-different-approaches
class Solution: def countLargestGroup(self, n: int) -> int: d = {} for i in range(1,n+1): x = sum(list(map(int, list(str(i))))) d[x] = d.get(x,[]) + [i] l = sorted(d.values(), key= lambda x:len(x), reverse = True) max = len(l[0]) count = 0 for i in l: if len(i) == max: count += 1 return count
count-largest-group
Python3 simple solution using two different approaches
EklavyaJoshi
0
74
count largest group
1,399
0.672
Easy
20,992
https://leetcode.com/problems/count-largest-group/discuss/1067026/Python3-simple-solution-using-two-different-approaches
class Solution: def countLargestGroup(self, n: int) -> int: d = {} for i in range(1,n+1): x = sum(list(map(int, list(str(i))))) d[x] = d.get(x,[]) + [i] max_len = 0 count = 1 for i in d.values(): if len(i) == max_len: count += 1 elif len(i) > max_len: count = 1 max_len = len(i) return count
count-largest-group
Python3 simple solution using two different approaches
EklavyaJoshi
0
74
count largest group
1,399
0.672
Easy
20,993
https://leetcode.com/problems/count-largest-group/discuss/859672/Python3-frequency-table
class Solution: def countLargestGroup(self, n: int) -> int: freq = {} for x in range(1, n+1): key = sum(int(d) for d in str(x)) freq[key] = 1 + freq.get(key, 0) vals = list(freq.values()) return vals.count(max(vals))
count-largest-group
[Python3] frequency table
ye15
0
93
count largest group
1,399
0.672
Easy
20,994
https://leetcode.com/problems/construct-k-palindrome-strings/discuss/1806250/Python-Solution
class Solution: def canConstruct(self, s: str, k: int) -> bool: if k > len(s): return False dic = {} for i in s: if i not in dic: dic[i] = 1 else: dic[i] += 1 c = 0 for i in dic.values(): if i % 2 == 1: c += 1 if c > k: return False return True
construct-k-palindrome-strings
Python Solution
MS1301
1
91
construct k palindrome strings
1,400
0.632
Medium
20,995
https://leetcode.com/problems/construct-k-palindrome-strings/discuss/1079932/Python3-freq-table
class Solution: def canConstruct(self, s: str, k: int) -> bool: freq = {} for c in s: freq[c] = 1 + freq.get(c, 0) return sum(freq[c]&amp;1 for c in freq) <= k <= len(s)
construct-k-palindrome-strings
[Python3] freq table
ye15
1
97
construct k palindrome strings
1,400
0.632
Medium
20,996
https://leetcode.com/problems/construct-k-palindrome-strings/discuss/2814817/C%2B%2B-Python3-Solution-or-XOR
class Solution: def canConstruct(self, S, K): return bin(reduce(operator.xor, map(lambda x: 1 << (ord(x) - 97), S))).count('1') <= K <= len(S)
construct-k-palindrome-strings
✔ [C++ / Python3] Solution | XOR
satyam2001
0
3
construct k palindrome strings
1,400
0.632
Medium
20,997
https://leetcode.com/problems/construct-k-palindrome-strings/discuss/2761235/Python-3-Solution
class Solution: def canConstruct(self, s: str, k: int) -> bool: D = defaultdict(int) for x in s: D[x] = (D[x] + 1)%2 if sum(list(D.values())) <= k and len(s) >= k: return True return False
construct-k-palindrome-strings
Python 3 Solution
mati44
0
4
construct k palindrome strings
1,400
0.632
Medium
20,998
https://leetcode.com/problems/construct-k-palindrome-strings/discuss/2413366/easy-python-solution
class Solution: def canConstruct(self, s: str, k: int) -> bool: char = [i for i in s] if len(char) < k : return False else : char_dic, odd_count = {}, 0 for ch in set(char) : char_dic[ch] = char.count(ch) if (char.count(ch))%2 != 0 : odd_count += 1 if odd_count > k : return False else : return True
construct-k-palindrome-strings
easy python solution
sghorai
0
55
construct k palindrome strings
1,400
0.632
Medium
20,999