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/search-suggestions-system/discuss/2168566/python-3-or-simple-two-pointer-solution
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: products.sort() left, right = 0, len(products) - 1 res = [] for i, c in enumerate(searchWord): while left <= right and (len(products[left]) <= i or products[left...
search-suggestions-system
python 3 | simple two pointer solution
dereky4
0
27
search suggestions system
1,268
0.665
Medium
18,900
https://leetcode.com/problems/search-suggestions-system/discuss/2156626/python-optimize-of-Brute-Fore-similar-to-trie
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: products.sort() lookup = defaultdict(list) for prod in products: for i in range(1,len(prod)+1): lookup[prod[:i]].append(prod) res = [] fo...
search-suggestions-system
python optimize of Brute Fore, similar to trie
hardernharder
0
51
search suggestions system
1,268
0.665
Medium
18,901
https://leetcode.com/problems/search-suggestions-system/discuss/2079025/Python3-solution-faster-than-79.04-and-consumes-less-memory-than-84.48-of-submissions
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: prducts = list(map(lambda x:x.lower(),products)) searchWord = searchWord.lower() realtime_keyword = '' found_products = products output= [] for lett...
search-suggestions-system
Python3 solution faster than %79.04 and consumes less memory than %84.48 of submissions
aliie62
0
77
search suggestions system
1,268
0.665
Medium
18,902
https://leetcode.com/problems/search-suggestions-system/discuss/2017188/Python3-Easy
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: products.sort() self.products = products self.searchWord = searchWord return self.findTheProducts() def findTheProducts(self): char_list = list(self.searchWord) c...
search-suggestions-system
Python3 Easy
MicroRay
0
67
search suggestions system
1,268
0.665
Medium
18,903
https://leetcode.com/problems/search-suggestions-system/discuss/1612917/Python3-Solution-with-using-two-pointers-and-sorting
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: products.sort() begin = 0 end = len(products) - 1 max_returns_cnt = 3 res = [] for i in range(len(searchWord)): while begin <= end and (i > len(products...
search-suggestions-system
[Python3] Solution with using two pointers and sorting
maosipov11
0
228
search suggestions system
1,268
0.665
Medium
18,904
https://leetcode.com/problems/search-suggestions-system/discuss/1243439/python3-Trie-solution-for-reference.
class Solution: def suggestedProducts(self, products, searchWord: str): trie = {'#': []} limit = 3 # sort products so that the lexicograph is resumed. products.sort() # insert the products into the trie with ending as '#' # store in trie such that... ...
search-suggestions-system
[python3] Trie solution for reference.
vadhri_venkat
0
79
search suggestions system
1,268
0.665
Medium
18,905
https://leetcode.com/problems/search-suggestions-system/discuss/1243172/Python-3-Trie-approach
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: trie = {} # build trie for product in products: node = trie for p in product: node = node.setdefault(p, {}) node['*'] = product ...
search-suggestions-system
[Python 3] Trie approach
chestnut890123
0
121
search suggestions system
1,268
0.665
Medium
18,906
https://leetcode.com/problems/search-suggestions-system/discuss/1189743/simple-of-simple
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: COUNT = 3 products = sorted(products) l = len(searchWord) result = [] for i in range(l): s = searchWord[:i+1] t = [] ...
search-suggestions-system
simple of simple
seunggabi
0
52
search suggestions system
1,268
0.665
Medium
18,907
https://leetcode.com/problems/search-suggestions-system/discuss/923918/Simple-Beginner-Friendly-Python-Solution
class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: ans = [] n = len(products) now = products #iterate through length of searchWord for i in range(len(searchWord)): temp = [] #iter...
search-suggestions-system
Simple -Beginner Friendly Python Solution
SaSha59
0
138
search suggestions system
1,268
0.665
Medium
18,908
https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/discuss/2488667/LeetCode-The-Hard-Way-DP-with-Explanation
class Solution: def numWays(self, steps: int, arrLen: int) -> int: M = 10 ** 9 + 7 @lru_cache(None) def dfs(pos, steps): # if we walk outside the array or use all the steps # then return 0 if pos < 0 or pos > steps or pos > arrLen - 1: return 0 ...
number-of-ways-to-stay-in-the-same-place-after-some-steps
[LeetCode The Hard Way] DP with Explanation
wingkwong
1
94
number of ways to stay in the same place after some steps
1,269
0.436
Hard
18,909
https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/discuss/2786758/Python-Memo-EASY-TO-UNDERSTAND
class Solution: def numWays(self, steps: int, arrLen: int) -> int: MOD = (10**9)+7 @cache def dfs(steps,index): if steps == 0 and index == 0: return 1 if steps == 0: return 0 goLeft = 0 goRight = 0 ...
number-of-ways-to-stay-in-the-same-place-after-some-steps
Python Memo EASY TO UNDERSTAND
tupsr
0
2
number of ways to stay in the same place after some steps
1,269
0.436
Hard
18,910
https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/discuss/2422581/Python-3-dp
class Solution: def numWays(self, steps: int, arrLen: int) -> int: mod=10**9+7 to_consider=min(arrLen,steps//2+1) dp=[1]+[0]*(to_consider-1) for i in range(steps): new_dp=[0]*to_consider for j in range(to_consider): new_dp[j]=(dp[j]+(dp[j+1] if j+1<to_consider else 0)+(dp[j-1] if j-1>=0 else 0))%mod ...
number-of-ways-to-stay-in-the-same-place-after-some-steps
[Python 3] dp
gabhay
0
15
number of ways to stay in the same place after some steps
1,269
0.436
Hard
18,911
https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/discuss/437749/Python3-dynamic-programming-(98.45)-with-explanation
class Solution: def numWays(self, steps: int, arrLen: int) -> int: n = min(arrLen, steps//2+1) #reachable right end ways = [1] + [0]*(n-1) for step in range(steps): ways = [sum(ways[max(0,i-1):i+2]) % (10**9+7) for i in range(min(step+2,steps-step,n))] return ways[0]
number-of-ways-to-stay-in-the-same-place-after-some-steps
Python3 dynamic programming (98.45%) with explanation
ye15
0
86
number of ways to stay in the same place after some steps
1,269
0.436
Hard
18,912
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1767406/Python-3-solution-with-comments
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: # keep track of the "net score" of each row/col/diagonal # player A adds 1 to the "net score" of each row/col/diagonal they play in, # player B subtracts 1 # scores[0], scores[1] and scores[2] are for rows 0, 1 and 2...
find-winner-on-a-tic-tac-toe-game
Python 3 solution with comments
dereky4
15
871
find winner on a tic tac toe game
1,275
0.543
Easy
18,913
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/441452/Python3-memo-scores
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: score = [[0]*8 for _ in range(2)] for p, (i, j) in enumerate(moves): p %= 2 score[p][i] += 1 score[p][3+j] += 1 if i == j: score[p][6] += 1 if i+j == 2: score[p][7...
find-winner-on-a-tic-tac-toe-game
[Python3] memo scores
ye15
15
1,300
find winner on a tic tac toe game
1,275
0.543
Easy
18,914
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/441652/Python-3-(four-lines)-(beats-100)-(24-ms)-(With-Explanation)
class Solution: def tictactoe(self, M: List[List[int]]) -> str: L, P, [x,y], N = len(M), 1 - len(M) % 2, M[-1], [M[::2], M[1::2]] if all(p in N[P] for p in [[x,0],[x,1],[x,2]]) or all(p in N[P] for p in [[0,y],[1,y],[2,y]]): return ['A','B'][P] if all(p in N[P] for p in [[0,0],[1,1],[2,2]]) ...
find-winner-on-a-tic-tac-toe-game
Python 3 (four lines) (beats 100%) (24 ms) (With Explanation)
junaidmansuri
3
1,000
find winner on a tic tac toe game
1,275
0.543
Easy
18,915
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1442163/Using-issubset()-98-speed
class Solution: wins = [{(0, 0), (0, 1), (0, 2)}, {(1, 0), (1, 1), (1, 2)}, {(2, 0), (2, 1), (2, 2)}, {(0, 0), (1, 0), (2, 0)}, {(0, 1), (1, 1), (2, 1)}, {(0, 2), (1, 2), (2, 2)}, {(0, 0), (1, 1), (2, 2)}, {(2, 0), (1, 1), (0, 2)}] def tictactoe(self, moves: List[List[int]])...
find-winner-on-a-tic-tac-toe-game
Using issubset(), 98% speed
EvgenySH
2
244
find winner on a tic tac toe game
1,275
0.543
Easy
18,916
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1947959/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: game = [['','',''],['','',''],['','','']] a = 0 for i,j in moves: if a%2 == 0: game[i][j] = 'A' a+=1 else: game[i][j] = 'B' a+=1 ...
find-winner-on-a-tic-tac-toe-game
Python (Simple Approach and Beginner-Friendly)
vishvavariya
1
172
find winner on a tic tac toe game
1,275
0.543
Easy
18,917
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1836738/7-lines-Python-Solution-oror-93-Faster-oror-Memory-less-than-60
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: wins= [ [[0,0],[1,1],[2,2]], [[0,2],[1,1],[2,0]], [[0,0],[1,0],[2,0]], [[0,1],[1,1],[2,1]], [[0,2],[1,2],[2,2]], [[0,0],[0,1],[0,2]], [[1,0],[1,1],[1,2]], [[2,0],[2,1],[2,2]] ] MA=[moves[i] for i in range(0,len(moves),2)] ; MB=[move...
find-winner-on-a-tic-tac-toe-game
7-lines Python Solution || 93% Faster || Memory less than 60%
Taha-C
1
229
find winner on a tic tac toe game
1,275
0.543
Easy
18,918
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1264367/Generalize-approach-work-with-%22n%22-square-board
class Solution: player = { "A" : 1, "B" : 2, } def __get_player_code(self, player:str)->int: return self.player.get(player) def win_player(self, board : List[List[str]], player : str) -> bool: return self.__win_row_wise(board, player) or self.__win_column_wise(board,...
find-winner-on-a-tic-tac-toe-game
Generalize approach work with "n" square board
Sanjaychandak95
1
218
find winner on a tic tac toe game
1,275
0.543
Easy
18,919
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1216138/Pyhton-or-Faster-than-89.19-or-Easy-Solution
class Solution(object): def tictactoe(self, moves): if (len(moves)<5): return "Pending" c1=0 c2=0 a=[[0 for i in range(3)]for j in range(3)] for i in range (len(moves)): for j in range (len(moves[0])-1): if i==0 or i%2==0: ...
find-winner-on-a-tic-tac-toe-game
Pyhton | Faster than 89.19% | Easy Solution
divyadharshini12
1
194
find winner on a tic tac toe game
1,275
0.543
Easy
18,920
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/717528/Python-3-brute-force-(easy-to-understand)
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: counter = 0 arr = [["" for i in range(3)] for j in range(3)] def helper(): diag = [ row[i] for i,row in enumerate(arr) ] other_diag = [ row[-i-1] for i,row in e...
find-winner-on-a-tic-tac-toe-game
Python 3 brute-force (easy to understand)
ermolushka2
1
132
find winner on a tic tac toe game
1,275
0.543
Easy
18,921
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2818822/Python-bitmanipulations
class Solution: def tictactoe(self, moves) -> str: # 1, 2, 4 # 8, 16, 32 # 64, 128, 256 a = 0 b = 0 flag = True counter = 0 def isWinner(value): for pattern in list([(1 + 2 + 4), (8 + 16 + 32), (64 + 128 + 256), (1 + 8 + 6...
find-winner-on-a-tic-tac-toe-game
Python, bitmanipulations
swepln
0
3
find winner on a tic tac toe game
1,275
0.543
Easy
18,922
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2805970/tic-tac-toe
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: grid = [ [None] * 3 for i in range(3)] for i in range(len(moves)): move = moves[i] if i % 2 == 0: grid[move[0]][move[1]] = 'X' if self.won(grid) == True: re...
find-winner-on-a-tic-tac-toe-game
tic tac toe
user4363G
0
3
find winner on a tic tac toe game
1,275
0.543
Easy
18,923
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2794455/Beats-99.94-solutions
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: diagr,to_row,to_col,diag = 0,moves[-1][0],moves[-1][1],0 row,col,i = 0,0,len(moves)-1 while i >= 0: if moves[i][0] == to_row : row +=1 if moves[i][1] == to_col : col+=1 if moves[i][0] == mo...
find-winner-on-a-tic-tac-toe-game
Beats 99.94% solutions
ishitakumardps
0
4
find winner on a tic tac toe game
1,275
0.543
Easy
18,924
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2769392/Find-Winner-on-a-Tic-Tac-Toe-Game-or-PYTHON
class Solution: def check(self, matrix: List[List[int]]) -> bool: # checking row wise if matrix[0][0]==matrix[0][1] and matrix[0][1]==matrix[0][2]: if matrix[0][0]!="": return True if matrix[1][0]==matrix[1][1] and matrix[1][1]==matrix[1][2]: if matri...
find-winner-on-a-tic-tac-toe-game
Find Winner on a Tic Tac Toe Game | PYTHON
saptarishimondal
0
12
find winner on a tic tac toe game
1,275
0.543
Easy
18,925
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2605258/An-Optimized-Solution-with-a-%22Scoring%22-System
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: if len(moves) < 5: # It's impossible to win before turn five return "Pending" # Keep track of players' row, column, and diagonal "scores" rowPts, colPts, diagPts = [0,0,0], [0,0,0], [0,0] ...
find-winner-on-a-tic-tac-toe-game
An Optimized Solution with a "Scoring" System
kcstar
0
22
find winner on a tic tac toe game
1,275
0.543
Easy
18,926
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2367231/Python-Modular-solution-sc-tc-%3A-O(n)-O(n)
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: row, col, diag, anti = [0] * 3, [0] * 3, 0, 0 for i in range(len(moves)): offset = 1 if i % 2 == 0 else -1 row[moves[i][0]] += offset col[moves[i][1]] += offset if moves[i][0] == moves...
find-winner-on-a-tic-tac-toe-game
Python Modular solution sc, tc : O(n), O(n)
byusuf
0
59
find winner on a tic tac toe game
1,275
0.543
Easy
18,927
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/2324381/Straightforward-Python3-solution.-Faster-than-95
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: # There can't be a winner if neither player has made 3 moves yet if len(moves) < 5: return "Pending" # Build grid grid = [[0] * 3 for _ in range(3)] play = 1 # Player "...
find-winner-on-a-tic-tac-toe-game
Straightforward Python3 solution. Faster than 95%
nigh_anxiety
0
66
find winner on a tic tac toe game
1,275
0.543
Easy
18,928
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1720153/Python3-easy-to-understand
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: if len(moves)<5: return "Pending" def helper(arr:list): arr = sorted(arr) for i in arr: if i[0] == 0 and i[1] == 0: if ([0,1] in arr and [0,2] in arr ...
find-winner-on-a-tic-tac-toe-game
Python3 easy to understand
jasonjfk
0
103
find winner on a tic tac toe game
1,275
0.543
Easy
18,929
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1477138/Python3-Score-counting-and-some-optimization-to-cut-the-search-time-by-half!-(24-ms)
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: turns = len(moves) if turns < 5: # There's no way winner can be determined return "Pending" rows, cols, tlbr, trbl = [0] * 3, [0] * 3, 0, 0 cand = "A" if turns % 2 == 1 else "B" # Player who moved la...
find-winner-on-a-tic-tac-toe-game
[Python3] Score counting and some optimization to cut the search time by half! (24 ms)
park-csu
0
103
find winner on a tic tac toe game
1,275
0.543
Easy
18,930
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1424204/Python3-Detailed-Solution-Faster-Than-97.61-Memory-Less-Than-96.48
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: grid = [[".", ".", "."], [".", ".", "."], [".", ".", "."]] def check(grid): if grid[0][0] == grid[1][1] == grid[2][2] and grid[0][0] != '.': if grid[0][0] == 'x': return 'A' ...
find-winner-on-a-tic-tac-toe-game
Python3 Detailed Solution Faster Than 97.61%, Memory Less Than 96.48%
Hejita
0
128
find winner on a tic tac toe game
1,275
0.543
Easy
18,931
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/1316284/Python3-simple-verification-solution
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: grid=[[0]*3 for _ in range(3)] for i in range(len(moves)): x,y=moves[i] if i%2==0: grid[x][y]="A" else: grid[x][y]="B" def winner(ch): ...
find-winner-on-a-tic-tac-toe-game
Python3 simple verification solution
atm1504
0
177
find winner on a tic tac toe game
1,275
0.543
Easy
18,932
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/916789/Python3-easy-and-Fast-solution
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: grid = [[0] * 3 for i in range(3)] idx = 0 # fill the grid, 1 represents "X" (A's move) and 2 represents "O" (B's move) for r,c in moves: if idx % 2 == 0: grid[r][c] = 1 else: ...
find-winner-on-a-tic-tac-toe-game
Python3 easy and Fast solution
marzi_ash
0
194
find winner on a tic tac toe game
1,275
0.543
Easy
18,933
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/819192/Intuitive-approach-by-checking-who-wins-and-then-%22Draw%22-or-%22Pending%22
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: r''' X X 0 0 X ''' def is_win(moves): # Check row, column, diagnol for i in range(3): if len(list(map(lambda t: t[1], filter(lambda t: t[0]==i, moves)))) == 3...
find-winner-on-a-tic-tac-toe-game
Intuitive approach by checking who wins and then "Draw" or "Pending"
puremonkey2001
0
78
find winner on a tic tac toe game
1,275
0.543
Easy
18,934
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/660124/Python-Solution-based-on-Game-simulation-(-faster-than-72.84-)-Memory-Usage%3A-13.9-MB
class Solution: def vertical1(self, data, ch): if data[0][0]==ch and data[1][0]==ch and data[2][0]==ch: return True return False def vertical3(self, data, ch): if data[0][2]==ch and data[1][2]==ch and data[2][2]==ch: return True return False ...
find-winner-on-a-tic-tac-toe-game
Python Solution based on Game simulation ( faster than 72.84% ) Memory Usage: 13.9 MB
hasan08sust
0
85
find winner on a tic tac toe game
1,275
0.543
Easy
18,935
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/552910/Python3-solution-using-a-transpose
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: board = [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']] for i in range(len(moves)): if i%2 == 0: player = 'A' else: player = 'B' board[moves[i][0]][moves[i][1]]...
find-winner-on-a-tic-tac-toe-game
Python3 solution using a transpose
altareen
0
111
find winner on a tic tac toe game
1,275
0.543
Easy
18,936
https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/discuss/441445/Python3-solution-faster-than-100-using-less-memory-than-100-(as-of-1st-Dec-2019-%3AD)
class Solution: def tictactoe(self, moves: List[List[int]]) -> str: class Score: def __init__(self): self.rows = [0 for i in range(3)] self.cols = [0 for i in range(3)] self.diag = 0 self.adiag = 0 A = Score() B = Sc...
find-winner-on-a-tic-tac-toe-game
Python3 solution faster than 100%, using less memory than 100% (as of 1st Dec 2019 :D)
akasprzak
0
110
find winner on a tic tac toe game
1,275
0.543
Easy
18,937
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/551868/Math-%2B-Python-Using-2-variables-linear-algebra-to-solve-the-problem
class Solution: def numOfBurgers(self, tomatoSlices, cheeseSlices): # on the basis of the matrix solution ans = [0.5 * tomatoSlices - cheeseSlices, -0.5 * tomatoSlices + 2 * cheeseSlices] # using the constraints to see if solution satisfies it if 0 <= int(ans[0]) == ans[0] and 0 <= int(ans[1]...
number-of-burgers-with-no-waste-of-ingredients
[Math + Python] Using 2 variables linear algebra to solve the problem
Suraj1127
3
203
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,938
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/441667/Python-3-(one-line)-(beats-100)-(24-ms)-(With-Explanation)
class Solution: def numOfBurgers(self, T: int, C: int) -> List[int]: return [[T//2 - C, 2*C - T//2],[]][T % 2 or T < 2*C or 4*C < T] - Junaid Mansuri - Chicago, IL
number-of-burgers-with-no-waste-of-ingredients
Python 3 (one line) (beats 100%) (24 ms) (With Explanation)
junaidmansuri
2
139
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,939
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/2326286/Python-easy-understanding-beats-100.
class Solution(object): def numOfBurgers(self, t, c): if t==c==0: return [0,0] four=(t-2*c)//2 # no of jumbo burgers by solving 4x+2y=t and x+y=c two=c-four #number of small burgers if c>=t or (t-2*c)%2==1 or four<0 or two<0: #if cheese is less than tomatoes or ...
number-of-burgers-with-no-waste-of-ingredients
Python easy understanding beats 100%.
babashankarsn
0
29
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,940
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/2120256/python-3-oror-simple-O(1)-math-solution
class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: if tomatoSlices % 2: return [] jumbo = tomatoSlices // 2 - cheeseSlices if jumbo < 0 or cheeseSlices < jumbo: return [] return [jumbo, cheeseSlices - jumbo]
number-of-burgers-with-no-waste-of-ingredients
python 3 || simple O(1) math solution
dereky4
0
44
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,941
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/2072905/Python3-oror-Greedy-oror-readable-code-with-comments
class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: if tomatoSlices%2: return [] x=tomatoSlices//2 diff=x-cheeseSlices if diff<0: return [] if x-(2*diff)<0: return [] return [diff,x-(2*diff)] ...
number-of-burgers-with-no-waste-of-ingredients
Python3 || Greedy || readable code with comments
Aniket_liar07
0
49
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,942
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/1391208/Easy-Python-Solution-(Faster-than-99)
class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: eq1, eq2 = 0, 0 a = tomatoSlices//2 x = a - cheeseSlices y = (2*cheeseSlices) - a if x >= 0 and y >= 0: eq1 = (4*x) + (2*y) eq2 = x + y if eq1 == ...
number-of-burgers-with-no-waste-of-ingredients
Easy Python Solution (Faster than 99%)
the_sky_high
0
111
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,943
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/1210878/python-sol
class Solution: def numOfBurgers(self, tomato: int, cheese: int) -> List[int]: if (tomato-2*cheese)%2!=0: return [] x=(tomato-2*cheese)//2 y=cheese-x if x>=0 and y>=0: return [x,y] return []
number-of-burgers-with-no-waste-of-ingredients
python sol
heisenbarg
0
48
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,944
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/1055764/Python-Solution
class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: if tomatoSlices == 0 and cheeseSlices ==0: return [0,0] if tomatoSlices <= cheeseSlices: return [] prod1 = 2*cheeseSlices ans1 = tomatoSlices - prod1 x = 0 ...
number-of-burgers-with-no-waste-of-ingredients
Python Solution
SaSha59
0
71
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,945
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/699715/Python3-chicken-and-rabbit-problem-Number-of-Burgers-with-No-Waste-of-Ingredients
class Solution: def numOfBurgers(self, t: int, c: int) -> List[int]: jumbo = (t - 2*c) // 2 small = c - jumbo if jumbo >= 0 and small >= 0 and jumbo*4 + small*2 == t and jumbo + small == c: return [jumbo, small] return []
number-of-burgers-with-no-waste-of-ingredients
Python3 chicken and rabbit problem - Number of Burgers with No Waste of Ingredients
r0bertz
0
97
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,946
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/448090/O(log-M)-where-M-is-cheese-slice-num-Binary-Search-20-Line-Python-3
class Solution: # O(log N) def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int): if tomatoSlices < cheeseSlices * 2: return [] if tomatoSlices == 0 and cheeseSlices == 0: return [0, 0] l, r = 0, cheeseSlices while l <= r: middle = l ...
number-of-burgers-with-no-waste-of-ingredients
O(log M) where M is cheese slice num- Binary Search 20 Line - Python 3
ilkercankaya
0
89
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,947
https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/discuss/441460/Python3-Linear-algebra
class Solution: def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: if tomatoSlices &amp; 1 or not (2*cheeseSlices <= tomatoSlices <= 4*cheeseSlices): return [] return [(tomatoSlices - 2*cheeseSlices)//2, (4*cheeseSlices - tomatoSlices)//2]
number-of-burgers-with-no-waste-of-ingredients
[Python3] Linear algebra
ye15
0
101
number of burgers with no waste of ingredients
1,276
0.506
Medium
18,948
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1736397/Python-Thought-process-for-the-DP-solution-with-very-simple-explanation-(with-images)
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: count=matrix.count(1) count=0 for r in range(len(matrix)): for c in range(len(matrix[0])): if matrix[r][c]==1: count+=1 if r==0 or c==0: continue ...
count-square-submatrices-with-all-ones
Python 🐍 Thought process for the DP solution with very simple explanation (with images)
InjySarhan
4
354
count square submatrices with all ones
1,277
0.744
Medium
18,949
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1179327/Python3-simple-solution-beats-80-users
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: count = 0 for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j] == 1 and (i != 0 and j != 0): matrix[i][j] = min(matrix[i-1][j-1], matrix[i-1][j], matrix[i][...
count-square-submatrices-with-all-ones
Python3 simple solution beats 80% users
EklavyaJoshi
4
169
count square submatrices with all ones
1,277
0.744
Medium
18,950
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1183726/Python3-oror-DP-oror-Simple-Code-oror-98-Faster-oror
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m=len(matrix) n=len(matrix[0]) for i in range(1,m): for j in range(1,n): if matrix[i][j]: matrix[i][j]=min(matrix[i-1][j],matrix[i][j-1],matrix[i-1][j-1])+1 res=0 for p in range(m): ...
count-square-submatrices-with-all-ones
Python3 || DP || Simple Code || 98% Faster ||
abhi9Rai
2
329
count square submatrices with all ones
1,277
0.744
Medium
18,951
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1149133/Python3-DP-or-Easy-to-understand
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: m,n=len(matrix),len(matrix[0]) dp=[[0]*n for _ in range(m)] ans=0 for i in range(m): for j in range(n): if i==0 or j==0: dp[i][j]=matrix[i][j] else:...
count-square-submatrices-with-all-ones
[Python3] DP | Easy to understand
_vaishalijain
2
321
count square submatrices with all ones
1,277
0.744
Medium
18,952
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441696/Python-3-(w-and-wo-DP)-(two-lines)
class Solution: def countSquares(self, G: List[List[int]]) -> int: M, N, t = len(G), len(G[0]), 0; H = [[0]*(N+1) for _ in range(M+1)] for i in range(M): c = 0 for j in range(N): c += G[i][j]; H[i+1][j+1] = H[i][j+1] + c for k in range(1, 1 + m...
count-square-submatrices-with-all-ones
Python 3 (w/ and w/o DP) (two lines)
junaidmansuri
2
387
count square submatrices with all ones
1,277
0.744
Medium
18,953
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441696/Python-3-(w-and-wo-DP)-(two-lines)
class Solution: def countSquares(self, G: List[List[int]]) -> int: for i,j in itertools.product(range(1,len(G)),range(1,len(G[0]))): G[i][j] = G[i][j] and 1 + min(G[i-1][j], G[i][j-1], G[i-1][j-1]) return sum(map(sum,G)) - Junaid Mansuri - Chicago, IL
count-square-submatrices-with-all-ones
Python 3 (w/ and w/o DP) (two lines)
junaidmansuri
2
387
count square submatrices with all ones
1,277
0.744
Medium
18,954
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/2519879/Py-or-Easy-solution-or-Best-Approch
class Solution: def countSquares(self, arr: List[List[int]]) -> int: n=len(arr) m=len(arr[0]) dp=[[0 for i in range(m)]for j in range(n)] for i in range(n): dp[i][0]=arr[i][0] for j in range(m): dp[0][j]=arr[0][j] for i in range(1,n): ...
count-square-submatrices-with-all-ones
Py | Easy solution | Best Approch 💯 ✅
adarshg04
1
64
count square submatrices with all ones
1,277
0.744
Medium
18,955
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/2059294/Python-or-DP
class Solution: def countSquares(self, mat: List[List[int]]) -> int: ans = [[0 for i in range(len(mat[0]))]for j in range(len(mat))] t = 0 for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j] == 1: if i==0 or j==0: ...
count-square-submatrices-with-all-ones
Python | DP
Shivamk09
1
198
count square submatrices with all ones
1,277
0.744
Medium
18,956
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1367928/Python-Clean-DP-or-94.76-faster
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: M, N = len(matrix), len(matrix[0]) count = sum(matrix[-1]) + sum(matrix[row][-1] for row in range(M-1)) for r in range(M-2, -1, -1): for c in range(N-2, -1, -1): if matrix[r][c] == 1: ...
count-square-submatrices-with-all-ones
[Python] Clean DP | 94.76% faster
soma28
1
185
count square submatrices with all ones
1,277
0.744
Medium
18,957
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441557/Python3-DP-solution
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: ans = 0 for i in range(len(matrix)): for j in range(len(matrix[0])): if i > 0 and j > 0 and matrix[i][j]: matrix[i][j] = min(matrix[i-1][j], matrix[i-1][j-1], matrix[i][j-1]) + 1 ...
count-square-submatrices-with-all-ones
[Python3] DP solution
ye15
1
113
count square submatrices with all ones
1,277
0.744
Medium
18,958
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/441537/Python-easy-understand-explain
class Solution: def countSquares(self, matrix: List[List[int]], rc = None, cc = None) -> int: """ :type matrix: List[List[int]] :rtype: int """ # Main logic: # It just like board game "Gigamic Pylos" # We can use 4 index nearby to check a next round possib...
count-square-submatrices-with-all-ones
Python, easy understand, explain
Chumicat
1
368
count square submatrices with all ones
1,277
0.744
Medium
18,959
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/2779118/Python-(Simple-Dynamic-Programming)
class Solution: def countSquares(self, A): n, m = len(A), len(A[0]) dp = [[0 for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): if i>0 and j>0: if A[i-1][j] == A[i][j-1] == A[i-1][j-1] == A[i][j] == 1: ...
count-square-submatrices-with-all-ones
Python (Simple Dynamic Programming)
rnotappl
0
9
count square submatrices with all ones
1,277
0.744
Medium
18,960
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/2639753/Python-Simple-Solution
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: #First row count=sum(matrix[0]) #First column for i in range(1,len(matrix)): count+=matrix[i][0] #Square for i in range(1,len(matrix)): for j in range(1,len(matrix[0])): ...
count-square-submatrices-with-all-ones
Python Simple Solution
beingab329
0
16
count square submatrices with all ones
1,277
0.744
Medium
18,961
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1903122/One-pass-and-O(1)-space-DP-in-Python
class Solution: def countSquares(self, M: List[List[int]]) -> int: ans, m, n = 0, len(M), len(M[0]) from itertools import product for i, j in product(range(m), range(n)): if i > 0 and j > 0: if M[i - 1][j] and M[i][j - 1] and M[i - 1][j - 1] and M[i][j]: ...
count-square-submatrices-with-all-ones
One-pass and O(1)-space DP in Python
mousun224
0
193
count square submatrices with all ones
1,277
0.744
Medium
18,962
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/1722718/Simple-python-easy-to-understand
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: def helper(i,j,r_mac,c_mac,mat,level=0): if i+level+1> r_mac or j+level+1>c_mac: return 0 sqr = True total = 0 for m in range(i,i+level+1): for n in range(j...
count-square-submatrices-with-all-ones
Simple python easy to understand
jasonjfk
0
94
count square submatrices with all ones
1,277
0.744
Medium
18,963
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/841131/Easy-Better-than-99.5-Dp-solution
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: n = len(matrix) m = len(matrix[0]) if n<2 or m<2: return matrix.count(1) dp = [[0]*m for _ in range(n)] sum = 0 for i in range(n): if i==0: for j in range(m...
count-square-submatrices-with-all-ones
Easy Better than 99.5% Dp solution
oossiiris
0
391
count square submatrices with all ones
1,277
0.744
Medium
18,964
https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/644948/Simple-Intuitive-Python-DP-Solution-O(n)-Time-and-Space
class Solution: def countSquares(self, matrix: List[List[int]]) -> int: total = 0 dp = [[0] * len(row) for row in matrix] for i, row in enumerate(matrix): for j, num in enumerate(row): if not num: continue upper = 0 ...
count-square-submatrices-with-all-ones
Simple, Intuitive Python DP Solution - O(n) Time and Space
schedutron
0
147
count square submatrices with all ones
1,277
0.744
Medium
18,965
https://leetcode.com/problems/palindrome-partitioning-iii/discuss/2593400/Dynamic-Programming-oror-Recursion-oror-Memoization-oror-Easy-Intuition-oror-Python
class Solution: def palindromePartition(self, s: str, k: int) -> int: #This is the cost function def Cost(s): i,j,c=0,len(s)-1,0 while i<j: if s[i]!=s[j]:c+=1 j-=1 i+=1 return c ...
palindrome-partitioning-iii
Dynamic Programming || Recursion || Memoization || Easy Intuition || Python
srikarsai550
2
70
palindrome partitioning iii
1,278
0.608
Hard
18,966
https://leetcode.com/problems/palindrome-partitioning-iii/discuss/2814529/Python3-Solution-or-DP
class Solution: def palindromePartition(self, S, K): N = len(S) dp = [[N] * (N + 1) for _ in range(K + 1)] dp[0][0] = 0 for i in range(2 * N - 1): c = 0 l = i // 2 r = l + (i &amp; 1) while 0 <= l and r < N: if S[l] != S...
palindrome-partitioning-iii
✔ Python3 Solution | DP
satyam2001
0
5
palindrome partitioning iii
1,278
0.608
Hard
18,967
https://leetcode.com/problems/palindrome-partitioning-iii/discuss/2803724/Python3-or-Recursive-Dp-Approach
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) def costToMakePalindrome(left,right): change = 0 while left<=right: if s[left]!=s[right]: change+=1 left+=1 right-=1 ...
palindrome-partitioning-iii
[Python3] | Recursive Dp Approach
swapnilsingh421
0
6
palindrome partitioning iii
1,278
0.608
Hard
18,968
https://leetcode.com/problems/palindrome-partitioning-iii/discuss/2333666/Python3-or-O(n2-*-k)-or-DP-or-pre-computing-cost-matrix
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) def getChanges(i, j): ret = 0 while i < j: if s[i] != s[j]: ret += 1 i += 1 j -= 1 return ret ...
palindrome-partitioning-iii
Python3 | O(n^2 * k) | DP | pre-computing cost matrix
DheerajGadwala
0
27
palindrome partitioning iii
1,278
0.608
Hard
18,969
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1713663/Python-3-(20ms)-or-3-Solutions-or-Fastest-Iterative-and-One-Liners-or-Super-Easy
class Solution: def subtractProductAndSum(self, n: int) -> int: p,s=1,0 while n!=0: p*=(n%10) s+=(n%10) n//=10 return p-s
subtract-the-product-and-sum-of-digits-of-an-integer
Python 3 (20ms) | 3 Solutions | Fastest Iterative & One-Liners | Super Easy
MrShobhit
12
659
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,970
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1713663/Python-3-(20ms)-or-3-Solutions-or-Fastest-Iterative-and-One-Liners-or-Super-Easy
class Solution: def subtractProductAndSum(self, n: int) -> int: return prod(list(map(int, str(n))))-sum(list(map(int, str(n))))
subtract-the-product-and-sum-of-digits-of-an-integer
Python 3 (20ms) | 3 Solutions | Fastest Iterative & One-Liners | Super Easy
MrShobhit
12
659
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,971
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1713663/Python-3-(20ms)-or-3-Solutions-or-Fastest-Iterative-and-One-Liners-or-Super-Easy
class Solution: def subtractProductAndSum(self, n: int) -> int: arr = list(map(int, str(n))) return prod(arr)-sum(arr)
subtract-the-product-and-sum-of-digits-of-an-integer
Python 3 (20ms) | 3 Solutions | Fastest Iterative & One-Liners | Super Easy
MrShobhit
12
659
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,972
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1713663/Python-3-(20ms)-or-3-Solutions-or-Fastest-Iterative-and-One-Liners-or-Super-Easy
class Solution: def subtractProductAndSum(self, n: int) -> int: arr = list(map(int, str(n))) return reduce(operator.mul, arr) - sum(arr)
subtract-the-product-and-sum-of-digits-of-an-integer
Python 3 (20ms) | 3 Solutions | Fastest Iterative & One-Liners | Super Easy
MrShobhit
12
659
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,973
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2215978/Python3-simple-solution-one-pass-using-divMod
class Solution: def subtractProductAndSum(self, n: int) -> int: plus = 0 product = 1 n = str(n) for i in n: plus = plus + int(i) product = product * int(i) return product - plus
subtract-the-product-and-sum-of-digits-of-an-integer
📌 Python3 simple solution one pass using divMod
Dark_wolf_jss
6
143
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,974
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1752896/1281-Product-sum-of-an-int
class Solution(object): def subtractProductAndSum(self, n): sumOfInts = 0 productOfInts = 1 while(n>0): remainder1 = n % 10 sumOfInts += remainder1 productOfInts *= remainder1 n -= remainder1 n /= 10 return productOfI...
subtract-the-product-and-sum-of-digits-of-an-integer
1281 Product - sum of an int
ankit61d
4
211
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,975
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1930463/Python-One-Liner!
class Solution: def subtractProductAndSum(self, n): def prod(arr): return reduce(lambda a,b: a * b, arr) x = [int(i) for i in str(n)] return prod(x) - sum(x)
subtract-the-product-and-sum-of-digits-of-an-integer
Python - One Liner!
domthedeveloper
3
234
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,976
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1930463/Python-One-Liner!
class Solution: def subtractProductAndSum(self, n): x = [int(i) for i in str(n)] return prod(x) - sum(x)
subtract-the-product-and-sum-of-digits-of-an-integer
Python - One Liner!
domthedeveloper
3
234
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,977
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1930463/Python-One-Liner!
class Solution: def subtractProductAndSum(self, n): return prod(x := [int(i) for i in str(n)]) - sum(x)
subtract-the-product-and-sum-of-digits-of-an-integer
Python - One Liner!
domthedeveloper
3
234
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,978
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1699640/Python3-oror-easy-to-understand-code-oror-faster-than-100-%2B-one-liner
class Solution: def subtractProductAndSum(self, n: int) -> int: a,b=1,0 for i in list(str(n)): a*=int(i) b+=int(i) return (a-b)
subtract-the-product-and-sum-of-digits-of-an-integer
✔ Python3 || easy to understand code || faster than 100% + one liner
Anilchouhan181
3
168
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,979
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1699640/Python3-oror-easy-to-understand-code-oror-faster-than-100-%2B-one-liner
class Solution: def subtractProductAndSum(self, n: int) -> int: return prod(list(map(int,str(n))))-sum(list(map(int,str(n))))
subtract-the-product-and-sum-of-digits-of-an-integer
✔ Python3 || easy to understand code || faster than 100% + one liner
Anilchouhan181
3
168
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,980
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1203246/Much-simple-and-readable.
class Solution: def subtractProductAndSum(self, n: int) -> int: mult,add=1,0 for i in str(n): mult*=int(i) add+=int(i) return mult-add
subtract-the-product-and-sum-of-digits-of-an-integer
Much simple and readable.
_jorjis
3
141
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,981
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/446430/Python-3-(one-line)-(beats-100)
class Solution: def subtractProductAndSum(self, n: int) -> int: s, p = 0, 1 for i in str(n): s += int(i); p *= int(i) return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python 3 (one line) (beats 100%)
junaidmansuri
3
858
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,982
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2598758/Python-very-short-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: p, s = 1, 0 while n: n, digit = divmod(n, 10) p, s = p * digit, s + digit return p - s
subtract-the-product-and-sum-of-digits-of-an-integer
Python very short solution
Mark_computer
1
114
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,983
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2574747/Python-easy-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: pdt = 1 sm = 0 while(n>0): d = n%10 pdt *= d sm += d n //=10 return pdt-sm
subtract-the-product-and-sum-of-digits-of-an-integer
Python easy solution
Jack_Chang
1
65
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,984
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2471326/Python3-Easy-solution-with-type-casting
class Solution: def subtractProductAndSum(self, n: int) -> int: n = str(n) sum_n =0 product = 1 for i in n: i = int(i) product = product*i sum_n += i res = product - sum_n return res
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 Easy solution with type casting
prakashpandit
1
14
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,985
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2231889/Easiest-Solution
class Solution: def subtractProductAndSum(self, n: int) -> int: arr = [] while n: arr.append(n%10) n = n // 10 sum = 0 prod = 1 for i in arr: sum += i prod *= i return prod-sum
subtract-the-product-and-sum-of-digits-of-an-integer
Easiest Solution
EbrahimMG
1
44
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,986
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2145222/Python3-or-O(log-n)-or-Easy-math-logic
class Solution: def subtractProductAndSum(self, n: int) -> int: product = 1 total = 0 while n: product*=(n%10) total+=(n%10) n//=10 return product-total
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 | O(log n) | Easy math logic
theKshah
1
93
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,987
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2136059/Easy-and-simple-Python-Solution
class Solution: def subtractProductAndSum(self, n: int) -> int: a=[] sum=0 prdt=1 while(n!=0): a.append(n%10) n=n//10 for i in range(0,len(a)): sum=sum+a[i] prdt=prdt*a[i] return prdt-sum
subtract-the-product-and-sum-of-digits-of-an-integer
Easy and simple - Python Solution
pruthashouche
1
88
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,988
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2100593/Very-easy-python-solution-with-stepwise-explaination
class Solution: def subtractProductAndSum(self, n: int) -> int: prod = 1 #initializing prod with 1 sum1 = 0 # Initializing sum1 with 0 # Initializing temp with the number so that if we want to check the original number we can directly check the temp value temp = n while...
subtract-the-product-and-sum-of-digits-of-an-integer
Very easy python solution with stepwise explaination
yashkumarjha
1
62
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,989
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2099164/PYTHON-or-Simple-python-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: n = str(n) s = 0 product = 1 for i in n: s += int(i) product *= int(i) return product - s
subtract-the-product-and-sum-of-digits-of-an-integer
PYTHON | Simple python solution
shreeruparel
1
143
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,990
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/2009289/PYTHON-oror-WITHOUT-RECURSION-oror-BEGINNER-FRIENDLY-oror-EASY-UNDERSTANDING
class Solution: def subtractProductAndSum(self, n: int) -> int: p = 1 s = 0 n = str(n) for i in n: p = p * (int(i)) for i in n: s = s + (int(i)) return p - s ```
subtract-the-product-and-sum-of-digits-of-an-integer
PYTHON || WITHOUT RECURSION || BEGINNER FRIENDLY || EASY UNDERSTANDING
Airodragon
1
49
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,991
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1998770/Easy-Python-Solution-Without-Using-Strings-oror-Better-Runtime-and-Memory
class Solution: def subtractProductAndSum(self, n: int) -> int: num, pro, sum_ = 0, 1, 0 while n!=0: num, n = n%10, n//10 pro*=num sum_ +=num return pro-sum_
subtract-the-product-and-sum-of-digits-of-an-integer
Easy Python Solution Without Using Strings || Better Runtime and Memory
YungMoMoney
1
81
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,992
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1897912/Python-solution-with-generator-expression-(instead-of-list-comprehension)
class Solution: def subtractProductAndSum(self, n: int) -> int: generator_expression = (int(digit) for digit in str(n)) prod_result = 1 sum_result = 0 for digit in generator_expression: prod_result *= digit sum_result += digit return prod_result - sum_...
subtract-the-product-and-sum-of-digits-of-an-integer
Python solution with generator expression (instead of list comprehension)
dashaternovskaya
1
29
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,993
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1826511/Python3-solution-(-and-operators)-with-comments
class Solution: def subtractProductAndSum(self, n: int) -> int: suma = 0 # keeps track of the sum product = 1 # keeps track of the products while n != 0: numero = n%10 # gets a digit e.g. 4 out of 234. suma += numero # adds the digit e.g. 4 product *= nume...
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 solution (% and // operators) with comments
nanicp2202
1
41
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,994
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1339738/Simple-Python-Solution-for-%22Subtract-the-Product-and-Sum-of-Digits-of-an-Integer%22
class Solution: def subtractProductAndSum(self, n: int) -> int: s = digit = 0 product = 1 while(n>0): digit = n%10 s = s + digit product = product * digit n = n//10 return product-s
subtract-the-product-and-sum-of-digits-of-an-integer
Simple Python Solution for "Subtract the Product and Sum of Digits of an Integer"
sakshikhandare2527
1
151
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,995
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/1129910/Python-easy-divmod-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: total = 0 product = 1 while n: n, remainder = divmod(n, 10) total += remainder product *= remainder return product - total
subtract-the-product-and-sum-of-digits-of-an-integer
Python easy divmod solution
alexanco
1
219
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,996
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/999822/Python-o(n)-or-3-Methods-or-Efficient-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: product = 1 sums = 0 while n: temp = n%10 n//=10 product*=temp sums+=temp return product-sums
subtract-the-product-and-sum-of-digits-of-an-integer
Python o(n) | 3 Methods | Efficient solution
hritik5102
1
131
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,997
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/999822/Python-o(n)-or-3-Methods-or-Efficient-solution
class Solution: def subtractProductAndSum(self, n: int) -> int: product=1 sums = 0 for i in str(n): product*=int(i) sums += int(i) return product-sums
subtract-the-product-and-sum-of-digits-of-an-integer
Python o(n) | 3 Methods | Efficient solution
hritik5102
1
131
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,998
https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/discuss/489790/Python3-99.9-faster-16ms-Simple-and-straight-forward
class Solution: def subtractProductAndSum(self, n: int) -> int: produc_sum = 1 sum_digit = 0 while n != 0: t = n % 10 n = n // 10 produc_sum = produc_sum * t sum_digit += t return produc_sum - sum_digit
subtract-the-product-and-sum-of-digits-of-an-integer
Python3 99.9% faster 16ms - Simple and straight forward
Dmx2
1
169
subtract the product and sum of digits of an integer
1,281
0.867
Easy
18,999