Problem
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Example:
Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2
Output: 45
Note:
Assume that the total area is never beyond the maximum possible value of int.
Solutionclass Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int a1 = (C-A)*(D-B); int a2 = (G-E)*(H-F); int left = Math.max(A,E); int right = Math.min(C,G); int top = Math.min(D,H); int bottom = Math.max(B,F); if (left < right && bottom < top) return a1+a2-(right-left)*(top-bottom); else return a1+a2; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/73014.html
摘要:思路一暴力循環(huán)如果我們將矩陣中的每個子矩陣都枚舉出來,并計(jì)算其元素和,從而得出小于的最大值即可。 題目要求 Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. E...
摘要:數(shù)學(xué)法復(fù)雜度時間空間思路基本的數(shù)學(xué)題,考察的是我們能否全面的考慮到所有可能。如果兩個矩形沒有重疊部分,則直接計(jì)算兩個矩形面積之和就行了。因?yàn)閮蓚€矩形的坐標(biāo)都可能比對方小,所以我們一共有四種可能情況是不重疊的。 Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. ...
Problem For a web developer, it is very important to know how to design a web pages size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose l...
Problem Given n non-negative integers representing the histograms bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. showImg(https://segmentfault.com/img...
摘要:以此類推,如果一直到棧為空時,說明剛出來的豎條之前的所有豎條都比它自己高,不然不可能棧為空,那我們以左邊全部的寬度作為長方形的寬度。 Largest Rectangle in Histogram Given n non-negative integers representing the histograms bar height where the width of each bar...
閱讀 2234·2021-11-17 09:33
閱讀 2774·2021-11-12 10:36
閱讀 3396·2021-09-27 13:47
閱讀 884·2021-09-22 15:10
閱讀 3485·2021-09-09 11:51
閱讀 1392·2021-08-25 09:38
閱讀 2757·2019-08-30 15:55
閱讀 2608·2019-08-30 15:53