Skip to content

Commit e0aeec3

Browse files
authored
Merge pull request ByteByteGoHq#52 from ongshunping/cpp-solutions-math-and-geometry
Add C++ solutions for Chapter 19 (Math and Geometry)
2 parents b7edba6 + b838f30 commit e0aeec3

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed

cpp/Math and Geometry/josephus.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int josephus(int n, int k) {
2+
// Base case: If there's only one person, the last person is person 0.
3+
if (n == 1) {
4+
return 0;
5+
}
6+
// Calculate the position of the last person remaining in the reduced problem
7+
// with 'n - 1' people. We use modulo 'n' to ensure the answer doesn't exceed
8+
// 'n - 1'.
9+
return (josephus(n - 1, k) + k) % n;
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int josephusOptimized(int n, int k) {
2+
int res = 0;
3+
for (int i = 2; i <= n; i++) {
4+
// res[i] = (res[i - 1] + k) % i;
5+
res = (res + k) % i;
6+
}
7+
return res;
8+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <vector>
2+
#include <map>
3+
#include <utility>
4+
#include <algorithm>
5+
#include <numeric>
6+
7+
int maximumCollinearPoints(std::vector<std::vector<int>>& points) {
8+
int res = 0;
9+
// Treat each point as a focal point, and determine the maximum
10+
// number of points that are collinear with each focal point. The
11+
// largest of these maximums is the answer.
12+
for (int i = 0; i < points.size(); i++) {
13+
res = std::max(res, maxPointsFromFocalPoint(i, points));
14+
}
15+
return res;
16+
}
17+
18+
int maxPointsFromFocalPoint(int focalPointIndex, std::vector<std::vector<int>>& points) {
19+
std::map<std::pair<int, int>, int> slopesMap;
20+
int maxPoints = 0;
21+
// For the current focal point, calculate the slope between it and
22+
// every other point. This allows us to group points that share the
23+
// same slope.
24+
for (int j = 0; j < points.size(); j++) {
25+
if (j != focalPointIndex) {
26+
std::pair<int, int> currSlope = getSlope(points[focalPointIndex], points[j]);
27+
slopesMap[currSlope]++;
28+
// Update the maximum count of collinear points for the
29+
// current focal point.
30+
maxPoints = std::max(maxPoints, slopesMap[currSlope]);
31+
}
32+
}
33+
// Add 1 to the maximum count to include the focal point itself.
34+
return maxPoints + 1;
35+
}
36+
37+
std::pair<int, int> getSlope(std::vector<int>& p1, std::vector<int>& p2) {
38+
int rise = p2[1] - p1[1];
39+
int run = p2[0] - p1[0];
40+
// Handle vertical lines separately to avoid dividing by 0.
41+
if (run == 0) {
42+
return {1, 0};
43+
}
44+
// Simplify the slope to its reduced form.
45+
int gcdVal = std::gcd(rise, run);
46+
return {rise / gcdVal, run / gcdVal};
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <climits>
2+
3+
int reverse32BitInteger(int n) {
4+
int reversedN = 0;
5+
// Keep looping until we've added all digits of 'n' to 'reversedN'
6+
// in reverse order.
7+
while (n != 0) {
8+
// Extract the last digit of 'n'.
9+
int digit = n % 10;
10+
// Remove the last digit from 'n'.
11+
n /= 10;
12+
// Check for integer overflow or underflow.
13+
if (reversedN > INT_MAX / 10 || reversedN < INT_MIN / 10) {
14+
return 0;
15+
}
16+
// Add the current digit to 'reversedN'.
17+
reversedN = reversedN * 10 + digit;
18+
}
19+
return reversedN;
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <vector>
2+
3+
std::vector<int> spiralMatrix(std::vector<std::vector<int>>& matrix) {
4+
if (matrix.empty()) {
5+
return {};
6+
}
7+
std::vector<int> result;
8+
// Initialize the matrix boundaries.
9+
int top = 0, bottom = matrix.size() - 1;
10+
int left = 0, right = matrix[0].size() - 1;
11+
// Traverse the matrix in spiral order.
12+
while (top <= bottom && left <= right) {
13+
// Move from left to right along the top boundary.
14+
for (int i = left; i <= right; i++) {
15+
result.push_back(matrix[top][i]);
16+
}
17+
top++;
18+
// Move from top to bottom along the right boundary.
19+
for (int i = top; i <= bottom; i++) {
20+
result.push_back(matrix[i][right]);
21+
}
22+
right--;
23+
// Check that the bottom boundary hasn't passed the top boundary
24+
// before moving from right to left along the bottom boundary.
25+
if (top <= bottom) {
26+
for (int i = right; i >= left; i--) {
27+
result.push_back(matrix[bottom][i]);
28+
}
29+
bottom--;
30+
}
31+
// Check that the left boundary hasn't passed the right boundary
32+
// before moving from bottom to top along the left boundary.
33+
if (left <= right) {
34+
for (int i = bottom; i >= top; i--) {
35+
result.push_back(matrix[i][left]);
36+
}
37+
left++;
38+
}
39+
}
40+
return result;
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int triangleNumbers(int n) {
2+
// If n is an odd-numbered row, the first even number always starts at position
3+
// 2.
4+
if (n % 2 != 0) {
5+
return 2;
6+
}
7+
// If n is a multiple of 4, the first even number always starts at position 3.
8+
else if (n % 4 == 0) {
9+
return 3;
10+
}
11+
// For all other rows, the first even number always starts at position 4.
12+
return 4;
13+
}

0 commit comments

Comments
 (0)