The trick here is to name the state correctly, then let the implementation follow. For 1401. Circle and Rectangle Overlapping, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are inCircle, inSquare, checkOverlap.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Circle and Rectangle Overlapping.
02//Memory Usage: 6.1 MB, less than 100.00% of C++ online submissions for Circle and Rectangle Overlapping.
03class Solution {
04public:
05 bool inCircle(double radius, double x_center, double y_center, double x, double y){
06 return (x-x_center)*(x-x_center) + (y-y_center)*(y-y_center) <= radius*radius;
07 };
08
09 bool inSquare(int x1, int y1, int x2, int y2, int x, int y){
10 if(x >= x1 && x <= x2 && y >= y1 && y <= y2) return true;
11 return false;
12 };
13
14 bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
15 double delta = 1.0;
16 for(int x = x1, y = y1; x <= x2; x += delta){
17 if(inCircle(radius, x_center, y_center, x, y)){
18 return true;
19 }
20 }
21 for(int x = x1, y = y2; x <= x2; x += delta){
22 if(inCircle(radius, x_center, y_center, x, y)){
23 return true;
24 }
25 }
26 for(int x = x1, y = y1; y <= y2; y += delta){
27 if(inCircle(radius, x_center, y_center, x, y)){
28 return true;
29 }
30 }
31 for(int x = x2, y = y1; y <= y2; y += delta){
32 if(inCircle(radius, x_center, y_center, x, y)){
33 return true;
34 }
35 }
36
37 if(inSquare(x1, y1, x2, y2, x_center, y_center)) return true;
38 if(inSquare(x1, y1, x2, y2, x_center-radius, y_center)) return true;
39 if(inSquare(x1, y1, x2, y2, x_center+radius, y_center)) return true;
40 if(inSquare(x1, y1, x2, y2, x_center, y_center-radius)) return true;
41 if(inSquare(x1, y1, x2, y2, x_center, y_center+radius)) return true;
42
43 return false;
44 }
45};
46
47//Find point in rectangle closest to circle first
48//https://leetcode.com/problems/circle-and-rectangle-overlapping/discuss/563441/JAVA-compare-distance-between-radius-and-closest-point-on-rectangle-to-circle
49//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Circle and Rectangle Overlapping.
50//Memory Usage: 6 MB, less than 100.00% of C++ online submissions for Circle and Rectangle Overlapping.
51class Solution {
52public:
53 bool checkOverlap(int radius, int x_center, int y_center, int x1, int y1, int x2, int y2) {
54 int x_closest = (x_center < x1) ? x1 : (x_center > x2) ? x2 : x_center;
55 int y_closest = (y_center < y1) ? y1 : (y_center > y2) ? y2 : y_center;
56
57 int dist_squared = (x_center-x_closest)*(x_center-x_closest) +
58 (y_center-y_closest)*(y_center-y_closest);
59
60 return dist_squared <= radius * radius;
61 }
62};
Cost