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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
number-of-rectangles-that-can-form-the-largest-square | C++ easy solution | c-easy-solution-by-arvindxsharma-fodh | \nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n vector<int>arr;\n int temp=1;\n for(int i=0;i | arvindxsharma | NORMAL | 2022-07-30T17:38:50.498700+00:00 | 2022-07-30T17:38:50.498740+00:00 | 244 | false | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n vector<int>arr;\n int temp=1;\n for(int i=0;i<rectangles.size();i++){\n int length=rectangles[i][0];\n int breadth=rectangles[i][1];\n int ans=min(length,breadth);\n arr.push_back(ans);\n }\n \n sort(arr.begin(),arr.end());\n int l=arr.size()-1;\n for(int i=l;i>0;i--){\n if(arr[i]!=arr[i-1]){\n break;\n \n }\n temp++;\n }\n return temp;\n }\n};\n``` | 2 | 0 | ['Array', 'C'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | javascript solution | javascript-solution-by-webbyelena-ss7v | \nvar countGoodRectangles = function(rectangles) {\n let [min, max, count] = [0, 0, 0];\n \n for(let i = 0; i < rectangles.length; i++) {\n let | webbyelena | NORMAL | 2022-06-24T16:15:26.111668+00:00 | 2022-06-24T22:55:23.054669+00:00 | 148 | false | ```\nvar countGoodRectangles = function(rectangles) {\n let [min, max, count] = [0, 0, 0];\n \n for(let i = 0; i < rectangles.length; i++) {\n let rec = rectangles[i];\n min = foundMin(rec[0], rec[1]); \n if( min == max ) {\n count++;\n }\n if( min > max ) {\n max = min;\n count = 1;\n }\n }\n return count;\n};\n\nfunction foundMin(a, b) {\n if( a < b ) return a;\n else return b;\n}\n``` | 2 | 0 | ['JavaScript'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | [Java] Traversal Easy Approach | 1ms | 100% | java-traversal-easy-approach-1ms-100-by-blwfe | java []\nclass Solution {\n\tpublic int countGoodRectangles(int[][] rectangles) {\n\t\tint res = 0, max = -1;\n\t\tfor (int[] rect : rectangles) {\n\t\t\tint cu | ethanrao | NORMAL | 2022-02-04T00:46:33.126751+00:00 | 2022-10-16T08:59:49.155188+00:00 | 130 | false | ```java []\nclass Solution {\n\tpublic int countGoodRectangles(int[][] rectangles) {\n\t\tint res = 0, max = -1;\n\t\tfor (int[] rect : rectangles) {\n\t\t\tint cur = Math.min(rect[0], rect[1]);\n\t\t\tif (cur == max) {\n\t\t\t\tres++;\n\t\t\t} else if (cur > max) {\n\t\t\t\tmax = cur;\n\t\t\t\tres = 1;\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t}\n}\n``` | 2 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | [Python] O(n) time and O(1) space | python-on-time-and-o1-space-by-nttncs-0ngo | \ndef countGoodRectangles(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: int\n """\n max_len = 0\n | nttncs | NORMAL | 2021-10-25T12:18:49.660981+00:00 | 2021-10-25T12:18:49.661010+00:00 | 73 | false | ```\ndef countGoodRectangles(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: int\n """\n max_len = 0\n count_max = 0\n for l, w in rectangles:\n min_len = min(l, w)\n if min_len > max_len:\n max_len = min_len \n count_max = 0\n if min_len == max_len:\n count_max += 1\n return count_max\n``` | 2 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple and Easy C++ Solution | simple-and-easy-c-solution-by-pranav_ahe-wfo1 | \nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n map <int, int> m;\n\n for (auto i : rectangles)\n | Pranav_Aher | NORMAL | 2021-07-29T04:21:54.120617+00:00 | 2021-07-29T04:21:54.120653+00:00 | 157 | false | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n map <int, int> m;\n\n for (auto i : rectangles)\n m[min(i[0], i[1])]++;\n\n return m.rbegin()->second;\n }\n};\n``` | 2 | 0 | ['C', 'C++'] | 1 |
number-of-rectangles-that-can-form-the-largest-square | JS O(N) solution | js-on-solution-by-makhmudgaly-pm0m | Please check out this solution\n\n/**\n * @param {number[][]} rectangles\n * @return {number}\n */\nvar countGoodRectangles = function(rectangles) {\n let ha | makhmudgaly | NORMAL | 2021-07-21T00:06:25.704117+00:00 | 2021-07-21T00:06:25.704150+00:00 | 89 | false | Please check out this solution\n```\n/**\n * @param {number[][]} rectangles\n * @return {number}\n */\nvar countGoodRectangles = function(rectangles) {\n let hash = {}\n let max = 0\n for(let [l,w] of rectangles) {\n let side = Math.min(l,w)\n max = Math.max(max, side)\n hash[side] = hash[side] + 1 || 1\n }\n \n return hash[max]\n};``` | 2 | 0 | ['JavaScript'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | C++ fast and Simple (You'll love it guaranteed.) | c-fast-and-simple-youll-love-it-guarante-zn2q | \nIf you have Learned something new please give it upvote;\notherwise thank you for having a look \uD83D\uDE4F\uD83C\uDFFB\n\nWe have to find out the minimum le | krishna_patil1 | NORMAL | 2021-06-22T10:52:50.866007+00:00 | 2021-06-22T10:52:50.866057+00:00 | 54 | false | \nIf you have Learned something new please give it upvote;\notherwise thank you for having a look \uD83D\uDE4F\uD83C\uDFFB\n\nWe have to find out the minimum length of rectangles.\nbcoz we have to cut out the remaining part of rectangle in order to make it a Square.\n\n```\nint countGoodRectangles(vector<vector<int>>& rectangles) {\n\tint cnt = 0; int mx = 0;\n\tfor(int i=0; i<rectangles.size(); i++){\n\t\tint mn = min(rectangles[i][0], rectangles[i][1]);\n\t\tif(mn > mx){\n\t\t\tcnt = 1;\n\t\t\tmx = mn;\n\t\t}\n\t\telse if(mn == mx){\n\t\t\tcnt++;\n\t\t}\n\t}\n\treturn cnt;\n}\n``` | 2 | 1 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Python Faster than 96.52%. Easy to understand | python-faster-than-9652-easy-to-understa-5576 | \nclass Solution(object):\n def countGoodRectangles(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: int``\n | user0791 | NORMAL | 2021-04-26T17:32:20.692287+00:00 | 2021-04-26T17:32:20.692334+00:00 | 90 | false | ```\nclass Solution(object):\n def countGoodRectangles(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: int``\n """\n lst = [min(item) for item in rectangles]\n return lst.count(max(lst))\n``` | 2 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Ruby: Map, max, count. | ruby-map-max-count-by-user9697n-y592 | Leetcode: 1725. Number Of Rectangles That Can Form The Largest Square.\n\nRuby: Map, max, count.\n\nMap array from array of pairs into array of pair minumums, f | user9697n | NORMAL | 2021-04-21T16:10:32.219431+00:00 | 2021-04-21T16:10:44.373625+00:00 | 37 | false | ## Leetcode: 1725. Number Of Rectangles That Can Form The Largest Square.\n\n**Ruby: Map, max, count.**\n\nMap array from array of pairs into array of pair minumums, find maximum value of this array, count number of maximum value matches. That\'s it.\n\n\nRuby code:\n```Ruby\n# Leetcode: 1725. Number Of Rectangles That Can Form The Largest Square.\n# https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/\n# Runtime: 68 ms, faster than 57.14% of Ruby online submissions for Number Of Rectangles That Can Form The Largest Square.\n# Memory Usage: 210.6 MB, less than 61.90% of Ruby online submissions for Number Of Rectangles That Can Form The Largest Square.\n# Thanks God!\n# @param {Integer[][]} rectangles\n# @return {Integer}\ndef count_good_rectangles(rectangles)\n squares = rectangles.map(&:min)\n max = squares.max\n squares.count(max)\nend\n```\n\n | 2 | 0 | ['Ruby'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java simple O(n) solution > 100% | java-simple-on-solution-100-by-titasdatt-f1v3 | \n\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int largestSide = Integer.MIN_VALUE;\n int countLargest = 0;\n | titasdatta93 | NORMAL | 2021-04-10T12:35:28.411570+00:00 | 2021-04-10T12:35:28.411614+00:00 | 75 | false | ```\n\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int largestSide = Integer.MIN_VALUE;\n int countLargest = 0;\n \n for(int[] rect: rectangles) {\n int localMinSide = Math.min(rect[0], rect[1]); //Find the largest square that can be created from this rect\n \n if(localMinSide > largestSide) {\n countLargest = 1; //Reset to 1 for the new largest side\n largestSide = localMinSide;\n } else if(localMinSide == largestSide) {\n countLargest++; //Or else keep incrementing\n }\n }\n \n return countLargest;\n }\n}\n\n```\n\n```TC: O(n) SC: O(1)``` | 2 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Python - Easy to understand | python-easy-to-understand-by-sbaisden25-bu64 | \n\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n \n #Runtime: 184 ms, faster than 65.71% of Python3 onli | sbaisden25 | NORMAL | 2021-04-07T16:09:49.134908+00:00 | 2021-04-07T17:17:33.606845+00:00 | 202 | false | ```\n\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n \n #Runtime: 184 ms, faster than 65.71% of Python3 online submissions\n #Memory Usage: 14.9 MB, less than 39.90% of Python3 online submissions\n \n res = []\n \n minSide = 0\n maxLen = 0\n \n for l, w in rectangles:\n \n minSide = min(l, w) #Gets minSide of each rectangle\n \n if minSide > maxLen: #Checks if rectangle holds new maxLen\n maxLen = minSide #Tracks the current maxLen \n \n res.append(minSide) #Holds each rectangles minSIde\n \n \n return res.count(maxLen)#Returns the count of rectangles whos minSide is equal to maxLen\n\t\t\n``` | 2 | 0 | ['Python'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java Accepted Runtime:O(n) and Space:O(n) | java-accepted-runtimeon-and-spaceon-by-m-c9mh | \nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n \n int[] sq=new int[rectangles.length];\n int max=Integer.MI | manasviiiii | NORMAL | 2021-03-18T07:55:06.464724+00:00 | 2021-03-18T07:55:06.464770+00:00 | 87 | false | ```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n \n int[] sq=new int[rectangles.length];\n int max=Integer.MIN_VALUE; //will stores maxLen\n for(int i=0;i<rectangles.length;i++)\n {\n sq[i]=Math.min(rectangles[i][0],rectangles[i][1]);\n if(max<sq[i])\n {\n max=sq[i];\n }\n }\n int count=0;\n\t\t//checking how many squares will have sides equal to maxLen\n for(int i=0;i<sq.length;i++)\n {\n if(max==sq[i])\n {\n count++;\n }\n }\n return count;\n \n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | [GO] ~20ms | 98% faster | go-20ms-98-faster-by-coty91-oiei | \nfunc countGoodRectangles(rectangles [][]int) int {\n ml := 0 \n tml := 0\n \n for _, v := range rectangles {\n l := 0\n if v[0] > v[ | coty91 | NORMAL | 2021-02-11T01:16:25.473784+00:00 | 2021-02-11T01:16:25.473828+00:00 | 100 | false | ```\nfunc countGoodRectangles(rectangles [][]int) int {\n ml := 0 \n tml := 0\n \n for _, v := range rectangles {\n l := 0\n if v[0] > v[1] {\n l = v[1]\n } else {\n l = v[0]\n }\n if l > ml {\n ml = l\n tml = 1\n } else if ml == l {\n tml += 1\n }\n }\n \n return tml\n}\n``` | 2 | 0 | ['Go'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Python One pass | python-one-pass-by-leovam-28hh | py\n\'\'\'\nw: hashmap + greedy\nh: count the frequency of each maxLen for each rectangle\n in the meanwhile, record the largest maxLex so far\n then at t | leovam | NORMAL | 2021-01-25T05:01:21.008396+00:00 | 2021-01-25T05:01:21.008455+00:00 | 196 | false | ```py\n\'\'\'\nw: hashmap + greedy\nh: count the frequency of each maxLen for each rectangle\n in the meanwhile, record the largest maxLex so far\n then at the end, just return the frequency of the largest maxLen\n\'\'\'\nimport collections\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n rec = collections.defaultdict(int)\n maxLen = 0\n \n for recta in rectangles:\n s = min(recta)\n rec[s] += 1\n maxLen = max(maxLen, s)\n \n return rec[maxLen]\n``` | 2 | 0 | ['Greedy', 'Python'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java O(N) 3ms with easy to understand | java-on-3ms-with-easy-to-understand-by-a-iefx | Very easy question. Just follow what is being said.\n\n\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int maxLen = 0;\n | ankitrathore | NORMAL | 2021-01-17T06:33:39.465432+00:00 | 2021-01-17T06:33:39.465458+00:00 | 150 | false | Very easy question. Just follow what is being said.\n\n```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int maxLen = 0;\n int count = 0;\n for(int i=0;i<rectangles.length;i++){\n int len = Math.min(rectangles[i][0], rectangles[i][1]);\n if(len > maxLen){\n maxLen = len;\n count = 1;\n }else if(len == maxLen){\n count++;\n }\n }\n return count;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | javascript three lines 124ms | javascript-three-lines-124ms-by-henryche-cf9o | \nconst countGoodRectangles = (rectangles) => {\n let res = rectangles.map(x => Math.min(x[0], x[1]));\n let max = Math.max.apply(Math, res);\n return | henrychen222 | NORMAL | 2021-01-17T04:32:14.255005+00:00 | 2021-01-17T04:32:14.255032+00:00 | 186 | false | ```\nconst countGoodRectangles = (rectangles) => {\n let res = rectangles.map(x => Math.min(x[0], x[1]));\n let max = Math.max.apply(Math, res);\n return res.filter(x => x == max).length;\n};\n``` | 2 | 0 | ['JavaScript'] | 1 |
number-of-rectangles-that-can-form-the-largest-square | Hashmap C++ | hashmap-c-by-ishankochar-3sed | \nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& r) {\n unordered_map<int,int>mp;\n for(int i=0;i<r.size();i++){\n | ishankochar | NORMAL | 2021-01-17T04:01:50.489094+00:00 | 2021-01-17T04:01:50.489124+00:00 | 225 | false | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& r) {\n unordered_map<int,int>mp;\n for(int i=0;i<r.size();i++){\n if(r[i][0]<r[i][1]){\n mp[r[i][0]]++;\n }else{\n mp[r[i][1]]++;\n }\n }\n int m=INT_MIN,c=0;\n for(auto i:mp){\n if(i.first>m){\n m=i.first;\n c=i.second;\n }\n }\n return c;\n }\n};\n``` | 2 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | ⭐️ [ Kt, Js, Py3, Cpp ] 🎯 Best == Max of Min(length, width) | kt-js-py3-cpp-best-max-of-minlength-widt-jcdg | \n Synopsis:\n\n1. Find the best square length as the maximum of the minimum of each rectangle\'s length l and width w\n2. Return the count of rectangles with | claytonjwong | NORMAL | 2021-01-17T04:01:43.704886+00:00 | 2021-01-17T04:05:17.058481+00:00 | 162 | false | \n **Synopsis:**\n\n1. Find the `best` square length as the maximum of the minimum of each rectangle\'s length `l` and width `w`\n2. Return the count of rectangles with length `l` and width `w` greater-than `best`\n\n---\n\n*Kotlin*\n```\nclass Solution {\n fun countGoodRectangles(A: Array<IntArray>): Int {\n var best = A.map{ (l, w) -> Math.min(l, w) }!!.max()!!\n return A.filter{ (l, w) -> best <= l && best <= w }.size\n }\n}\n```\n\n*Javascript*\n```\nlet countGoodRectangles = A => {\n let best = Math.max(...A.map(([l, w]) => Math.min(l, w)));\n return A.filter(([l, w]) => best <= l && best <= w).length;\n};\n```\n\n*Javascript: 1-liner... just for fun! :)*\n```\nlet countGoodRectangles = (A, best = Math.max(...A.map(([l, w]) => Math.min(l, w)))) => A.filter(([l, w]) => best <= l && best <= w).length;\n```\n\n\n*Python3*\n```\nclass Solution:\n def countGoodRectangles(self, A: List[List[int]]) -> int:\n best = max(min(l, w) for l, w in A)\n return len([l for l, w in A if best <= l and best <= w])\n```\n\n*C++*\n```\nclass Solution {\npublic:\n using VI = vector<int>;\n using VVI = vector<VI>;\n int countGoodRectangles(VVI& A, int best = 0) {\n for (auto& rect: A)\n best = max(best, min(rect[0], rect[1]));\n return count_if(A.begin(), A.end(), [=](auto& rect) {\n return best <= rect[0] && best <= rect[1];\n }); \n }\n};\n```\n | 2 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple solution with O(n) time complexity ... | simple-solution-with-on-time-complexity-hru5u | IntuitionWe should take the smallest side from each rectangle and maximum from these numbers. And if there are many this kind of numbers we should count them .. | Shohruh11 | NORMAL | 2025-03-05T15:59:08.158675+00:00 | 2025-03-05T16:15:42.843307+00:00 | 89 | false | # Intuition
We should take the smallest side from each rectangle and maximum from these numbers. And if there are many this kind of numbers we should count them ...
# Approach
In for loop I took minimum side of each rectangle and checked weather it's the max and if it's the second max I incremented my count ...
# Complexity
- Time complexity:
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
***C++*** solution :
# Code
```cpp []
class Solution {
public:
int countGoodRectangles(vector<vector<int>>& rectangles) {
int max = std::min(rectangles[0][0], rectangles[0][1]);
int count = 0;
for (int i = 0; i < rectangles.size(); i++) {
int currentMin = std::min(rectangles[i][0], rectangles[i][1]);
if (max < currentMin) {
max = currentMin;
count = 1;
} else if (max == currentMin) {
count++;
}
}
return count;
}
};
```
Here the ***Java*** solution :
# Code
```Java []
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int max = Math.min(rectangles[0][0],rectangles[0][1]);
int count = 0;
for(int i = 0; i<rectangles.length; i++){
if(max < Math.min(rectangles[i][0],rectangles[i][1])){
max = Math.min(rectangles[i][0],rectangles[i][1]);
count = 1;
}else if(max == Math.min(rectangles[i][0],rectangles[i][1])) count ++;
}
return count;
}
}
``` | 1 | 0 | ['C++', 'Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Best Solution | C++ | best-solution-c-by-nssvaishnavi-7h6w | IntuitionTo solve this problem, we need to determine the largest possible square side length (maxLen) that can be cut from any of the given rectangles and then | nssvaishnavi | NORMAL | 2025-02-06T18:17:20.084384+00:00 | 2025-02-06T18:17:20.084384+00:00 | 38 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To solve this problem, we need to determine the largest possible square side length (maxLen) that can be cut from any of the given rectangles and then count how many rectangles can form a square of that size.
# Approach
<!-- Describe your approach to solving the problem. -->
Find the Maximum Possible Square Side (maxLen)
The largest square that can be cut from a rectangle [li, wi] is min(li, wi).
Iterate through all rectangles and track the maximum min(li, wi), which gives maxLen.
Count Rectangles That Can Form maxLen
Iterate through the rectangles again and count how many of them have min(li, wi) == maxLen.
# 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
```cpp []
#include <vector>
#include <algorithm>
class Solution {
public:
int countGoodRectangles(std::vector<std::vector<int>>& rectangles) {
int maxLen = 0, count = 0;
for (const auto& rect : rectangles) {
int side = std::min(rect[0], rect[1]); // Maximum square side that can be cut
if (side > maxLen) {
maxLen = side;
count = 1; // Reset count since we found a new max
} else if (side == maxLen) {
count++; // Increment count if another rectangle can form the largest square
}
}
return count;
}
};
``` | 1 | 0 | ['C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Easy Solution in C | easy-solution-in-c-by-sathurnithy-jefs | Code | Sathurnithy | NORMAL | 2025-01-25T13:05:22.452087+00:00 | 2025-01-25T13:05:22.452087+00:00 | 27 | false | # Code
```c []
int countGoodRectangles(int** rectangles, int rectanglesSize, int* rectanglesColSize){
int max = INT_MIN;
int count = 0;
for(int i=0; i<rectanglesSize; i++){
int temp = (rectangles[i][0] > rectangles[i][1]) ? rectangles[i][1] : rectangles[i][0];
if(temp > max){
max = temp;
count = 0;
}
if(temp == max) count++;
}
return count;
}
``` | 1 | 0 | ['Array', 'C'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Solution in C | solution-in-c-by-vickyy234-e8dq | Code | vickyy234 | NORMAL | 2025-01-24T17:12:17.190645+00:00 | 2025-01-24T17:12:57.453649+00:00 | 19 | false | # Code
```c []
int countGoodRectangles(int** rectangles, int rectanglesSize, int* rectanglesColSize) {
int max = 0;
int* arr = malloc(sizeof(int) * rectanglesSize);
for (int i = 0; i < rectanglesSize; i++) {
if (rectangles[i][0] < rectangles[i][1])
arr[i] = rectangles[i][0];
else
arr[i] = rectangles[i][1];
if (max < arr[i])
max = arr[i];
}
int count = 0;
for (int i = 0; i < rectanglesSize; i++) {
if (max == arr[i]) count++;
}
free(arr);
return count;
}
```
```c []
int countGoodRectangles(int** rectangles, int rectanglesSize, int* rectanglesColSize) {
int max = 0;
for (int i = 0; i < rectanglesSize; i++) {
if (rectangles[i][0] > rectangles[i][1]) {
rectangles[i][0] = rectangles[i][1];
}
if (max < rectangles[i][0]) {
max = rectangles[i][0];
}
}
int count = 0;
for (int i = 0; i < rectanglesSize; i++) {
if (max == rectangles[i][0])
count++;
}
return count;
}
``` | 1 | 0 | ['Array', 'C', 'Counting'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Kotlin solution (beats 100% of users in time and space complexity) | kotlin-solution-beats-100-of-users-in-ti-02uy | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | Nihad04 | NORMAL | 2025-01-09T12:09:40.983664+00:00 | 2025-01-09T12:09:40.983664+00:00 | 11 | false | # Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```kotlin []
class Solution {
fun countGoodRectangles(rectangles: Array<IntArray>): Int {
val squareLengths = IntArray(rectangles.size)
var mx = 0
for (i in squareLengths.indices) {
val mn = rectangles[i].min()
squareLengths[i] = mn
if (mn > mx) mx = mn
}
return squareLengths.count {it == mx}
}
}
``` | 1 | 0 | ['Kotlin'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java | O(n) | Easy solution ✅ | java-on-easy-solution-by-vaibhav_parmar7-ox0j | Approach\n Easy Approach\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n\nclass Solution {\n public int countGoodRectangles | vaibhav_parmar72 | NORMAL | 2024-08-07T10:18:08.694625+00:00 | 2024-08-07T10:18:08.694653+00:00 | 13 | false | # Approach\n Easy Approach\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int[] temp = new int[rectangles.length];\n int max = Integer.MIN_VALUE;\n int res = 0;\n for(int i=0; i<rectangles.length; i++){\n temp[i] = Math.min(rectangles[i][0],rectangles[i][1]);\n }\n for(int i=0; i<temp.length; i++){\n max = Math.max(temp[i],max);\n }\n for(int i=0; i<temp.length; i++){\n if(temp[i]==max){\n res++;\n }\n }\n return res;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Python is Beautiful | python-is-beautiful-by-giorgizazadze-sth8 | Complexity\n- Time complexity: O(N)\n\n\n# Code\n\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n m = [min(n[0 | giorgizazadze | NORMAL | 2024-07-19T21:32:33.744394+00:00 | 2024-07-19T21:32:33.744439+00:00 | 64 | false | # Complexity\n- Time complexity: O(N)\n\n\n# Code\n```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n m = [min(n[0], n[1])**2 for n in rectangles]\n return m.count(max(m))\n``` | 1 | 0 | ['Python3'] | 1 |
number-of-rectangles-that-can-form-the-largest-square | Easy method in python | easy-method-in-python-by-arun_balakrishn-jufx | \n\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n ans = 0\n count = 0\n for i in rectangles:\n | arun_balakrishnan | NORMAL | 2024-02-10T17:17:58.734153+00:00 | 2024-02-10T17:17:58.734175+00:00 | 45 | false | \n```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n ans = 0\n count = 0\n for i in rectangles:\n temp = min(i)\n if temp > ans:\n ans = temp\n count = 1\n elif temp == ans:\n count += 1\n return count\n \n``` | 1 | 0 | ['Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Beats 94.06% of users with Python in python || 126 ms | beats-9406-of-users-with-python-in-pytho-cxfv | Code\n\nclass Solution(object):\n def countGoodRectangles(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: int\n | aditikprof | NORMAL | 2024-01-31T12:18:27.029226+00:00 | 2024-01-31T12:18:27.029254+00:00 | 9 | false | # Code\n```\nclass Solution(object):\n def countGoodRectangles(self, rectangles):\n """\n :type rectangles: List[List[int]]\n :rtype: int\n """\n max_length = max(min(rec) for rec in rectangles)\n count = sum(1 for rec in rectangles if min(rec) == max_length)\n return count\n``` | 1 | 0 | ['Python'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | ✅ C++ | Easy Code | Map | O(N) | c-easy-code-map-on-by-jagdish9903-obc8 | Complexity\n- Time complexity: O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(N)\n Add your space complexity here, e.g. O(n) \n\n# Co | Jagdish9903 | NORMAL | 2024-01-21T13:29:03.327254+00:00 | 2024-01-21T13:29:03.327280+00:00 | 7 | false | # Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int ans = 0;\n unordered_map<int,int> m;\n for(int i = 0; i < rectangles.size(); i++)\n {\n int tmp;\n ans = max(ans,tmp = min(rectangles[i][0], rectangles[i][1]));\n m[tmp]++;\n }\n return m[ans];\n }\n};\n``` | 1 | 0 | ['Array', 'Ordered Map', 'C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple java code 0 ms beats 100 % | simple-java-code-0-ms-beats-100-by-arobh-3uar | \n# Complexity\n- \n\n# Code\n\nclass Solution {\n int count=0;\n public void result(int[][] matrix,int i,int max){\n if(i==matrix.length){\n | Arobh | NORMAL | 2024-01-15T12:29:31.355608+00:00 | 2024-01-15T12:29:31.355642+00:00 | 8 | false | \n# Complexity\n- \n\n# Code\n```\nclass Solution {\n int count=0;\n public void result(int[][] matrix,int i,int max){\n if(i==matrix.length){\n return ;\n }\n int temp= Math.min(matrix[i][0],matrix[i][1]);\n if(temp>max){\n count=1;\n max=temp;\n }\n else if(max==temp){\n count++;\n }\n result(matrix,i+1,max); \n }\n public int countGoodRectangles(int[][] rectangles) {\n result(rectangles,0,Integer.MIN_VALUE);\n return count;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | c++(simple solution) | csimple-solution-by-barsha_511-d3b7 | \n# Approach\n Describe your approach to solving the problem. \n\nInitialization:\n\nInitialize a vector v to store the minimum side lengths of each rectangle.\ | Barsha_511 | NORMAL | 2024-01-04T11:44:46.107595+00:00 | 2024-01-04T11:44:46.107626+00:00 | 120 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nInitialization:\n\nInitialize a vector v to store the minimum side lengths of each rectangle.\nInitialize variables minv to keep track of the minimum side length for each rectangle, maxx to store the count of rectangles with the maximum side length, and an unordered map m to store the frequency of each minimum side length.\nIterating over Rectangles:\n\nIterate over each rectangle in the input vector rect.\nFor each rectangle, calculate the minimum side length (minv) by taking the minimum value between rect[i][0] and rect[i][1].\nPush the minv value into the vector v.\nSorting the Minimum Side Lengths:\n\nSort the vector v in descending order using std::sort with greater<int>() as the comparator.\nCounting Maximum Side Length Occurrences:\n\nInitialize maxx to 0 and s to the first element of the sorted vector v.\nIterate over the sorted vector v.\nIf the current element is equal to s, increment maxx.\nReturn Result:\n\nThe variable maxx now contains the count of rectangles with the maximum side length.\nReturn the value of maxx.\n\n# Complexity\n- Time complexity:O(N log N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rect) {\n vector<int>v;\n int minv=0;\n unordered_map<int,int>m;\n for(int i=0;i<rect.size();i++){\n minv=min(rect[i][0],rect[i][1]);\n v.push_back(minv);\n }\n sort(v.begin(),v.end(),greater<int>());\n int maxx=0;\n int s=v[0];\n for(int i=0;i<v.size();i++){\n if(s==v[i])maxx++;\n }\n return maxx;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
number-of-rectangles-that-can-form-the-largest-square | Easy solution in Python3 that beats 99.12% users | easy-solution-in-python3-that-beats-9912-2dfv | 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 | naikadesahil2840 | NORMAL | 2023-12-27T16:34:33.480536+00:00 | 2023-12-27T16:34:33.480570+00:00 | 24 | 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```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n squ = []\n cnt = 0\n for i in rectangles:\n squ.append(min(i))\n cnt+=1\n count = Counter(squ)\n return count[max(squ)]\n \n``` | 1 | 0 | ['Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple solution with HashMap in Python3 | simple-solution-with-hashmap-in-python3-jw1gx | Intuition\nHere we have:\n- list of rectangles with height and width\n- our goal is to find maximized count of rects we can cut, to have a perfect square \n\nAt | subscriber6436 | NORMAL | 2023-11-20T08:53:39.194854+00:00 | 2023-11-20T08:55:04.151665+00:00 | 379 | false | # Intuition\nHere we have:\n- list of `rectangles` with height and width\n- our goal is to find **maximized count** of rects **we can cut**, to have a **perfect square** \n\nAt each step we should somehow **cut** all of the rects to find a `maxLen`.\n\nFor this problem we\'re going to use **HashMap** to track **frequency** of **max possible length of size of a square**.\n\nDefinitely, we **can\'t** choose a maximum size of a **rect**, since we only **cut** (and not **extend**) sides.\n\nThus, at each step **we should consider the least length** of a particular rectangle.\n\n# Approach\n1. define `rects` to be a **HashMap**\n2. iterate over `rectangles` and find minimum sides for each rect, then increase a frequency\n3. define `ans` and `maxFreq` to iterate over all enumerated `rects` and update `ans`, if `maxFreq` at a particular step is **more**, than previous one.\n4. return `ans`\n\n# Complexity\n- Time complexity: **O(N + K)**, for twice-iterating over `rectangles` and `rects`\n\n- Space complexity: **O(K)**, since we store `rects`\n\n# Code\n```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n rects = defaultdict(int)\n\n for a, b in rectangles:\n rects[min(a, b)] += 1\n\n ans = 0\n maxFreq = 0\n\n for k, v in rects.items():\n if k > maxFreq:\n maxFreq = k\n ans = v\n\n return ans\n``` | 1 | 0 | ['Array', 'Hash Table', 'Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | One line python3 solution | one-line-python3-solution-by-fobos_hero-7lz4 | Approach\nFunction counts how many rectangles have both sides equal to the maximum of the minimum values (length and width) of each rectangle using the sum met | fobos_hero | NORMAL | 2023-09-25T13:24:32.000640+00:00 | 2023-09-25T13:24:32.000673+00:00 | 9 | false | # Approach\nFunction counts how many rectangles have both sides equal to the maximum of the minimum values (length and width) of each rectangle using the sum method of the rectangles list\n\n\n# Complexity\n- Time complexity: O(1)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n return sum(1 for i in rectangles if min(i)==max(min(i) for i in rectangles))\n``` | 1 | 0 | ['Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | easy understanding JAVA code | easy-understanding-java-code-by-maddy_mr-i6r3 | Intuition\n Describe your first thoughts on how to solve this problem. \nnew array of min integers, find max in that array with count;\n# Approach\n Describe yo | maddy_MR | NORMAL | 2023-09-17T12:05:09.359513+00:00 | 2023-09-17T12:05:09.359530+00:00 | 4 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nnew array of min integers, find max in that array with count;\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```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max[] =new int[rectangles.length];\n int count=0;//c=0\n for(int i=0; i<rectangles.length; i++){\n Arrays.sort(rectangles[i]);//5,8 3,9 3,12;\n max[i] = rectangles[i][0];//[5,3,3];\n }\n Arrays.sort(max);\n int maxi = max[max.length-1];\n for(int j=0; j<max.length; j++){\n if(max[j] == maxi){\n count ++;\n }\n }\n return count;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Beats 68.09%of users with Python😁 and Beats 77.66%of users with Python😁 6.8 and 7.7 balance | beats-6809of-users-with-python-and-beats-plmd | 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 | Hushigiro | NORMAL | 2023-08-29T07:23:37.426306+00:00 | 2023-08-29T07:23:37.426342+00:00 | 106 | 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```\nclass Solution(object):\n def countGoodRectangles(self, rectangles):\n new_list = []\n k = 0\n for i in rectangles:\n h = min(i[0], i[1])\n new_list.append(h)\n k = max(k, h)\n return sum(1 for i in new_list if i >=k)\n``` | 1 | 0 | ['Python'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | With basic of python code | with-basic-of-python-code-by-paulsoumyad-ge04 | 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 | paulsoumyadeep7 | NORMAL | 2023-08-18T04:34:04.853413+00:00 | 2023-08-18T04:34:04.853431+00:00 | 178 | 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```\nfrom collections import Counter\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n result=[]\n for i in rectangles:\n result.append(min(i))\n c=Counter(result)\n larg=max(c.keys())\n return c[larg]\n \n``` | 1 | 0 | ['Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | python3 highly efficient solution using a very different approach beats 98% | python3-highly-efficient-solution-using-twzzb | 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 | ResilientWarrior | NORMAL | 2023-08-13T07:48:31.589555+00:00 | 2023-08-13T07:48:31.589579+00:00 | 4 | 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```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n min_nums = []\n for rectangle in rectangles:\n if rectangle[0] < rectangle[1]:\n min_nums.append(rectangle[0])\n else:\n min_nums.append(rectangle[1])\n\n min_nums.sort()\n ans = min_nums.count(min_nums[-1])\n return ans\n\n``` | 1 | 0 | ['Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple Solution | simple-solution-by-suchitaupatil-kv89 | 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 | suchitaupatil | NORMAL | 2023-07-20T04:51:31.196200+00:00 | 2023-07-20T04:51:31.196218+00:00 | 531 | 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```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int count = 0;\n vector<int> ans;\n \n for(int i=0; i<rectangles.size(); i++){\n if(rectangles[i][0] > rectangles[i][1]){\n ans.push_back(rectangles[i][1]);\n }\n else{\n ans.push_back(rectangles[i][0]);\n }\n }\n\n int n = ans.size();\n int max = INT_MIN;\n for(int i=0 ; i<n; i++){\n if(ans[i] > max){\n max = ans[i];\n }\n }\n\n for(int i=0 ; i<n; i++){\n if(ans[i] == max){\n count++;\n }\n }\n\n return count;\n\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Number Of Rectangles That Can Form The Largest Square Solution in C++ | number-of-rectangles-that-can-form-the-l-4bn5 | 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 | The_Kunal_Singh | NORMAL | 2023-03-04T05:39:15.918579+00:00 | 2023-03-04T05:39:15.918630+00:00 | 20 | 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)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int i, j, min, m=0, count=0;\n for(i=0 ; i<rectangles.size() ; i++)\n {\n min = *min_element(rectangles[i].begin() , rectangles[i].end());\n if(min>m)\n {\n m = min;\n count = 0;\n }\n if(min==m)\n {\n count++;\n }\n }\n return count;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Kotlin solution | kotlin-solution-by-xemura-3uy2 | 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 | xemura | NORMAL | 2023-02-14T13:16:52.174580+00:00 | 2023-02-14T13:16:52.174627+00:00 | 160 | 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```\nclass Solution {\n fun countGoodRectangles(rectangles: Array<IntArray>): Int {\n var maxSquareSide : Int = 0\n val list = mutableListOf<Int>()\n var countSquare : Int = 0\n for (i in rectangles)\n {\n var minSide = minOf(i[0], i[1])\n list.add(minSide)\n if (minSide > maxSquareSide) maxSquareSide = minSide\n }\n\n list.forEach { element ->\n if (element == maxSquareSide) countSquare++\n }\n return countSquare\n }\n}\n``` | 1 | 0 | ['Kotlin'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | java || Beats 100% | java-beats-100-by-md_aziz_ali-f6a4 | \nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max = 0,len = 0;\n for(int i = 0;i<rectangles.length;i++){\n | Md_Aziz_Ali | NORMAL | 2023-01-27T12:34:10.131669+00:00 | 2023-01-27T12:34:10.131720+00:00 | 23 | false | ```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max = 0,len = 0;\n for(int i = 0;i<rectangles.length;i++){\n int side = Math.min(rectangles[i][0],rectangles[i][1]);\n if(side > len){\n len = side;\n max = 1;\n }\n else if(side == len)\n max++;\n }\n return max;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | easy to understand || 0ms || accepted solution | easy-to-understand-0ms-accepted-solution-296p | Intuition\nwe need to first find minima of each sub-array and then find maxima of all the minimas and then check how many times that maxima is occuring in the | Chetali1 | NORMAL | 2023-01-02T08:03:49.365962+00:00 | 2023-01-02T08:03:49.366003+00:00 | 20 | false | # Intuition\nwe need to first find minima of each sub-array and then find maxima of all the minimas and then check how many times that maxima is occuring in the array of minimas.\n# Approach\nwe first iterate through the array rectangles and find minima of each sub-array and store them in a new array mins. then find the maxima of mins and initate a variable count with 0. now iterate through the array mins and check how many of its elements are equal to max and increment count each time the element is equal to max and return count. \n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$ \n\n# Code\n```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int n = rectangles.length;\n int min = 0;\n int[] mins = new int[n];\n for(int i = 0; i<n; i++){\n min = rectangles[i][0];\n if(min > rectangles[i][1]){\n min = rectangles[i][1];\n }\n mins[i] = min;\n }\n int max = mins[0];\n for(int j = 0; j<n; j++){\n max = Math.max(max, mins[j]);\n }\n int count = 0;\n for(int k = 0; k<n; k++){\n if(mins[k]==max){\n count++;\n }\n }\n\n return count;\n }\n}\n``` | 1 | 0 | ['Array', 'Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | JAVA Simple Solution - 3ms beats 81% | java-simple-solution-3ms-beats-81-by-smr-vmth | \nclass Solution {\n public int countGoodRectangles(int[][] arr) {\n int[] sq = new int[arr.length];\n int m=arr.length;\n int n=arr[0]. | SmritiGangwar | NORMAL | 2022-12-01T19:34:56.990978+00:00 | 2022-12-01T19:36:43.941891+00:00 | 710 | false | ```\nclass Solution {\n public int countGoodRectangles(int[][] arr) {\n int[] sq = new int[arr.length];\n int m=arr.length;\n int n=arr[0].length;\n for(int i=0;i<m;i++){\n if(arr[i][0]<arr[i][1]){\n sq[i]=arr[i][0];\n }\n else{\n sq[i]=arr[i][1];\n }\n }\n int max = Largest(sq,m,0);\n int count =0;\n for(int i=0;i<m;i++){\n if(max==sq[i]){\n count+=1;\n }\n }\n return count;\n }\n\n public static int Largest(int [] sq, int m, int i){\n if(i==m-1){\n return sq[i];\n }\n int large=Largest(sq,m,i+1);\n return Math.max(large,sq[i]);\n }\n}\n``` | 1 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | JAVA Simple Solution - 3ms beats 81.4% | java-simple-solution-3ms-beats-814-by-it-qr18 | \n\n# Code\n\nclass Solution {\n public int countGoodRectangles(int[][] arr) {\n int[] sq = new int[arr.length];\n int m=arr.length;\n i | Its_Smriti | NORMAL | 2022-12-01T18:00:58.540502+00:00 | 2022-12-10T07:13:47.402144+00:00 | 1,124 | false | \n\n# Code\n```\nclass Solution {\n public int countGoodRectangles(int[][] arr) {\n int[] sq = new int[arr.length];\n int m=arr.length;\n int n=arr[0].length;\n for(int i=0;i<m;i++){\n if(arr[i][0]<arr[i][1]){\n sq[i]=arr[i][0];\n }\n else{\n sq[i]=arr[i][1];\n }\n \n }\n int max = Largest(sq,m,0);\n int count =0;\n for(int i=0;i<m;i++){\n if(max==sq[i]){\n count+=1;\n }\n }\n return count;\n }\n public static int Largest(int [] sq, int m, int i){\n if(i==m-1){\n return sq[i];\n }\n int large=Largest(sq,m,i+1);\n return Math.max(large,sq[i]);\n }\n}\n``` | 1 | 0 | ['Array', 'Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Python | easy solution | python-easy-solution-by-arshia_ilaty-j0ht | Please upvote if you like my solution. Let me know in the comments if you have any suggestions to increase performance or readability.\n\nclass Solution:\n d | arshia_ilaty | NORMAL | 2022-11-19T18:33:35.857830+00:00 | 2022-11-19T18:33:35.857879+00:00 | 58 | false | **Please upvote if you like my solution. Let me know in the comments if you have any suggestions to increase performance or readability.**\n```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n large = []\n t = 0\n for rec in rectangles:\n mi = min(rec[0], rec[1])\n large.append(mi)\n t = large.count(max(large))\n return t\n``` | 1 | 0 | ['Python'] | 1 |
number-of-rectangles-that-can-form-the-largest-square | easy solution || Python | easy-solution-python-by-maary-662g | \n\n# Code\n\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n squares=[min(i) for i in rectangles]\n ret | maary_ | NORMAL | 2022-10-16T01:02:53.001293+00:00 | 2022-10-16T01:02:53.001337+00:00 | 200 | false | \n\n# Code\n```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n squares=[min(i) for i in rectangles]\n return squares.count(max(squares))\n``` | 1 | 0 | ['Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java | O(n) | Easy solution ✅ | java-on-easy-solution-by-geethub17-4anr | \npublic int countGoodRectangles(int[][] rectangles) {\n\t\tint min, max = 0, previousMax=0, highesht = 0;\n\t\tfor (int j[] : rectangles) {\n\t\t\tmin = Math.m | geethub17 | NORMAL | 2022-10-08T17:22:39.894282+00:00 | 2022-10-08T17:26:39.272087+00:00 | 180 | false | ```\npublic int countGoodRectangles(int[][] rectangles) {\n\t\tint min, max = 0, previousMax=0, highesht = 0;\n\t\tfor (int j[] : rectangles) {\n\t\t\tmin = Math.min(j[0], j[1]); \n\t\t\tpreviousMax = max;\n\t\t\tmax = Math.max(max, min);\n\t\t\thighesht = (max == min && previousMax == max) ? highesht + 1 : (previousMax <max) ? highesht = 1 : highesht;\n\t\t}\n\t\treturn highesht;\n }\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | c++ | easy | step by step | c-easy-step-by-step-by-venomhighs7-gnze | \n# Code\n\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n unordered_map<int,int> map;\n for(vector<i | venomhighs7 | NORMAL | 2022-10-01T04:50:17.077821+00:00 | 2022-10-01T04:50:17.077867+00:00 | 775 | false | \n# Code\n```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n unordered_map<int,int> map;\n for(vector<int> rectangle: rectangles){\n map[min(rectangle[0],rectangle[1])]++;\n }\n int cnt=INT_MIN,maxlen=INT_MIN;\n for(auto m: map){\n if(m.first>maxlen) cnt=m.second,maxlen=m.first; \n }\n return cnt;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java easy very easy solution, single loop and just if else condition.. | java-easy-very-easy-solution-single-loop-rp1a | \nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max=0;\n int count=0;\n for(int i=0;i<rectangles.length;i++ | MukulAlmora | NORMAL | 2022-09-08T21:12:03.345869+00:00 | 2022-09-08T21:12:03.345963+00:00 | 555 | false | ```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max=0;\n int count=0;\n for(int i=0;i<rectangles.length;i++){\n int min=Math.min(rectangles[i][0],rectangles[i][1]); \n if(max<min){\n max=min;\n count=1;\n }\n else if(max==min){\n count++;\n }\n else{\n continue;\n } \n }\n return count;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | JAVA -1ms | java-1ms-by-rupa_02-xfc8 | class Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max=Integer.MIN_VALUE,c=0;\n for(int i=0;i<rectangles.length;i++) | rupa_02 | NORMAL | 2022-09-04T05:08:13.620330+00:00 | 2022-09-04T05:08:13.620377+00:00 | 27 | false | class Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int max=Integer.MIN_VALUE,c=0;\n for(int i=0;i<rectangles.length;i++){\n int nmax=Math.min(rectangles[i][0],rectangles[i][1]);\n if(max<nmax){\n max=nmax;\n c=1;\n } \n else if(max==nmax)\n c++; \n }\n return c;\n }\n}\n | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | ✅✅Easy C++ Solution || Simple to Understand | easy-c-solution-simple-to-understand-by-6qalt | \tclass Solution {\n\tpublic:\n\t\tint countGoodRectangles(vector>& rectangles) {\n\n\t\t\tint n = rectangles.size();\n\t\t\tvector v;\n\n\t\t\tfor(int i=0 ; i< | debav2 | NORMAL | 2022-08-11T20:05:09.786788+00:00 | 2022-08-11T20:05:22.447624+00:00 | 161 | false | \tclass Solution {\n\tpublic:\n\t\tint countGoodRectangles(vector<vector<int>>& rectangles) {\n\n\t\t\tint n = rectangles.size();\n\t\t\tvector<int> v;\n\n\t\t\tfor(int i=0 ; i<n ; i++){ \n\t\t\t\tv.push_back(min(rectangles[i][0], rectangles[i][1]));\n\t\t\t}\n\n\t\t\tint maxLen = *max_element(v.begin(), v.end());\n\n\t\t\treturn count(v.begin(), v.end(), maxLen);\n\t\t}\n\t};\nI hope that you\'ve found the solution useful.\nIn that case, please do upvote. Happy Coding :) | 1 | 0 | ['C', 'C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | [GO] Two Solutions Faster than 90%| | SPEED: 23 ms || MEMORY: 6.5 mb | | go-two-solutions-faster-than-90-speed-23-96lw | Hey,mate! This is my quick solution\n# \nFIRST \n\n\tfunc countGoodRectangles(rectangles [][]int) int {\n\t\tarray := make(map[int]int,0)\n\t\tfor ,rectangle := | blessingman | NORMAL | 2022-08-06T15:01:12.643312+00:00 | 2022-08-06T15:05:40.586038+00:00 | 90 | false | # Hey,mate! This is my quick solution\n# \nFIRST \n\n\tfunc countGoodRectangles(rectangles [][]int) int {\n\t\tarray := make(map[int]int,0)\n\t\tfor _,rectangle := range rectangles {\n\t\t\tk := rectangle[0]\n\t\t\tif k > rectangle[1]{\n\t\t\t k = rectangle[1]\n\t\t\t}\n\t\t\tarray[k]++\n\t\t}\n\t\tmax :=0 \n\t\tfor key, _ := range array {\n\t\t\tif max < key {\n\t\t\t\tmax = key\n\t\t\t}\n\t\t}\n\t\treturn array[max]\n\t}\n\t\nSECOND \n\n\t\tfunc countGoodRectangles(rectangles [][]int) int {\n\t\t\tsum,maxLen := 0,0\n\t\t\tfor _,rectangle := range rectangles {\n\t\t\t\tk := rectangle[0]\n\t\t\t\tif k > rectangle[1]{\n\t\t\t\t k = rectangle[1]\n\t\t\t\t}\n\t\t\t\tif maxLen < k {\n\t\t\t\t\tsum = 1 \n\t\t\t\t\tmaxLen = k\n\t\t\t\t} else if maxLen == k{\n\t\t\t\t\tsum++\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn sum\n\t\t}\n\t\n# if you like it ,please, push arrow to lift up solution to the top ,thanks! | 1 | 0 | ['Go'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Easy Optimise Solution | easy-optimise-solution-by-sanket_jadhav-n9p1 | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector>& rectangles) {\n int side=INT_MIN,ct=0;\n \n for(auto ele:rectangles){\ | Sanket_Jadhav | NORMAL | 2022-08-06T04:57:26.780054+00:00 | 2022-08-06T04:57:26.780102+00:00 | 78 | false | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int side=INT_MIN,ct=0;\n \n for(auto ele:rectangles){\n side=max(side,(min(ele[0],ele[1])));\n }\n \n for(auto ele:rectangles){\n if(min(ele[0],ele[1])==side)ct++;\n }\n return ct;\n }\n}; | 1 | 0 | ['Array', 'C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | C++ : Easy Solution | c-easy-solution-by-beast_paw-h5eh | \nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int ans=0,mn=0;\n for(int i=0;i<rectangles.size();i+ | beast_paw | NORMAL | 2022-07-21T09:31:44.950832+00:00 | 2022-07-21T09:31:44.950883+00:00 | 42 | false | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int ans=0,mn=0;\n for(int i=0;i<rectangles.size();i++)\n mn=max(mn,min(rectangles[i][0],rectangles[i][1]));\n for(int i=0;i<rectangles.size();i++)\n if(rectangles[i][0]>=mn&&rectangles[i][1]>=mn)\n ans++;\n return ans;\n }\n};\n``` | 1 | 0 | ['C'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Number of rectangles that can form the largest square - C solution | number-of-rectangles-that-can-form-the-l-i1g0 | \n\n\nint countGoodRectangles(int** rectangles, int rectanglesSize, int* rectanglesColSize){\n int i,j=0,maxlength=0,count=0;\n int * a = malloc( rectangl | himanshiiarora | NORMAL | 2022-07-19T09:05:41.461854+00:00 | 2022-07-19T09:06:36.357232+00:00 | 23 | false | ```\n\n\nint countGoodRectangles(int** rectangles, int rectanglesSize, int* rectanglesColSize){\n int i,j=0,maxlength=0,count=0;\n int * a = malloc( rectanglesSize * sizeof(int));\n for(i=0;i<rectanglesSize;i++){\n if(rectangles[i][j] <= rectangles[i][j+1]){\n maxlength = rectangles[i][j];\n }\n else{\n maxlength = rectangles[i][j+1];\n }\n a[i]=maxlength;\n }\n for(i=0; i<rectanglesSize ;i++){\n for(j=0 ;j<rectanglesSize; j++){\n if(a[i] < a[j]){\n break;\n }\n else{ //a[i]>=a[j]\n if(j==(rectanglesSize-1)){\n count++;\n }\n }\n }\n }\n return count;\n}\n\n``` | 1 | 0 | ['Array'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple Java Solution | Brute Force | simple-java-solution-brute-force-by-aman-tiue | \nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n\t\n int[] k = new int[rectangles.length];\n\t\t\n for(int i = 0; i | amankesarwani34 | NORMAL | 2022-07-19T08:21:59.005601+00:00 | 2022-07-19T08:22:50.662789+00:00 | 55 | false | ```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n\t\n int[] k = new int[rectangles.length];\n\t\t\n for(int i = 0; i < rectangles.length; i++){\n int minSideLength = rectangles[i][0];\n for(int j = 0; j < rectangles[i].length; j++){\t\n if(minSideLength >= rectangles[i][j]){\n minSideLength = rectangles[i][j];\n }\n }\n k[i] = minSideLength;\n }\n\t\t\n int max = k[0];\n for(int i = 0; i < k.length; i++){\n if(max <= k[i]){\n max = k[i];\n }\n }\n\t\t\n int count = 0;\n for(int i = 0; i < k.length; i++){\n if(max == k[i]){\n count++;\n }\n }\n\t\t\n return count;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Basic Approach (From Youtube) C++ | basic-approach-from-youtube-c-by-sagarke-8jvb | \nclass Solution {\npublic:\n \n \n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int maxlen=0,maxwid=0,count=0;\nfor(int i=0;i<rectangle | sagarkesharwnnni | NORMAL | 2022-07-18T19:45:55.995176+00:00 | 2022-07-18T19:45:55.995209+00:00 | 28 | false | ```\nclass Solution {\npublic:\n \n \n int countGoodRectangles(vector<vector<int>>& rectangles) {\n int maxlen=0,maxwid=0,count=0;\nfor(int i=0;i<rectangles.size();i++)\n{\nmaxwid= min(rectangles[i][0],rectangles[i][1]);\nif(maxwid>maxlen)\n{\nmaxlen=maxwid;\ncount=0;\n}\nif(maxlen==maxwid)\n{\ncount++;\n}\n}\nreturn count; }\n};\n``` | 1 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | ✅C++ || Simple && Straightforward || EASY | c-simple-straightforward-easy-by-abhinav-hv4o | \n\nn==r.size()\nT->O(n) && S->O(1)\n\n\tclass Solution {\n\tpublic:\n\t\tint countGoodRectangles(vector>& r) {\n\t\t\tint m=INT_MIN,count=0;\n\t\t\tfor(int i=0 | abhinav_0107 | NORMAL | 2022-07-08T19:34:21.149429+00:00 | 2022-07-08T19:34:21.149478+00:00 | 89 | false | \n\n**n==r.size()\nT->O(n) && S->O(1)**\n\n\tclass Solution {\n\tpublic:\n\t\tint countGoodRectangles(vector<vector<int>>& r) {\n\t\t\tint m=INT_MIN,count=0;\n\t\t\tfor(int i=0;i<r.size();i++) m=max(min(r[i][0],r[i][1]),m); \n\t\t\tfor(int i=0;i<r.size();i++){\n\t\t\t\tif(m==min(r[i][0],r[i][1]))count++;\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\t}; | 1 | 0 | ['C', 'C++'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | 📌Fastest & Easy Java☕ solution using hashmap | fastest-easy-java-solution-using-hashmap-to1d | ```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n HashMap hmap = new HashMap<>();\n int k=0;\n for(int rect | saurabh_173 | NORMAL | 2022-07-08T10:08:21.840999+00:00 | 2022-07-08T10:08:21.841036+00:00 | 52 | false | ```\nclass Solution {\n public int countGoodRectangles(int[][] rectangles) {\n HashMap<Integer,Integer> hmap = new HashMap<>();\n int k=0;\n for(int rectangle[]:rectangles)\n {\n int min = Math.min(rectangle[0],rectangle[1]);\n if(min>=k)\n {\n k = min;\n hmap.put(min,hmap.getOrDefault(min,0)+1);\n }\n }\n return hmap.get(k);\n }\n} | 1 | 0 | ['Java'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Simple C++ Solution TC: O(N) SC: O(1) | simple-c-solution-tc-on-sc-o1-by-lawliet-th7q | \nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n \n int max = 0, count = 0;\n \n //Loop | lawliet007 | NORMAL | 2022-06-18T18:18:22.000388+00:00 | 2022-06-18T18:18:29.450438+00:00 | 50 | false | ```\nclass Solution {\npublic:\n int countGoodRectangles(vector<vector<int>>& rectangles) {\n \n int max = 0, count = 0;\n \n //Loop through each of the rectangles\n for(vector side: rectangles) {\n //find the minimum size of rectangle\n int s = side[0] < side[1] ? side[0] : side[1];\n \n // if the current size is greater than the other sizes set the current size as max and \n // initialize the count to 0 \n if(s > max) {\n max = s;\n count = 1;\n } \n //if current size is equal to the max size increment the counter\n else if (s == max) {\n count++;\n }\n //ignore if the current size is lesser than the max size\n }\n return count;\n }\n};\n``` | 1 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Javascript solution :) | javascript-solution-by-milosdjurica-drjt | In first loop MaxLen is found.\nIn second loop counting number of MaxLen rectangles.\n\n\n\n/*\n * @param {number[][]} rectangles\n * @return {number}\n */\nvar | milosdjurica | NORMAL | 2022-06-16T23:24:48.225896+00:00 | 2022-06-16T23:24:48.225936+00:00 | 143 | false | In first loop MaxLen is found.\nIn second loop counting number of MaxLen rectangles.\n\n```\n\n/*\n * @param {number[][]} rectangles\n * @return {number}\n */\nvar countGoodRectangles = function (rectangles) {\n let max = 0\n for (let i = 0; i < rectangles.length; i++) {\n rectangles[i][0] < rectangles[i][1] ? rectangles[i][1] = rectangles[i][0] : rectangles[i][0] = rectangles[i][1]\n rectangles[i][0] > max ? max = rectangles[i][0] : max = max\n }\n\n let count = 0\n for (let i = 0; i < rectangles.length; i++) {\n rectangles[i][0] === max ? count++ : count = count\n }\n return count\n};\n``` | 1 | 0 | ['JavaScript'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Java Solution Without map O(1) space linear time | java-solution-without-map-o1-space-linea-a7tf | class Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int countRec=0;\n int maxLen=0;\n for(int i[]:rectangles){\n | draculatapes | NORMAL | 2022-06-03T16:39:44.203054+00:00 | 2022-06-03T16:41:10.471155+00:00 | 42 | false | class Solution {\n public int countGoodRectangles(int[][] rectangles) {\n int countRec=0;\n int maxLen=0;\n for(int i[]:rectangles){\n int length=i[0];\n int width=i[1];\n int currMaxLen=Math.min(length,width);\n if(currMaxLen>maxLen){\n maxLen=currMaxLen;\n countRec=1;\n }\n else if(currMaxLen==maxLen){++countRec;}\n }\n return countRec;\n }\n} | 1 | 0 | [] | 0 |
number-of-rectangles-that-can-form-the-largest-square | faster than 99.01%| Python Easy Solution | faster-than-9901-python-easy-solution-by-2znw | def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n maxlenL=[min(i) for i in rectangles]\n maxlen=max(maxlenL)\n return(ma | nidasaqib05 | NORMAL | 2022-05-19T18:51:53.797033+00:00 | 2022-05-19T18:51:53.797063+00:00 | 152 | false | def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n maxlenL=[min(i) for i in rectangles]\n maxlen=max(maxlenL)\n return(maxlenL.count(maxlen)) | 1 | 0 | ['Python', 'Python3'] | 0 |
number-of-rectangles-that-can-form-the-largest-square | Python3 | python3-by-excellentprogrammer-sua4 | \nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n s=[]\n for i in range(len(rectangles)):\n s | ExcellentProgrammer | NORMAL | 2022-05-13T12:52:44.773832+00:00 | 2022-05-13T12:52:44.773862+00:00 | 36 | false | ```\nclass Solution:\n def countGoodRectangles(self, rectangles: List[List[int]]) -> int:\n s=[]\n for i in range(len(rectangles)):\n s.append(min(rectangles[i]))\n return(s.count(max(s)))\n``` | 1 | 0 | [] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Sort and skip | sort-and-skip-by-votrubac-7tmd | C++\ncpp\nint minimumDifference(vector<int>& nums, int k) {\n int res = INT_MAX;\n sort(begin(nums), end(nums));\n for (int i = k - 1; i < nums.size(); | votrubac | NORMAL | 2021-08-29T04:46:54.185968+00:00 | 2021-08-29T04:46:54.186001+00:00 | 9,084 | false | **C++**\n```cpp\nint minimumDifference(vector<int>& nums, int k) {\n int res = INT_MAX;\n sort(begin(nums), end(nums));\n for (int i = k - 1; i < nums.size(); ++i)\n res = min(res, nums[i] - nums[i - k + 1]);\n return res;\n}\n``` | 67 | 4 | [] | 13 |
minimum-difference-between-highest-and-lowest-of-k-scores | Python simple solution | python-simple-solution-by-tovam-c2pm | Python :\n\n\ndef minimumDifference(self, nums: List[int], k: int) -> int:\n\tif len(nums) <= 1:\n\t\treturn 0\n\n\tnums = sorted(nums)\n\tres = nums[k-1] - num | TovAm | NORMAL | 2021-10-30T19:40:22.100198+00:00 | 2021-10-30T20:43:26.092467+00:00 | 6,557 | false | **Python :**\n\n```\ndef minimumDifference(self, nums: List[int], k: int) -> int:\n\tif len(nums) <= 1:\n\t\treturn 0\n\n\tnums = sorted(nums)\n\tres = nums[k-1] - nums[0]\n\n\tfor i in range(k, len(nums)):\n\t\tres = min(res, nums[i] - nums[i - k + 1])\n\n\treturn res\n```\n\n**Like it ? please upvote !** | 35 | 0 | ['Python', 'Python3'] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | C++ Simple and Clean 4-Line Explained Solution | c-simple-and-clean-4-line-explained-solu-khnl | Idea:\nSort the array.\nThen, we use a window of size k and calculate the difference between the first and last element in the window.\nres will be the minimum | yehudisk | NORMAL | 2021-08-29T12:25:06.753221+00:00 | 2021-08-29T12:26:33.288616+00:00 | 4,030 | false | **Idea:**\nSort the array.\nThen, we use a window of size k and calculate the difference between the first and last element in the window.\n`res` will be the minimum of these values.\n**Time Complexity:** O(nlogn)\n**Space Complexity:** O(1)\n```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int res = nums[k-1] - nums[0];\n for (int i = k; i < nums.size(); i++) res = min(res, nums[i] - nums[i-k+1]);\n return res;\n }\n};\n```\n**Like it? please upvote!** | 24 | 2 | ['C'] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | JAVA | Sorting | Sliding Window | java-sorting-sliding-window-by-sourin_br-7d9b | Please Upvote :D\n\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n if (k == 1) return 0;\n\n Arrays.sort(nums);\n\t\t\n | sourin_bruh | NORMAL | 2022-11-17T18:28:38.000979+00:00 | 2022-11-17T18:28:46.976937+00:00 | 3,440 | false | ### **Please Upvote** :D\n```\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n if (k == 1) return 0;\n\n Arrays.sort(nums);\n\t\t\n int i = 0, j = k - 1, \n min = Integer.MAX_VALUE;\n\n while (j < nums.length) {\n min = Math.min(min, nums[j++] - nums[i++]);\n }\n\n return min;\n }\n}\n\n// TC: O(n * logn) + O(n) => O(n * logn)\n// SC: O(1)\n``` | 19 | 0 | ['Sliding Window', 'Sorting', 'Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Very Fast || Explained line by line || Easy to understand | very-fast-explained-line-by-line-easy-to-otgs | Intuition\n Describe your first thoughts on how to solve this problem. \nOur intution is to calculate minimum possible differance between maxmimum and minimum o | anand_shukla1312 | NORMAL | 2024-08-26T14:08:34.798363+00:00 | 2024-08-26T14:17:40.785787+00:00 | 1,816 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOur intution is to calculate minimum possible differance between maxmimum and minimum of any K integer from given arry `nums`.\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- **Sorting:** If we sort the list than we can find min_difference by just find absulute difference between first and last element of subarray of k element.\n - **Step 1.** sort the list `nums`\n - **Step 2.** initiate a variabel `min_diff` which will always get update with new mini_difference.\n - **Step 3.** Initiate a for loop from `0` to size of `nums` - `k`.\n - **Step 4.** calulate absulute difference between current element and [current element position + k ]th element.\n - **Step 5.** keep update `min_diff` with new min(diff).\n - **Step 6.** come out of the loop and return the `min_diff` .\n- **Edge case :** if only one element in list than return 0.\n---\n\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n---\n\n\n# Code\n```python3 []\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n if k == 1:\n return 0\n nums.sort()\n min_diff = max(nums)\n for i in range(len(nums)-k+1):\n diff = abs(nums[i]- nums[i+k-1])\n min_diff = min(min_diff, diff)\n return min_diff\n\n \n \n```\n\n---\n\n\n | 14 | 0 | ['Sliding Window', 'Sorting', 'Iterator', 'Python3'] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | [Java] Sorting, O(NlogN) | java-sorting-onlogn-by-blackpinklisa-nm3i | We can sort the array and then compare every element at index i with the element at index i+k-1. Take the minimum difference of all such pairs.\n\nTime Complexi | blackpinklisa | NORMAL | 2021-08-29T04:44:53.331639+00:00 | 2021-08-29T04:44:53.331670+00:00 | 2,359 | false | We can sort the array and then compare every element at index `i` with the element at index `i+k-1`. Take the minimum difference of all such pairs.\n\nTime Complexity - O(NlogN)\nSpace Complexity - O(1)\n```\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n Arrays.sort(nums);\n int res = Integer.MAX_VALUE;\n for(int i=0; i<nums.length; i++){\n if(i+k-1 >= nums.length) break;\n res = Math.min(res, nums[i+k-1] - nums[i]);\n }\n return res;\n }\n}\n``` | 14 | 1 | [] | 4 |
minimum-difference-between-highest-and-lowest-of-k-scores | O(nlogn) || Sliding Window || 4 Line Solution || Explanation | onlogn-sliding-window-4-line-solution-ex-1yoj | The logic is that after sorting the array in ascending order it will be divided into n windows of k sizes\n\nEg :- [9,4,1,7]\nSort :- [1,4,7,9]\n\nNow the quest | aayushme | NORMAL | 2021-08-29T04:02:08.032198+00:00 | 2021-09-28T02:00:31.668084+00:00 | 3,036 | false | **The logic is that after sorting the array in ascending order it will be divided into n windows of k sizes**\n\nEg :- `[9,4,1,7]`\nSort :- `[1,4,7,9]`\n\nNow the question simply becomes find the minimum difference between lowest and highest element of window of size k **Windows :- `[1,4] [4,7] [7,9]`**\nAnswer = `min((4-1),(7-4),(9-7))`\n\n\n<iframe src="https://leetcode.com/playground/fYW2RmZg/shared" frameBorder="0" width="800" height="300"></iframe> | 14 | 1 | [] | 5 |
minimum-difference-between-highest-and-lowest-of-k-scores | Java solution using sliding window. | java-solution-using-sliding-window-by-sa-0s5q | Intuition\n Describe your first thoughts on how to solve this problem. \nIf we sort the array then the the min difference occurs at the neighbours\n\n# Approach | saivinaybathula22 | NORMAL | 2023-06-27T10:42:31.870740+00:00 | 2023-06-27T10:42:31.870764+00:00 | 2,403 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf we sort the array then the the min difference occurs at the neighbours\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe sork the array and create a sliding window of size k.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n * logn) --> Sorting\n O(n) --> Sliding\n O(n * logn) --> Total Time Complexity\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1) -- No extra memory\n\n# Code\n```\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n //This is a sliding window problem, The intution is we want the min possible difference\n // So if we sort the array then for the minimum we need to just check the neighbours so used sorting\n\n // Edge case\n if(k == 1){\n return 0;\n }\n\n Arrays.sort(nums);\n int min = Integer.MAX_VALUE;\n int i = 0;\n int j = k-1;\n while(j < nums.length){\n min = Math.min(min, nums[j] - nums[i]); //Finding the min\n i++;\n j++;\n }\n return min;\n }\n}\n``` | 11 | 0 | ['Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Python-3- sliding window | python-3-sliding-window-by-abuomar2-2fu8 | Slide the window and update the min. each time we increase the window, if needed!\n\n\ndef minimumDifference(self, nums: List[int], k: int) -> int:\n num | abuomar2 | NORMAL | 2021-08-31T05:04:40.336263+00:00 | 2021-08-31T05:05:51.684343+00:00 | 2,371 | false | Slide the window and update the min. each time we increase the window, if needed!\n\n```\ndef minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\n l, r = 0, k-1\n \n minDiff = float(\'inf\')\n \n while r < len(nums):\n minDiff = min(minDiff, nums[r] - nums[l])\n l, r = l+1, r+1\n \n return minDiff \n``` | 11 | 0 | ['Sliding Window', 'Sorting', 'Python', 'Python3'] | 3 |
minimum-difference-between-highest-and-lowest-of-k-scores | simple solution | simple-solution-by-aneesh-aj-bzap | 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 | Aneesh-aj | NORMAL | 2023-11-14T05:29:25.017420+00:00 | 2023-11-14T05:29:25.017444+00:00 | 857 | 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```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar minimumDifference = function(nums, k) {\n \n nums = nums.sort((a,b)=> a-b)\n\n let mini = 80000000000\n\n for(let i=0;i < nums.length- k+1;i++){\n mini = Math.min(mini,nums[i+k-1] - nums[i])\n }\n return mini\n};\n``` | 10 | 0 | ['JavaScript'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Easiest Solution | [C++] | Basic Approach | easiest-solution-c-basic-approach-by-ksh-3khd | Approach\n Describe your approach to solving the problem. \nIf we have to take difference of MAX and MIN Element in a pack of K elements, then why not sort it a | kshzz24 | NORMAL | 2023-02-25T14:36:11.725864+00:00 | 2023-02-25T14:36:11.725910+00:00 | 2,046 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nIf we have to take difference of MAX and MIN Element in a pack of K elements, then why not sort it and take 2 Pointers placed at 0th Position and (0+k-1)th Position respectively and take the difference of both and continue the same process until array is finished.\n \n\n# Code\n```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int i = 0;\n int j = k-1;\n sort(nums.begin(), nums.end());\n int n = nums.size();\n int ans =INT_MAX;\n while(j<n){\n ans = min(ans,nums[j++]-nums[i++]);\n }\n return ans;\n }\n};\n``` | 9 | 0 | ['C++'] | 1 |
minimum-difference-between-highest-and-lowest-of-k-scores | Python || 99.43% Faster || Sliding Window + Sorting | python-9943-faster-sliding-window-sortin-nsgf | \nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\n m,n=100001,len(nums)\n i,j=0,k-1\n | pulkit_uppal | NORMAL | 2022-11-30T21:57:25.014149+00:00 | 2022-11-30T22:08:08.653387+00:00 | 1,928 | false | ```\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\n m,n=100001,len(nums)\n i,j=0,k-1\n while j<n:\n m=min(m,nums[j]-nums[i])\n i+=1\n j+=1\n return m\n```\n\n**An upvote will be encouraging** | 9 | 0 | ['Sliding Window', 'Sorting', 'Python', 'Python3'] | 1 |
minimum-difference-between-highest-and-lowest-of-k-scores | Python || C++ || Just sort | python-c-just-sort-by-_helios-v14t | Python\n\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\n answer=9999999999\n for i in ra | _helios | NORMAL | 2021-08-29T04:35:18.223376+00:00 | 2021-08-29T04:35:18.223423+00:00 | 774 | false | Python\n```\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\n answer=9999999999\n for i in range(0,len(nums)-k+1):\n val=nums[i+k-1]-nums[i]\n answer=min(answer,val)\n return answer\n \n```\n\n\nC++\n```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int answer=INT_MAX;\n for(int i=0;i<nums.size()-k+1;i++){\n int val=nums[k+i-1]-nums[i];\n answer=min(answer,val);\n }\n return answer;\n }\n};\n``` | 7 | 1 | [] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | [Python3] 1-Liner | python3-1-liner-by-blackspinner-2nqw | \nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n return min(y - x for x, y in zip(sorted(nums)[:-k + 1], sorted(nums | blackspinner | NORMAL | 2021-08-30T06:19:18.394568+00:00 | 2021-08-30T06:19:18.394615+00:00 | 880 | false | ```\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n return min(y - x for x, y in zip(sorted(nums)[:-k + 1], sorted(nums)[k - 1:])) if k > 1 else 0\n``` | 6 | 0 | [] | 3 |
minimum-difference-between-highest-and-lowest-of-k-scores | ✔️ Easy C++ || 97% Faster || 70% Space || Sliding Window | easy-c-97-faster-70-space-sliding-window-zy3q | \nint minimumDifference(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int ans = 100000;\n for(int i=k-1;i<nums.size();++i)\n an | soorajks2002 | NORMAL | 2022-04-09T09:48:46.174415+00:00 | 2022-04-09T09:48:46.174444+00:00 | 1,477 | false | ```\nint minimumDifference(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int ans = 100000;\n for(int i=k-1;i<nums.size();++i)\n ans = min(ans, nums[i]-nums[i-k+1]);\n return ans;\n}\n``` | 5 | 1 | ['C', 'Sliding Window', 'Sorting', 'C++'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Java easy solution (sorting and sliding) | java-easy-solution-sorting-and-sliding-b-76bv | ```\n public int minimumDifference(int[] nums, int k) {\n Arrays.sort(nums);\n int minDiff = Integer.MAX_VALUE;\n for(int i=0;i=k-1) minDif | tusharkandwal99 | NORMAL | 2022-02-15T06:28:34.729699+00:00 | 2022-02-15T06:28:34.729726+00:00 | 2,012 | false | ```\n public int minimumDifference(int[] nums, int k) {\n Arrays.sort(nums);\n int minDiff = Integer.MAX_VALUE;\n for(int i=0;i<nums.length;i++){\n if(i>=k-1) minDiff = Math.min(minDiff,nums[i]-nums[i-(k-1)]);\n }\n return minDiff;\n } | 5 | 0 | ['Sliding Window', 'Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | [Python3] greedy 2-line | python3-greedy-2-line-by-ye15-nezu | Please see this commit for solutions of weekly 256.\n\n\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\ | ye15 | NORMAL | 2021-08-29T17:01:22.871761+00:00 | 2021-08-30T04:23:22.724912+00:00 | 371 | false | Please see this [commit](https://github.com/gaosanyong/leetcode/commit/7abfd85d1a68e375fcc0be60558909fd98b270f3) for solutions of weekly 256.\n\n```\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n nums.sort()\n return min(nums[i+k-1]-nums[i] for i in range(len(nums)-k+1))\n``` | 5 | 0 | ['Python3'] | 1 |
minimum-difference-between-highest-and-lowest-of-k-scores | C++ | EASY TO UNDERSTAND WITH EXPLAINATION | c-easy-to-understand-with-explaination-b-3mhr | Intuition\n Describe your first thoughts on how to solve this problem. \nSorting the array seems like a sensible approach to finding the minimum difference betw | nikhil_mane | NORMAL | 2024-03-06T23:26:56.232378+00:00 | 2024-03-06T23:26:56.232398+00:00 | 668 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSorting the array seems like a sensible approach to finding the minimum difference between elements in the array. With the sorted array, we can then iterate through sliding windows of size k and find the minimum difference within these windows.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort the input array nums.\nInitialize two pointers, left and right, to denote the start and end of the sliding window respectively. Set left = 0 and right = k - 1.\nInitialize a variable res to store the minimum difference, initially set to INT_MAX.\nIterate through the array using the sliding window approach:\nCalculate the difference between the element at index right and the element at index left.\nUpdate res with the minimum of its current value and the calculated difference.\nMove both left and right pointers to the right by one step.\nReturn the minimum difference res.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nSorting the array takes O(n log n) time.\nIterating through the array with the sliding window approach takes O(n) time.\nSo, the overall time complexity is O(n log n).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nSorting the array in-place takes O(1) space.\nThe additional space used is O(1).\nSo, the overall space complexity is O(1).\n\n# Code\n```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n \n\n sort(nums.begin(),nums.end());\n int left = 0;\n int right = k-1;\n int res = INT_MAX;\n\n while(right<nums.size()){\n res = min(res,nums[right] - nums[left]);\n left++;\n right++;\n }\n\n return res;\n\n }\n};\n\n\n\n``` | 4 | 0 | ['Python', 'C++', 'Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Simple Soltuion || Sliding Window || Sort || Javascript | simple-soltuion-sliding-window-sort-java-yekb | \n\n/*\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n /\nvar minimumDifference = function(nums, k) {\n nums.sort((a,b)=>a-b)\n le | cursedcoder7 | NORMAL | 2022-02-18T04:11:48.854602+00:00 | 2022-02-18T04:11:56.929464+00:00 | 1,014 | false | ```\n\n```/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar minimumDifference = function(nums, k) {\n nums.sort((a,b)=>a-b)\n let min=nums[0],max=nums[k-1],diff=max-min\n for(let i=k;i<nums.length;i++){\n diff=Math.min(diff,nums[i]-nums[i-k+1])\n }\n return diff\n};`` | 4 | 0 | ['Sliding Window', 'Sorting', 'JavaScript'] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | JavaScript - brute force with sliding window | javascript-brute-force-with-sliding-wind-e2rt | \nvar minimumDifference = function(nums, k) {\n nums.sort((a,b)=>a-b);\n if(k===1)\n {\n return 0;\n }\n var n = nums.length;\n var res | jialihan | NORMAL | 2021-08-29T04:55:02.461789+00:00 | 2021-08-29T04:55:02.461829+00:00 | 894 | false | ```\nvar minimumDifference = function(nums, k) {\n nums.sort((a,b)=>a-b);\n if(k===1)\n {\n return 0;\n }\n var n = nums.length;\n var res = Infinity;\n for(var i = 0; i<= n-k; i++)\n {\n res = Math.min(res, nums[i+k-1]-nums[i]);\n }\n return res;\n};\n``` | 4 | 0 | ['JavaScript'] | 1 |
minimum-difference-between-highest-and-lowest-of-k-scores | a few solutions | a-few-solutions-by-claytonjwong-1zfk | Sort the input array A, then perform a linear scan to find and return the minimal difference of each "k away" values i..i + k - 1 as the best answer.\n\n---\n\n | claytonjwong | NORMAL | 2021-08-29T04:00:47.666749+00:00 | 2021-08-29T04:22:58.647567+00:00 | 712 | false | Sort the input array `A`, then perform a linear scan to find and return the minimal difference of each "`k` away" values `i..i + k - 1` as the `best` answer.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n fun minimumDifference(A: IntArray, k: Int): Int {\n var best = (1e9 + 7).toInt()\n A.sort()\n for (i in 0..A.size - k)\n best = Math.min(best, A[i + k - 1] - A[i])\n return best\n }\n}\n```\n\n*Javascript*\n```\nlet minimumDifference = (A, k, best = Infinity) => {\n A.sort((a, b) => a - b);\n for (let i = 0; i + k - 1 < A.length; ++i)\n best = Math.min(best, A[i + k - 1] - A[i]);\n return best;\n};\n```\n\n*Python3*\n```\nclass Solution:\n def minimumDifference(self, A: List[int], K: int, best = float(\'inf\')) -> int:\n A.sort()\n for i in range(len(A) - K + 1):\n best = min(best, A[i + K - 1] - A[i])\n return best\n```\n\n*C++*\n```\nclass Solution {\npublic:\n using VI = vector<int>;\n int minimumDifference(VI& A, int K, int best = 1e9 + 7) {\n sort(A.begin(), A.end());\n for (auto i{ 0 }; i + K - 1 < A.size(); ++i)\n best = min(best, A[i + K - 1] - A[i]);\n return best;\n }\n};\n``` | 4 | 0 | [] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | Minimum difference || easy Sliding window approach || 100% beat | minimum-difference-easy-sliding-window-a-22st | Intuition
Sorting the array helps because once the array is sorted, the subarray with the smallest difference between its maximum and minimum values will be a c | rajpal1905 | NORMAL | 2025-01-20T13:18:21.488703+00:00 | 2025-01-20T13:18:21.488703+00:00 | 634 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- Sorting the array helps because once the array is sorted, the subarray with the smallest difference between its maximum and minimum values will be a contiguous block of k elements.
- By simply comparing the first and last elements of these sorted subarrays, we can efficiently find the minimum difference.
- The sliding window mechanism ensures that all possible subarrays of length k are considered without redundantly recalculating differences.
# Approach
<!-- Describe your approach to solving the problem. -->
**1. Sort the Array:** First, sort the input array nums to easily find subarrays where the difference between the maximum and minimum values is minimized.
**2 Iterate Over Subarrays:** Use a sliding window approach to examine every possible subarray of length k. For each subarray, compute the difference between its maximum and minimum (which are the last and first elements in the sorted subarray, respectively).
**3. Track Minimum Difference:** Keep track of the smallest difference encountered across all subarrays of length k.
**4. Return the Result:** Return the smallest difference found.
# Complexity
- Time complexity:O(nLogn)
<!-- 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 minimumDifference(vector<int>& nums, int k) {
if(k == 1){
return 0;
}
sort(nums.begin(),nums.end());
int minNum = INT_MAX;
int i = 0;
while(i<=nums.size()-k){
int diff = nums[i+k-1] - nums[i];
i++;
minNum = min(minNum , diff);
}
return minNum;
}
};
``` | 3 | 0 | ['C++'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | ✅✔️Short and Easy || C++ || Sorting || Sliding Window ✈️✈️✈️✈️✈️ | short-and-easy-c-sorting-sliding-window-nshgb | 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 | ajay_1134 | NORMAL | 2023-06-15T04:54:05.966644+00:00 | 2023-06-15T04:54:05.966669+00:00 | 641 | 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```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n = nums.size(), i = 0, ans = INT_MAX, winSize = 0;\n for(int j=0; j<n; j++){\n winSize++;\n if(winSize < k) continue;\n ans = min(ans,nums[j] - nums[i]);\n i++;\n }\n return ans;\n }\n};\n``` | 3 | 0 | ['Array', 'Sliding Window', 'Sorting', 'C++'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Java Easy 4 lines solution | java-easy-4-lines-solution-by-avadarshve-1hc4 | \n Add your space complexity here, e.g. O(n) \n\n# Code\n\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n \n Arrays.sor | avadarshverma737 | NORMAL | 2023-01-31T08:20:56.369336+00:00 | 2023-01-31T08:20:56.369365+00:00 | 1,008 | false | \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n \n Arrays.sort(nums);\n int diff = nums[k-1]-nums[0];\n for(int i=k;i<nums.length;i++){\n diff = Math.min(nums[i]-nums[i-k+1],diff);\n }\n return diff;\n }\n}\n``` | 3 | 0 | ['Sorting', 'Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | C++ Solution | c-solution-by-nagaraj08-lfka | \n# Code\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n\n int left | NAGARAJ08 | NORMAL | 2023-01-14T12:09:30.474101+00:00 | 2023-01-17T07:35:04.470618+00:00 | 1,069 | false | \n# Code\n```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n\n int left=0,right=k-1;\n\n int result =100000;\n\n while(right<nums.size())\n {\n result=min(result,nums[right]-nums[left]);\n \n left++;\n right++;\n\n }\n\n return result;\n }\n};\n``` | 3 | 0 | ['Sorting', 'C++'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Min Diff || Sliding Window || Two Pointer || Sorting | min-diff-sliding-window-two-pointer-sort-o3mz | Intuition\nSort it and then run a sliding window of size K.\n\n# Approach\n Describe your approach to solving the problem. \nSort the Array.\n\nSo now for every | 0sachin00 | NORMAL | 2022-10-22T07:08:12.864147+00:00 | 2022-10-22T07:08:12.864185+00:00 | 489 | false | # Intuition\nSort it and then run a sliding window of size K.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort the Array.\n\nSo now for every K number of elements(i.e. One window) the *max = last element* of that window and the *min = first element* of that window. Determine the difference.\n\nLikeways do it for all the window of size K and determine the differnece. \n\nInitialized the difference as the Maximum value of Integer so that I can get the desired minimum difference.\n\n```\ndiff = Math.min(diff, nums[r++] - nums[l++]);\n```\n`nums[r++] - nums[l++]` = difference between the max and min.\n\n\n\n# Complexity\n- Time complexity:\nO(NlogN)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n Arrays.sort(nums);\n int l = 0;\n int r = l + k - 1;;\n int diff = Integer.MAX_VALUE;\n while(r < nums.length){\n diff = Math.min(diff, nums[r++] - nums[l++]);\n }\n return diff;\n }\n}\n``` | 3 | 0 | ['Two Pointers', 'Sliding Window', 'Sorting', 'Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | C++ EASY SOLUTION :) | c-easy-solution-by-amitzzz-ikpz | \nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n int diff=INT_MAX;\n sort(nums.begin(),nums.end());\n | AMITZZZ | NORMAL | 2022-09-26T18:18:42.681353+00:00 | 2022-09-26T18:18:42.681395+00:00 | 807 | false | ```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) \n {\n int diff=INT_MAX;\n sort(nums.begin(),nums.end());\n \n int i=0,j=0;\n \n while(j<nums.size())\n {\n if(j-i+1<k)\n {\n j++;\n }\n \n if(j-i+1==k)\n {\n diff=min(ans,nums[j]-nums[i]);\n i++;\n j++;\n }\n }\n \n return diff; \n \n }\n};\n``` | 3 | 0 | ['C', 'Sliding Window', 'Sorting'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Java solution | nLogn & O(1) | 5ms & Faster than 81% | java-solution-nlogn-o1-5ms-faster-than-8-vohp | \nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n if(nums.length==1) return 0;\n Arrays.sort(nums);\n int n= k-1; | razoan | NORMAL | 2022-03-07T19:10:25.315189+00:00 | 2022-03-07T19:10:25.315236+00:00 | 295 | false | ```\nclass Solution {\n public int minimumDifference(int[] nums, int k) {\n if(nums.length==1) return 0;\n Arrays.sort(nums);\n int n= k-1;\n int minDiff = Math.abs(nums[0]-nums[n]);\n for(int i=1; i<nums.length-n; i++) {\n int min = Math.abs(nums[i]-nums[i+n]);\n minDiff = Math.min(minDiff, min); \n }\n return minDiff;\n }\n}\n``` | 3 | 0 | [] | 1 |
minimum-difference-between-highest-and-lowest-of-k-scores | Java Sorting and Sliding Window | java-sorting-and-sliding-window-by-joyes-dlq9 | \nclass Solution {\n public int minimumDifference(int[] A, int k) {\n \n Arrays.sort(A);\n \n int start=0;\n int end=k-1;\n | joyesh | NORMAL | 2022-03-03T09:25:07.832634+00:00 | 2022-03-03T09:25:07.832674+00:00 | 537 | false | ```\nclass Solution {\n public int minimumDifference(int[] A, int k) {\n \n Arrays.sort(A);\n \n int start=0;\n int end=k-1;\n int OvrMin=Integer.MAX_VALUE;\n \n int N=A.length;\n //after sorting in a window of size k the smallest no is at start and highest no is At end \n \n while(end<N)\n {\n int diff=A[end]-A[start];\n \n OvrMin=Math.min(OvrMin,diff);\n \n start++;\n end++;\n }//\n \n return OvrMin;\n \n }\n}\n``` | 3 | 0 | ['Sliding Window', 'Sorting', 'Java'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Rust solution | rust-solution-by-bigmih-yidq | \nimpl Solution {\n pub fn minimum_difference(mut nums: Vec<i32>, k: i32) -> i32 {\n nums.sort_unstable();\n nums.windows(k as usize)\n | BigMih | NORMAL | 2021-08-29T10:43:26.420961+00:00 | 2021-08-29T10:43:51.045346+00:00 | 143 | false | ```\nimpl Solution {\n pub fn minimum_difference(mut nums: Vec<i32>, k: i32) -> i32 {\n nums.sort_unstable();\n nums.windows(k as usize)\n .map(|pair| pair[(k - 1) as usize] - pair[0])\n .min()\n .unwrap()\n }\n}\n``` | 3 | 0 | ['Sliding Window', 'Rust'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Easy and Short 5 line of code With Explanation | easy-and-short-5-line-of-code-with-expla-n5c5 | Steps``\n1. First we sort the array in increasing order.\n2. Now we consider every window of size k .\n3. in each window we find difference between Max and Min | JNVJS11 | NORMAL | 2021-08-29T07:39:31.790349+00:00 | 2021-08-29T07:52:24.611373+00:00 | 205 | false | * **Steps**``\n1. First we sort the array in increasing order.\n2. Now we consider every window of size k .\n3. in each window we find difference between Max and Min element,\n4. we return our Minimum difference of all the vlaues in step 3.\n**Code**\n\'\'\'\n int minimumDifference(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n=nums.size(),res=INT_MAX;\n for(int i=0;i<=n-k;i++){\n res=min(res,nums[i+k-1]-nums[i]);\n \n }\n return res;\n \n \n }\n\'\'\'\nI hope you will get my solution. if you have any doubt please comment . \n\n | 3 | 2 | ['C', 'Sliding Window', 'Sorting', 'C++'] | 1 |
minimum-difference-between-highest-and-lowest-of-k-scores | 1984. Minimum Difference Between Highest and Lowest of K Scores | 1984-minimum-difference-between-highest-ttg04 | ---\n\nThis looks a bit difficult for an Easy problem\n\n---\n\nHope it is simple to understand.\n\n---\n\n\nvar minimumDifference = function (nums, k) {\n r | pgmreddy | NORMAL | 2021-08-29T04:30:51.924817+00:00 | 2021-08-29T04:30:51.924845+00:00 | 1,667 | false | ---\n\nThis looks a bit difficult for an Easy problem\n\n---\n\nHope it is simple to understand.\n\n---\n\n```\nvar minimumDifference = function (nums, k) {\n return nums.length === 1\n ? 0\n : nums\n .sort((a, b) => a - b)\n .reduce(\n (minDiff, _, i) =>\n i >= k - 1 //\n ? Math.min(minDiff, nums[i] - nums[i - k + 1])\n : Infinity,\n Infinity\n );\n};\n```\n\n---\n | 3 | 0 | ['JavaScript'] | 2 |
minimum-difference-between-highest-and-lowest-of-k-scores | C++ || sorting || easy solution | c-sorting-easy-solution-by-vineet_raosah-5fdq | \nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int diff=INT_MAX;\n sort(nums.begin(),nums.end());\n f | VineetKumar2023 | NORMAL | 2021-08-29T04:02:24.278507+00:00 | 2021-08-29T04:02:24.278544+00:00 | 175 | false | ```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int diff=INT_MAX;\n sort(nums.begin(),nums.end());\n for(int i=0;i<=nums.size()-k;i++)\n {\n diff=min(diff,nums[i+k-1]-nums[i]);\n }\n return diff;\n }\n};\n``` | 3 | 0 | [] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Easiest Solution || C++ || Beginner Approach | easiest-solution-c-beginner-approach-by-q8ndd | Beats 75% in Time Complexity\n\n\n\n# Code\n\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n=nums.size();\n | sanon2025 | NORMAL | 2024-03-23T11:51:45.585298+00:00 | 2024-03-23T11:51:45.585323+00:00 | 677 | false | # Beats 75% in Time Complexity\n\n\n\n# Code\n```\nclass Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n=nums.size();\n if(n==1){\n return 0;\n }\n\n int minDiff=INT_MAX;\n sort(nums.begin(), nums.end());\n for(int i=0;i<=n-k;i++){\n int min=nums[i+k-1]-nums[i];\n if(min<minDiff){\n minDiff=min;\n }\n }\n return minDiff;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Simple Solution | simple-solution-by-adwxith-w3a8 | 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 | adwxith | NORMAL | 2023-11-14T04:42:07.222983+00:00 | 2023-11-14T04:42:07.223016+00:00 | 210 | 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```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar minimumDifference = function(nums, k) {\n \n if(nums.length==1) return 0\n\n nums.sort((a,b)=>a-b)\n let start=0,end=k-1,diffrence=nums[nums.length-1];\n while(end<nums.length){\n diffrence=Math.min(nums[end]-nums[start],diffrence)\n start++\n end++\n }\n return diffrence\n};\n``` | 2 | 0 | ['JavaScript'] | 0 |
minimum-difference-between-highest-and-lowest-of-k-scores | Python3 Beats 99% sliding window of length K | python3-beats-99-sliding-window-of-lengt-ilty | 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 | gagemac | NORMAL | 2023-07-30T21:42:07.984374+00:00 | 2023-07-30T21:42:07.984392+00:00 | 781 | 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```\nclass Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n #sliding window\n nums.sort()\n l, r = 0, k-1\n res = float("inf")\n while r < len(nums):\n res = min(res, nums[r] - nums[l])\n r += 1\n l += 1\n return res\n\n\n\n``` | 2 | 0 | ['Python3'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.