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/check-array-formation-through-concatenation/discuss/928090/Python3-(Using-Hashmap)
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: #To avoid creating any of the ds or fors if not necessary if not arr and not pieces: return True if not arr or not pieces: return False result = list() hashmap = dict() for p in pieces: hashmap[p[0]] = p #Dictionary with key: first integer of pieces[i]; and value: entire pieces[i] for a in arr: if a in hashmap: result.extend(hashmap[a]) #if a is equal to one of the keys in the hashmap, we add to the result the value associated to that key (the entire pieces[i]) return arr == result
check-array-formation-through-concatenation
Python3 (Using Hashmap)
mcl91
1
104
check array formation through concatenation
1,640
0.561
Easy
23,800
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/2698392/python-O(n*n)-solution
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: hash_list = Counter(arr) for i in range(0 , len(pieces)): flag = True start = False k = 0 for j in range(0 , len(arr)): if k >= len(pieces[i]): break if arr[j] == pieces[i][k]: hash_list[arr[j]] -= 1 start = True k += 1 elif arr[j] != pieces[i][k]: if not start: continue else: flag = False break for i in hash_list: if hash_list[i] != 0: return False return True
check-array-formation-through-concatenation
python O(n*n) solution
akashp2001
0
4
check array formation through concatenation
1,640
0.561
Easy
23,801
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/2665960/Python3-solution
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: d = defaultdict(list) for piece in pieces: d[piece[0]] = piece i = 0 while i < len(arr): if arr[i] in d: temp = d[arr[i]][1:] i += 1 for t in temp: if arr[i] != t: return False i += 1 else: return False return True
check-array-formation-through-concatenation
Python3 solution
mediocre-coder
0
6
check array formation through concatenation
1,640
0.561
Easy
23,802
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1903856/Python-Solution
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: for piece in pieces: if piece[0] in arr: index = arr.index(piece[0]) if piece != arr[index: index + len(piece)]: return False else: return False return True
check-array-formation-through-concatenation
Python Solution
hgalytoby
0
76
check array formation through concatenation
1,640
0.561
Easy
23,803
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1903856/Python-Solution
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: for piece in pieces: if piece[0] in arr: result = False index = 0 for i in range(arr.count(piece[0])): index = arr.index(piece[0], index) if piece == arr[index: index + len(piece)]: result = True break index += 1 if not result: return False else: return False return True
check-array-formation-through-concatenation
Python Solution
hgalytoby
0
76
check array formation through concatenation
1,640
0.561
Easy
23,804
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1796160/5-Lines-Python-Solution-(very-simple)-oror-90-Faster-(40ms)-oror-Memory-less-than-90
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: ans = [] for a in arr: for piece in pieces: if a == piece[0]: ans += piece ; break return ans == arr
check-array-formation-through-concatenation
5-Lines Python Solution (very simple) || 90% Faster (40ms) || Memory less than 90%
Taha-C
0
82
check array formation through concatenation
1,640
0.561
Easy
23,805
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1569281/Python-Conditional-logic-Easy-to-understand-Beats-80.4
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: # Runtime: 40 ms, faster than 80.48% # Memory Usage: 14.3 MB, less than 34.81% src_len = len(arr) for piece in pieces: # If the first element of the piece not in source if piece[0] not in arr: return False # If the length of the current piece is 1, just continue if len(piece) == 1: continue else: # Get the start index of the element in source start_idx = arr.index(piece[0]) if src_len - start_idx < len(piece): return False # If there are not enough elements after the first found element in source for i in range(1, len(piece)): start_idx += 1 if piece[i] != arr[start_idx]: return False # Finally return return True
check-array-formation-through-concatenation
Python - Conditional logic; Easy to understand; Beats 80.4%
inosuke4
0
67
check array formation through concatenation
1,640
0.561
Easy
23,806
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1477120/Dictionary-on-pieces-94-speed
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: dict_p = dict() for lst in pieces: dict_p[lst[0]] = lst i, len_arr = 0, len(arr) while i < len_arr: if (arr[i] in dict_p and arr[i: i + len(dict_p[arr[i]])] == dict_p[arr[i]]): i += len(dict_p[arr[i]]) else: return False return True
check-array-formation-through-concatenation
Dictionary on pieces, 94% speed
EvgenySH
0
136
check array formation through concatenation
1,640
0.561
Easy
23,807
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1218042/compare-list
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: l=[] for num in arr: i=0 while i <len(pieces): if num==pieces[i][0]: l.extend(pieces[i]) pieces.remove(pieces[i]) break i+=1 return (l==arr)
check-array-formation-through-concatenation
compare list
janhaviborde23
0
38
check array formation through concatenation
1,640
0.561
Easy
23,808
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/1200518/Python-Simple-Solution-98.3-Faster
class Solution(object): def canFormArray(self, arr, pieces): """ :type arr: List[int] :type pieces: List[List[int]] :rtype: bool """ firstElem = [i[0] for i in pieces] i = 0 while i < len(arr): # print("Start Matching: ",arr[i]) if arr[i] in firstElem: getIndex = firstElem.index(arr[i]) lenArrPicesArr = len(pieces[getIndex]) # print("Arr to be matched", arr[i:i+lenArrPicesArr]) # print("Pices: ",pieces[getIndex]) if pieces[getIndex] == arr[i:i+lenArrPicesArr]: i += lenArrPicesArr else: return False else: return False return True
check-array-formation-through-concatenation
Python Simple Solution 98.3 Faster
sunnysharma03
0
136
check array formation through concatenation
1,640
0.561
Easy
23,809
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/997171/Python-99-time-or-List-Slicing-or-Happy-New-Year
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: match = dict() for i in range(len(arr)): match[arr[i]] = i for piece in pieces: if piece[0] not in match: return False start = match[piece[0]] if arr[start: start+len(piece)] != piece: return False return True
check-array-formation-through-concatenation
[Python] 99% time | List Slicing | Happy New Year
ryandeng32
0
103
check array formation through concatenation
1,640
0.561
Easy
23,810
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/996361/Simple-python-solution-using-hashmap
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: hashmap = dict() for start in pieces: hashmap[start[0]] = start ans = [] for i in arr: ans += hashmap.get(i, []) return ans == arr
check-array-formation-through-concatenation
Simple python solution using hashmap
amoghrajesh1999
0
37
check array formation through concatenation
1,640
0.561
Easy
23,811
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/935217/Intuitive-approach-(recursive-search)
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: # 0) Build search dict # pieces = [[1], [1,2], [2], [2,3]] # head_dict = {1: [[1], [1,2]], 2: [[2], [2,3]]} head_dict = {} for e in pieces: elist = head_dict.get(e[0], []) elist.append(e) head_dict[e[0]] = elist # 1) Define recursive function to search element in `pieces` # which can compose `arr` def recursive_search(arr): if len(arr) == 0: return True else: for e in head_dict.get(arr[0], []): if len(e) <= len(arr) and str(e) == str(arr[:len(e)]) and recursive_search(arr[len(e):]): return True return False # 2) Start searching and return answer return recursive_search(arr)
check-array-formation-through-concatenation
Intuitive approach (recursive search)
puremonkey2001
0
37
check array formation through concatenation
1,640
0.561
Easy
23,812
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/924595/Python3-solution-Beats-100-memory
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: if len(arr) == 0 or len (pieces) == 0: return False p_f = {} for p in pieces: p_f[p[0]] = p flag = False i = 0 while i < len(arr): print(arr[i]) if arr[i] in p_f: y = arr[i] for x in p_f[y]: if arr[i] != x: return False i=i+1 flag = True else: return False return flag
check-array-formation-through-concatenation
Python3 solution Beats 100 % memory
sgrdswl
0
101
check array formation through concatenation
1,640
0.561
Easy
23,813
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/920748/Python3-recursive-solution-44ms-14.1MB
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: # recursion ends when both arr and pieces become empty if not arr and not pieces: return True # check which pieces can be used as the array's prefix for piece in pieces: if piece == arr[:len(piece)]: # do a recursive call with the prefix sliced off the array and # the matched piece removed from the pieces list return self.canFormArray(arr[len(piece):], [p for p in pieces if p != piece]) return False
check-array-formation-through-concatenation
Python3 recursive solution, 44ms, 14.1MB
vashik
0
71
check array formation through concatenation
1,640
0.561
Easy
23,814
https://leetcode.com/problems/check-array-formation-through-concatenation/discuss/918562/Python3-Easy-solution-with-replacement-idea
class Solution: def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool: joined = "##".join([str(i) for i in arr]) joined = "#" + joined + "#" pieces.sort() for pce in pieces: pJoined = "##".join(str(i) for i in pce) replaceStr = "#" + pJoined + "#" joined = joined.replace(replaceStr, "") if joined == "": return True return False
check-array-formation-through-concatenation
[Python3] Easy solution with replacement idea
suthagar23
0
114
check array formation through concatenation
1,640
0.561
Easy
23,815
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027288/Python-4-approaches-(DP-Maths)
class Solution: def countVowelStrings(self, n: int) -> int: dp = [[0] * 6 for _ in range(n+1)] for i in range(1, 6): dp[1][i] = i for i in range(2, n+1): dp[i][1]=1 for j in range(2, 6): dp[i][j] = dp[i][j-1] + dp[i-1][j] return dp[n][5]
count-sorted-vowel-strings
✅ Python 4 approaches (DP, Maths)
constantine786
45
3,200
count sorted vowel strings
1,641
0.772
Medium
23,816
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027288/Python-4-approaches-(DP-Maths)
class Solution: def countVowelStrings(self, n: int) -> int: dp = [1] * 5 for i in range(2, n+1): for j in range(4, -1, -1): dp[j] += sum(dp[:j]) return sum(dp)
count-sorted-vowel-strings
✅ Python 4 approaches (DP, Maths)
constantine786
45
3,200
count sorted vowel strings
1,641
0.772
Medium
23,817
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027288/Python-4-approaches-(DP-Maths)
class Solution: def countVowelStrings(self, k: int) -> int: dp = [1] * 5 for _ in range(1, k): for i in range(1, 5): dp[i] = dp[i] + dp[i-1] return sum(dp)
count-sorted-vowel-strings
✅ Python 4 approaches (DP, Maths)
constantine786
45
3,200
count sorted vowel strings
1,641
0.772
Medium
23,818
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027288/Python-4-approaches-(DP-Maths)
class Solution: def countVowelStrings(self, n: int) -> int: return (n + 4) * (n + 3) * (n + 2) * (n + 1) // 24;
count-sorted-vowel-strings
✅ Python 4 approaches (DP, Maths)
constantine786
45
3,200
count sorted vowel strings
1,641
0.772
Medium
23,819
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027288/Python-4-approaches-(DP-Maths)
class Solution: def countVowelStrings(self, n: int) -> int: return math.comb(n + 4, 4)
count-sorted-vowel-strings
✅ Python 4 approaches (DP, Maths)
constantine786
45
3,200
count sorted vowel strings
1,641
0.772
Medium
23,820
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/1146804/Recursion-and-memoization-in-python
class Solution: def countVowelStrings(self, n: int) -> int: def count(n,vow): if vow == 0: mem[n][vow] = 0 return mem[n][vow] if n == 0: mem[n][vow] = 1 return mem[n][vow] if mem[n][vow]!=-1: return mem[n][vow] mem[n][vow] = count(n,vow-1)+count(n-1,vow) return mem[n][vow] mem = [[-1]*6 for i in range(n+1)] return count(n,5)
count-sorted-vowel-strings
Recursion and memoization in python
deleted_user
3
254
count sorted vowel strings
1,641
0.772
Medium
23,821
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2112845/Python-O(1)-one-liner-very-easy
class Solution: def countVowelStrings(self, n: int) -> int: # The formula is (n+5-1)C(n)=(n+4)C(n) return (n+4)*(n+3)*(n+2)*(n+1)//24
count-sorted-vowel-strings
Python O(1) one liner, very easy
pbhuvaneshwar
1
86
count sorted vowel strings
1,641
0.772
Medium
23,822
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2030183/Python-Math
class Solution(object): def countVowelStrings(self, n): """ :type n: int :rtype: int """ if n == 1: return 5 prev = [1, 1, 1, 1, 1] for i in range(n - 1): for j in range(1, len(prev)): prev[j] += prev[j - 1] return sum(prev)
count-sorted-vowel-strings
Python Math
Kennyyhhu
1
27
count sorted vowel strings
1,641
0.772
Medium
23,823
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/1381544/Simple-Solution-Python
class Solution: def countVowelStrings(self, n: int) -> int: @lru_cache(None) def factorial(n): return n * factorial(n-1) if n else 1 return factorial(4 + n)//(factorial(4) * factorial(n))
count-sorted-vowel-strings
Simple Solution Python
tenart
1
118
count sorted vowel strings
1,641
0.772
Medium
23,824
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/1381544/Simple-Solution-Python
class Solution: def countVowelStrings(self, n: int) -> int: # factorial(4) == 24 # factorial(n+k)/factorial(n) == (n + 1) * ... * (n + k) return (n + 1) * (n + 2) * (n + 3) * (n + 4) // 24
count-sorted-vowel-strings
Simple Solution Python
tenart
1
118
count sorted vowel strings
1,641
0.772
Medium
23,825
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/957800/Python3-Just-Simple-Math
class Solution: def countVowelStrings(self, n: int) -> int: if n == 1: return 5 cnt = [5, 4, 3, 2, 1] while (n > 2): cnt = [sum(cnt[i:]) for i in range(5)] n -= 1 return sum(cnt)
count-sorted-vowel-strings
[Python3] Just Simple Math
tp99
1
140
count sorted vowel strings
1,641
0.772
Medium
23,826
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/918403/Python3-top-down-dp-and-math
class Solution: def countVowelStrings(self, n: int) -> int: @lru_cache(None) def fn(n, k): """Return number of sorted strings of length n consisting of k vowels.""" if n == 1: return k # base case return sum(fn(n-1, kk) for kk in range(1, k+1)) return fn(n, 5)
count-sorted-vowel-strings
[Python3] top-down dp & math
ye15
1
120
count sorted vowel strings
1,641
0.772
Medium
23,827
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/918403/Python3-top-down-dp-and-math
class Solution: def countVowelStrings(self, n: int) -> int: cnt = [1]*5 for _ in range(n-1): for i in range(1, 5): cnt[i] = cnt[i-1] + cnt[i] return sum(cnt)
count-sorted-vowel-strings
[Python3] top-down dp & math
ye15
1
120
count sorted vowel strings
1,641
0.772
Medium
23,828
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/918403/Python3-top-down-dp-and-math
class Solution: def countVowelStrings(self, n: int) -> int: return comb(n+4, 4)
count-sorted-vowel-strings
[Python3] top-down dp & math
ye15
1
120
count sorted vowel strings
1,641
0.772
Medium
23,829
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2721224/just-for-understanding
class Solution: def countVowelStrings(self, n: int) -> int: arr=["a","e","i","o","u"] self.res=0 sub=[] def fun(i): if len(sub.copy())==n: self.res+=1 return if i==len(arr) or len(sub[:])>n: return sub.append(arr[i]) fun(i) sub.pop() fun(i+1) fun(0) return self.res
count-sorted-vowel-strings
just for understanding
hemanth_12
0
1
count sorted vowel strings
1,641
0.772
Medium
23,830
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2697989/Easy-One-Line-Solution-Python
class Solution: def countVowelStrings(self, n: int) -> int: return comb(n+4,4)
count-sorted-vowel-strings
Easy One Line Solution Python
AnandSinhaProjects
0
6
count sorted vowel strings
1,641
0.772
Medium
23,831
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2680133/Python-Solution-using-math-calculation.
class Solution: @lru_cache def countVowelStrings(self, n: int) -> int: if(n == 1): return 5 dp = [[0]*5 for i in range(n)] dp[0] = [5,4,3,2,1] print(dp) for i in range(1,n): for j in range(4,-1,-1): if(j == 4): dp[i][j] = 1 else: dp[i][j] = dp[i-1][j] + dp[i][j+1] return dp[i][0]
count-sorted-vowel-strings
Python Solution using math calculation.
user2800NJ
0
5
count sorted vowel strings
1,641
0.772
Medium
23,832
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2588868/Python3-or-DP-Bottom-up
class Solution: def countVowelStrings(self, target_n: int) -> int: memo = {} def countVowel(n, prev_v): if n > target_n: return 0 if n == target_n: return 1 if (n, prev_v) in memo: return memo[(n, prev_v)] count = 0 for v in ['a', 'e', 'i', 'o', 'u']: if ord(prev_v) <= ord(v): count += countVowel(n+1, v) memo[(n, prev_v)] = count return memo[(n, prev_v)] return countVowel(0, 'a')
count-sorted-vowel-strings
Python3 | DP Bottom up
Ploypaphat
0
34
count sorted vowel strings
1,641
0.772
Medium
23,833
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2581729/Python-easy-solution
class Solution: def countVowelStrings(self, n: int) -> int: d = {'a':1,'e':1,'i':1,'o':1,'u':1} for nn in range(1, n): u = 1 o = u+d['o'] i = o+d['i'] e = i+d['e'] a = e+d['a'] d['a']=a; d['e']=e; d['i']=i; d['o']=o; d['u']=u return d['a']+d['e']+d['i']+d['o']+d['u']
count-sorted-vowel-strings
Python easy solution
Jack_Chang
0
36
count sorted vowel strings
1,641
0.772
Medium
23,834
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2574418/SIMPLE-PYTHON3-SOLUTION-faster
class Solution: def countVowelStrings(self, n: int) -> int: dp=[[0 for _ in range(5)] for _ in range(n)] for i in range(5): dp[0][i]=1 for i in range(1,n): for j in range(0,5): dp[i][j]=sum(dp[i-1][j:]) return sum(dp[-1])
count-sorted-vowel-strings
✅✔ SIMPLE PYTHON3 SOLUTION ✅✔faster
rajukommula
0
25
count sorted vowel strings
1,641
0.772
Medium
23,835
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2393067/Python-Accurate-Solution-using-Tuples-as-DP-Numbers-oror-Documented
class Solution: def countVowelStrings(self, n: int) -> int: a, e, i, o, u = 1, 1, 1, 1, 1 # dp numbers with initial value of 1 while n > 1: # modify the dp numbers by adding previous values based on rules a, e, i, o, u = (a+e+i+o+u), (e+i+o+u), (i+o+u), (o+u), u n -= 1 return (a+e+i+o+u) # result is sum of all dp numbers
count-sorted-vowel-strings
[Python] Accurate Solution using Tuples as DP Numbers || Documented
Buntynara
0
6
count sorted vowel strings
1,641
0.772
Medium
23,836
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2165292/Simple-Python-1-liner-with-builtin-math.comb-method
class Solution: def countVowelStrings(self, n: int) -> int: return math.comb(n + 4, n)
count-sorted-vowel-strings
Simple Python 1 liner with builtin math.comb method
rahulsura
0
40
count sorted vowel strings
1,641
0.772
Medium
23,837
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2029507/Python3-Solution-with-using-dynamic-programming
class Solution: def countVowelStrings(self, n: int) -> int: dp = [[0 for _ in range(5 + 1)] for _ in range(n)] for i in range(len(dp[0]) - 2, -1, -1): dp[0][i] = len(dp[0]) -1 - i for i in range(1, len(dp)): for j in range(len(dp[0]) - 2, -1, -1): dp[i][j] = dp[i - 1][j] + dp[i][j + 1] return dp[-1][0]
count-sorted-vowel-strings
[Python3] Solution with using dynamic programming
maosipov11
0
8
count sorted vowel strings
1,641
0.772
Medium
23,838
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2029353/Simple-Python-Solution-Using-itertools-oror-Explained
class Solution(object): def countVowelStrings(self, n): vowels = 'aeiou' comb = set(itertools.combinations_with_replacement(vowels,n)) return len(comb)
count-sorted-vowel-strings
Simple Python Solution Using itertools || Explained
NathanPaceydev
0
20
count sorted vowel strings
1,641
0.772
Medium
23,839
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2029353/Simple-Python-Solution-Using-itertools-oror-Explained
class Solution(object): def countVowelStrings(self, n): return len(set(itertools.combinations_with_replacement('aeiou',n)))
count-sorted-vowel-strings
Simple Python Solution Using itertools || Explained
NathanPaceydev
0
20
count sorted vowel strings
1,641
0.772
Medium
23,840
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2029244/Count-Sorted-Vowel-Strings
class Solution: def countVowelStrings(self, n: int) -> int: return ((n+4)*(n+3)*(n+2)*(n+1)//24)
count-sorted-vowel-strings
Count Sorted Vowel Strings
im_harshal11
0
14
count sorted vowel strings
1,641
0.772
Medium
23,841
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2028475/Python-Math-One-Line-Solution
class Solution: def countVowelStrings(self, n: int) -> int: return (n + 4) * (n + 3) * (n + 2) * (n + 1) // 24
count-sorted-vowel-strings
[ Python ] Math One Line Solution
crazypuppy
0
13
count sorted vowel strings
1,641
0.772
Medium
23,842
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2028068/python-java-simple-and-small-DP-solution-(commented)
class Solution: def countVowelStrings(self, n: int) -> int: vowels = [1,1,1,1,1] while(True): n -= 1 if n == 0 : return sum(vowels) for i in range(1,5): vowels[i] += vowels[i-1]
count-sorted-vowel-strings
python, java - simple & small DP solution (commented)
ZX007java
0
7
count sorted vowel strings
1,641
0.772
Medium
23,843
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027754/Easy-Python3-Solution
class Solution: def __init__(self): self.vowels = ["a","e","i","o","u"] def countVowelStrings(self, n: int) -> int: self.result = [] for i in range(n): if i==0: self.result = self.vowels else: self.result = [el+res for el in self.vowels for res in self.result if el<=res ] return len(self.result)
count-sorted-vowel-strings
Easy Python3 Solution
yashchandani98
0
6
count sorted vowel strings
1,641
0.772
Medium
23,844
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027600/python-3-oror-one-line-oror-O(1)O(1)
class Solution: def countVowelStrings(self, n: int) -> int: return ((n + 1)*(n + 1)*(n + 2)*(2*n + 3) // 6 + (n + 1)*(n + 1)*(n + 2) // 2 - (n + 1)*(n + 1)*(n + 2)*(n + 2) // 4 + (n + 1)*(n + 2) // 2) // 2
count-sorted-vowel-strings
python 3 || one line || O(1)/O(1)
dereky4
0
25
count sorted vowel strings
1,641
0.772
Medium
23,845
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027390/Python-backtracking.-Slow-but-steady-and-does-the-job-%3A-)
class Solution: def countVowelStrings(self, n: int) -> int: def backtrack(i, v): if i == n: self.result += 1 return for idx in range(v, 5): backtrack(i + 1, idx) self.result = 0 backtrack(0, 0) return self.result
count-sorted-vowel-strings
Python, backtracking. Slow, but steady and does the job :-)
blue_sky5
0
12
count sorted vowel strings
1,641
0.772
Medium
23,846
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027304/Python-or-Backtrack-or-DP
class Solution: def countVowelStrings(self, n: int) -> int: arr = ['a', 'e', 'i', 'o', 'u'] result = [] candidate = [] count = 0 def backtrack(index, n): nonlocal count if n == 0: count += 1 result.append(''.join(candidate)) return for i in range(index, 5): candidate.append(arr[i]) backtrack(i, n-1) candidate.pop() backtrack(0, n) return count
count-sorted-vowel-strings
Python | Backtrack | DP
Mikey98
0
26
count sorted vowel strings
1,641
0.772
Medium
23,847
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027304/Python-or-Backtrack-or-DP
class Solution: def countVowelStrings(self, n: int) -> int: # dp[i][j] represents total no. of string of length i , starting with characters of column j and after j. # dp[i][j] = dp[i - 1][j] + dp[i][j + 1] dp = [[i for i in range(5,0,-1)] for _ in range(n)] for i in range(1,n): for j in range(3,-1,-1): dp[i][j] = dp[i - 1][j] + dp[i][j + 1] return dp[n-1][0]
count-sorted-vowel-strings
Python | Backtrack | DP
Mikey98
0
26
count sorted vowel strings
1,641
0.772
Medium
23,848
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/2027285/Python-Simple-and-Easy-Solution-oror-O(1)-Time-complexity
class Solution: def countVowelStrings(self, n: int) -> int: return int((n + 4) * (n + 3) * (n + 2) * (n + 1) / 24)
count-sorted-vowel-strings
Python - Simple and Easy Solution || O(1) Time complexity
dayaniravi123
0
27
count sorted vowel strings
1,641
0.772
Medium
23,849
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/1185347/Python-oror-99.43-faster-oror-Simple-Math-formula-oror-Well-Explained
class Solution: def countVowelStrings(self, r: int) -> int: # (n+r-1)!//(n-1!)*(r!) # n=5 and r as parameter num = math.factorial(5+r-1) d1 = 24 # math.fact(4) = 24 d2 = math.factorial(r) den = d1*d2 return num//den
count-sorted-vowel-strings
Python || 99.43% faster || Simple Math formula || Well Explained
abhi9Rai
0
188
count sorted vowel strings
1,641
0.772
Medium
23,850
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/1122374/Python-DP-Solution-or-O(n)-Time-and-Space-asymptotics-or-Faster-than-91.57-solutions
class Solution: def countVowelStrings(self, n: int) -> int: dp = [[0]*5 for _ in range(51)] dp[0] = [1]*5 for i in range(1, 51): c = 0 for j in range(4, -1, -1): c += dp[i-1][j] dp[i][j] = c return(dp[n][0])
count-sorted-vowel-strings
[Python] DP Solution | O(n) Time & Space asymptotics | Faster than 91.57% solutions
vvedant99
0
115
count sorted vowel strings
1,641
0.772
Medium
23,851
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/963952/Simple-Python-Recursive-Solution
class Solution(object): def countVowelStrings(self, n, p=0, sumVal=0): if n == 0: return 1 for i in range(p, 5): sumVal += self.countVowelStrings(n-1, i) return sumVal
count-sorted-vowel-strings
Simple Python Recursive Solution
theriley106
0
98
count sorted vowel strings
1,641
0.772
Medium
23,852
https://leetcode.com/problems/count-sorted-vowel-strings/discuss/963952/Simple-Python-Recursive-Solution
class Solution(object): def countVowelStrings(self, n, p=0): if n == 0: return 1 return sum([self.countVowelStrings(n-1, i) for i in range(p, 5)])
count-sorted-vowel-strings
Simple Python Recursive Solution
theriley106
0
98
count sorted vowel strings
1,641
0.772
Medium
23,853
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2176666/Python-or-Min-Heap-or-With-Explanation-or-Easy-to-Understand
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: # prepare: use a min heap to store each difference(climb) between two contiguous buildings # strategy: use the ladders for the longest climbs and the bricks for the shortest climbs min_heap = [] n = len(heights) for i in range(n-1): climb = heights[i+1] - heights[i] if climb <= 0: continue # we need to use a ladder or some bricks, always take the ladder at first if climb > 0: heapq.heappush(min_heap, climb) # ladders are all in used, find the current shortest climb to use bricks instead! if len(min_heap) > ladders: # find the current shortest climb to use bricks brick_need = heapq.heappop(min_heap) bricks -= brick_need if bricks < 0: return i return n-1
furthest-building-you-can-reach
Python | Min Heap | With Explanation | Easy to Understand
Mikey98
32
1,800
furthest building you can reach
1,642
0.483
Medium
23,854
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/1881964/Python-easy-to-read-and-understand-or-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: pq = [] n = len(heights) for i in range(n-1): diff = heights[i+1] - heights[i] if diff > 0: heapq.heappush(pq, diff) if len(pq) > ladders: bricks = bricks-heapq.heappop(pq) if bricks < 0: return i return n-1
furthest-building-you-can-reach
Python easy to read and understand | heap
sanial2001
4
235
furthest building you can reach
1,642
0.483
Medium
23,855
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/918419/Python3-greedy-via-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: pq = [] # max heap (priority queue) for i in range(1, len(heights)): ht = heights[i] - heights[i-1] # height diff if ht > 0: heappush(pq, -ht) bricks -= ht if bricks < 0: # not enough bricks if ladders == 0: return i-1 # i not reachable bricks += -heappop(pq) # swapping ladder with most bricks ladders -= 1 return i
furthest-building-you-can-reach
[Python3] greedy via heap
ye15
3
181
furthest building you can reach
1,642
0.483
Medium
23,856
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/918419/Python3-greedy-via-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: pq = [] for i in range(1, len(heights)): diff = heights[i] - heights[i-1] if diff > 0: heappush(pq, -diff) bricks -= diff if bricks < 0: if not ladders: return i-1 bricks -= heappop(pq) ladders -= 1 return len(heights)-1
furthest-building-you-can-reach
[Python3] greedy via heap
ye15
3
181
furthest building you can reach
1,642
0.483
Medium
23,857
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/918419/Python3-greedy-via-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: pq = [] # min heap for i in range(1, len(heights)): diff = heights[i] - heights[i-1] if diff > 0: heappush(pq, diff) if len(pq) > ladders: bricks -= heappop(pq) if bricks < 0: return i-1 return len(heights) - 1
furthest-building-you-can-reach
[Python3] greedy via heap
ye15
3
181
furthest building you can reach
1,642
0.483
Medium
23,858
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2176571/Python-heap.-Time%3A-O(N-log-N)-Space%3A-O(N)
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: idx = 0 h = [] while idx < len(heights) - 1: diff = heights[idx + 1] - heights[idx] if diff <= 0: pass elif diff <= bricks: heapq.heappush(h, -diff) bricks -= diff elif ladders > 0: heapq.heappush(h, -diff) max_bricks = -heapq.heappop(h) ladders -= 1 bricks += max_bricks - diff else: break idx += 1 return idx
furthest-building-you-can-reach
Python, heap. Time: O(N log N), Space: O(N)
blue_sky5
1
74
furthest building you can reach
1,642
0.483
Medium
23,859
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2139261/python-3-oror-simple-min-heap-solution
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: laddersUsed = [] for i in range(len(heights) - 1): diff = heights[i + 1] - heights[i] if diff <= 0: continue heapq.heappush(laddersUsed, diff) if ladders: ladders -= 1 continue bricks -= heapq.heappop(laddersUsed) if bricks < 0: return i return len(heights) - 1
furthest-building-you-can-reach
python 3 || simple min heap solution
dereky4
1
116
furthest building you can reach
1,642
0.483
Medium
23,860
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/1634796/Python3-O(n)-faster-than-92-Solution-using-a-min-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: furthest = 0 q = [] for i in range(1, len(heights)): delta = heights[i] - heights[i-1] if delta > 0: heapq.heappush(q, delta) if ladders == 0: bricks -= heapq.heappop(q) else: ladders -= 1 if bricks < 0: return i - 1 return len(heights)-1
furthest-building-you-can-reach
[Python3] [O(n)] [faster than 92%] Solution using a min-heap
lolitsgab
1
149
furthest building you can reach
1,642
0.483
Medium
23,861
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/1179221/PythonPython3-Solution-using-minHeap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: minHeap = [] for i in range(len(heights)-1): buildHeight = heights[i+1]-heights[i] if buildHeight <= 0: continue heappush(minHeap,buildHeight) if len(minHeap) >ladders: bricks -= heappop(minHeap) if bricks < 0: return i return len(heights) - 1
furthest-building-you-can-reach
Python/Python3 Solution using minHeap
prasanthksp1009
1
115
furthest building you can reach
1,642
0.483
Medium
23,862
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/918653/Python-Binary-Search
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: # return max if we have inf ladders length = len(heights) if ladders == length: return length-1 # # def helper function canIReach(List, l, b): return true if I can, return false if I can not # def canIReach(mylist, l, b): if mylist == None: mylist = hdef mylist.sort() if l==0: test = mylist else: test = mylist[:-l] if b >= sum(test): return True else: return False # hdef this is the list of [#bricks needed to get to next building] # bum is the list of [building # that need either bricks or ladders] hdef= [] bnum=[] for i in range(length-1): dif = heights[i]-heights[i+1] if dif < 0: hdef.append(-dif) bnum.append(i) # if we have more ladders, return max if len(hdef) <= ladders: return length-1 # Otherwise, we compare from hdef[ladders:] do a binary search: left = ladders right = len(hdef) while (left< right): mid = left + (right-left)//2 if (canIReach(hdef[:mid+1],ladders,bricks)): left = mid+1 else: right = mid # now left is last building that we can reach with a tool if left >= len(hdef): return length-1 return bnum[left]
furthest-building-you-can-reach
Python Binary Search
meiyaowen
1
159
furthest building you can reach
1,642
0.483
Medium
23,863
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2228568/Python3-Solution-with-using-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: heap = [] for i in range(len(heights) - 1): h_diff = heights[i + 1] - heights[i] if h_diff > 0: heapq.heappush(heap, h_diff) if len(heap) > ladders: bricks -= heapq.heappop(heap) if bricks < 0: return i return len(heights) - 1
furthest-building-you-can-reach
[Python3] Solution with using heap
maosipov11
0
37
furthest building you can reach
1,642
0.483
Medium
23,864
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2179457/Python-Recursion-%2B-Memoization-%2B-Heap
class Solution: def furthestBuilding(self, heights: list[int], bricks: int, ladders: int) -> int: return self.furthestBuildingHelper(heights, bricks, ladders, 0) # Hypothesis: Function, will always return maximum number of buildings climbed def furthestBuildingHelper(self, heights, bricks, ladders, i) -> int: # if at final building if i >= len(heights) - 1: return 0 # If no bricks left and no ladders left and also next building height is greater if heights[i+1] > heights[i] and (ladders <= 0 and bricks <= (heights[i+1] - heights[i])): return 0 # If buliding can be climbed, without the ladder or bricks # CASE: 1 if Building is of same length or lower if heights[i+1] <= heights[i]: return 1 + self.furthestBuildingHelper(heights, bricks, ladders, i + 1) else: # CASE: 2 if we use ladder to climb the building index1 = 0 if ladders > 0: index1 = 1 + self.furthestBuildingHelper( heights, bricks, ladders - 1, i + 1) # CASE: 3 if we use bricks to climb the building index2 = 0 if bricks > (heights[i + 1] - heights[i]): index2 = 1 + self.furthestBuildingHelper( heights, bricks - (heights[i + 1] - heights[i]), ladders, i + 1) # Now, we need maximum from both options from CASE: 2 and CASE: 3 return max(index1, index2)
furthest-building-you-can-reach
Python Recursion + Memoization + Heap
zippysphinx
0
61
furthest building you can reach
1,642
0.483
Medium
23,865
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2179457/Python-Recursion-%2B-Memoization-%2B-Heap
class Solution: def furthestBuilding(self, heights: list[int], bricks: int, ladders: int) -> int: memo = [[[-1 for _ in range(ladders + 1)] for _ in range(bricks + 1)] for _ in range(len(heights) + 1)] return self.furthestBuildingHelper(heights, bricks, ladders, 0, memo) def furthestBuildingHelper(self, heights, bricks, ladders, i, memo) -> int: if i >= len(heights) - 1: return 0 if bricks <= 0 and ladders <= 0 and heights[i + 1] > heights[i]: return 0 if memo[i][bricks][ladders] != -1: return memo[i][bricks][ladders] if heights[i+1] <= heights[i]: return 1 + self.furthestBuildingHelper(heights, bricks, ladders, i + 1, memo) output1 = 0 output2 = 0 if heights[i + 1] - heights[i] <= bricks: output1 = 1 + self.furthestBuildingHelper(heights, bricks - (heights[i + 1] - heights[i]), ladders, i + 1, memo) if ladders > 0: output2 = 1 + self.furthestBuildingHelper(heights, bricks, ladders - 1, i + 1, memo) memo[i][bricks][ladders] = max(output1, output2) return memo[i][bricks][ladders]
furthest-building-you-can-reach
Python Recursion + Memoization + Heap
zippysphinx
0
61
furthest building you can reach
1,642
0.483
Medium
23,866
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/2179457/Python-Recursion-%2B-Memoization-%2B-Heap
class Solution: def furthestBuilding(self, heights: list[int], bricks: int, ladders: int) -> int: memo = [[-1 for _ in range(ladders + 1)] for _ in range(bricks + 1)] return self.furthestBuildingHelper(heights, bricks, ladders, 0, memo) def furthestBuildingHelper(self, heights, bricks, ladders, i, memo) -> int: if i >= len(heights) - 1: return 0 if bricks <= 0 and ladders <= 0 and heights[i + 1] > heights[i]: return 0 if memo[bricks][ladders] != -1: return memo[bricks][ladders] if heights[i+1] <= heights[i]: return 1 + self.furthestBuildingHelper(heights, bricks, ladders, i + 1, memo) output1 = 0 output2 = 0 if heights[i + 1] - heights[i] <= bricks: output1 = 1 + self.furthestBuildingHelper(heights, bricks - (heights[i + 1] - heights[i]), ladders, i + 1, memo) if ladders > 0: output2 = 1 + self.furthestBuildingHelper(heights, bricks, ladders - 1, i + 1, memo) memo[bricks][ladders] = max(output1, output2) return memo[bricks][ladders]
furthest-building-you-can-reach
Python Recursion + Memoization + Heap
zippysphinx
0
61
furthest building you can reach
1,642
0.483
Medium
23,867
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/1140107/Python-clean-logic-without-heap
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: l=len(heights) l1=[] i=0 while(i<l-1): if(heights[i]>=heights[i+1]): i=i+1 continue else: break if(ladders>0): while(i<l-1): if(heights[i]>=heights[i+1]): i=i+1 continue else: l1.append(heights[i+1]-heights[i]) if(len(l1)==ladders): i=i+1 break i=i+1 l1.sort() if(bricks>0): while(i<l-1): if(heights[i]>=heights[i+1]): i=i+1 continue else: if(len(l1)>0): if(l1[0]<(heights[i+1]-heights[i])): if(l1[0]<=bricks): bricks=bricks-l1[0] l1[0]=heights[i+1]-heights[i] l1.sort() i=i+1 continue if(bricks>=(heights[i+1]-heights[i])): bricks=bricks-(heights[i+1]-heights[i]) else: break i=i+1 return i
furthest-building-you-can-reach
Python clean logic without heap
Rajashekar_Booreddy
0
155
furthest building you can reach
1,642
0.483
Medium
23,868
https://leetcode.com/problems/furthest-building-you-can-reach/discuss/918884/Python-100-speed-100-space-Clean-code-Explanation
class Solution: def furthestBuilding(self, heights: List[int], bricks: int, ladders: int) -> int: s = 0 for i in range(1,len(heights)): if heights[i] > heights[i-1]: s += heights[i] - heights[i-1] # height to be climbed if bricks >= s: # if bricks are available, continue continue elif ladders > 0: # else use ladder and note: remove the height to be climbed from total sum s -= heights[i] - heights[i-1] ladders -= 1 # remove a ladder else: return i - 1 # i-1 th building was the last building which was reached return len(heights) - 1 # last building reached
furthest-building-you-can-reach
Python [100% speed] [100% space] [Clean code] [Explanation]
mihirrane
-1
111
furthest building you can reach
1,642
0.483
Medium
23,869
https://leetcode.com/problems/kth-smallest-instructions/discuss/918429/Python3-greedy
class Solution: def kthSmallestPath(self, destination: List[int], k: int) -> str: m, n = destination # m "V" &amp; n "H" in total ans = "" while n: kk = comb(m+n-1, n-1) # (m+n-1 choose n-1) instructions starting with "H" if kk >= k: ans += "H" n -= 1 else: ans += "V" m -= 1 k -= kk return ans + m*"V"
kth-smallest-instructions
[Python3] greedy
ye15
5
286
kth smallest instructions
1,643
0.469
Hard
23,870
https://leetcode.com/problems/kth-smallest-instructions/discuss/2682554/Eay-Python-O(n**2)
class Solution: def kthSmallestPath(self, destination: List[int], k: int) -> str: x,y=destination arr=[[0 for i in range(y+1)] for j in range(x+1)] arr[x][y]=1 for i in range(x,-1,-1): for j in range(y,-1,-1): if i+1<=x: arr[i][j]+=arr[i+1][j] if j+1<=y: arr[i][j]+=arr[i][j+1] i,j=0,0 r='' for _ in range(x+y): if i<=x and j+1<=y and arr[i][j+1]>=k: r+='H' j+=1 else: r+='V' k-=arr[i][j+1] if i<=x and j+1<=y else 0 i+=1 return r
kth-smallest-instructions
Eay Python O(n**2)
saianirudh_me
0
5
kth smallest instructions
1,643
0.469
Hard
23,871
https://leetcode.com/problems/kth-smallest-instructions/discuss/1965604/Python-solution
class Solution: def kthSmallestPath(self, destination: List[int], k: int) -> str: from math import comb result=[] r=destination[0] c=destination[1] down=r for i in range(r+c): total=r+c-(i+1) step=comb(total,down) if step>=k: result.append('H') else: down-=1 result.append('V') k-=step return ''.join(result)
kth-smallest-instructions
Python solution
g0urav
0
55
kth smallest instructions
1,643
0.469
Hard
23,872
https://leetcode.com/problems/kth-smallest-instructions/discuss/918667/Python-real-O(M%2BN)
class Solution: def fact(self, n): """factorial with cache""" if n in self.fact_cache: return self.fact_cache[n] x = self.fact(n-1) * n self.fact_cache[n] = x return x def max_k(self, x, y): """number of combinations for ordering x and y identical elements if x < 0, return 0 """ if x < 0: # special case for our problem where there is no H left return 0 return self.fact(x+y) // self.fact(x) // self.fact(y) def kthSmallestPath(self, destination: List[int], k: int) -> str: self.fact_cache = {0: 1} y, x = destination # numbers of V and H moves out = '' for i in range(x+y): # the number of combinations if the next element is H split = self.max_k(x-1, y) if k > split: out += 'V' y -= 1 k -= split else: out += 'H' x -= 1 return out
kth-smallest-instructions
[Python] real O(M+N)
louis925
0
83
kth smallest instructions
1,643
0.469
Hard
23,873
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1754391/Python-O(1)-solution
class Solution: def getMaximumGenerated(self, n: int) -> int: max_nums = [0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 11, 11, 11, 11, 11, 11, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21] return max_nums[n]
get-maximum-in-generated-array
Python O(1) solution
wssx349
18
510
get maximum in generated array
1,646
0.502
Easy
23,874
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1158473/Python3-simple-solution-best-90-users
class Solution: def getMaximumGenerated(self, n: int) -> int: if n == 0: return 0 elif n == 1: return 1 else: nums = [0,1] for i in range(2,n+1): if i%2 == 0: nums.append(nums[i//2]) else: nums.append(nums[i//2]+nums[(i//2)+1]) return max(nums)
get-maximum-in-generated-array
Python3 simple solution best 90% users
EklavyaJoshi
2
62
get maximum in generated array
1,646
0.502
Easy
23,875
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2433098/Simple-Python-Solution
class Solution: def getMaximumGenerated(self, n: int) -> int: if n < 2: return n arr = [0]*(n+1) arr[1] = 1 max_num = 0 for i in range(2, n+1): if i%2 == 0: arr[i] = arr[i//2] else: arr[i] = arr[i//2] + arr[i//2 + 1] max_num = max(max_num, arr[i]) return max_num
get-maximum-in-generated-array
Simple Python Solution
zip_demons
1
90
get maximum in generated array
1,646
0.502
Easy
23,876
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2271104/Solution-(-96.51-Faster)
class Solution: def getMaximumGenerated(self, n: int) -> int: l1 = [0,1] if n == 2 or n == 1: return (1) elif n == 0: return (0) for i in range(1,n): l1.append(l1[i]) if (i * 2) == n: break l1.append((l1[i]) + (l1[i+1])) if (((i * 2)+1) == n): break return (max(l1))
get-maximum-in-generated-array
Solution ( 96.51% Faster)
fiqbal997
1
78
get maximum in generated array
1,646
0.502
Easy
23,877
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1587108/Python-3-O(n)
class Solution: def getMaximumGenerated(self, n: int) -> int: if n <= 1: return n nums = [0, 1] res = 1 for k in range(2, n + 1): if k % 2 == 0: nums.append(nums[k // 2]) else: nums.append(nums[k // 2] + nums[k // 2 + 1]) res = max(res, nums[-1]) return res
get-maximum-in-generated-array
Python 3 O(n)
dereky4
1
118
get maximum in generated array
1,646
0.502
Easy
23,878
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/927516/Python3-create-array-as-told
class Solution: def getMaximumGenerated(self, n: int) -> int: if not n: return 0 # edge case ans = [0, 1] for i in range(2, n+1): ans.append(ans[i>>1] + (ans[i+1>>1] if i&amp;1 else 0)) return max(ans)
get-maximum-in-generated-array
[Python3] create array as told
ye15
1
136
get maximum in generated array
1,646
0.502
Easy
23,879
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/927516/Python3-create-array-as-told
class Solution: def getMaximumGenerated(self, n: int) -> int: @lru_cache(None) def fn(k): """Return kth value.""" if k <= 1: return k if k&amp;1: return fn(k>>1) + fn(k+1>>1) return fn(k>>1) return max(fn(k) for k in range(n+1))
get-maximum-in-generated-array
[Python3] create array as told
ye15
1
136
get maximum in generated array
1,646
0.502
Easy
23,880
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2836329/Python-easy-code-solution-or-For-Beginners
class Solution: def getMaximumGenerated(self, n: int) -> int: nums = [0] * (n+1) if(n == 0): return 0 elif(n == 1): return 1 else: nums[0] = 0 nums[1] = 1 for i in range(1, n+1): if(2 <= 2 * i <= n): nums[2*i] = nums[i] if(2 <= (2 * i) + 1 <= n): nums[(2 * i) + 1] = nums[i] + nums[i+1] return max(nums)
get-maximum-in-generated-array
Python easy code solution | For Beginners
anshu71
0
4
get maximum in generated array
1,646
0.502
Easy
23,881
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2779207/Easy-code
class Solution: def getMaximumGenerated(self, n: int) -> int: temp=[] for i in range (0,n+1): if i==0: temp.append(0) elif i==1: temp.append(1) elif i%2==0: temp.append(temp[i//2]) else : temp.append(temp[i//2]+temp[(i//2)+1]) return max(temp)
get-maximum-in-generated-array
Easy code
nishithakonuganti
0
2
get maximum in generated array
1,646
0.502
Easy
23,882
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2697899/Easy-Solution-using-list-manipulation
class Solution: def getMaximumGenerated(self, n: int) -> int: nums = [0]*(n+2) nums[0] = 0 nums[1] = 1 if n<1: return 0 else: for i in range(2,n+1): if i%2==0: nums[i]=nums[i//2] else: nums[i]=nums[i//2]+nums[(i//2)+1] return max(nums)
get-maximum-in-generated-array
Easy Solution using list manipulation
AnandSinhaProjects
0
4
get maximum in generated array
1,646
0.502
Easy
23,883
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2561912/EASY-PYTHON3-SOLUTION
class Solution: def getMaximumGenerated(self, n: int) -> int: if n==0 or n==1: return n arr=[0]*(n+1) arr[1]=1 largest=1 for i in range(2,n+1): if i%2==0: arr[i]=arr[i//2] else: arr[i]=arr[i//2]+arr[i//2+1] if arr[i]>largest: largest=arr[i] return largest
get-maximum-in-generated-array
✅✔ EASY PYTHON3 SOLUTION
rajukommula
0
36
get maximum in generated array
1,646
0.502
Easy
23,884
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2270136/Get-Max.-in-Gen.-Array-or-Python-3(DP-with-Bottom-Up-Approach)
class Solution: def getMaximumGenerated(self, n: int) -> int: #memoization array w/ bottom-up approach! memo = [0, 1] #base cases! if(n == 0): return 0 if(n == 1): return 1 for a in range(2, n+1): if(a % 2 == 0): reference = memo[int(a/2)] memo.append(reference) else: index = int(a // 2) one_val = memo[index] another_val = memo[index + 1] summation = one_val + another_val memo.append(summation) #run through each element in memo and update for max! maximum = float(-inf) for element in memo: if element > maximum: maximum = element return maximum
get-maximum-in-generated-array
Get Max. in Gen. Array | Python 3(DP with Bottom-Up Approach)
JOON1234
0
20
get maximum in generated array
1,646
0.502
Easy
23,885
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2162198/Dynamic-Programming-oror-Fastest-Optimal-Solution-oror-TC-%3A-O(N)-oror-SC-%3A-O(N)
class Solution: def getMaximumGenerated(self, n: int) -> int: dpArray = [-1]*(n+1) if n <= 1: return n dpArray[0] = 0 dpArray[1] = 1 for i in range(2,n+1): if i%2 == 0: dpArray[i] = dpArray[i//2] else: dpArray[i] = dpArray[(i-1)//2] + dpArray[((i-1)//2)+1] return max(dpArray)
get-maximum-in-generated-array
Dynamic Programming || Fastest Optimal Solution || TC :- O(N) || SC :- O(N)
Vaibhav7860
0
78
get maximum in generated array
1,646
0.502
Easy
23,886
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/2066657/19ms-100-Faster-(Best)
class Solution: def getMaximumGenerated(self, n: int) -> int: nums = [0] * (n + 1) for i in range(1, n + 1): if i == 1: nums[i] = 1 val = 2 * i if 2 <= val <= n: nums[val] = nums[i] val += 1 if 2 <= val <= n: nums[val] = nums[i] + nums[i + 1] return max(nums)
get-maximum-in-generated-array
19ms 100% Faster (Best)
andrewnerdimo
0
39
get maximum in generated array
1,646
0.502
Easy
23,887
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1993210/Python-easy-iterative-solution-for-beginners
class Solution: def getMaximumGenerated(self, n: int) -> int: if n == 0: return 0 if n == 1: return 1 nums = [0] * (n + 1) nums[1] = 1 pos = 1 ind = 2 while ind < n: nums[ind] = nums[pos] ind += 1 nums[ind] = nums[pos] + nums[pos + 1] ind += 1 pos += 1 return max(nums)
get-maximum-in-generated-array
Python easy iterative solution for beginners
alishak1999
0
28
get maximum in generated array
1,646
0.502
Easy
23,888
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1889734/Python
class Solution: def getMaximumGenerated(self, n: int) -> int: if n==0: return 0 nums = [0]*(n+1) nums[1]=1 for i in range(1,n//2+1): nums[2 * i] = nums[i] if 2 * i + 1 <= n: nums[2 * i + 1] = nums[i] + nums[i + 1] return max(nums)
get-maximum-in-generated-array
Python
zouhair11elhadi
0
67
get maximum in generated array
1,646
0.502
Easy
23,889
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1831228/6-Lines-Python-Solution-oror-95-Faster-(28ms)-oror-Memory-less-than-60
class Solution: def getMaximumGenerated(self, n: int) -> int: if n==0: return 0 nums = [0,1] for i in range(2,n+1): if i%2==0: nums.append(nums[i//2]) else: nums.append(nums[i//2]+nums[i//2+1]) return max(nums)
get-maximum-in-generated-array
6-Lines Python Solution || 95% Faster (28ms) || Memory less than 60%
Taha-C
0
52
get maximum in generated array
1,646
0.502
Easy
23,890
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1763902/Python-dollarolution
class Solution: def getMaximumGenerated(self, n: int) -> int: v = [0,1] for i in range(2,n+1,2): v.append(v[i//2]) v.append(v[i//2] + v[i//2+1]) if n%2 ==0: return max(v[:-1]) return max(v)
get-maximum-in-generated-array
Python $olution
AakRay
0
38
get maximum in generated array
1,646
0.502
Easy
23,891
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1759513/Python-Dynamic-Programming-solution-explained
class Solution: def getMaximumGenerated(self, n: int) -> int: # for n in [0, 1]; return early if n < 2: return n # maintain a dp array which will # store the generated array dp = [0] * (n + 1) # initialize the first two elements # dp[0] = 0 is already set above fyi dp[1] = 1 # the whole game is about having an # index variable, if you think about # the pattern, it lends itself to dp # something like this: # # if i is odd: dp[i] = dp[idx] + dp[idx + 1] # if i is even: dp[i] = dp[idx] # Now, we're only going to increment idx # when i is odd as you can see in the pattern below: # # idx = 1 (starting) # dp[(idx*2)] = dp[2] # dp[(idx*2)+1] = dp[3]; idx += 1 # dp[(idx*2)] = dp[4] # dp[(idx*2)+1] = dp[5]; idx += 1 idx, res = 1, 0 for i in range(2, n+1): if i % 2 != 0: dp[i] = dp[idx] + dp[idx+1] idx += 1 else: dp[i] = dp[idx] res = max(res, dp[i]) return res
get-maximum-in-generated-array
[Python] Dynamic Programming solution explained
buccatini
0
139
get maximum in generated array
1,646
0.502
Easy
23,892
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1632310/C%2B%2B-and-Python-or-0ms-Faster-than-100-or-Simple-and-Readable
class Solution: def getMaximumGenerated(self, n: int) -> int: if n < 2: return n nums = [0, 1] result = -2000 for i in range(2, n + 1): if i % 2 == 0: nums.append(nums[i // 2]) else: nums.append(nums[i // 2] + nums[(i // 2) + 1]) result = max(result, nums[i]) # you could ditch result and return max(nums) at the end too return result
get-maximum-in-generated-array
C++ & Python | 0ms Faster than 100% | Simple and Readable
indujashankar
0
84
get maximum in generated array
1,646
0.502
Easy
23,893
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1547294/loop-for-n2-times-only
class Solution: def getMaximumGenerated(self, n: int) -> int: if n == 0: return 0 dp = [0]*(n+1) dp[0], dp[1] = 0, 1 for i in range((n+1)//2): dp[2*i] = dp[i] dp[(2*i)+1] = dp[i] +dp[i+1] return max(dp)
get-maximum-in-generated-array
loop for n//2 times only
siddp6
0
68
get maximum in generated array
1,646
0.502
Easy
23,894
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/1426233/Python3-Simulation-Short-Solution-Faster-Than-85.63
class Solution: def getMaximumGenerated(self, n: int) -> int: nums, i = [0, 1], 1 while len(nums) < n + 1: nums += [nums[i]] nums += [nums[i] + nums[i + 1]] i += 1 return max(nums[:-1]) if n % 2 == 0 else max(nums)
get-maximum-in-generated-array
Python3 Simulation Short Solution, Faster Than 85.63%
Hejita
0
32
get maximum in generated array
1,646
0.502
Easy
23,895
https://leetcode.com/problems/get-maximum-in-generated-array/discuss/943746/Intuitive-approach-by-caching-calculation-result
class Solution: def __init__(self): self.nums = [0, 1] for i in range(2, 101): d, r = divmod(i, 2) if r == 0: self.nums.append(self.nums[d]) else: p = int(i/2) self.nums.append(self.nums[d] + self.nums[d+1]) def getMaximumGenerated(self, n: int) -> int: ''' 0 1 2 3 4 5 6 7 8 9 ... 0 1 1 2 1 3 2 3 1 4 ... ''' return max(self.nums[:n+1])
get-maximum-in-generated-array
Intuitive approach by caching calculation result
puremonkey2001
0
29
get maximum in generated array
1,646
0.502
Easy
23,896
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/927527/Python3-most-to-least-frequent-characters
class Solution: def minDeletions(self, s: str) -> int: freq = {} # frequency table for c in s: freq[c] = 1 + freq.get(c, 0) ans = 0 seen = set() for k in sorted(freq.values(), reverse=True): while k in seen: k -= 1 ans += 1 if k: seen.add(k) return ans
minimum-deletions-to-make-character-frequencies-unique
[Python3] most to least frequent characters
ye15
34
4,100
minimum deletions to make character frequencies unique
1,647
0.592
Medium
23,897
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/927527/Python3-most-to-least-frequent-characters
class Solution: def minDeletions(self, s: str) -> int: freq = {} # frequency table for c in s: freq[c] = 1 + freq.get(c, 0) ans = 0 seen = set() for k in freq.values(): while k in seen: k -= 1 ans += 1 if k: seen.add(k) return ans
minimum-deletions-to-make-character-frequencies-unique
[Python3] most to least frequent characters
ye15
34
4,100
minimum deletions to make character frequencies unique
1,647
0.592
Medium
23,898
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207086/Python-Easy-Sorting-approach
class Solution: def minDeletions(self, s: str) -> int: freqs=[0]*26 # capture count of all lowercase characters for c in s: freqs[ord(c)-ord('a')]+=1 freqs.sort(reverse=True) # reverse sort: O(nlogn) deletions=0 #initialize with max allowed frequency # eg: if 5 is taken, then the next best frequency count is 4 # hence, max_freq = 5 - 1 = 4 max_freq=freqs[0]-1 # set max frequency upper limit for freq in freqs[1:]: # if a character frequency is above freq, it means it needs to be reduced # eg: if max_freq=4, then it means all counts above 4 has been already allotted # So we need to reduce the current frequency if freq>max_freq: deletions+=freq-max_freq # update the deletions count freq=max_freq max_freq=max(0, freq-1) # update the max frequency upper limit return deletions
minimum-deletions-to-make-character-frequencies-unique
Python Easy Sorting approach
constantine786
9
848
minimum deletions to make character frequencies unique
1,647
0.592
Medium
23,899