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/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/491784/Python3-Floyd-Warshall-algo | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
"""Floyd-Warshall algorithm"""
dist = [[float("inf")]*n for _ in range(n)]
for i in range(n): dist[i][i] = 0
for i, j, w in edges: dist[i][j] = dist[j][i] = w
fo... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | [Python3] Floyd-Warshall algo | ye15 | 2 | 98 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,000 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/1137275/Python-or-Dijkstra's-Algorithm-or-Faster-than-97 | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
graph = {i:dict() for i in range(n)}
for u,v,w in edges:
graph[u][v] = w
graph[v][u] = w
neighbors = [0]*n
for k in range(n):
di... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Python | Dijkstra's Algorithm | Faster than 97% | anuraggupta29 | 1 | 377 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,001 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/490606/Python-Floyd-Warshall-DP | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], dt: int) -> int:
dist = [[int(1e9)] * n for i in range(n)]
for i, j, d in edges:
dist[i][j] = dist[j][i] = d
for i in range(n):
dist[i][i] = 0
for k in range(n... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Python Floyd Warshall DP | Bug_Exceeded | 1 | 201 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,002 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/2735594/Simple-floyd-warshall | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
distances = [[float("inf")]*n for _ in range(n)]
for city1,city2,dist in edges:
distances[city1][city2] = dist
distances[city2][city1] = dist
for city in range(n):
... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Simple floyd warshall | shriyansnaik | 0 | 2 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,003 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/2735576/Using-Djikstra | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
graph = defaultdict(list)
for city1,city2,dist in edges:
graph[city1].append((city2,dist))
graph[city2].append((city1,dist))
best_count = n+1
city = -1
... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Using Djikstra | shriyansnaik | 0 | 4 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,004 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/2733062/Python-Dijksta's-Solution-explained | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
graph = {i:[] for i in range(n)}
for u, v, wt in edges:
graph[u].append((v, wt))
graph[v].append((u, wt))
def dijksta(city):
dist = [float('inf')] * n
... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Python Dijksta's Solution explained | kritikaparmar | 0 | 17 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,005 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/2671727/Python-diji-beats-98.85 | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
graph = {node : [] for node in range(n)}
adj = [[float('inf')]*n for _ in range(n)]
for i,j,k in edges:
graph[i].append(j)
graph[j].append(i)
adj[i][j]=mi... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Python diji beats 98.85% | Ttanlog | 0 | 19 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,006 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/2671719/Python-diji-beats-73.5 | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
graph = {node : [] for node in range(n)}
adj = [[float('inf')]*n for _ in range(n)]
for i,j,k in edges:
graph[i].append(j)
graph[j].append(i)
adj[i][j]=k
... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Python diji beats 73.5% | Ttanlog | 0 | 8 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,007 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/2418868/City-with-smallest-number-of-neighbours-with-threshold-oror-Python3-oror-Floyd-Warshall | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
mat = [[math.inf] * n for i in range(n)]
for i in range(n):
# Distance of node to itself is zero
mat[i][i] = 0
for i, j, wt in edges:
# Adding wei... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | City with smallest number of neighbours with threshold || Python3 || Floyd-Warshall | vanshika_2507 | 0 | 25 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,008 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/1060544/python3-dijkstra%3A-bfs-%2B-heap | class Solution:
def findTheCity(self, n: int,
edges: List[List[int]],
distanceThreshold: int) -> int:
# issue with DFS approach is that the typical seen set might cut off a
# potential path, and maintaining a growing additional set for each
# recursive... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | python3 - dijkstra: bfs + heap | dachwadachwa | 0 | 76 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,009 |
https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/discuss/640979/Easy-Python-Solution-Floyd-Warshall-Algorithm | class Solution:
def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int:
dis=[]
for i in range(n):
x=[]
for j in range(n):
x.append(float("inf"))
dis.append(x)
for i in range(len(e... | find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance | Easy Python Solution-Floyd Warshall Algorithm | Ayu-99 | 0 | 108 | find the city with the smallest number of neighbors at a threshold distance | 1,334 | 0.533 | Medium | 20,010 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2709132/91-Faster-Solution | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
jobCount = len(jobDifficulty)
if jobCount < d:
return -1
@lru_cache(None)
def topDown(jobIndex: int, remainDayCount: int) -> int:
remainJobCount = jobCount - jobIndex
... | minimum-difficulty-of-a-job-schedule | 91% Faster Solution | namanxk | 4 | 485 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,011 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2708974/Python-3-Memoized-and-Tabulated-Solution-(Credits%3A-Striver-DP-Series) | class Solution1: # Memoized Solution
def minDifficulty(self, arr: List[int], d: int) -> int:
if d>len(arr): return -1
n = len(arr)
dp = [[-1 for i in range(d+1)] for j in range(n)]
def f(ind,d):
if ind==n: return float('inf')
if d==1: # if we have only one d... | minimum-difficulty-of-a-job-schedule | Python 3 Memoized and Tabulated Solution (Credits: Striver DP Series) | Akash3502 | 2 | 91 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,012 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2708778/python3-or-dp-or-dfs-or-memoziation | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
@lru_cache(None)
def dfs(index, remaining_days):
if remaining_days == 0:
if index == n: return 0
else: return sys.maxsize... | minimum-difficulty-of-a-job-schedule | python3 | dp | dfs | memoziation | H-R-S | 2 | 129 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,013 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2812707/Most-efficient-solution-with-detailed-comments-or-O(d*n)-100-or-O(n)-98.97 | class Solution:
def minDifficulty(self, diff: List[int], d: int) -> int:
n = len(diff)
if n < d:
return -1
today = [None] * n
yesterday = [diff[0]] + [None] * (n - 1)
# Solution for day 0 is just the running maximum
for i in range(1, n):
yeste... | minimum-difficulty-of-a-job-schedule | ✨Most efficient solution with detailed comments | ⏱️O(d*n) - 100% | 💾O(n) - 98.97% | lone17 | 0 | 6 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,014 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2711599/Python-Memoized-Recursion-Simple-and-Intuitive | class Solution:
def minDifficulty(self, jobDifficulties: List[int], days: int) -> int:
n = len(jobDifficulties)
@cache
def dfs(i: int, days: int) -> int:
if days == 0 or n - i < days:
return -1
if days == 1:
# ... | minimum-difficulty-of-a-job-schedule | Python Memoized Recursion - Simple and Intuitive | CompileTimeError | 0 | 22 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,015 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2711060/Python-Top-down-approach-with-mem-(Faster-than-93) | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
if n < d:
return -1
dp = [[-1] * (d + 1) for _ in range(n + 1)]
def helper(index, dayleft):
if dp[index][dayleft] != -1:
return d... | minimum-difficulty-of-a-job-schedule | Python Top-down approach with mem (Faster than 93%) | KevinJM17 | 0 | 15 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,016 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2710875/PYTHON-solution-beats-96-users | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
@lru_cache(None)
def dp(idx,d,curr):
if idx == len(jobDifficulty) and d == 0: return curr
if idx >= len(jobDifficulty) or d <= 0: return inf
return min(dp(i... | minimum-difficulty-of-a-job-schedule | PYTHON solution beats 96% users | mritunjayyy | 0 | 19 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,017 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2710476/DP-beating-95-in-time | class Solution:
def minDifficulty(self, jd: List[int], d: int) -> int:
n = len(jd)
if n < d:
return -1
elif n == d:
return sum(jd)
dic = {}
def calc(i, restd, prev_max):
if i == n or restd < 0:
return float('inf... | minimum-difficulty-of-a-job-schedule | DP, beating 95% in time | leonine9 | 0 | 23 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,018 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2710470/Python-DP-solution | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
@cache
def dfs(i,m,day):
if day==d and i==len(jobDifficulty):
return 0
if day==d or i==len(jobDifficulty):
return inf
return min(dfs(i+1,max(m,jobDiff... | minimum-difficulty-of-a-job-schedule | Python DP solution | DhruvBagrecha | 0 | 13 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,019 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2710355/python3-Recursion-and-Dp-solution-for-reference | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
N = len(jobDifficulty)
if len(jobDifficulty) < d: return -1
maxLookup = [-1]*N
maxLookup[-1] = jobDifficulty[-1]
for i in range(N-2, -1, -1):
maxLookup[i] = max(jobDifficulty[i], ma... | minimum-difficulty-of-a-job-schedule | [python3] Recursion and Dp solution for reference | vadhri_venkat | 0 | 17 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,020 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2710355/python3-Recursion-and-Dp-solution-for-reference | class Solution:
def minDifficulty(self, jobDifficulty, d: int) -> int:
N = len(jobDifficulty)
if len(jobDifficulty) < d: return -1
dp = [[defaultdict(int) for _ in range(N+1)] for _ in range(N+1)]
for i in range(N+1):
dp[0][i][0] = 0
for i in range(1... | minimum-difficulty-of-a-job-schedule | [python3] Recursion and Dp solution for reference | vadhri_venkat | 0 | 17 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,021 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2709117/Python-beats-100-solution-explained-11-lines | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
if len(jobDifficulty) < d: return -1
n = len(jobDifficulty)
@lru_cache(None)
def dp(cur_difficulty, i, d):
if d == 1: return max(jobDifficulty[i:])
if i == n-1: return inf
... | minimum-difficulty-of-a-job-schedule | Python beats 100 % solution explained [11 lines] | amuwal | 0 | 25 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,022 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2709117/Python-beats-100-solution-explained-11-lines | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
if len(jobDifficulty) < d: return -1 # don't forget
n = len(jobDifficulty)
@lru_cache(None)
def dp(cur_difficulty, i, d):
# If there is only 1 day left
# We simply return the... | minimum-difficulty-of-a-job-schedule | Python beats 100 % solution explained [11 lines] | amuwal | 0 | 25 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,023 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2709099/Python-Simple-Solution-With-Explanation | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
from functools import cache
n = len(jobDifficulty)
if d > n: # There will always be days that are empty
return -1
@cache # Does memoization with key (ji, di, mx)
... | minimum-difficulty-of-a-job-schedule | Python Simple Solution With Explanation | abdotaker608 | 0 | 36 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,024 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2709089/Python-3-or-Beats-95.99-in-time | class Solution:
def minDifficulty(self, arr: List[int], d: int) -> int:
@lru_cache(None)
def recurse(idx, d, currMax):
if idx == len(arr):
if d == 0: return currMax
else: return math.inf
if d == 0:
return math.inf
... | minimum-difficulty-of-a-job-schedule | Python 3 | Beats 95.99 in time | TheMaroonKnight | 0 | 34 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,025 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2708858/Python-Solution-or-Memoization | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
@cache
def helper(ind, cut):
if cut==d:
return max(jobDifficulty[ind:])
curr=-1e9
ans=1e9
for i in range(ind, n-d+cut):
curr=max(curr, job... | minimum-difficulty-of-a-job-schedule | Python Solution | Memoization | Siddharth_singh | 0 | 21 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,026 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2708853/Python | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n= len(jobDifficulty)
@lru_cache(None)
def dfs(idx,k,m):
if idx == n :
if k == 0:
return 0
return math.inf
if k <= 0:
... | minimum-difficulty-of-a-job-schedule | Python | Akhil_krish_na | 0 | 35 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,027 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2707950/Python-oror-Easily-Understood-oror-Faster-than-96-oror-explained-with-comments | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
# left_days: the number of days left
# i: index of jobDifficulty
# prev_max: the max difficulty at the same day
@cache
def foo1335(left_days: int, i: int, prev_max: int):
if i == le... | minimum-difficulty-of-a-job-schedule | 🔥 Python || Easily Understood ✅ || Faster than 96% || explained with comments | rajukommula | 0 | 38 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,028 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2707821/python3-or-top-down-dp-(faster-than-95) | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
# left_days: the number of days left
# i: index of jobDifficulty
# prev_max: the max difficulty at the same day
@cache
def foo1335(left_days: int, i: int, prev_max: int):
if i == le... | minimum-difficulty-of-a-job-schedule | python3 | top-down dp (faster than 95%) | henryluo108 | 0 | 12 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,029 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2650944/Python-Memo-dp-solution | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
memo = {}
def dp(day,index):
if day==1:
return max(jobDifficulty[index:])
elif (day,index) in memo:
return memo[(day,index)]
m = float('-inf')
... | minimum-difficulty-of-a-job-schedule | Python Memo dp solution | Brillianttyagi | 0 | 34 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,030 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2323905/Python3-or-Beats-98-or-O(n*d*max(jobDifficulty))-Solution-or-Memoization | class Solution:
def minDifficulty(self, jd: List[int], d: int) -> int:
@cache
def res(pos = 0, d = d, mx = 0):
if d == 0 and pos != len(jd):
return math.inf
elif d == 0 and pos == len(jd):
return 0
elif d == 0 or pos == len... | minimum-difficulty-of-a-job-schedule | Python3 | Beats 98% | O(n*d*max(jobDifficulty)) Solution | Memoization | DheerajGadwala | 0 | 112 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,031 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2148654/Python-easy-to-read-and-understand-or-memoization | class Solution:
def solve(self, nums, index, d):
if index == len(nums) or d == 1:
#print(max(nums[index:]))
return max(nums[index:])
ans = float("inf")
for i in range(index, len(nums)-d+1):
curr = max(nums[index:i+1]) + self.solve(nums, i+1, d-1)
... | minimum-difficulty-of-a-job-schedule | Python easy to read and understand | memoization | sanial2001 | 0 | 173 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,032 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/2148654/Python-easy-to-read-and-understand-or-memoization | class Solution:
def solve(self, nums, index, d):
if index == len(nums) or d == 1:
#print(max(nums[index:]))
return max(nums[index:])
if (index, d) in self.d:
return self.d[(index, d)]
self.d[(index, d)] = float("inf")
for i in range(index, len(nums... | minimum-difficulty-of-a-job-schedule | Python easy to read and understand | memoization | sanial2001 | 0 | 173 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,033 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/1762005/DP-3-Top-down | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
# dp state : index of the next job, i, and number of days left, j
n = len(jobDifficulty)
if n<d: return -1
@cache
def max_job(i,k): # max(jobDifficulty[i:k]) ... | minimum-difficulty-of-a-job-schedule | DP 3 Top-down | haydarevren | 0 | 158 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,034 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/1756141/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up) | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
if n < d:
return -1
hardest_job_remaining = [0]*n
hardest_job = 0
for i in range(n-1, -1, -1):
hardest_job = max(hardest_job, jobDifficulty[i])
... | minimum-difficulty-of-a-job-schedule | ✅ [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up) | JawadNoor | 0 | 185 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,035 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/1756141/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up) | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
# If we cannot schedule at least one job per day,
# it is impossible to create a schedule
if n < d:
return -1
dp = [[float("inf")] * (d + 1) for _ in range(n)... | minimum-difficulty-of-a-job-schedule | ✅ [Java / Python3] Simple DP Solution (Top-Down & Bottom-Up) | JawadNoor | 0 | 185 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,036 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/1491916/Duplicate-Question-DP-or-Python3 | class Solution:
def minDifficulty(self, s: List[int], k: int) -> int:
n = len(s)
dp = {}
def helper(i,k):
if k<0:
return float("inf")
if k==0:
if i<n:
return max(s[i:n])
else:
retu... | minimum-difficulty-of-a-job-schedule | [Duplicate Question] DP | Python3 | Sanjaychandak95 | 0 | 178 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,037 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/1462608/Python3-or-2-D-dp | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
jobs=len(jobDifficulty)
dp=[[float('inf') for i in range(jobs)] for j in range(d)]
dp[0][0]=jobDifficulty[0]
for i in range(1,jobs):
dp[0][i]=max(jobDifficulty[i],dp[0][i-1])
for day... | minimum-difficulty-of-a-job-schedule | [Python3] | 2-D dp | swapnilsingh421 | 0 | 165 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,038 |
https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/discuss/1063122/Python-DP-O(n-*-n-*-d) | class Solution:
def minDifficulty(self, jobDifficulty: List[int], d: int) -> int:
n = len(jobDifficulty)
if d > n:
return -1
# Memoize maximum for every cut
max_dp = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(i, n):
... | minimum-difficulty-of-a-job-schedule | Python DP O(n * n * d) | michaellin986 | 0 | 493 | minimum difficulty of a job schedule | 1,335 | 0.587 | Hard | 20,039 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1201679/C%2B%2B-Python3-No-Heap-No-BS-Simple-Sort-99.20 | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
m = len(mat)
rows = sorted(range(m), key=lambda i: (mat[i], i))
del rows[k:]
return rows | the-k-weakest-rows-in-a-matrix | [C++, Python3] No Heap, No BS, Simple Sort 99.20% | mycoding1729 | 163 | 6,300 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,040 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/496647/Python-3-(one-line)-(beats-100) | class Solution:
def kWeakestRows(self, G: List[List[int]], k: int) -> List[int]:
S = [[sum(g),i] for i,g in enumerate(G)]
R = sorted(S)
return [r[1] for r in R[:k]] | the-k-weakest-rows-in-a-matrix | Python 3 (one line) (beats 100%) | junaidmansuri | 28 | 3,500 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,041 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/496647/Python-3-(one-line)-(beats-100) | class Solution:
def kWeakestRows(self, G: List[List[int]], k: int) -> List[int]:
return [r[1] for r in heapq.nsmallest(k,[[sum(g),i] for i,g in enumerate(G)])]
- Junaid Mansuri
- Chicago, IL | the-k-weakest-rows-in-a-matrix | Python 3 (one line) (beats 100%) | junaidmansuri | 28 | 3,500 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,042 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1326292/Solution-3-line-with-python | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
matIndexAndSumRow = [[sum(val),idx] for idx,val in enumerate(mat)]
matIndexAndSumRow.sort()
return [matIndexAndSumRow[i][-1] for i in range(k)] | the-k-weakest-rows-in-a-matrix | Solution 3 line with python | qanghaa | 9 | 311 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,043 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1066759/Python.-Super-simple-and-Easy-understanding-solution. | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
helper = {}
for index, row in enumerate(mat):
helper[index] = 0
for num in row:
if num: helper[index] += 1
else: break
ans = sorted(helper, key = helper.get)
return ans[:k] | the-k-weakest-rows-in-a-matrix | Python. Super simple & Easy-understanding solution. | m-d-f | 8 | 599 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,044 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2384316/Python-Accurate-Solution-using-Tuples-oror-Documented | class Solution:class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
sums = [] # list to store Tuples
for i, row in enumerate(mat):
sums.append((sum(row), i)) # append sum & index of row as a Tuple
sums.sort() ... | the-k-weakest-rows-in-a-matrix | [Python] Accurate Solution using Tuples || Documented | Buntynara | 6 | 94 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,045 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2197823/Python3-solution-without-binary-search-and-heap | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
NoOfOnesInARow = {}
for row in range(len(mat)):
count = mat[row].count(1)
if count in NoOfOnesInARow:
NoOfOnesInARow[count].append(row)
else:
... | the-k-weakest-rows-in-a-matrix | 📌 Python3 solution without binary search and heap | Dark_wolf_jss | 6 | 105 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,046 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1066761/Python.-One-liner-easy-solution.-faster-than-97.05 | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
return sorted(range(len(mat)), key = lambda row: sum(mat[row]))[:k] | the-k-weakest-rows-in-a-matrix | Python. One-liner easy solution. faster than 97.05% | m-d-f | 4 | 197 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,047 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/496633/Python3-sorting | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
strength = ((sum(mat[i]), i) for i in range(len(mat)))
return [x for _, x in sorted(strength)[:k]] | the-k-weakest-rows-in-a-matrix | [Python3] sorting | ye15 | 3 | 293 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,048 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/496633/Python3-sorting | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
m, n = len(mat), len(mat[0])
vals = []
for i in range(m):
lo, hi = 0, n
while lo < hi:
mid = lo + hi >> 1
if mat[i][mid]: lo = mid + 1
... | the-k-weakest-rows-in-a-matrix | [Python3] sorting | ye15 | 3 | 293 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,049 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2583290/SIMPLE-PYTHON3-SOLUTION-easy-to-understand | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
indexlist = [i for i in range(len(mat))]
valuelist = []
ans = []
for i in mat:
valuelist.append(i.count(1))
for j in range(k):
m = valuelist.index(min(valuelist... | the-k-weakest-rows-in-a-matrix | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔ easy to understand | rajukommula | 2 | 162 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,050 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2550398/python3-oror-O(N)-oror-optimized-solution | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
def comp(val):
return val[0]
tmp = []
for x in mat:
tmp.append(x.count(1))
for i in range(len(tmp)):
tmp[i] = (tmp[i],i)
tmp.sort(key = comp)
... | the-k-weakest-rows-in-a-matrix | python3 || O(N) || optimized solution | shacid | 2 | 69 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,051 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2327425/Python3-or-solution | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
rowDict = {}
for i in range(len(mat)):
rowDict[i] = sum(mat[i])
weakestList = list(sorted(rowDict, key=rowDict.get))
return weakestList[:k] | the-k-weakest-rows-in-a-matrix | Python3 | solution | arvindchoudhary33 | 2 | 73 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,052 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1888414/Python-One-Liner | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
return [i[0] for i in sorted(enumerate(mat), key = lambda x: x[1].count(1))[:k]] | the-k-weakest-rows-in-a-matrix | ✅ Python One Liner | dhananjay79 | 2 | 60 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,053 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1888414/Python-One-Liner | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
arr = []
for i in range(len(mat)):
arr.append([mat[i].count(1), i])
return [i[1] for i in sorted(arr)[:k]] | the-k-weakest-rows-in-a-matrix | ✅ Python One Liner | dhananjay79 | 2 | 60 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,054 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1888414/Python-One-Liner | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
def countOne(row):
lo, hi = 0, len(row) - 1
while lo <= hi:
mid = (lo + hi) // 2
if row[mid]: lo = mid + 1
else: hi = mid - 1
return hi + 1
... | the-k-weakest-rows-in-a-matrix | ✅ Python One Liner | dhananjay79 | 2 | 60 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,055 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1888414/Python-One-Liner | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
def countOne(row):
lo, hi = 0, len(row) - 1
while lo <= hi:
mid = (lo + hi) // 2
if row[mid]: lo = mid + 1
else: hi = mid - 1
return hi + 1
... | the-k-weakest-rows-in-a-matrix | ✅ Python One Liner | dhananjay79 | 2 | 60 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,056 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1887878/Naive-Solution-Python3-oror-First-Daily-Challenge-Solved-oror-Without-Help-from-Discuss(999%2B) | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
ans=defaultdict() #dictionary to store count of 1s in each row
c=0
for i,r in enumerate(mat): #keeps track of position i of row r
c=r.count(1) ... | the-k-weakest-rows-in-a-matrix | Naïve Solution [Python3] || First Daily Challenge Solved || Without Help from Discuss(999+) | Sh4d0w-2099 | 2 | 32 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,057 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1657613/PythonSimplest-Solution-Only-Need-Heap-Lexicographical-Ordering | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
new_array = [(mat[i],i) for i in range(len(mat))]
heapq.heapify(new_array)
return([heapq.heappop(new_array)[1] for _ in range(len(new_array))][:k]) | the-k-weakest-rows-in-a-matrix | [Python][Simplest Solution] Only Need Heap - Lexicographical Ordering | ifanwang1028 | 2 | 113 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,058 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2573955/Python3-Heap | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
pairs = [(sum(r),i) for i,r in enumerate(mat)]
return [n[1] for n in heapq.nsmallest(k, pairs)] | the-k-weakest-rows-in-a-matrix | [Python3] Heap | ruosengao | 1 | 43 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,059 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2356501/python-3-liner | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
mat_with_indices = [(idx, elem) for (idx, elem) in enumerate(mat)]
sorted_by_strength = sorted(mat_with_indices, key=lambda row: row[1].count(1))
return [elem[0] for elem in sorted_by_strength[:k]] | the-k-weakest-rows-in-a-matrix | python 3-liner | Potentis | 1 | 56 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,060 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2295733/Python-or-Faster-than-96.83-other-python-solution | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
res = []
# calculate the soldiers of each row, mark the row index and total
for i in range(len(mat)):
res.append((i, sum(mat[i])))
# resort the mark list by soldiers total value
res.sor... | the-k-weakest-rows-in-a-matrix | Python | Faster than 96.83% other python solution | iamdhj | 1 | 56 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,061 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2169661/Python-Two-Simple-Solutions | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
#Approach 1
temp=[]
for i,m in enumerate(mat):
sol=(sum(m),i) #since it has only 1 and 0, the total sum will be total 1s.
temp.append(sol)
temp.sort()
idx=[]
for... | the-k-weakest-rows-in-a-matrix | Python Two Simple Solutions | pruthashouche | 1 | 191 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,062 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2169661/Python-Two-Simple-Solutions | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
#Approach-2
row=len(mat)
col=len(mat[0])
arr=[]
for i, r in enumerate(mat):
c=0
for j in range(col):
if r[j]==0:
break
... | the-k-weakest-rows-in-a-matrix | Python Two Simple Solutions | pruthashouche | 1 | 191 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,063 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1963447/Python-Sort-and-Heap-Clean-and-Simple! | class Solution:
def kWeakestRows(self, mat, k):
soldiers = [(row.count(1), i) for i,row in enumerate(mat)]
return [i for row,i in sorted(soldiers)][:k] | the-k-weakest-rows-in-a-matrix | Python - Sort and Heap - Clean and Simple! | domthedeveloper | 1 | 151 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,064 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1963447/Python-Sort-and-Heap-Clean-and-Simple! | class Solution:
def kWeakestRows(self, mat, k):
soldiers = [sum(row) for row in mat]
return nsmallest(k, range(len(mat)), key=lambda i : soldiers[i]) | the-k-weakest-rows-in-a-matrix | Python - Sort and Heap - Clean and Simple! | domthedeveloper | 1 | 151 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,065 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1963447/Python-Sort-and-Heap-Clean-and-Simple! | class Solution:
def kWeakestRows(self, mat, k):
return nsmallest(k, range(len(mat)), key=lambda i : sum(mat[i])) | the-k-weakest-rows-in-a-matrix | Python - Sort and Heap - Clean and Simple! | domthedeveloper | 1 | 151 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,066 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1941046/Python3-Easy-heap-solution-or-O(n-log-n) | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
heap = []
heapq.heapify(heap)
for row in range(len(mat)):
soldiers = mat[row].count(1)
heapq.heappush(heap, (soldiers, row))
result = []
for i in range(k):
... | the-k-weakest-rows-in-a-matrix | Python3 Easy heap solution | O(n log n) | YifanYang0527 | 1 | 77 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,067 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1929168/Python-Solution-or-One-Liner-or-Sorting-Based-or-List-Comprehensions | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
return [x for _,x in sorted(zip([x.count(1) for x in mat],range(len(mat))))][:k] | the-k-weakest-rows-in-a-matrix | Python Solution | One Liner | Sorting Based | List Comprehensions | Gautam_ProMax | 1 | 78 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,068 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1888210/Python3-Solution-with-heap | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
heap = []
for i in range(len(mat)):
cnt = 0
for j in range(len(mat[i])):
cnt += mat[i][j]
heapq.heappush(heap, (cnt, i))
res =... | the-k-weakest-rows-in-a-matrix | [Python3] Solution with heap | maosipov11 | 1 | 26 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,069 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1888099/Python-Shortest-Easiest-Solution-using-heap | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
minheap, ans = [], []
for i in range(len(mat)):
heappush(minheap,(mat[i].count(1),i))
while k:
ans.append( heappop(minheap)[1])
k-=1
return ans | the-k-weakest-rows-in-a-matrix | [Python] Shortest Easiest Solution using heap | RaghavGupta22 | 1 | 68 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,070 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1865437/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
dict = {}
for i,n in enumerate(mat):
dict[i] = sum(n)
abc = sorted(dict, key = dict.get)
return abc[:k] | the-k-weakest-rows-in-a-matrix | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 1 | 67 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,071 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1112694/Python3-Simple-solution-100-ms-runtime | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
count_mat = {}
j = 0
for i in mat:
ones = i.count(1)
count_mat[j] = ones
j = j + 1
count_mat = sorted(count_mat, key=lambda k: count_mat[k])
return count_mat... | the-k-weakest-rows-in-a-matrix | Python3, Simple solution, 100 ms runtime | naiem_ece | 1 | 59 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,072 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/1066599/Simple-python-solution-with-Binary-Search | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
t=[]
def binarySearch(target,l,h,nums):
while l<=h:
mid=(l+h)//2
if nums[mid]>target:
l=mid+1
else:
h=mid-1
... | the-k-weakest-rows-in-a-matrix | Simple python solution with Binary Search | Umadevi_R | 1 | 87 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,073 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2832857/Simple-Python-Solution | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
answer = []
soldiers_and_index = []
for idx, soldiers in enumerate(mat):
soldiers_and_index.append([sum(soldiers), idx])
soldiers_and_index.sort()
for idx in range(k):
... | the-k-weakest-rows-in-a-matrix | Simple Python Solution | corylynn | 0 | 3 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,074 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2818286/With-using-dictionary | class Solution:
def kWeakestRows(self, matrix: List[List[int]], k: int) -> List[int]:
dictionary = {}
dict_sorted = {}
for i in range(len(matrix)):
dictionary[i] = matrix[i].count(1)
for i in sorted(dictionary.values()):
for key in dictionary.keys():
... | the-k-weakest-rows-in-a-matrix | With using dictionary | pkozhem | 0 | 4 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,075 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2789663/Easy-Python-Solution | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
d = {}; n = len(mat); arr = []
for i in range(n):
x = mat[i].count(1);
d[i] = x; arr.append(i);
arr.sort(key = lambda i: d[i]);
return arr[:k] | the-k-weakest-rows-in-a-matrix | Easy Python Solution | avinashdoddi2001 | 0 | 9 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,076 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2776556/Binary-search-to-find-the-first-0-or-last-1 | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
res = []
for i in range(len(mat)):
row = mat[i]
left = 0
right = len(row) - 1
# find the index of first 0:
while left < right:
mid = left + ... | the-k-weakest-rows-in-a-matrix | Binary search to find the first 0 or last 1 | michaelniki | 0 | 3 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,077 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2776556/Binary-search-to-find-the-first-0-or-last-1 | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
res = []
for i in range(len(mat)):
row = mat[i]
left = 0
right = len(row) - 1
# find the index of last 1:
while left < right:
mid = left + (... | the-k-weakest-rows-in-a-matrix | Binary search to find the first 0 or last 1 | michaelniki | 0 | 3 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,078 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2703617/My-solution-Python3 | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
row = []
for i in range(len(mat)):
n_s = mat[i].count(1)
row.append((n_s, i))
row.sort()
lst2 = []
for key, value in row[:k]:
lst2.append(value)
ret... | the-k-weakest-rows-in-a-matrix | My solution, Python3 | hafid-hub | 0 | 7 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,079 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2676008/Easy-Python-Solution-or-Hash-Maps-or-Sorting | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
hashmap = {}
index = 0
for i in mat:
hashmap[index] = i.count(1)
index += 1
sorted_map = [k for k, v in sorted(hashmap.items(), key=lambda a:a[1], reverse=False)]
return... | the-k-weakest-rows-in-a-matrix | Easy Python Solution | Hash Maps | Sorting | atharva77 | 0 | 4 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,080 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2650754/Python-Hash-Table-no-Sorting-beats-90.58 | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
shape = (len(mat), len(mat[0])) # (rows, cols)
# A dictionary to collect indices of rows with the calculated ones amount
# The following structure: { ones_amount : array[row_index_1, ...] }
co... | the-k-weakest-rows-in-a-matrix | Python Hash Table, no Sorting, beats 90.58 % | Andrii_Ihnatiuk | 0 | 3 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,081 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2556722/python-heap-solution | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
mat=[[sum(val),i] for i,val in enumerate(mat)]
ans=[]
heapq.heapify(mat)
for i in range(k):
x=heappop(mat)
ans.append(x[1])
return ans | the-k-weakest-rows-in-a-matrix | python heap solution | pranjalmishra334 | 0 | 48 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,082 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2540340/Python3-One-Liner | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
return list(dict(sorted({i: sum(mat[i]) for i in range(len(mat))}.items(), key=(lambda x: x[1]))))[:k] | the-k-weakest-rows-in-a-matrix | [Python3] One-Liner | ivnvalex | 0 | 67 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,083 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2537784/python-solution-95.28-faster-using-dictionary!-O(n) | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
dict_num_soldiers = {}
count_soldiers = 0
for i in range(len(mat)):
for j in range(len(mat[i])):
if mat[i][j] == 1:
count_soldiers += 1
dict... | the-k-weakest-rows-in-a-matrix | python solution 95.28% faster using dictionary! O(n) | samanehghafouri | 0 | 99 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,084 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2514353/Python3-oror-One-Liner-oror-97-speed-90-memory | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
return [a for _,a in sorted([(sum(mat[row]),row) for row in range(len(mat))], key = lambda x: (x[0],x[1]))[:k]] | the-k-weakest-rows-in-a-matrix | Python3 || One-Liner || 97% speed 90% memory | TimGrimbergen | 0 | 48 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,085 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2450116/C%2B%2BPython-or-O(nlogn)-or-Optimized-Solution | class Solution:
def binarysearch(self,arr):
l,r = 0,len(arr)
while(l<=r and l<len(arr) and r>=0):
m = (l+r)//2
if m == len(arr)-1:
if arr[m] == 1:
return len(arr)
else:
r = m-1
elif arr[m] == ... | the-k-weakest-rows-in-a-matrix | C++/Python | O(nlogn) | Optimized Solution | arpit3043 | 0 | 80 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,086 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2437677/easy-python-code-or-O(n-log-n)-or-heap-sort-or-binary-search | class Solution:
def binarysearch(self,arr):
l,r = 0,len(arr)
while(l<=r and l<len(arr) and r>=0):
m = (l+r)//2
if m == len(arr)-1:
if arr[m] == 1:
return len(arr)
else:
r = m-1
elif arr[m] == ... | the-k-weakest-rows-in-a-matrix | easy python code | O(n log n) | heap sort | binary search | dakash682 | 0 | 61 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,087 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2390249/Python-simple-solutions-or-sort-or-heap | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
row = []
for i in range(len(mat)):
row.append((sum(mat[i]), i))
row.sort()
ans = [idx for (val, idx) in row[:k]]
return ans | the-k-weakest-rows-in-a-matrix | Python simple solutions | sort | heap | wilspi | 0 | 73 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,088 |
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/discuss/2390249/Python-simple-solutions-or-sort-or-heap | class Solution:
def kWeakestRows(self, mat: List[List[int]], k: int) -> List[int]:
row = []
for i in range(len(mat)):
row.append((sum(mat[i]), i))
heapq.heapify(row)
ans = []
while k>0:
(val, idx) = heapq.heappop(row)
ans.append(idx)
... | the-k-weakest-rows-in-a-matrix | Python simple solutions | sort | heap | wilspi | 0 | 73 | the k weakest rows in a matrix | 1,337 | 0.728 | Easy | 20,089 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2443490/Easy-to-understand-or-C%2B%2B-or-PYTHON-or | class Solution:
def minSetSize(self, arr: List[int]) -> int:
freq = Counter(arr);
f = [];
for val in freq.values():
f.append(val);
f.sort(reverse=True)
ans = 0;
n = 0;
while(len(arr)//2>n):
n += f[ans];
ans += 1;
ret... | reduce-array-size-to-the-half | Easy to understand | C++ | PYTHON | | dharmeshkporiya | 3 | 43 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,090 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441786/Python-100-runtime-Simple-and-Fast-please-upvote-%3A) | class Solution(object):
def minSetSize(self, arr):
d = {}
for x in arr:
if x not in d:
d[x] = 1
else:
d[x] += 1
l = sorted(d.values())
N = len(arr) // 2
idx = 0
while N > 0:
... | reduce-array-size-to-the-half | [Python] 100% runtime, Simple & Fast; please upvote :) | SteveShin_ | 2 | 233 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,091 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/496667/Python-3-(two-lines)-(beats-100) | class Solution:
def minSetSize(self, A: List[int]) -> int:
L, C = len(A), collections.Counter(A)
S = sorted(C.values(), reverse = True)
T = itertools.accumulate(S)
for i,v in enumerate(T):
if v >= len(A)//2: return i + 1 | reduce-array-size-to-the-half | Python 3 (two lines) (beats 100%) | junaidmansuri | 2 | 616 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,092 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/496667/Python-3-(two-lines)-(beats-100) | class Solution:
def minSetSize(self, A: List[int]) -> int:
for i,v in enumerate(itertools.accumulate(sorted(collections.Counter(A).values(), reverse = True))):
if v >= len(A)//2: return i + 1
- Junaid Mansuri
- Chicago, IL | reduce-array-size-to-the-half | Python 3 (two lines) (beats 100%) | junaidmansuri | 2 | 616 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,093 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2443552/Removing-most-common-99-speed | class Solution:
def minSetSize(self, arr: List[int]) -> int:
n_removed = count = 0
half_len_arr = ceil(len(arr) / 2)
for _, n in Counter(arr).most_common():
n_removed += 1
count += n
if count >= half_len_arr:
return n_removed | reduce-array-size-to-the-half | Removing most common, 99% speed | EvgenySH | 1 | 37 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,094 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2441633/Python3-Counter-%2B-PQ | class Solution:
def minSetSize(self, arr: List[int]) -> int:
counts = Counter(arr)
pq = []
for num in counts.keys():
heapq.heappush(pq, -1 * counts[num])
length = len(arr)
cutoff = length//2
res = 0
while length > cutoff:
length += heap... | reduce-array-size-to-the-half | Python3 Counter + PQ | answer610 | 1 | 72 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,095 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/1371858/Easy-and-Simple-Python-Solution | class Solution:
def minSetSize(self, arr):
arr_len=(len(arr)-1)//2
dic={}
for i in arr:
if i not in dic:
dic[i]=1
else:
dic[i]+=1
sum=0
count=0
for key,val in sorted(dic.items(),key = lambda x: x[1], reverse = T... | reduce-array-size-to-the-half | Easy and Simple Python Solution | sangam92 | 1 | 151 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,096 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/1038032/Python-3-2-LINE-Commented-Solution. | class Solution:
def minSetSize(self, arr: List[int]) -> int:
#Sort the array in increasing order based on FREQUENCY.
result = [item for items, c in Counter(arr).most_common() for item in [items] * c][::-1]
# Instead of removing elements, we can simply iterate throu... | reduce-array-size-to-the-half | [Python 3] - 2 LINE Commented Solution. | mb557x | 1 | 155 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,097 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2597246/python3-using-counters | class Solution:
def minSetSize(self, arr: List[int]) -> int:
counts = Counter(arr)
counts_sorted = sorted(counts.items(), key=lambda items:items[1], reverse=True)
s=0
for i in range(len(counts_sorted)):
s+=counts_sorted[i][1]
if s>=(len(arr)/... | reduce-array-size-to-the-half | [python3] using counters | hhlinwork | 0 | 17 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,098 |
https://leetcode.com/problems/reduce-array-size-to-the-half/discuss/2588629/Python-Solution-faster-than-97.33-of-Python3 | class Solution:
def minSetSize(self, arr: List[int]) -> int:
d = Counter(arr)
val = sorted(d.values())[::-1]
out = len(arr)//2
i = 0
res=0
for i in val:
out -= i
res +=1
if out <= 0:
break
re... | reduce-array-size-to-the-half | Python Solution faster than 97.33% of Python3 | anshsharma17 | 0 | 29 | reduce array size to the half | 1,338 | 0.697 | Medium | 20,099 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.