Sort by

recency

|

615 Discussions

|

  • + 0 comments

    def largestRectangle3(h):

    area = float("-inf")
    numOfBuildings = 0
    
    minHeight = float("inf")
    
    for i in range(len(h)):
        numOfBuildings += 1
        minHeight = min(minHeight, h[i])
    
        for j in range(i-1, -1,-1):
            if  h[j] >= minHeight:
                numOfBuildings += 1
            else:
                break     
    
        for j in range(i+1, len(h)):    
            if  h[j] >= minHeight:
                numOfBuildings += 1
            else:
                break
    
        area = max(area, (numOfBuildings * minHeight)) 
        numOfBuildings = 0
        minHeight = float("inf")       
    return area
    
  • + 0 comments

    Brute force solution without stacks that passes some tests but only fails with time limit exceeded. So, I'd say logic is correct. This is O(N!) algorithm, it generates all the possible arrangements of size 1, size 2, .... size N. Absolutely not optimal but it uncovers the neccessary thought pattern to solve optimally.

    def largestRectangle(h): maxRectangleArea = float("-inf")

    size = 1
    while size <= len(h):
        for i in range(len(h)):
            minHeight = float("inf")
    
            if  i + size > len(h):
                break 
    
            for j in range(i,i+size):
                minHeight = min(minHeight, h[j])
            maxRectangleArea = max(maxRectangleArea, (minHeight * size))
        size += 1
    return maxRectangleArea
    
  • + 0 comments

    took me 4 hours to do this, the logic is to find how much range that the number [i] can spread. spread is spread to max [index left] and to max [index right] starting from [i]. the valid range is when the number is higher than [i] number. i dont know if this acceptable since i dont use stack at all.

    public static long largestRectangle(List<Integer> h) {
    		long result = Long.MIN_VALUE;
            
            for (int i = 0; i <= h.size() - 1; i++) {
                int left = i;
                int right = i;
                boolean goleft = true;
                boolean goright = true;
                
                while (goleft || goright) {
                    int tempLeft = left - 1;
                    if (tempLeft >= 0 && h.get(tempLeft) >= h.get(i)) {
                        left = tempLeft;
                    } else {
                        goleft = false;
                    }
                    
                    int tempRight = right + 1;
                    if (tempRight <= h.size() - 1 && h.get(tempRight) >= h.get(i)) {
                        right = tempRight;
                    } else {
                        goright = false;
                    }
                }
                
                long tempResult = h.get(i) * (right - left + 1);
                if (result < tempResult) {
                    result = tempResult;
                }
            }
        
            return result;
    }
    
  • + 0 comments
    def largestRectangle(h):
    	def checkR(number, index, h):
    			counter = 0
    			for element in range((index +1), len(h)):
    					if number <= h[element]:
    							counter+=1
    					else:
    							break
    			return counter
    
    	def checkL(number, index, h):
    			counter = 0
    			for element in reversed(range(index)):
    					if number <= h[element]:
    							counter+=1
    					else:
    							break
    			return counter
    
  • + 0 comments

    def largestRectangle(h):

    def checkR(number, index, h):
        counter = 0
        for element in range((index +1), len(h)):
            if number <= h[element]:
                counter+=1
            else:
                break
        return counter
    
    def checkL(number, index, h):
        counter = 0
        for element in reversed(range(index)):
            if number <= h[element]:
                counter+=1
            else:
                break
        return counter
    
    maxArea = 0
    currentArea = 0
    # Write your code here
    for i in range(len(h)):
    
        if (i==0):
            R = checkR(h[i], i, h)
            currentArea = (R+1)* h[i]
    
        elif (i==(len(h)-1)):
            L=checkL(h[i], i, h)
            currentArea = (L+1)*h[i]
    
        else:
            L = checkL(h[i], i, h)
            R = checkR(h[i], i, h)
            currentArea = (L+R+1)*h[i]
    
        maxArea = max(currentArea, maxArea)
    
    return maxArea