|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/**Find the total area covered by two rectilinear rectangles in a 2D plane. |
| 3 | +/** |
| 4 | + * 223. Rectangle Area |
| 5 | + * |
| 6 | + * Find the total area covered by two rectilinear rectangles in a 2D plane. |
| 7 | + * Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. |
| 8 | + * Rectangle Area |
4 | 9 |
|
5 |
| - Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. |
| 10 | + Example: |
6 | 11 |
|
7 |
| - Rectangle Area |
8 |
| - Assume that the total area is never beyond the maximum possible value of int.*/ |
| 12 | + Input: A = -3, B = 0, C = 3, D = 4, E = 0, F = -1, G = 9, H = 2 |
| 13 | + Output: 45 |
| 14 | +
|
| 15 | + Note: Assume that the total area is never beyond the maximum possible value of int.*/ |
9 | 16 | public class _223 {
|
10 | 17 |
|
11 |
| - public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { |
12 |
| - int areaA = (C - A) * (D - B); |
13 |
| - int areaB = (G - E) * (H - F); |
| 18 | + public static class Solution1 { |
| 19 | + public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { |
| 20 | + int areaA = (C - A) * (D - B); |
| 21 | + int areaB = (G - E) * (H - F); |
14 | 22 |
|
15 |
| - int top = Math.min(D, H); |
16 |
| - int bottom = Math.max(B, F); |
17 |
| - int left = Math.max(A, E); |
18 |
| - int right = Math.min(C, G); |
| 23 | + int top = Math.min(D, H); |
| 24 | + int bottom = Math.max(B, F); |
| 25 | + int left = Math.max(A, E); |
| 26 | + int right = Math.min(C, G); |
19 | 27 |
|
20 |
| - int overlap = 0; |
21 |
| - if (top > bottom && right > left) { |
22 |
| - overlap = (top - bottom) * (right - left); |
| 28 | + int overlap = 0; |
| 29 | + if (top > bottom && right > left) { |
| 30 | + overlap = (top - bottom) * (right - left); |
| 31 | + } |
| 32 | + return areaA + areaB - overlap; |
23 | 33 | }
|
24 |
| - return areaA + areaB - overlap; |
25 | 34 | }
|
26 |
| - |
27 | 35 | }
|
0 commit comments