From 3b34f9a82a28e1540616237ffadfe4d612d0e786 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Thu, 17 Nov 2022 16:56:24 +0500 Subject: [PATCH] Problem 223: Rectangle area --- problems/223-rectangle-area/Makefile | 14 +++++++++++ problems/223-rectangle-area/lib.c | 35 +++++++++++++++++++++++++++ problems/223-rectangle-area/lib.h | 2 ++ problems/223-rectangle-area/main.c | 12 +++++++++ problems/223-rectangle-area/readme.md | 14 +++++++++++ 5 files changed, 77 insertions(+) create mode 100644 problems/223-rectangle-area/Makefile create mode 100644 problems/223-rectangle-area/lib.c create mode 100644 problems/223-rectangle-area/lib.h create mode 100644 problems/223-rectangle-area/main.c create mode 100644 problems/223-rectangle-area/readme.md diff --git a/problems/223-rectangle-area/Makefile b/problems/223-rectangle-area/Makefile new file mode 100644 index 0000000..ab746c4 --- /dev/null +++ b/problems/223-rectangle-area/Makefile @@ -0,0 +1,14 @@ +all: lib.o main.o + gcc -Wall -o bin lib.o main.o + +lib.o: lib.c + gcc -c -Wall lib.c + +main.o: main.c + gcc -c -Wall main.c + +run: + ./bin + +clean: + rm *.o bin diff --git a/problems/223-rectangle-area/lib.c b/problems/223-rectangle-area/lib.c new file mode 100644 index 0000000..caeee90 --- /dev/null +++ b/problems/223-rectangle-area/lib.c @@ -0,0 +1,35 @@ +#include "lib.h" + +int _findIntersection(int a_start, int a_end, int b_start, int b_end) { + // aaaa + // bbbb + if (a_end > b_start && a_start <= b_start && a_end <= b_end) + return a_end - b_start; + // aa + // bbbbbb + if (a_start >= b_start && a_end <= b_end) + return a_end - a_start; + // aaaa + // bbbb + if (a_start >= b_start && a_start < b_end && a_end >= b_end) + return b_end - a_start; + // aaaaa + // bb + if (b_start >= a_start && b_end <= a_end) + return b_end - b_start; + return 0; +} + +int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) { + // Area of the first rectangle + int a = (ax2 - ax1) * (ay2 - ay1); + + // Area of the second rectangle + int b = (bx2 - bx1) * (by2 - by1); + + // Find intersection + int x_intersection = _findIntersection(ax1, ax2, bx1, bx2); + int y_intersection = _findIntersection(ay1, ay2, by1, by2); + + return a + b - x_intersection * y_intersection; +} diff --git a/problems/223-rectangle-area/lib.h b/problems/223-rectangle-area/lib.h new file mode 100644 index 0000000..55cb74f --- /dev/null +++ b/problems/223-rectangle-area/lib.h @@ -0,0 +1,2 @@ +#pragma once +int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2); diff --git a/problems/223-rectangle-area/main.c b/problems/223-rectangle-area/main.c new file mode 100644 index 0000000..6cb5335 --- /dev/null +++ b/problems/223-rectangle-area/main.c @@ -0,0 +1,12 @@ +#include +#include "lib.h" + +int main() { + int res1 = computeArea(-3, 0, 3, 4, 0, -1, 9, 2); + printf("Test 1: %d\n", res1 == 45); + + int res2 = computeArea(-2, -2, 2, 2, -2, -2, 2, 2); + printf("Test 2: %d\n", res2 == 16); + + return 0; +} diff --git a/problems/223-rectangle-area/readme.md b/problems/223-rectangle-area/readme.md new file mode 100644 index 0000000..1ab62c2 --- /dev/null +++ b/problems/223-rectangle-area/readme.md @@ -0,0 +1,14 @@ +# 223. Rectangle area + +[Leetcode](https://leetcode.com/problems/rectangle-area/) + +Given the coordinates of two rectilinear rectangles in a 2D plane, +return the total area covered by the two rectangles. + +The first rectangle is defined by its +bottom-left corner (ax1, ay1) and its +top-right corner (ax2, ay2). + +The second rectangle is defined by its +bottom-left corner (bx1, by1) and its +top-right corner (bx2, by2).