Skip to content

Commit 6e575f6

Browse files
🧑‍🤝‍🧑 Day 29
1 parent a31b564 commit 6e575f6

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
## Week 5 🚧
4949

50-
Coming Soon...
50+
1. [Maximize Distance to Closest Person](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/563/week-5-october-29th-october-31st/3512/) ➡️ [CPP Solution](Week5/maxDistToClosest.cpp)
5151

5252
## Other Challenges 💪
5353

Week5/maxDistToClosest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int maxDistToClosest(vector<int>& seats) {
4+
vector<int> onesPos;
5+
int n = seats.size();
6+
int maxDistance = 0;
7+
8+
for(int i = 0; i < n; ++i) {
9+
if(seats[i] == 1) onesPos.push_back(i);
10+
}
11+
12+
int m = onesPos.size();
13+
14+
if(onesPos[0] != 0)
15+
maxDistance = max(maxDistance, onesPos[0]);
16+
17+
for(int i = 0; i < m - 1; ++i)
18+
maxDistance = max(maxDistance, (onesPos[i + 1] - onesPos[i]) / 2);
19+
20+
if(onesPos[m - 1] != n - 1)
21+
maxDistance = max(maxDistance, n - 1 - onesPos[m - 1]);
22+
23+
return maxDistance;
24+
}
25+
};

0 commit comments

Comments
 (0)