LeetCode 2728: Count Houses in a Circular Street Solution
Master LeetCode problem 2728 (Count Houses in a Circular Street), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2728. Count Houses in a Circular Street
Problem Explanation
Explanation:
To solve this problem, we can use the concept of finding the greatest common divisor (GCD) of two numbers. Given the number of houses n in a circular street, we need to count the number of houses that are not adjacent to each other.
The total number of ways to choose two non-adjacent houses is calculated as n - GCD(n, 2), where GCD(n, 2) represents the greatest common divisor of n and 2.
:
Solution Code
class Solution {
public int countHouses(int n) {
return n - gcd(n, 2);
}
private int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2728 (Count Houses in a Circular Street)?
This page provides optimized solutions for LeetCode problem 2728 (Count Houses in a Circular Street) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2728 (Count Houses in a Circular Street)?
The time complexity for LeetCode 2728 (Count Houses in a Circular Street) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2728 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2728 in Java, C++, or Python.