-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution849.cs
39 lines (33 loc) · 973 Bytes
/
Solution849.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution849
{
/// <summary>
/// 849. Maximize Distance to Closest Person - Medium
/// <a href="https://leetcode.com/problems/shifting-letters">See the problem</a>
/// </summary>
public int MaxDistToClosest(int[] seats)
{
var n = seats.Length;
var left = new int[n];
var right = new int[n];
var maxDistance = 0;
for (var i = 0; i < n; i++)
{
left[i] = seats[i] == 1 ? 0 : (i == 0 ? n : left[i - 1] + 1);
}
for (var i = n - 1; i >= 0; i--)
{
right[i] = seats[i] == 1 ? 0 : (i == n - 1 ? n : right[i + 1] + 1);
}
for (var i = 0; i < n; i++)
{
if (seats[i] == 0)
{
maxDistance = Math.Max(maxDistance, Math.Min(left[i], right[i]));
}
}
return maxDistance;
}
}