question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
maximum-rows-covered-by-columns
C++ || solution
c-solution-by-soujash_mandal-cw2u
\nclass Solution {\npublic:\n int bits(int n)\n {\n int count = 0;\n while (n) {\n n =n& (n - 1);\n count++;\n
soujash_mandal
NORMAL
2022-09-03T16:02:57.817593+00:00
2022-09-03T16:09:27.019948+00:00
62
false
```\nclass Solution {\npublic:\n int bits(int n)\n {\n int count = 0;\n while (n) {\n n =n& (n - 1);\n count++;\n }\n return count;\n }\n int binexp(int x,int n){\n int a=x;\n int prod=1;\n while(n)\n {\n if(n%2==1)prod=prod*a;\n a=a*a;\n n=n/2;\n }\n return prod;\n }\n int maximumRows(vector<vector<int>>& mat, int cols) {\n int m=mat.size();\n int n=mat[0].size();\n int res=0;\n \n for(int i=0;i<binexp(2,n);i++)\n {\n if(bits(i)==cols)\n {\n vector<int> v(n,0);\n int x=i;\n for(int i=0;i<n;i++)\n {\n v[i]=x%2;\n x/=2;\n }\n int count=0;\n for(int j=0;j<m;j++)\n {\n bool check=true;\n for(int k=0;k<n;k++)\n {\n if(mat[j][k]==1 && v[k]==0)\n {\n check=false;break;\n }\n }\n if(check) count++;\n \n }\n res=max(res,count);\n }\n }\n return res;\n }\n};\n```
1
0
[]
1
maximum-rows-covered-by-columns
[JAVA] Bit-mask backtracking
java-bit-mask-backtracking-by-pr0d1g4ls0-2ja6
\nclass Solution {\n int max = 0;\n int mask = 1073741823;\n int n;\n public int maximumRows(int[][] mat, int cols) {\n int[] state = new int
pr0d1g4ls0n
NORMAL
2022-09-03T16:01:45.156593+00:00
2022-09-03T16:01:45.156639+00:00
65
false
```\nclass Solution {\n int max = 0;\n int mask = 1073741823;\n int n;\n public int maximumRows(int[][] mat, int cols) {\n int[] state = new int[mat.length];\n n = mat[0].length;\n for (int i = 0; i < mat.length; i++) {\n for (int j = 0; j < mat[0].length; j++) {\n state[i] <<= 1;\n state[i] += mat[i][j];\n }\n }\n backtrack(state, cols, 0);\n return max;\n }\n \n public void backtrack(int[] state, int cols, int pos) {\n if (pos == n || cols == 0) {\n int count = 0;\n for (int i = 0; i < state.length; i++) {\n if (state[i] == 0) count++;\n }\n max = Math.max(count, max);\n return;\n }\n \n // choose col\n int[] copystate = new int[state.length];\n for (int i = 0; i < state.length; i++) {\n copystate[i] = state[i] & (mask ^ (1 << pos));\n }\n backtrack(copystate, cols-1, pos+1);\n \n // don\'t choose col\n backtrack(state, cols, pos+1);\n }\n}\n```
1
0
['Backtracking', 'Bitmask']
0
maximum-rows-covered-by-columns
Gnerate all permutation || Easy to understand || C++
gnerate-all-permutation-easy-to-understa-zj2x
\nclass Solution {\npublic:\n int ans;\n int covered(vector<vector<int>>& mat,vector<int>&nums){\n int count=0;\n for(int i=0;i<mat.size();i
Ankitkr437
NORMAL
2022-09-03T16:01:44.851654+00:00
2022-09-03T16:02:39.548253+00:00
75
false
```\nclass Solution {\npublic:\n int ans;\n int covered(vector<vector<int>>& mat,vector<int>&nums){\n int count=0;\n for(int i=0;i<mat.size();i++){\n int f=1;\n for(int j=0;j<mat[0].size();j++){\n if(mat[i][j]==1 && nums[j]==0) f=0;\n }\n if(f==1) count++;\n }\n return count;\n }\n void solve(vector<int>&nums,int cols,vector<vector<int>>&mat,int i){\n if(i>=nums.size() && cols>0) return;\n if(cols==0){\n ans=max(ans,covered(mat,nums));\n return;\n }\n nums[i]=1;\n solve(nums,cols-1,mat,i+1);\n nums[i]=0;\n solve(nums,cols,mat,i+1);\n }\n int maximumRows(vector<vector<int>>& mat, int cols) {\n ans=0;\n vector<int>nums(mat[0].size(),0);\n solve(nums,cols,mat,0);\n return ans;\n }\n};\n```
1
0
['C', 'Probability and Statistics']
0
maximum-rows-covered-by-columns
C++ Solution Beats 100%
c-solution-beats-100-by-dakshg-nyzn
Code
dakshg
NORMAL
2025-04-12T06:54:11.341495+00:00
2025-04-12T06:54:11.341495+00:00
1
false
# Code ```cpp [] class Solution { public: int maximumRows(vector<vector<int>> &matrix, int numSelect) { int n = matrix.size(), m = matrix[0].size(); vector<int> comp(n); for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) comp[i] |= (matrix[i][j] << j); int mx = 0, curr = 0; auto solve = [&](int ind, int left, auto &solve) { if(!left) { int count = 0; for(int j : comp) count += (j & curr) == j; mx = max(mx, count); return; } if(ind == m) return; curr |= 1 << ind; solve(ind + 1, left - 1, solve); curr &= (~(1 << ind)); solve(ind + 1, left, solve); }; solve(0, numSelect, solve); return mx; } }; ```
0
0
['C++']
0
maximum-rows-covered-by-columns
Recursive | Python 3 | Beats 70%
recursive-python-3-beats-60-by-zhan1803-qhtx
IntuitionCollect rows whose col_idx = 1 into a dictionary, and enumerate columns picked, to see which has the max coverageApproachrow dictionary: dic[row] = [i,
zhan1803
NORMAL
2025-04-04T02:38:10.320616+00:00
2025-04-04T03:13:55.500690+00:00
2
false
# Intuition Collect rows whose col_idx = 1 into a dictionary, and enumerate columns picked, to see which has the max coverage # Approach row dictionary: dic[row] = [i, j, k] # column index with the cell [row, i] = 1 Enumerate all possible selection of columns with amount = numSelect During enumeration, we have two choice, choose column j or not? Recursion base case: (1) column_idx < n (2) number of selected columns = numSelect or not? If = numSelect, then time to find the max_row_cover # Complexity - Time complexity: O(2^n) # n is the number of columns in total - Space complexity: O(n) # Code ```python3 [] class Solution: def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int: m, n = len(matrix), len(matrix[0]) rowdic = defaultdict(list) for r in range(m): for c in range(n): if matrix[r][c] == 1: rowdic[r].append(c) allzero = m - len(rowdic) maxcover = 0 cols = [] def dfs(i: int) -> int: nonlocal maxcover if i >= n: if len(cols) == numSelect: colset = set(cols) cnt = 0 for key in list(rowdic.keys()): good = True for idx in rowdic[key]: if idx not in colset: good = False break if good: cnt += 1 cnt += allzero maxcover = max(maxcover, cnt) return # not pick ith column dfs(i+1) # pick ith column cols.append(i) dfs(i+1) cols.pop() dfs(0) return maxcover ```
0
0
['Backtracking', 'Matrix', 'Enumeration', 'Python3']
0
maximum-rows-covered-by-columns
【Java】2 Solutions Using Backtracking (+Pruning)
java-2-solutions-using-backtracking-prun-p3q7
Solution 1: Choose or Not ChooseCodeSolution 2: Pickup One Col from Remaining ColsCode
AlanChao8669
NORMAL
2025-02-21T16:56:25.715638+00:00
2025-02-21T16:56:25.715638+00:00
5
false
# Solution 1: Choose or Not Choose ![2397-1.jpg](https://assets.leetcode.com/users/images/5e9d3cf3-157c-40d3-8546-d4dbc62b1979_1740156649.3141794.jpeg) # Code ```java [] class Solution { int max; boolean[] selected; // record selected cols int selectedNum; int m,n; public int maximumRows(int[][] matrix, int numSelect) { // init max = 0; // selected = new boolean[n]; selectedNum = 0; m = matrix.length; n = matrix[0].length; selected = new boolean[n]; // use backtrack to try all possible combinations of cols dfs(0, matrix, numSelect); return max; } // ith col choose or not choose private void dfs(int i, int[][] matrix, int numSelect){ // end condition if(selectedNum == numSelect || i == n){ int coveredNum = 0; // check row-by-row for(int x=0; x<m; x++){ boolean covered = true; for(int y=0; y<n; y++){ if(matrix[x][y] == 1 && !selected[y]){ covered = false; break; } } if(covered) coveredNum++; } max = Math.max(max, coveredNum); return; } // choose selected[i] = true; selectedNum++; dfs(i+1, matrix, numSelect); // backtrack selected[i] = false; selectedNum--; // not choose // Pruning here!! (not-choose branch) if(i <= n-1-numSelect+selectedNum){ dfs(i+1, matrix, numSelect); } } } ``` # Solution 2: Pickup One Col from Remaining Cols ![2397-2.jpg](https://assets.leetcode.com/users/images/c4bf7c74-0549-433a-9d97-83d0998369b0_1740156776.089486.jpeg) # Code ```java [] class Solution { int m,n; boolean[] choosedCols; int selectedNum; int max; public int maximumRows(int[][] matrix, int numSelect) { // init m = matrix.length; n = matrix[0].length; choosedCols = new boolean[n]; selectedNum = 0; // use backtrack to try out all possible combinations of selected cols dfs(0, numSelect, matrix); return max; } private void dfs(int startIdx, int numSelect, int[][] matrix){ // end condition if(selectedNum == numSelect || startIdx == n){ max = Math.max(max, checkCoveredRows(matrix)); return; } // pick a col from remaining cols // Pruning here!! (for loop) for(int i=startIdx; i <= n-numSelect+selectedNum; i++){ // choose choosedCols[i] = true; selectedNum++; dfs(i+1, numSelect, matrix); choosedCols[i] = false; selectedNum--; } } private int checkCoveredRows(int[][] matrix){ int rows = 0; for(int row=0; row<m; row++){ boolean covered = true; // start checking cols for(int col=0; col<n; col++){ if(matrix[row][col] == 1 && !choosedCols[col]){ covered = false; break; } } if(covered) rows++; } return rows; } } ```
0
0
['Java']
0
maximum-rows-covered-by-columns
2397. Maximum Rows Covered by Columns
2397-maximum-rows-covered-by-columns-by-ezuxf
IntuitionApproachComplexity Time complexity: Space complexity: Code
G8xd0QPqTy
NORMAL
2025-01-18T13:58:20.405472+00:00
2025-01-18T13:58:20.405472+00:00
10
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int: m, n = len(matrix), len(matrix[0]) best = 0 for cols in combinations(range(n), numSelect): covered = 0 for row in matrix: if all(row[c] == 0 or c in cols for c in range(n)): covered += 1 best = max(best, covered) return best ```
0
0
['Python3']
0
maximum-rows-covered-by-columns
[Java] ✅ 1MS ✅ DFS ✅ PICK OR SKIP
java-1ms-dfs-pick-or-skip-by-stefanelsta-c9qs
Approach Get the count of 1s for each row. Use an int[matrix.length] onesCount. For each index, decide to select that column or to pick that column Picking a co
StefanelStan
NORMAL
2025-01-07T09:39:57.741845+00:00
2025-01-07T09:39:57.741845+00:00
9
false
# Approach 1. Get the count of 1s for each row. Use an int[matrix.length] onesCount. 2. For each index, decide to select that column or to pick that column 3. Picking a column will mean the onesCount[] will decrease for each row where that column has 1. Decrease the count for each row, but also restore it when before returning from function. 4. Return the max result. # Complexity - Time complexity:$$O(2 ^ n)$$ <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:$$O(n)$$ <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maximumRows(int[][] matrix, int numSelect) { int[] onesCount = getOnesCount(matrix); return pickColumns(0, numSelect, matrix, onesCount); } private int pickColumns(int index, int canSelect, int[][] matrix, int[] onesCount) { if (canSelect == 0 || canSelect > matrix[0].length - index || index == matrix[0].length) { return 0; } // skip int skip = pickColumns(index + 1, canSelect, matrix, onesCount); // pick int rowsCovered = coverRows(matrix, index, onesCount); int retVal = Math.max(skip, Math.max(rowsCovered, pickColumns(index + 1, canSelect - 1, matrix, onesCount))); uncoverRows(matrix, index, onesCount); return retVal; } private void uncoverRows(int[][] matrix, int index, int[] onesCount) { for (int i = 0; i < matrix.length; i++) { onesCount[i] += matrix[i][index]; } } private int coverRows(int[][] matrix, int index, int[] onesCount) { int coveredRows = 0; for (int i = 0; i < matrix.length; i++) { onesCount[i] -= matrix[i][index]; if (onesCount[i] == 0) { coveredRows++; } } return coveredRows; } private int[] getOnesCount(int[][] matrix) { int[] onesCount = new int[matrix.length]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { onesCount[i] += matrix[i][j]; } } return onesCount; } } ```
0
0
['Java']
0
maximum-rows-covered-by-columns
NO BITMASK | Full Intuition✅Backtracking | C++
no-bitmask-full-intuitionbacktracking-c-l60h9
IntuitionStraight forward backtracking, only covered rows calculation is bit tricky and difficult to understandApproach Generate all possible cols subset of siz
Heatw4ve
NORMAL
2025-01-02T16:41:00.070629+00:00
2025-01-02T16:41:00.070629+00:00
3
false
# Intuition Straight forward backtracking, only covered rows calculation is bit tricky and difficult to understand # Approach 1. Generate all possible cols subset of size exactly numSelect 2. A row is only considered valid, if all of the 1's that are present in that row, is in our current subset of cols # Complexity - Time complexity: $$O(2^n * (m*n))$$, n = columns - Space complexity: $$O(n)$$ # Code ```cpp [] class Solution { public: int findScore(set<int>& col, vector<vector<int>>& matrix) { int cur = 0; // current covered rows for(int i = 0; i < matrix.size(); i++) { bool f = true; // assume row is covered for(int j = 0; j < matrix[0].size(); j++) { // if current row has a value 1, and col of that row is not present in // subset of cols, row not covered if (matrix[i][j] == 1 && col.count(j) == 0) { f = false; break; } } if (f) cur++; } return cur; } void solve(int i, int m, vector<vector<int>>& matrix, int n, set<int>& col, int& mx) { // no columns left if (i == m) { // if selected exactly n columns if (col.size() == n) { // update score mx = max(mx, findScore(col, matrix)); } return; } // pick column col.insert(i); solve(i+1, m, matrix, n, col, mx); col.erase(i); // not pick col solve(i+1, m, matrix, n, col, mx); } int maximumRows(vector<vector<int>>& matrix, int numSelect) { // choose all possible ways of selecting numSelect columns from all possible cols // total_col C numSelect_col (C means combination) // we try generating all subsets of size exactly numSelect // and contains column indices // for each such subsets calculate covered rows // we calculate covered rows, by checking whether for each row // all 1's in that row has its column present in subset cols // so if one row has only value 1 in mat[i][j], then j should be in subset cols // we count all such rows + rows with no 1's // get the max out of them set<int> col; int m = matrix[0].size(); // number of columns int mx = 0; solve(0, m, matrix, numSelect, col, mx); return mx; } }; ```
0
0
['Backtracking', 'Matrix', 'C++']
0
maximum-rows-covered-by-columns
BackTracking
backtracking-by-linda2024-7r0y
IntuitionApproachComplexity Time complexity: Space complexity: Code
linda2024
NORMAL
2024-12-24T00:08:37.523786+00:00
2024-12-24T00:08:37.523786+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```csharp [] public class Solution { private int BackTrackingRows(HashSet<int>[] Ones, List<int> validRows, int idx, HashSet<int> visitedCols, int numSelect) { int validCnt = validRows.Count; // end: if(idx >= validCnt) return 0; // include current row: HashSet<int> comb = new HashSet<int>(visitedCols); comb.UnionWith(Ones[validRows[idx]]); int maxRows = 0; if(comb.Count <= numSelect) { maxRows = Math.Max(maxRows, 1+ BackTrackingRows(Ones, validRows, idx+1, comb, numSelect)); } maxRows = Math.Max(maxRows, BackTrackingRows(Ones, validRows, idx+1, visitedCols, numSelect)); return maxRows; } public int MaximumRows(int[][] matrix, int numSelect) { int rows = matrix.Length, cols = matrix[0].Length, res = 0; HashSet<int>[] Ones = new HashSet<int>[rows]; List<int> validRows = new(); if(numSelect == cols) return rows; for(int i = 0; i < rows; i++) { Ones[i] = new HashSet<int>(); for(int j = 0; j < cols; j++) { if(matrix[i][j] == 1) Ones[i].Add(j); } int oneNum = Ones[i].Count; if(oneNum == 0) res++; else if(oneNum <= numSelect) validRows.Add(i); } res += BackTrackingRows(Ones, validRows, 0, new HashSet<int>(), numSelect); return res; } } ```
0
0
['C#']
0
maximum-rows-covered-by-columns
very simple solution 🥷beats 💯🤟
very-simple-solution-beats-by-sudharsan3-3ecx
\n\n# Code\njava []\nclass Solution {\n public int maximumRows(int[][] matrix, int numSelect) {\n int n = matrix.length;\n int m = matrix[0].le
sudharsan3112
NORMAL
2024-11-30T18:44:59.901688+00:00
2024-11-30T18:44:59.901717+00:00
6
false
\n\n# Code\n```java []\nclass Solution {\n public int maximumRows(int[][] matrix, int numSelect) {\n int n = matrix.length;\n int m = matrix[0].length;\n if (numSelect > m) return 0;\n\n boolean[] taken = new boolean[m];\n return find(0, 0, n, m, matrix, numSelect, taken);\n }\n\n int find(int steps, int start, int n, int m, int[][] matrix, int numSelect, boolean[] taken) {\n if (steps == numSelect) {\n return calculate(n, m, matrix, taken);\n }\n\n int res = 0;\n for (int i = start; i < m; i++) {\n {\n taken[i] = true;\n res = Math.max(res, find(steps + 1, i + 1, n, m, matrix, numSelect, taken));\n taken[i] = false;\n }\n }\n return res;\n }\n\n int calculate(int n, int m, int[][] matrix, boolean[] taken) {\n int res = 0;\n for (int i = 0; i < n; i++) {\n boolean isCovered = true;\n for (int j = 0; j < m; j++) {\n if (matrix[i][j] == 1 && !taken[j]) {\n isCovered = false;\n break;\n }\n }\n if (isCovered) res++;\n }\n return res;\n }\n}\n\n```
0
0
['Java']
0
maximum-rows-covered-by-columns
C# Brute Force with combinatorics
c-brute-force-with-combinatorics-by-getr-a804
Intuition\n- Describe your first thoughts on how to solve this problem. When approaching this problem, the first intuition involves systematically identifying
GetRid
NORMAL
2024-11-25T18:47:09.461588+00:00
2024-11-25T18:47:09.461623+00:00
0
false
# Intuition\n- <!-- Describe your first thoughts on how to solve this problem. -->When approaching this problem, the first intuition involves systematically identifying the columns that contribute most to row coverage. A brute-force solution immediately comes to mind due to the following factors: The Problem\'s Small Scale: The constraints make it feasible to generate and evaluate all combinations of columns without hitting performance limits.\n- Need for Exhaustiveness: Since the goal is to maximize coverage, exploring all possible subsets of numSelect columns ensures that we don\'t miss any optimal solution.\n- Row and Column Coverage: The problem inherently links rows to columns based on 1s in the matrix. A key observation is that a row is covered if all its 1s are matched by the selected columns, which can be checked efficiently.\n- Brute-Force Over Combinations: Given that we need to try all possible subsets of columns, generating combinations of columns seems like the natural first step.\n- Using a brute-force approach allows us to understand the problem better and serves as a stepping stone for further optimizations, if needed.\n___\n# Approach\n- <!-- Describe your approach to solving the problem. -->Conbinations Generator: This recursive function generates all possible combinations of numSelect columns using a simple backtracking approach.\n- Check Covered Rows: For a given set of selected columns, this function iterates over all rows and checks whether the row is covered by the selected columns.\n- Brute Force over Combinations: The main loop iterates over all possible column combinations and calculates the number of rows covered for each combination, keeping track of the maximum number of covered rows.\n___\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(( n / numSelect) x m x n).\n___\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(numSelect).\n___\n# Code\n```csharp []\nusing System;\nusing System.Collections.Generic;\n\npublic class Solution {\n public int MaximumRows(int[][] matrix, int numSelect) {\n int m = matrix.Length; // Number of rows\n int n = matrix[0].Length; // Number of columns\n int maxCoveredRows = 0;\n\n // Generate all combinations of numSelect columns\n IEnumerable<List<int>> Combinations(int start, int count, List<int> current) {\n if (current.Count == count) {\n yield return new List<int>(current);\n yield break;\n }\n for (int i = start; i < n; i++) {\n current.Add(i);\n foreach (var combination in Combinations(i + 1, count, current)) {\n yield return combination;\n }\n current.RemoveAt(current.Count - 1);\n }\n }\n\n // Check how many rows are covered for a given set of selected columns\n int CountCoveredRows(List<int> selectedColumns) {\n int coveredRows = 0;\n foreach (var row in matrix) {\n bool isCovered = true;\n for (int j = 0; j < n; j++) {\n if (row[j] == 1 && !selectedColumns.Contains(j)) {\n isCovered = false;\n break;\n }\n }\n if (isCovered) {\n coveredRows++;\n }\n }\n return coveredRows;\n }\n\n // Iterate through all combinations of columns and find the maximum rows covered\n foreach (var combination in Combinations(0, numSelect, new List<int>())) {\n maxCoveredRows = Math.Max(maxCoveredRows, CountCoveredRows(combination));\n }\n\n return maxCoveredRows;\n }\n}\n```
0
0
['Array', 'Backtracking', 'Bit Manipulation', 'Matrix', 'Enumeration', 'C#']
0
maximum-rows-covered-by-columns
[C++] Backtracking
c-backtracking-by-wcf29-qolp
Intuition\n Describe your first thoughts on how to solve this problem. \n\nUsing backtracking.\n\nfor a col with index i, there are two decisions: choose it and
wcf29
NORMAL
2024-11-08T21:37:26.377223+00:00
2024-11-08T21:37:26.377249+00:00
5
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nUsing backtracking.\n\nfor a col with index i, there are two decisions: choose it and not choose it.\n\nSo the decision tree is like: \n\n " "\n / \\\n " " "0" \n / \\ / \\\n " " "1" "0" "0 1"\n\nThe number within the " " represents the col index you have chosen.\n\n# Complexity\n- Time complexity: $O(2^{selectCols} * mn)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\ndfs takes $O(2^{selectCols}$, and to verify every leaves of decision tree taks $O(mn)$.\n\n- Space complexity: $O(n)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nstd::vector IsCovered takes $O(n)$ space(n is number of cols). And the recursion stack space is also $O(n)$.\n\n# Code\n```cpp []\nclass Solution {\n int IsCovered(const vector<int> &row, const vector<bool>& selectCols) {\n for (int i = 0; i < row.size(); ++i) {\n if (row[i] == 1 && selectCols[i] == false) {\n return 0;\n }\n }\n return 1;\n }\n\n int CoveredNumber(const vector<vector<int>>& matrix, const vector<bool>& selectCols) {\n int num = 0;\n for (const auto &row : matrix) {\n num += IsCovered(row, selectCols);\n }\n return num;\n }\n\npublic:\n int maximumRows(vector<vector<int>>& matrix, int numSelect) {\n int row = matrix.size();\n if (row == 0) {\n return 0;\n }\n int col = matrix[0].size();\n if (col == 0) {\n return 0;\n }\n\n int have_choose = 0;\n int max_rows = 0;\n vector<bool> selectCols(col, false);\n\n std::function<void(int)> dfs = [&](int i) {\n if (have_choose == numSelect) {\n int num = CoveredNumber(matrix, selectCols);\n max_rows = std::max(max_rows, num);\n return;\n }\n if (i == col) {\n return;\n }\n dfs(i + 1);\n selectCols[i] = true;\n have_choose += 1;\n dfs(i + 1);\n selectCols[i] = false;\n have_choose -= 1;\n };\n\n dfs(0);\n return max_rows;\n }\n};\n```
0
0
['C++']
0
maximum-rows-covered-by-columns
Backtrack solution beats 70%
backtrack-solution-beats-70-by-gopigaura-ngtn
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
gopigaurav
NORMAL
2024-11-02T08:16:37.221031+00:00
2024-11-02T08:16:37.221054+00:00
10
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n def maximumRows(self, mat: List[List[int]], numSelect: int) -> int:\n \n # using Backtracking and recursion to solve this problem\n m = len(mat)\n n = len(mat[0])\n\n vis = [False] * n\n ans = 0\n\n def backtrack(index, cols, cur_cols):\n nonlocal ans\n if cols == cur_cols:\n count = 0\n for i in range(m):\n flag = True\n for j in range(n):\n if mat[i][j] == 1 and not vis[j]:\n flag = False\n break\n \n if flag:\n count += 1\n ans = max(ans, count)\n return \n\n if index >= n:\n return\n\n vis[index] = True\n backtrack(index + 1, cols, cur_cols + 1)\n vis[index] = False\n \n backtrack(index + 1, cols, cur_cols)\n \n backtrack(0, numSelect, 0)\n return ans\n \n\n\n\n```
0
0
['Python3']
0
maximum-rows-covered-by-columns
Fast Solution
fast-solution-by-dshrey27-f9hq
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
dshrey279
NORMAL
2024-10-20T11:56:45.716265+00:00
2024-10-20T11:56:45.716304+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int maximumRows(vector<vector<int>>& mat, int numSelect) {\n int n = mat.size() , m = mat[0].size();\n vector<int> v; // bitmask of all the rows\n for(int i = 0 ; i < n ; i++){\n int curr = 0;\n for(int j = 0 ; j < m ; j++){\n if(mat[i][j]){\n curr += (1<<j);\n }\n }\n v.push_back(curr);\n }\n\n int ans = 0;\n for(int i = 0 ; i < (1<<m) ; i++){\n if( __builtin_popcount(i) != numSelect) continue;\n int tmp = 0;\n for(auto it : v) if((it&i) == it) tmp++;\n ans = max(ans , tmp);\n }\n return ans;\n \n }\n};\n```
0
0
['C++']
0
maximum-rows-covered-by-columns
Java backtrack
java-backtrack-by-noturproblem-efbq
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
noturproblem
NORMAL
2024-10-16T18:15:25.398960+00:00
2024-10-16T18:15:25.398992+00:00
1
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n public int maximumRows(int[][] matrix, int numSelect) { \n return max(matrix, numSelect, new ArrayList<>(), 0);\n }\n\n int max(int[][] matrix, int numSelect, List<Integer> current, int from) {\n if (current.size() == numSelect) {\n return countCovered(matrix, current);\n }\n int n = matrix[0].length;\n\n var max = Integer.MIN_VALUE;\n for (int j = from; j < n; j++) {\n current.add(j);\n max = Math.max(max, max(matrix, numSelect, current, j+1));\n current.remove(current.size() - 1);\n }\n \n return max;\n }\n\n int countCovered(int[][] matrix, List<Integer> current) {\n\n int m = matrix.length;\n int n = matrix[0].length;\n int count = 0;\n for (int i = 0; i <m; i++) {\n var covered = checkCovered(matrix, i, current);\n if (covered) {\n count++;\n }\n }\n return count;\n }\n\n boolean checkCovered(int[][] matrix, int row, List<Integer> selectedCols) {\n int n = matrix[0].length;\n\n for (int j = 0; j < n; j++) {\n if (matrix[row][j] == 1 && !selectedCols.contains(j)) {\n return false;\n }\n }\n return true;\n }\n}\n```
0
0
['Java']
0
maximum-rows-covered-by-columns
Easy to follow c++ code
easy-to-follow-c-code-by-ashkanxy-k1an
Intuition\n\nFinding all combination of the columns (question 77) and check the maximum covered rows by each of them and return the maximum.\n\n\n# Approach\n\n
ashkanxy
NORMAL
2024-10-11T01:23:00.623765+00:00
2024-10-11T01:24:07.020230+00:00
5
false
# Intuition\n\nFinding all combination of the columns (question 77) and check the maximum covered rows by each of them and return the maximum.\n\n\n# Approach\n\n1, Generate all combinations of columns with a size of numSelect. For instance, for a matrix with 4 columns and k=2, the combinations are {{0,1}, {0,2}, {0,3}, {1,2}, {1,3}, {2,3}}.\n\n2, Store these column combinations in a vector of unordered_set.\n\n3, Iterate through the vector of combinations, and for each combination, call a helper function.\n\n4, Within the helper function, initialize the return value to the total number of rows. Loop through the matrix for all rows, skipping any unselected columns. As soon as a \'1\' is encountered, decrement the return value and move to the next row.\n\n# Complexity\n- Time complexity:\n\nComplexity of finding all of the combinations which is \n\nFinding combinations is a well-studied problem in combinatorics. The number of combinations of length k from a set of n elements is equal to the binomial coefficient, also known as "n choose k".\n\n(n, k)= n! / k! (n-k)!\n\n\u200BThen for each set of columns we iterate through the 2D matrix which means m * n * m\n\n\n- Space complexity:\n\nnumber of combinations * m\n\n# Code\n```cpp []\nclass Solution {\npublic:\n vector<unordered_set<int>> col_combs;\n int maximumRows(vector<vector<int>>& m, int numSelect) {\n find_combs(m[0].size(), numSelect);\n int ret= 0;\n for (int i=0; i<col_combs.size(); ++i)\n ret= max(ret, covered_rows(m, col_combs[i]) ); \n //print_combs();\n return ret;\n }\n\n int covered_rows (vector<vector<int>>& m, unordered_set<int> & cols) {\n int r = m.size();\n int c = m[0].size();\n \n int ret= r;\n for(int i= 0; i<r;++i) {\n for(int j=0;j<c ;++j) {\n if(cols.find(j) != cols.end()) {\n continue;\n }\n if (m[i][j] == 1) {\n ret--;\n break;\n }\n }\n }\n return ret;\n }\n\n void find_combs (int n, int k) {\n unordered_set<int> sol;\n helper (n, 0, sol, k);\n }\n\n void print_combs() {\n for(auto e: col_combs) {\n for( auto ee: e) {\n cout<<ee<<" ";\n }\n cout<<endl;\n }\n }\n\n void helper(int n, int beg, unordered_set<int> &sol, int k) {\n if(k==0) {\n col_combs.push_back(sol);\n return;\n }\n for(int i = beg ; i<n;++i) {\n sol.insert(i);\n helper(n, i+1, sol, k-1);\n sol.erase(i);\n }\n }\n};\n```
0
0
['Backtracking', 'Combinatorics', 'C++']
0
maximum-rows-covered-by-columns
Simple and Intuitive Java Solution | Beats 100%!
simple-and-intuitive-java-solution-beats-hcxk
Approach\nBacktrack through all possible solutions with length equal to numSelect.\n\nMark visited elements in a boolean array selected and check if the selecte
hmanolov
NORMAL
2024-10-08T06:26:08.356529+00:00
2024-10-08T06:26:08.356560+00:00
1
false
# Approach\nBacktrack through all possible solutions with length equal to `numSelect`.\n\nMark visited elements in a boolean array `selected` and check if the selected row count exceeds the maximum, when enough elements are selected.\n\n# Code\n```java []\nclass Solution {\n int maxCount = 0;\n public int maximumRows(int[][] matrix, int numSelect) {\n getRows(matrix, numSelect, 0, new boolean[matrix[0].length]);\n return maxCount;\n }\n\n void getRows(int[][] matrix, int numSelect, int idx, boolean[] selected) {\n if (numSelect == 0) {\n checkCount(matrix, selected);\n return;\n }\n\n for (int i = idx; i <= matrix[0].length - numSelect; i++) {\n selected[i] = true;\n getRows(matrix, numSelect-1, i+1, selected);\n selected[i] = false;\n }\n }\n\n void checkCount(int[][] matrix, boolean[] selected) {\n int count = 0;\n for (int i = 0; i < matrix.length; i++) {\n boolean isSelected = true;\n for (int j = 0; j < matrix[0].length; j++) {\n if (matrix[i][j] == 1 && selected[j] == false) {\n isSelected = false;\n break;\n }\n }\n if (isSelected) {\n count++;\n }\n }\n\n maxCount = maxCount < count ? count : maxCount;\n }\n}\n```
0
0
['Backtracking', 'Java']
0
maximum-containers-on-a-ship
⌚ONE-LINER⌚| O(1)🚀 || [C++/Java/Py3/JS]⚡EASY SOLUTION w EXPLANATION🍨
one-liner-o1-cjavapy3jseasy-solution-w-e-e8st
IntuitionThe problem requires us to determine the maximum number of containers that can be placed on an n x n cargo deck while ensuring the total weight does no
Fawz-Haaroon
NORMAL
2025-03-23T05:10:31.791258+00:00
2025-03-23T17:04:58.792534+00:00
1,288
false
## Intuition The problem requires us to determine the maximum number of containers that can be placed on an `n x n` cargo deck while ensuring the total weight does not exceed `maxWeight`. - The deck can hold at most `n * n` containers. - Each container has a weight of `w`, so the total weight of all `k` containers is `k * w`. - The ship's weight limit `maxWeight` restricts how many containers can be placed. Thus, we take the minimum of `n * n` (the deck's maximum capacity) and `maxWeight / w` (the weight-constrained maximum). --- # Approach 1. Compute the total number of containers that the `n x n` deck can hold: ` max_possible = n × n` 2. Compute the maximum number of containers that the ship can support under the weight constraint: `max_by_weight = ⌊maxWeight / w⌋` 3. Return the minimum of these two values: `result = min(max_possible, max_by_weight)` --- # Complexity - **Time Complexity:** `O(1)` - **Space Complexity:** `O(1)` --- # Code ```C++ [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return (n*n > maxWeight/w) ? maxWeight / w : n*n; } }; ``` ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return (n * n if n * n <= maxWeight // w else maxWeight // w) ``` ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return (n*n > maxWeight/w) ? maxWeight / w : n*n; } } ``` ```javascript [] /** * @param {number} n * @param {number} w * @param {number} maxWeight * @return {number} */ var maxContainers = function (n, w, maxWeight) { return (n * n > Math.floor(maxWeight / w)) ? Math.floor(maxWeight / w) : n * n; }; ``` ``` ✨ AN UPVOTE WILL BE APPRECIATED ^_~ ✨ ```
17
0
['Math', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']
1
maximum-containers-on-a-ship
Simple check
simple-check-by-kreakemp-4lqe
IntuitionApproachComplexity Time complexity: Space complexity: Code Here is an article of my last interview experience - A Journey to FAANG Company, I recomand
kreakEmp
NORMAL
2025-03-23T04:02:47.814455+00:00
2025-03-23T04:43:23.111829+00:00
697
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { if(n*n*w <= maxWeight) return n*n; return maxWeight/w; } }; ``` --- <span style="color:green"> <b>Here is an article of my last interview experience - A Journey to FAANG Company, I recomand you to go through this to know which all resources I have used & how I cracked interview at Amazon: </b> </span> https://leetcode.com/discuss/interview-experience/3171859/Journey-to-a-FAANG-Company-Amazon-or-SDE2-(L5)-or-Bangalore-or-Oct-2022-Accepted ---
9
0
['C++']
0
maximum-containers-on-a-ship
Easy & Detailed Solution✅ |TC: O(1) 🔥 | Step - By - Step Explain 🚀 | C++ | Java | Py | Js
easy-detailed-solution-step-by-step-expl-7we4
🧠 IntuitionThe problem requires us to determine the maximum number of containers that can be placed on an n x n desk while ensuring that their total weight does
himanshu_dhage
NORMAL
2025-03-23T04:03:18.513466+00:00
2025-03-23T04:09:40.072774+00:00
549
false
# 🧠 Intuition The problem requires us to determine the maximum number of containers that can be placed on an `n x n` desk while ensuring that their total weight does not exceed `maxWeight`. - The **total number of containers** that can fit on the desk is `n * n`. - Each container has a weight of `w`. - The goal is to find the maximum number of containers such that their total weight stays within `maxWeight`. --- # 🚀 Approach 1. **Calculate the total possible containers**: - Since the desk is a square of size `n x n`, it can hold `desk = n * n` containers. 2. **Check weight constraints**: - If all `desk` containers can fit within the given weight limit (`desk * w <= maxWeight`), return `desk`. - Otherwise, decrement the number of containers one by one until the condition holds. 3. **Optimized Calculation**: - Instead of iterating, we can directly compute the answer as: ```cpp int maxContainers = min(desk, maxWeight / w); ``` - This gives us the maximum valid number of containers in **constant time** instead of using a loop. --- # ⏳ Complexity Analysis - **Time Complexity**: - *Brute Force*: \(O(n^2)\) (if we check each possible count) - **Optimized Approach**: \(O(1)\) ✅ (since we use direct computation) - **Space Complexity**: - \(O(1)\) ✅ (Only a few integer variables are used) --- ## 📌 Step-by-Step Execution Example: `n = 3, w = 4, maxWeight = 20` | Step | Desk | desk * w | Condition (≤ maxWeight) | Action | |------|------|----------|------------------------|--------| | 1 | 9 | 36 | No ❌ | Reduce desk → 8 | | 2 | 8 | 32 | No ❌ | Reduce desk → 7 | | 3 | 7 | 28 | No ❌ | Reduce desk → 6 | | 4 | 6 | 24 | No ❌ | Reduce desk → 5 | | 5 | 5 | 20 | Yes ✅ | Store ans = 5, exit loop | --- # 💻 Code ```javascript [] class Solution { maxContainers(n, w, maxWeight) { // Calculate the total number of containers that can be placed on the desk let desk = n * n; let ans = 0; // Variable to store the maximum number of containers that can be supported // Iterate from the maximum possible number of containers down to 0 for (let i = desk; i >= 0; i--) { // Check if the total weight of 'desk' containers is within the maximum weight limit if (desk * w <= maxWeight) { ans = desk; // If valid, store the answer break; // Exit the loop as we've found the maximum possible containers } else { desk--; // If weight exceeds maxWeight, reduce the number of containers and check again } } return ans; // Return the maximum number of containers that can be supported } } ``` ```python [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: # Calculate the total number of containers that can be placed on the desk desk = n * n ans = 0 # Variable to store the maximum number of containers that can be supported # Iterate from the maximum possible number of containers down to 0 for i in range(desk, -1, -1): # Check if the total weight of 'desk' containers is within the maximum weight limit if desk * w <= maxWeight: ans = desk # If valid, store the answer break # Exit the loop as we've found the maximum possible containers else: desk -= 1 # If weight exceeds maxWeight, reduce the number of containers and check again return ans # Return the maximum number of containers that can be supported ``` ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { // Calculate the total number of containers that can be placed on the desk int desk = n * n; int ans = 0; // Variable to store the maximum number of containers that can be supported // Iterate from the maximum possible number of containers down to 0 for (int i = desk; i >= 0; i--) { // Check if the total weight of 'desk' containers is within the maximum weight limit if (desk * w <= maxWeight) { ans = desk; // If valid, store the answer break; // Exit the loop as we've found the maximum possible containers } else { desk--; // If weight exceeds maxWeight, reduce the number of containers and check again } } return ans; // Return the maximum number of containers that can be supported } } ``` ```C++ [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { // Calculate the total number of containers that can be placed on the desk int desk = n * n; int ans = 0; // Iterate from the maximum possible number of containers down to 0 for (int i = desk; i >= 0; i--) { // Check if the total weight of 'desk' containers is within the max weight limit if (desk * w <= maxWeight) { ans = desk; break; } else { desk--; } } return ans; } }; ```
5
1
['C++', 'Java', 'Python3', 'JavaScript']
1
maximum-containers-on-a-ship
JS | Two approach | Math (or) Binary search | O(1) | Beats 100% 🚀
js-binary-search-beats-100-by-akashcse20-vl1z
ApproachJust maths.Complexity Time complexity: O(1) Space complexity: O(1) CodeApproachWe can use Binary Search algorithm which efficiently searches for the max
akashcse2000
NORMAL
2025-03-23T04:03:42.832528+00:00
2025-03-23T04:20:17.128140+00:00
256
false
# Approach Just maths. # Complexity - Time complexity: O(1) - Space complexity: O(1) # Code ```javascript [] var maxContainers = function(n, w, m) { if((n * n * w) <= m) return n * n; return Math.floor(m / w); }; ``` # Approach We can use Binary Search algorithm which efficiently searches for the maximum number of containers by checking the mid-point. # Complexity - Time complexity: O(log(n)) - Space complexity: O(1) # Code ```javascript [] var maxContainers = function(n, w, m) { let l = 1, r = n*n, res = 0; while(l <= r) { const mid = Math.floor((l+r)/2); if (mid*w <= m) { res = mid; l = mid+1; } else { r = mid-1; } } return res; }; ```
4
0
['JavaScript']
3
maximum-containers-on-a-ship
100% Beats || One Line Of Code
100-beats-one-line-of-code-by-kdhakal-lsnx
IntuitionApproachComplexity Time complexity: Space complexity: Code
kdhakal
NORMAL
2025-04-09T13:41:17.139927+00:00
2025-04-09T13:41:17.139927+00:00
14
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return (n * n > maxWeight / w) ? maxWeight / w : n * n; } } ```
3
0
['Java']
0
maximum-containers-on-a-ship
One Linear
one-linear-by-charnavoki-leld
null
charnavoki
NORMAL
2025-03-24T09:24:43.722659+00:00
2025-03-24T09:24:43.722659+00:00
51
false
```javascript [] const maxContainers = (n, w, max) => Math.min(n * n, max / w) | 0; ```
3
0
['JavaScript']
0
maximum-containers-on-a-ship
beats 100% using binary search
beats-100-using-binary-search-by-s_malay-gz5s
Complexity Time complexity:O(logn) Space complexity:O(1) Code
s_malay
NORMAL
2025-03-23T05:08:41.518003+00:00
2025-03-23T05:08:41.518003+00:00
103
false
# Complexity - Time complexity:O(logn) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int possible(int mid,int &w,int &maxWeight) { if(mid*w <=maxWeight)return true; return false; } int maxContainers(int n, int w, int maxWeight) { int total_deck=n*n; int ans=0; int i=1,j=total_deck; while(i<=j) { int mid=i+(j-i)/2; if(possible(mid,w,maxWeight)) { ans=mid; i=mid+1; } else { j=mid-1; } } return ans; } }; ```
3
0
['Binary Search', 'C++']
1
maximum-containers-on-a-ship
🌟 Beats 100.00% || Python3 One Line 💯🔥🗿
beats-10000-python3-one-line-by-emmanuel-uuga
ApproachJust check that hint and convert to integer.Code
emmanuel011
NORMAL
2025-03-23T04:17:33.648774+00:00
2025-03-23T04:17:33.648774+00:00
115
false
# Approach Just check that hint and convert to integer. # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return int(min(n * n, maxWeight / w)) ```
3
0
['Python3']
3
maximum-containers-on-a-ship
[Python, Java, C++] Elegant & Short | Math
python-elegant-short-math-by-kyrylo-ktl-ccq1
Complexity Time complexity: O(1) Space complexity: O(1) Code
Kyrylo-Ktl
NORMAL
2025-03-24T08:04:57.666004+00:00
2025-03-24T08:17:27.159070+00:00
53
false
# Complexity - Time complexity: $$O(1)$$ - Space complexity: $$O(1)$$ # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, max_weight: int) -> int: return min(n * n, max_weight // w) ``` ```Java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n * n, maxWeight / w); } } ``` ```C++ [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return std::min(n * n, maxWeight / w); } }; ```
2
0
['Math', 'Python', 'C++', 'Java', 'Python3']
0
maximum-containers-on-a-ship
Binary search | Beats 100%
binary-search-beats-100-by-alishershaest-4zts
Code
alishershaesta
NORMAL
2025-03-23T09:18:41.449672+00:00
2025-03-23T09:18:41.449672+00:00
16
false
# Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: l, r = 1, n*n result = 0 while l <= r: mid = l + ((r-l)//2) if (mid * w) > maxWeight: r = mid - 1 else: l = mid + 1 result = mid return result ```
2
0
['Python3']
0
maximum-containers-on-a-ship
Easiest C++ | O(1) | Just 2 Lines | Explained
easiest-c-o1-just-2-lines-explained-by-a-1i2j
IntuitionApproachComplexity Time complexity: Space complexity: Code
AK200199
NORMAL
2025-03-23T06:46:34.162160+00:00
2025-03-23T06:46:34.162160+00:00
81
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { // This finds the maximum container we can load int d=maxWeight/w; // now we return min of d and the total container that is n*n return min(d,n*n); } }; ```
2
0
['C++']
0
maximum-containers-on-a-ship
✅ ⟣ Java Solution ⟢
java-solution-by-harsh__005-u8is
Code
Harsh__005
NORMAL
2025-03-23T04:03:55.283128+00:00
2025-03-23T04:03:55.283128+00:00
129
false
# Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n*n, maxWeight/w); } } ```
2
0
['Java']
1
maximum-containers-on-a-ship
Straight forward beginner friendly solution😊✅✅
straight-forward-beginner-friendly-solut-au2v
Intuition:We need to load as many containers as possible without exceeding the maxWeight. Each container weighs w, and there are n containers available.Approach
nilestiwari_7
NORMAL
2025-03-23T04:02:18.202347+00:00
2025-03-23T04:02:18.202347+00:00
32
false
### Intuition: We need to load as many containers as possible without exceeding the `maxWeight`. Each container weighs `w`, and there are `n` containers available. ### Approach: 1. Start with `count = 0` and `wt = 0`. 2. While there are containers left and we can add one more without exceeding `maxWeight`, load the container. 3. Return the total number of containers loaded. ### Time Complexity: - **O(n)** because we loop through the containers at most once. ### Space Complexity: - **O(1)** because we use only a few variables. ### Code: ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int count = 0, wt = 0; while (n > 0) { if (wt + w <= maxWeight) { wt += w; count++; } else { break; } n--; } return count; } }; ``` ``` Python [] class Solution: def maxContainers(self, n, w, maxWeight): count = 0 wt = 0 while n > 0: if wt + w <= maxWeight: wt += w count += 1 else: break n -= 1 return count ``` ``` JavaScript [] class Solution { maxContainers(n, w, maxWeight) { let count = 0; let wt = 0; while (n > 0) { if (wt + w <= maxWeight) { wt += w; count++; } else { break; } n--; } return count; } }
2
0
['Greedy', 'C++', 'Python3', 'JavaScript']
0
maximum-containers-on-a-ship
Beats 100.00%
beats-10000-by-darwadenidhi174-h6tc
IntuitionApproachComplexity Time complexity: Space complexity: Code
darwadenidhi174
NORMAL
2025-04-11T15:41:50.274103+00:00
2025-04-11T15:41:50.274103+00:00
7
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int p=n*n; int q=maxWeight/w; int container=Math.min(p,q); return container; } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
Easy One Liner | 0ms Runtime | Beats 100%🥷🏻✨
easy-one-liner-0ms-runtime-beats-100-by-2wplq
IntuitionApproachComplexity Time complexity: Space complexity: Code
tyagideepti9
NORMAL
2025-03-31T17:28:39.168879+00:00
2025-03-31T17:28:39.168879+00:00
18
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```csharp [] public class Solution { public int MaxContainers(int n, int w, int maxWeight) { return Math.Min(maxWeight/w, n*n); } } ```
1
0
['Math', 'C#']
0
maximum-containers-on-a-ship
☑️ Finding Maximum Containers on a Ship. ☑️
finding-maximum-containers-on-a-ship-by-8to85
Code
Abdusalom_16
NORMAL
2025-03-31T16:04:20.718377+00:00
2025-03-31T16:04:20.718377+00:00
9
false
# Code ```dart [] class Solution { int maxContainers(int n, int w, int maxWeight) { int sum = 0; for(int i = 1; i <= n*n; i++){ sum += w; if(maxWeight < sum){ return i-1; } } return n*n; } } ```
1
0
['Math', 'Dart']
0
maximum-containers-on-a-ship
📦 Easy & Shorter Solution: Maximum Containers in an n × n Grid Under Weight Constraint ⚖️
easy-shorter-solution-maximum-containers-a246
IntuitionThe problem requires determining the maximum number of containers that can be placed in an ( n * n ) grid while ensuring that the total weight does not
Jils_Patel
NORMAL
2025-03-29T12:22:30.886353+00:00
2025-03-29T12:22:30.886353+00:00
10
false
# Intuition The problem requires determining the maximum number of containers that can be placed in an \( n * n \) grid while ensuring that the total weight does not exceed `maxWeight`. - First, we calculate the total number of containers that can fit in the grid, which is \( n^2 \). - Then, we determine how many containers can be supported by the given weight capacity, which is `maxWeight // w`. - The final answer is the minimum of these two values. # Approach 1. Compute the total number of containers that can fit in the grid: - This is given by \( n^2 \), since it is an \( n * n \) grid. 2. Compute the maximum number of containers allowed by weight: - Since each container has a weight `w`, the maximum number of containers that can be supported is `maxWeight // w`. 3. Return the minimum of these two values to ensure both space and weight constraints are satisfied. # Complexity - Time complexity: - $$O(1)$$, since we only perform a few arithmetic operations. - Space complexity: - $$O(1)$$, as we use only a few integer variables and no additional data structures. # Code ```python [] class Solution(object): def maxContainers(self, n, w, maxWeight): total_cells = n ** 2 max_possible_containers = maxWeight // w return min(total_cells, max_possible_containers) ``` ![upvote1.jpeg](https://assets.leetcode.com/users/images/8e937efe-de4c-45c6-87b1-6542b403a394_1743250853.5973294.jpeg)
1
0
['Python']
0
maximum-containers-on-a-ship
Just one line solution😂😂😂😂😂, beats 100% of the solutions.
just-one-line-solution-beats-100-of-the-urutx
Code
adityamah2002
NORMAL
2025-03-27T13:31:38.079271+00:00
2025-03-27T13:31:38.079271+00:00
14
false
# Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(maxWeight/w, n*n); } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
One-liner | Beats 100% | Python3
one-liner-beats-100-python3-by-alpha2404-o09b
Please UpvoteCode
Alpha2404
NORMAL
2025-03-25T16:35:09.956558+00:00
2025-03-25T16:35:09.956558+00:00
18
false
# Please Upvote # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(maxWeight//w,n*n) ```
1
0
['Python3']
0
maximum-containers-on-a-ship
📦 Maximum Containers Calculation
maximum-containers-calculation-by-akhild-3og8
Intuition🚀 IntuitionThe problem requires us to calculate the maximum number of containers that can be used while considering two constraints:Since we need the m
akhildas675
NORMAL
2025-03-25T04:01:33.308708+00:00
2025-03-25T04:01:33.308708+00:00
6
false
# Intuition 🚀 Intuition The problem requires us to calculate the maximum number of containers that can be used while considering two constraints: 1. The total number of available containers, which is n * n (assuming a square arrangement). 2. The weight constraint, which limits the number of containers based on maxWeight / w (each container has weight w). Since we need the minimum of these two constraints, the final result is: min⁡(n×n,maxWeightw) min(n×n,wmaxWeight​) # Approach 💡 Approach 1. Compute the total available containers: n×n n×n 2. Compute the maximum containers allowed by weight: maxWeightw wmaxWeight​ 3. Take the minimum of both values. 4. Use parseInt() to ensure the result is an integer (since containers must be whole numbers). # Complexity Time Complexity: O(1)O(1) – Only a few mathematical operations are performed. Space Complexity: O(1)O(1) – Uses only a few variables. # Code ```javascript [] /** * @param {number} n * @param {number} w * @param {number} maxWeight * @return {number} */ var maxContainers = function(n, w, maxWeight) { return parseInt(Math.min(n*n,maxWeight/w)); }; ``` ✅ Explanation with Example Example 1 Input: n = 3, w = 2, maxWeight = 10 Calculations: n2=32=9n2=32=9 102=5210​=5 Math.min(9, 5) = 5 Output: 5 Example 2 Input: n = 4, w = 1, maxWeight = 20 Calculations: n2=42=16n2=42=16 201=20120​=20 Math.min(16, 20) = 16 Output: 16 🏆 Why This Solution is Efficient ✅ Only uses simple math operations – No loops needed. ✅ Runs in constant time O(1)O(1) – Fast execution. ✅ Handles all edge cases – Works for small and large values of n, w, and maxWeight. 🔥 Status: Accepted ✅
1
0
['JavaScript']
0
maximum-containers-on-a-ship
EASY C++ 100% O(1)
easy-c-100-o1-by-hnmali-un6t
Code
hnmali
NORMAL
2025-03-24T17:34:44.727115+00:00
2025-03-24T17:34:44.727115+00:00
9
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(n * n, maxWeight / w); } }; ```
1
0
['Math', 'C++']
0
maximum-containers-on-a-ship
Linear time Solution
linear-time-solution-by-sumitksr-fdca
Code
sumitksr
NORMAL
2025-03-24T14:16:03.235603+00:00
2025-03-24T14:16:03.235603+00:00
7
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(n * n, maxWeight / w); } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Easy C++ solution | Beats 100 %
easy-c-solution-beats-100-by-anuragpal01-k8r1
IntuitionThe problem requires us to determine the maximum number of containers that can be loaded onto an n x n cargo deck without exceeding a given weight limi
Anuragpal010104
NORMAL
2025-03-24T08:48:00.593767+00:00
2025-03-24T08:48:00.593767+00:00
6
false
# Intuition The problem requires us to determine the maximum number of containers that can be loaded onto an n x n cargo deck without exceeding a given weight limit. Since each cell can hold exactly one container of weight w, we must balance the number of containers with the ship’s maximum weight capacity <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> - Calculate the total number of cells: Since the cargo deck is an n x n grid, it has cells = n * n total slots for containers. - Compute the total weight if all cells are filled: If every cell holds a container, the total weight would be weight = cells * w. - Check if the total weight exceeds maxWeight: - If weight ≤ maxWeight, then we can load all cells containers. - Otherwise, the number of containers must be limited to maxWeight / w, as each container weighs w # Complexity - Time complexity: - The approach involves basic arithmetic calculations, which take constant time. - O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: - We only use a few integer variables, requiring constant space. - O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int cells=n*n; int weight=cells*w; if(weight<maxWeight) return cells; else return maxWeight/w; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Hmm..One Line
hmmone-line-by-runningfalcon-ft5d
IntuitionApproachComplexity Time complexity: Space complexity: Code
runningfalcon
NORMAL
2025-03-23T23:04:04.384956+00:00
2025-03-23T23:04:04.384956+00:00
11
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return (n * n * w < maxWeight ? n * n : maxWeight / w); } } ``` ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: if n*n*w < maxWeight: return n*n return maxWeight//w ```
1
0
['Python3']
0
maximum-containers-on-a-ship
O(1) Solution one linear Basic class 7th Maths
o1-solution-one-linear-basic-class-7th-m-zd5f
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1) Code
utk50090
NORMAL
2025-03-23T18:17:47.444883+00:00
2025-03-23T18:17:47.444883+00:00
11
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: - O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: - O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int container = maxWeight/w; int cell = n * n; if(cell > container) return container; return cell; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Calculating Maximum Containers on a Ship Deck Using Weight and Capacity Constraints
calculating-maximum-containers-on-a-ship-wi4a
IntuitionThe problem requires determining the maximum number of containers that can be loaded onto an n×n cargo deck without exceeding the ship's maximum weight
x7Fg9_K2pLm4nQwR8sT3vYz5bDcE6h
NORMAL
2025-03-23T17:14:14.855132+00:00
2025-03-23T17:14:14.855132+00:00
67
false
# Intuition The problem requires determining the maximum number of containers that can be loaded onto an n×n cargo deck without exceeding the ship's maximum weight capacity. My first thought was to calculate the total number of cells on the deck and compare it with the maximum number of containers allowed by the weight constraint. The answer would be the smaller of the two values. # Approach 1. Calculate Total Cells: Compute the total number of cells on the n×n deck, which is n^2. 2. Calculate Maximum Containers by Weight: Determine the maximum number of containers allowed by the weight constraint, which is ⌊maxWeight/w⌋. 3. Return the Minimum: The result is the minimum of the total number of cells and the maximum number of containers allowed by weight. # Complexity - Time complexity: $$O(1)$$ - Space complexity: $$O(1)$$ # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(n*n,maxWeight/w); } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Easy Solution
easy-solution-by-jordon-x-matter-0h2a
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1) Code
Jordon-x-Matter
NORMAL
2025-03-23T15:52:59.711868+00:00
2025-03-23T15:52:59.711868+00:00
7
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(n * n, maxWeight / w); } }; ```
1
0
['Math', 'C++']
0
maximum-containers-on-a-ship
O(1) time complexity || One liner || Java || Easiest Approach💯
o1-time-complexity-one-liner-java-easies-m3i1
IntuitionApproachComplexity Time complexity:O(1) Space complexity:O(1) Code
aryaman123
NORMAL
2025-03-23T11:20:36.992989+00:00
2025-03-23T11:21:04.440807+00:00
22
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { //maxweight < n*n*w :ans = n*n; //maxweight> n*n*w : ans = maxweight/w; if(maxWeight >= n*n*w) return n*n; else return maxWeight/w; } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
0ms 100% O(1) fmin
stdmin-by-michelusa-27ze
O(1)Cfmin() from the math libraryC++
michelusa
NORMAL
2025-03-23T11:07:06.005998+00:00
2025-03-23T11:22:11.429560+00:00
20
false
O(1) # C fmin() from the math library ```c [] int maxContainers(int n, int w, int maxWeight) { return w ? fmin(n * n, maxWeight / w) : 0; } ``` # C++ ```cpp [] class Solution { public: int maxContainers(const int n, const int w, const int maxWeight) { return w ? std::min(n * n, maxWeight / w) : 0; } }; ```
1
0
['C', 'C++']
0
maximum-containers-on-a-ship
Beats 100%,Easisest Solution, One-Liner
beats-100easisest-solution-by-harshbhush-8r0z
IntuitionApproachComplexity Time complexity:O(1) Space complexity:0(1) Code
Harshbhushandixit
NORMAL
2025-03-23T11:01:06.289738+00:00
2025-03-23T11:01:38.983015+00:00
19
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:0(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n*n,maxWeight/w); } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-by-danisd-dagh
Code
DanisDeveloper
NORMAL
2025-03-23T10:48:38.267847+00:00
2025-03-23T10:48:38.267847+00:00
14
false
# Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(n * n, maxWeight // w) ```
1
0
['Python3']
0
maximum-containers-on-a-ship
My Solution O(1) time complexity|| easy solution
my-solution-o1-time-complexity-easy-solu-34hi
IntuitionApproachComplexity Time complexity: Space complexity: Code
Preetam_123Pandey
NORMAL
2025-03-23T08:56:42.138280+00:00
2025-03-23T08:56:42.138280+00:00
11
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int count=0; int a=maxWeight/w; if(a<=n*n){ return a; } else{ return n*n; } return count; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
C++ GRANDMASTER LEGENDARY SOLUTION ;)
c-grandmaster-legendary-solution-by-half-x0jq
Code
Half-Dimension
NORMAL
2025-03-23T08:52:53.807651+00:00
2025-03-23T08:52:53.807651+00:00
13
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int cells=n*n; if(cells*w<=maxWeight){ return cells; } return maxWeight/w; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Linear | O(1) 👽| cpp 🤩
linear-o1-cpp-by-varuntyagig-vcla
Code
varuntyagig
NORMAL
2025-03-23T08:51:02.318621+00:00
2025-03-23T08:51:02.318621+00:00
8
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int total_Containers = n * n; int max_Container_loads = maxWeight / w; if (total_Containers <= max_Container_loads) { return total_Containers; } return max_Container_loads; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
100percent Affective code in JAVA
100percent-affective-code-in-java-by-anu-obya
IntuitionApproachComplexity Time complexity: Space complexity: Code
anuragk2
NORMAL
2025-03-23T08:28:53.675665+00:00
2025-03-23T08:28:53.675665+00:00
5
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int a=n*n; int b=maxWeight/w; return Math.min(a,b); } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
Simple 2 line code | Easily Understandable | O(1) time complexity | Java | Beats 100%
simple-2-line-code-easily-understandable-vl3p
Complexity Time complexity:O(1) Space complexity:O(1) Code
Aditya_Yadav_
NORMAL
2025-03-23T08:25:28.718478+00:00
2025-03-23T08:25:28.718478+00:00
3
false
# Complexity - Time complexity:$$O(1)$$ <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:$$O(1)$$ <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { if(n*n*w < maxWeight) return (n*n); return maxWeight/w; } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
<<0 LINER CODE || BEAT 100% SOLUTIONS>>
0-liner-code-beat-100-solutions-by-daksh-gzwd
PLEASE UPVOTE MECode
Dakshesh_vyas123
NORMAL
2025-03-23T08:21:00.232799+00:00
2025-03-23T08:21:00.232799+00:00
5
false
# PLEASE UPVOTE ME # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min((maxWeight/w),(n*n)); } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Maximum Containers That Can Be Filled
maximum-containers-that-can-be-filled-by-5iqc
IntuitionThe problem requires determining the maximum number of containers that can be filled given:Total available space: The number of containers that can fit
Sarthak_Kharwade
NORMAL
2025-03-23T07:08:22.957149+00:00
2025-03-23T07:08:22.957149+00:00
14
false
# Intuition The problem requires determining the maximum number of containers that can be filled given: Total available space: The number of containers that can fit in a given area, which is n * n. Weight constraint: Each container has a fixed weight w, and the total weight cannot exceed maxWeight. ### **Your Approach Explanation:** 1. **Calculate Space Constraint** → The maximum number of containers that can fit is `n * n`. 2. **Calculate Weight Constraint** → The maximum number of containers based on weight is `maxWeight / w`. 3. **Return the Minimum** → The final result is `Math.min(n * n, maxWeight / w)`, ensuring we don’t exceed either the space or weight limits. ### **Time Complexity:** - The solution performs only a few arithmetic operations and a `Math.min()` comparison, all of which run in **O(1) (constant time).** ### **Space Complexity:** - No extra data structures are used, so the space complexity is **O(1) (constant space).** ![Screenshot 2025-02-11 233508.png](https://assets.leetcode.com/users/images/949e9905-3179-4a2c-958c-a370fe3ede6a_1742713690.3508477.png) # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n*n,maxWeight/w); } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
simple solutions with 100% beat
simple-solutions-with-100-beat-by-vinay_-r3vr
IntuitionApproachComplexity Time complexity: Space complexity: Code
vinay_kumar_swami
NORMAL
2025-03-23T06:59:17.161146+00:00
2025-03-23T06:59:17.161146+00:00
7
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int count=0; int p=n*n; int re=maxWeight/w; if(re>p) { return p; } return re; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
cpp
cpp-by-ajay__j-7ejk
Code
Ajay__J
NORMAL
2025-03-23T06:36:11.990578+00:00
2025-03-23T06:36:11.990578+00:00
7
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int ans=0; for (int i=0;i<(n*n);i++){ if ((ans+1)*w>maxWeight) return ans; ans++; } return ans; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Simple without loops O(1) TC&SC solution||EASY SOLUTION✅
simple-without-loops-o1-tcsc-solutioneas-mfan
IntuitionSee they have asked the min num of decks that can be stored, so what you can do is without running a loop find n*n and maxWeigbt/w (to find the num of
Gaurav_SK
NORMAL
2025-03-23T05:24:49.913903+00:00
2025-03-23T05:24:49.913903+00:00
6
false
# Intuition See they have asked the min num of decks that can be stored, so what you can do is without running a loop find n*n and maxWeigbt/w (to find the num of decks which can be stored in the deck) # Approach <!-- Describe your approach to solving the problem. --> declare the variables to calculate # Complexity - Time complexity: - O(1) : No extra space used(no loops) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O(1): No extra space like vector, etc is used # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int totalDeck = n * n; int weight = maxWeight/w; return min(totalDeck, weight); } }; ```
1
0
['Math', 'C++']
0
maximum-containers-on-a-ship
C++ Easy Solution
c-easy-solution-by-namanverma01-azs5
Code
namanverma01
NORMAL
2025-03-23T05:23:47.141366+00:00
2025-03-23T05:23:47.141366+00:00
28
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int desk=n*n; int ans=0; for(int i=desk;i>=0;i--) { if(desk*w<=maxWeight) { ans=desk; break; } else{ desk--; } } return ans; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Half Line | O(1) | Rust
half-line-o1-rust-by-prog_jacob-ypjx
Complexity Time complexity: O(1) Space complexity: O(1) Code
Prog_Jacob
NORMAL
2025-03-23T04:55:32.694982+00:00
2025-03-23T04:55:32.694982+00:00
8
false
# Complexity - Time complexity: $$O(1)$$ <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: $$O(1)$$ <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```rust [] impl Solution { pub fn max_containers(n: i32, w: i32, max_weight: i32) -> i32 { (n * n * w).min(max_weight) / w } } ```
1
0
['Rust']
0
maximum-containers-on-a-ship
Beats 100% || Math 🚀🚀🚀🚀
beats-100-math-by-dheeraj_2602-pk0i
IntuitionThe problem involves maximizing the number of containers we can fill, given a constraint on individual container weight and the total allowable weight.
Dheeraj_2602
NORMAL
2025-03-23T04:43:38.100134+00:00
2025-03-23T04:43:38.100134+00:00
11
false
# Intuition The problem involves maximizing the number of containers we can fill, given a constraint on individual container weight and the total allowable weight. The two limiting factors are: Total number of containers available: 𝑛^2 (since it’s an 𝑛×𝑛 grid of containers). Maximum number of containers we can fill based on weight capacity: maxWeight/𝑤 (since each container has a weight of 𝑤). # Approach # Complexity - Time complexity: O(1) - Space complexity: O(1) # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n*n,(maxWeight/w)); } } ``` ```c++ [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(n * n, maxWeight / w); } }; ``` ```python [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(n * n, maxWeight // w) ```
1
0
['Math', 'Python', 'C++', 'Java']
0
maximum-containers-on-a-ship
🚢 Max Containers on a Cargo Deck 🚢
max-containers-on-a-cargo-deck-by-opwzpx-mnzz
💡 Intuition:The problem requires us to determine the maximum number of containers that can be loaded onto an n×n cargo deck without exceeding the ship's weight
opWzPXRgDd
NORMAL
2025-03-23T04:15:43.712812+00:00
2025-03-23T04:15:43.712812+00:00
35
false
# 💡 Intuition: <!-- Describe your first thoughts on how to solve this problem. --> The problem requires us to determine the maximum number of containers that can be loaded onto an n×n cargo deck without exceeding the ship's weight capacity. Each cell on the deck can hold exactly one container, meaning the maximum possible number of containers is n^2. However, each container has a weight w, and we must ensure that their total weight does not exceed maxWeight. # 🛠️ Approach: <!-- Describe your approach to solving the problem. --> 1. Brute Force Iteration: - Start with 0 containers and iterate from 1 to n^2. - Check if adding another container `(total weight = w×containers)` exceeds `maxWeight`. - If not, update the maximum number of containers. 2. Optimized Approach: - Instead of iterating through all values, we can use `min(n*n, maxWeight // w)` - Since each container weighs w, we can only fit at most `maxWeight // w` containers. - The total containers must also be ≤ n^2, so the final answer is: `min(n^2, (maxWeight/w))` # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> Brute Force: $$O(n^2)$$ (Iterating over all possible containers) Optimized: $$O(1)$$ (Direct computation using division and min function) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code 1. Brute force: ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: res = 0 for i in range(1, n*n+1): if(w*i <= maxWeight): res = max(res, i) return res ``` 2. Optimized: ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(n * n, maxWeight // w) ``` ![DALL·E 2025-03-23 09.45.16 - A fun and engaging poster asking for upvotes on a LeetCode solution. The poster features a cargo ship with containers labeled as 'Upvotes'. The text o.webp](https://assets.leetcode.com/users/images/9c10a314-7de8-44b0-a6e8-35af390cfb26_1742703333.1395214.webp)
1
0
['Greedy', 'Graph', 'Python3']
0
maximum-containers-on-a-ship
Easy and beginner friendly || O(1) || beats 💯🔥🔥
easy-and-beginner-friendly-o1-by-codewit-a2bx
IntuitionApproachComplexity Time complexity: Space complexity: Code
CodeWithMithun
NORMAL
2025-03-23T04:15:02.903392+00:00
2025-03-23T04:16:32.530084+00:00
20
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ -->O(1) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ -->O(1) # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int count= maxWeight/w; int cell= n*n; return (cell>count)? count: cell; } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Math || Java
math-java-by-akshay_kadamm-5f75
IntuitionApproachComplexity Time complexity: Space complexity: Code
Akshay_Kadamm
NORMAL
2025-03-23T04:09:48.689431+00:00
2025-03-23T04:09:48.689431+00:00
12
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int total= n*n*w; if(total>maxWeight) return maxWeight/w; else return n*n; } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
Java || Beats 100% || Time Complexity : O(1)
java-beats-100-time-complexity-o1-by-shi-nmgz
Complexity Time complexity: O(1) Space complexity: O(1) Code
shikhargupta0645
NORMAL
2025-03-23T04:09:17.987463+00:00
2025-03-23T04:09:17.987463+00:00
12
false
# Complexity - Time complexity: O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int c = n*n; int count = maxWeight/w; return Math.min(c,count); } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
Python Code: it's work
python-code-its-work-by-bhav5sh-y0vn
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1) Code
Bhav5sh
NORMAL
2025-03-23T04:08:24.915897+00:00
2025-03-23T04:08:24.915897+00:00
21
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: total_cells = n * n total_cells_weight = total_cells * w if total_cells_weight <= maxWeight: return total_cells else: return maxWeight // w ```
1
0
['Python3']
0
maximum-containers-on-a-ship
Java solution || Time Complexity : O(n)
java-solution-time-complexity-on-by-shik-d4fr
Complexity Time complexity: O(n) Space complexity: O(1) Code
shikhargupta0645
NORMAL
2025-03-23T04:07:34.025270+00:00
2025-03-23T04:07:34.025270+00:00
14
false
# Complexity - Time complexity: O(n) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int count = 0; int wt = 0; while(wt < maxWeight && count < n*n){ wt += w; if(wt <= maxWeight){ count++; } } return count; } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
One Liner
one-liner-by-tylerdurdn-obni
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1)Code
tylerdurdn
NORMAL
2025-03-23T04:05:28.344437+00:00
2025-03-23T04:05:28.344437+00:00
12
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> O(1) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O(1) # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(maxWeight//w, n*n) ```
1
0
['Python3']
0
maximum-containers-on-a-ship
Simple Solution - Using just one line - Python/Java/C++
simple-solution-using-just-one-line-pyth-jjcn
IntuitionApproachComplexity Time complexity: Space complexity: Code
cryandrich
NORMAL
2025-03-23T04:04:45.633210+00:00
2025-03-23T04:04:45.633210+00:00
3
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(maxWeight / w, n * n); } }; ``` ```python [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(maxWeight // w, n * n) ``` ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(maxWeight / w, n * n); } } ```
1
0
['C++']
0
maximum-containers-on-a-ship
Easy Solution || Java || Python || C++ || 🚀🚀 || 🔥🔥
easy-solution-java-python-c-by-vermaansh-g5iq
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1) Code
vermaanshul975
NORMAL
2025-03-23T04:04:09.047661+00:00
2025-03-23T04:04:09.047661+00:00
15
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int ans = 0; n*=n; while(n*w>maxWeight){ n-=1; } return n; } } ``` ```python [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: n *= n while n * w > maxWeight: n -= 1 return n ``` ```C++ [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int ans = 0; n *= n; while (n * w > maxWeight) { n -= 1; } return n; } }; ```
1
0
['Java']
0
maximum-containers-on-a-ship
|| ✅#DAY_64th_Of_Daily_Coding✅ ||
day_64th_of_daily_coding-by-coding_with_-lyt0
JAI SHREE DATNA🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏Code
Coding_With_Star
NORMAL
2025-03-23T04:03:58.940832+00:00
2025-03-23T04:04:47.614641+00:00
16
false
# JAI SHREE DATNA🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏 # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int totalSlots = n * n; int maxByWeight = maxWeight / w; return min(totalSlots, maxByWeight); } }; ```
1
0
['C++', 'Java', 'Python3']
0
maximum-containers-on-a-ship
Java | Easy | One Line Solution | Beats 100%
java-easy-one-line-solution-beats-100-by-d41f
IntuitionApproachComplexity Time complexity: Space complexity: Code
Pankajj_42
NORMAL
2025-03-23T04:03:22.393113+00:00
2025-03-23T04:03:22.393113+00:00
15
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min( n*n, maxWeight / w ); } } ```
1
0
['Java']
0
maximum-containers-on-a-ship
Math | C++ | Simple One liner
math-c-simple-one-liner-by-thangakumaran-cpr6
Complexity Time complexity: O (1) Space complexity: Code
Thangakumaran
NORMAL
2025-03-23T04:03:17.988716+00:00
2025-03-23T04:03:17.988716+00:00
15
false
# Complexity - Time complexity: O (1) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O (1) # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return ((maxWeight / w) <= (n * n)) ? (maxWeight / w) : (n * n); } }; ```
1
0
['Math', 'C++']
0
maximum-containers-on-a-ship
Easiest 👇👇O(1) 🔥 Maximum Containers on a Ship ⚡
easiest-o1-maximum-containers-on-a-ship-tx15t
IntuitionApproachComplexity Time complexity: Space complexity: Code
Vanshnigam
NORMAL
2025-03-23T04:02:51.946823+00:00
2025-03-23T04:02:51.946823+00:00
90
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { long m=n*n; if((long)m*w <= maxWeight)return (int)m; return maxWeight/(w); } } ```
1
0
['Array', 'C++', 'Java', 'Python3']
1
maximum-containers-on-a-ship
C++ 1 liner straightforward solution
c-1-liner-straightforward-solution-by-em-7hnu
Code
eminem18753
NORMAL
2025-03-23T04:02:39.626702+00:00
2025-03-23T04:02:39.626702+00:00
9
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(n*n,maxWeight/w); } }; ```
1
0
['C++']
0
maximum-containers-on-a-ship
Beats 100% || Simple and Easy Code
beats-100-simple-and-easy-code-by-balara-7956
IntuitionThe problem aims to find the maximum number of containers that can be filled, given a certain number of items, weight per item, and maximum total weigh
Balarakesh
NORMAL
2025-04-11T17:58:50.898157+00:00
2025-04-11T17:58:50.898157+00:00
1
false
# Intuition The problem aims to find the maximum number of containers that can be filled, given a certain number of items, weight per item, and maximum total weight. It involves calculating the maximum number of containers based on the number of items and the maximum number of containers based on the total weight constraint, and then returning the minimum of these two values. # Approach 1. Calculate the maximum number of containers that can be filled based on the number of items: `a = n * n`. 2. Calculate the maximum number of containers that can be filled based on the maximum total weight: `b = maxWeight // w`. 3. Return the minimum of `a` and `b`, as the actual number of containers cannot exceed either of these limits. # Complexity - Time complexity: $$O(1)$$ because the operations performed are constant-time arithmetic operations. - Space complexity: $$O(1)$$ because we are only using a constant amount of extra space for the variables `a` and `b`. # Code ```python3 class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: a = n*n b = maxWeight//w return min(a, b)
0
0
['Python3']
0
maximum-containers-on-a-ship
Basic Mathematical Logic - Beats 100%
basic-mathematical-logic-beats-100-by-dw-00hr
Intuition Basic Mathematical Logic Approach Calculate the maximal grid spaces available (nxn) Calculate the maximum number of crates for weight restriction. Tak
dworoniuk1
NORMAL
2025-04-10T14:53:44.371262+00:00
2025-04-10T14:53:44.371262+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> - Basic Mathematical Logic # Approach <!-- Describe your approach to solving the problem. --> - Calculate the maximal grid spaces available (nxn) - Calculate the maximum number of crates for weight restriction. - Take minimum of two outcomes # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> $$O(1)$$ - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> $$O(1)$$ # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(n**2, (maxWeight // w)) ```
0
0
['Python3']
0
maximum-containers-on-a-ship
Maximum Containers on a Ship
maximum-containers-on-a-ship-by-manish12-5ryk
IntuitionApproachComplexity Time complexity: Space complexity: Code
manish120903
NORMAL
2025-04-10T12:22:15.900334+00:00
2025-04-10T12:22:15.900334+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int cnt =0; int sum =0; int cap = (n*n)*w; while(sum+w<=maxWeight && sum+w<= cap){ sum+=w; cnt++; } return cnt; } }; ```
0
0
['C++']
0
maximum-containers-on-a-ship
C++ solution
c-solution-by-niteeshn158227-g5yn
Code
niteeshn158227
NORMAL
2025-04-09T07:34:02.522894+00:00
2025-04-09T07:34:02.522894+00:00
1
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { int totalCells = n*n; int totalWeight = totalCells*w; if(totalWeight <=maxWeight) { return totalCells; } while(totalWeight > maxWeight) { totalCells--; totalWeight -= w; } return totalCells; } }; ```
0
0
['C++']
0
maximum-containers-on-a-ship
one LINER (beats 100% runtime, 100% memory)
one-liner-beats-100-runtime-100-memory-b-e68j
IntuitionApproachComplexity Time complexity: Space complexity: Code
dpasala
NORMAL
2025-04-09T04:00:35.136732+00:00
2025-04-09T04:00:35.136732+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n * n, maxWeight / w); } } ```
0
0
['Java']
0
maximum-containers-on-a-ship
Swift💯 tiny 1liner
swift-tiny-1liner-by-upvotethispls-tb5v
One-Liner (accepted answer)
UpvoteThisPls
NORMAL
2025-04-08T23:06:45.371520+00:00
2025-04-08T23:06:45.371520+00:00
1
false
**One-Liner (accepted answer)** ``` class Solution { func maxContainers(_ n: Int, _ w: Int, _ maxWeight: Int) -> Int { min(n*n, maxWeight/w) } } ```
0
0
['Swift']
0
maximum-containers-on-a-ship
Easiest and simplest Solution
easiest-and-simplest-solution-by-sdkv-tvuz
Complexity Time complexity: O(1) Space complexity: O(1) Code
sdkv
NORMAL
2025-04-08T12:06:04.892206+00:00
2025-04-08T12:06:04.892206+00:00
1
false
# Complexity - Time complexity: O(1) - Space complexity: O(1) # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { if(maxWeight-n*n*w >= 0){ return n*n; }else{ return maxWeight/w; } } }; ```
0
0
['C++']
0
maximum-containers-on-a-ship
Runtime 0 ms Beats 100.00%
runtime-0-ms-beats-10000-by-iamsd-8q9k
Code
iamsd_
NORMAL
2025-04-08T05:05:31.444222+00:00
2025-04-08T05:05:31.444222+00:00
1
false
# Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n * n, maxWeight / w); } } ```
0
0
['Java']
0
maximum-containers-on-a-ship
Java Solution
java-solution-by-a_shekhar-mawb
IntuitionApproachComplexity Time complexity: O(n) Space complexity: O(1)Code
a_shekhar
NORMAL
2025-04-07T16:50:58.237073+00:00
2025-04-07T16:50:58.237073+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> O(n) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O(1) # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int containers = n * n; int currWeight = 0; int occupiedContainer = 0; while(occupiedContainer < containers){ if(currWeight + w <= maxWeight) { currWeight += w; occupiedContainer++; }else{ return occupiedContainer; } } return occupiedContainer; } } ```
0
0
['Java']
0
maximum-containers-on-a-ship
Pure Math approach - beats 100%
pure-math-approach-beats-100-by-jbehak-h53z
Code
JBehak
NORMAL
2025-04-07T15:18:51.906823+00:00
2025-04-07T15:18:51.906823+00:00
3
false
# Code ```csharp [] public class Solution { public int MaxContainers(int n, int w, int maxWeight) { if (n * n * w > maxWeight) { return maxWeight / w; } else { return n * n; } } } ```
0
0
['Math', 'C#']
0
maximum-containers-on-a-ship
Javascript one line solution
javascript-one-line-solution-by-meloman3-d0gv
IntuitionJavascript one line solutionComplexity Time complexity: O(1) Space complexity: O(1) Code
meloman370
NORMAL
2025-04-06T16:38:19.130101+00:00
2025-04-06T16:38:19.130101+00:00
2
false
# Intuition Javascript one line solution <!-- Describe your first thoughts on how to solve this problem. --> # Complexity - Time complexity: O(1) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```typescript [] function maxContainers(n: number, w: number, maxWeight: number): number { //const maxContainers = (maxWeight - maxWeight % w) / w; return Math.min((maxWeight - maxWeight % w) / w, Math.pow(n, 2)); }; ```
0
0
['TypeScript']
0
maximum-containers-on-a-ship
JavaScript - JS
javascript-js-by-mlienhart-leao
null
mlienhart
NORMAL
2025-04-06T16:20:22.105022+00:00
2025-04-06T16:20:22.105022+00:00
1
false
```javascript [] /** * @param {number} n * @param {number} w * @param {number} maxWeight * @return {number} */ var maxContainers = function (n, w, maxWeight) { return Math.min(Math.floor(maxWeight / w), n * n); }; ```
0
0
['JavaScript']
0
maximum-containers-on-a-ship
Simple solution PYTHON beats 100%
simple-solution-python-beats-100-by-shah-2ba1
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1)Code
shaheershakir
NORMAL
2025-04-03T19:36:15.973355+00:00
2025-04-03T19:36:15.973355+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> O(1) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O(1) # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: cell = min(int(n*n), int(maxWeight / w)) return cell ```
0
0
['Python3']
0
maximum-containers-on-a-ship
Java Easy solution
java-easy-solution-by-anujpachauri-jhn8
Intuition"Calculate the number of decks, then determine how many containers are needed to hold all the weights."Approach"Calculate the number of decks, then det
anujpachauri
NORMAL
2025-04-01T12:21:21.079090+00:00
2025-04-01T12:21:21.079090+00:00
1
false
# Intuition "Calculate the number of decks, then determine how many containers are needed to hold all the weights." # Approach "Calculate the number of decks, then determine how many containers are needed to hold all the weights." # Complexity - Time complexity: O(n) - Space complexity: O(n) # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int deck=n*n; int containers=maxWeight/w; return Math.min(deck,containers); } } ```
0
0
['Java']
0
maximum-containers-on-a-ship
C++ One linear
c-one-linear-by-the_ghuly-2evp
Code
the_ghuly
NORMAL
2025-04-01T08:41:29.362352+00:00
2025-04-01T08:41:29.362352+00:00
2
false
# Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxWeight) { return min(maxWeight/w, n*n); } }; ```
0
0
['C++']
0
maximum-containers-on-a-ship
Solution
solution-by-frolovdata0238-jda0
ApproachNo one line solution, easy to understand.Complexity Time complexity: O(1) Space complexity: O(1) Code
frolovdata0238
NORMAL
2025-04-01T05:57:41.777028+00:00
2025-04-01T05:57:41.777028+00:00
1
false
# Approach No one line solution, easy to understand. # Complexity - Time complexity: $$O(1)$$ - Space complexity: $$O(1)$$ # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: max_count_of_containers = maxWeight // w count_of_cells = n * n if count_of_cells >= max_count_of_containers: return max_count_of_containers return max_count_of_containers - (max_count_of_containers - count_of_cells) ```
0
0
['Python3']
0
maximum-containers-on-a-ship
my solution
my-solution-by-leman_cap13-7ca0
IntuitionApproachComplexity Time complexity: Space complexity: Code
leman_cap13
NORMAL
2025-03-31T13:13:38.110684+00:00
2025-03-31T13:13:38.110684+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: cell=n**2 if cell*w<=maxWeight: return int(cell) elif cell*w>=maxWeight: return int(maxWeight/w) ```
0
0
['Python3']
0
maximum-containers-on-a-ship
Use min to find max.
use-min-to-find-max-by-shaikh_muhaiminul-kq5r
IntuitionWe have to determine the maximum number of boxes we can take.We can reach the constraints in two ways either space or weight. We can take the lesser of
Shaikh_Muhaiminul_Hasan
NORMAL
2025-03-30T18:05:03.030430+00:00
2025-03-30T18:05:03.030430+00:00
1
false
# Intuition We have to determine the maximum number of boxes we can take.We can reach the constraints in two ways either space or weight. We can take the lesser of the two constraints because if we reach the maximum space before weight then we take the maximum space and vice versa. # Approach We return the minimum of n*n(maximum space) and number of box according to weight(maxWeight // m) and we use "//" because number of box must be an integer. # Complexity - Time complexity: $$O(1)$$ - Space complexity: $$O(1)$$ # Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(n*n, maxWeight // w) ```
0
0
['Python3']
0
maximum-containers-on-a-ship
Luke - max containers solution
luke-max-containers-solution-by-thunderl-bwdp
IntuitionI thought that I had to do extra coding.ApproachI only needed the hint to do this. No knowledge needed. Just return the minimum of n squared and the ma
thunderlukey
NORMAL
2025-03-29T23:59:08.090486+00:00
2025-03-29T23:59:08.090486+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> I thought that I had to do extra coding. # Approach <!-- Describe your approach to solving the problem. --> I only needed the hint to do this. No knowledge needed. Just return the minimum of n squared and the maxweight/w. Just read the hint and turn it into java code and thats it. # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { return Math.min(n*n, maxWeight/w); } } ```
0
0
['Java']
0
maximum-containers-on-a-ship
Scala solution
scala-solution-by-iyieo99amh-27x2
IntuitionApproachComplexity Time complexity: Space complexity: Code
iyIeO99AmH
NORMAL
2025-03-29T14:58:41.692435+00:00
2025-03-29T14:58:41.692435+00:00
1
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```scala [] object Solution { def maxContainers(n: Int, w: Int, maxWeight: Int): Int = { math.min(n * n, maxWeight / w) } } ```
0
0
['Scala']
0
maximum-containers-on-a-ship
easy approach
easy-approach-by-sidgogia20-yx3e
IntuitionApproachComplexity Time complexity: Space complexity: Code
sidgogia20
NORMAL
2025-03-29T12:01:20.651705+00:00
2025-03-29T12:01:20.651705+00:00
0
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```cpp [] class Solution { public: int maxContainers(int n, int w, int maxwt) { return min(n*n,maxwt/w); } }; ```
0
0
['C++']
0
maximum-containers-on-a-ship
Possible and but
possible-and-but-by-dinesh0212kumar-r65q
IntuitionApproachComplexity Time complexity: O(1) Space complexity: O(1)Code
Dinesh0212Kumar
NORMAL
2025-03-29T07:23:05.953459+00:00
2025-03-29T07:23:05.953459+00:00
2
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> O(1) - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> O(1) # Code ```java [] class Solution { public int maxContainers(int n, int w, int maxWeight) { int possible=n*n; int but=maxWeight/w; if(possible<but){ return possible; } else{ return but; } } } ```
0
0
['Java']
0
maximum-containers-on-a-ship
Containers on a Ship - Math.floor ( )
containers-on-a-ship-mathfloor-by-zemamb-7e26
null
zemamba
NORMAL
2025-03-28T22:03:09.623365+00:00
2025-03-28T22:04:19.112280+00:00
2
false
```javascript [] var maxContainers = function(n, w, maxWeight) { return Math.min(n * n, Math.floor(maxWeight / w)) }; ```
0
0
['JavaScript']
0
maximum-containers-on-a-ship
Python 1 liner
python-1-liner-by-plavak_d10-m319
Code
plavak_d10
NORMAL
2025-03-28T21:37:33.325770+00:00
2025-03-28T21:37:33.325770+00:00
1
false
# Code ```python3 [] class Solution: def maxContainers(self, n: int, w: int, maxWeight: int) -> int: return min(n*n,maxWeight//w) ```
0
0
['Python3']
0