Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7cf419e

Browse files
refactor 223
1 parent 8c47566 commit 7cf419e

File tree

1 file changed

+24
-16
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+24
-16
lines changed
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
package com.fishercoder.solutions;
22

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
49
5-
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
10+
Example:
611
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.*/
916
public class _223 {
1017

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);
1422

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);
1927

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;
2333
}
24-
return areaA + areaB - overlap;
2534
}
26-
2735
}

0 commit comments

Comments
 (0)