JAVA || C++ || O(n) || FASTER THAN 100% || WITH MAP || WITHOUT MAP
C++\n\n1.using map\n\nRuntime: 36 ms, faster than 100.00% of C++ online submissions \nMemory Usage: 20.2 MB, less than 100.00% of C++ online submissions\n\n\ncl
**C++**\n\n**1.using map**\n\n**Runtime: 36 ms, faster than 100.00% of C++ online submissions \nMemory Usage: 20.2 MB, less than 100.00% of C++ online submissions**\n\n```\nclass Solution {\npublic:\n    int countGoodRectangles(vector<vector<int>>& rectangles) {\n        unordered_map<int,int> map;\n        for(vector<int> rectangle: rectangles){\n\t\t    //storing number of time a particular square can obtain\n            map[min(rectangle[0],rectangle[1])]++; //[4,6], you can cut it to get a square with a side length of at most  4.\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; //finding out the maxlength square then storing its count in cnt\n        }\n        return cnt;\n    }\n};\n```\n**2.Without map**\n\n**Runtime: 48 ms, faster than 100.00% of C++ online submissions \nMemory Usage: 20.2 MB, less than 100.00% of C++ online submissions**\n\n```\nclass Solution {\npublic:\n    int countGoodRectangles(vector<vector<int>>& rectangles) {\n         int cnt = 0, max = 0;\n        for (vector<int>rectangle: rectangles) {\n\t\t//get the minimum from width and height\n            int side = min(rectangle[0], rectangle[1]); //[4,6], you can cut it to get a square with a side length of at most  4.\n            if (side > max) {  //comparing max side with new side \n                cnt = 1; // intialise count with 1\n                max = side; //update the max by new side \n            }else if (side == max) { // square with maxlength \n                cnt++; //just increase the count of square with max len\n            }\n        }\n        return cnt;\n    }\n};\n```\n\n**JAVA**\n\n**1.Without map**\n\n**Runtime: 1 ms, faster than 100.00% of Java online submissions\nMemory Usage: 39.1 MB, less than 100.00% of Java online submissions**\n\n```\nclass Solution {\n    public int countGoodRectangles(int[][] rectangles) {\n        int cnt = 0, max = 0;\n        for (int []rectangle: rectangles) {\n\t    //\tget the minimum from width and height\n            int side = Math.min(rectangle[0], rectangle[1]); //[4,6], you can cut it to get a square with a side length of at most  4.\n            if (side > max) {  //comparing max side with new side \n                cnt = 1; // intialise count with 1\n                max = side; //update the max by new side \n            }else if (side == max) { // square with maxlength \n                cnt++; //just increase the count of square with max len\n            }\n        }\n        return cnt;\n    }\n}\n```\n\n**2.using map**\n\n**Runtime: 5 ms, faster than 80.00% of Java online submissions \nMemory Usage: 39.7 MB, less than 20.00% of Java online submissions** \n\n```\nclass Solution {\n    public int countGoodRectangles(int[][] rectangles) {\n        HashMap<Integer, Integer> map = new HashMap<>();\n        int max = 0, ans = 0;\n        for (int[] rectangle : rectangles) {\n            int min = Math.min(rectangle[0], rectangle[1]); //get the minimum from width and height\n            map.put(min, map.getOrDefault(min, 0) + 1);\n            ans = (min >= max) ? map.get(min) : ans;\n            max = Math.max(max, min);\n        }\n        return ans;\n    }\n}\n```\n**Feel free to ask any question in the comment section.**\nI hope that you\'ve found the solution useful.\nIn that case, **please do upvote and encourage me** to on my quest to document all leetcode problems\uD83D\uDE03\nHappy Coding :)\n
['C', 'C++', 'Java']