Maximizing Square Hole Area in Grid

Day 97/100 Problem Statement : You are given the two integers, n and m and two integer arrays, hBars and vBars. The grid has n + 2 horizontal and m + 2 vertical bars, creating 1 x 1 unit cells. The bars are indexed starting from 1. You can remove some of the bars in hBars from horizontal bars and some of the bars in vBars from vertical bars. Note that other bars are fixed and cannot be removed. Return an integer denoting the maximum area of a square-shaped hole in the grid, after removing some bars (possibly none). Input: n = 2, m = 1, hBars = [2,3], vBars = [2] Output: 4 Solution : https://lnkd.in/gJ4FkbK9 public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) { Arrays.sort(hBars); Arrays.sort(vBars); int s = Math.min(maxSpan(hBars), maxSpan(vBars)); return s * s; } private int maxSpan(int[] bars) { int res = 1, streak = 1; for (int i = 1; i < bars.length; i++) { if (bars[i] - bars[i - 1] == 1) streak++; else streak = 1; res = Math.max(res, streak); } return ++res; } #100DaysDSA #100DaysOfCode #Java #Leetcode #Neetcode #Neetcode250 #TUF

To view or add a comment, sign in

Explore content categories