classSolution { public: intmaxDistToClosest(vector<int>& seats){ int res = 0; int l = 0; while (l < seats.size() && seats[l] == 0) { ++l; } res = max(res, l); while (l < seats.size()) { int r = l + 1; while (r < seats.size() && seats[r] == 0) { ++r; } if (r == seats.size()) { res = max(res, r - l - 1); } else { res = max(res, (r - l) / 2); } l = r; } return res; } };
publicclassSolution { publicintMaxDistToClosest(int[] seats) { int res = 0; int l = 0; while (l < seats.Length && seats[l] == 0) { ++l; } res = Math.Max(res, l); while (l < seats.Length) { int r = l + 1; while (r < seats.Length && seats[r] == 0) { ++r; } if (r == seats.Length) { res = Math.Max(res, r - l - 1); } else { res = Math.Max(res, (r - l) / 2); } l = r; } return res; } }
intmaxDistToClosest(int* seats, int seatsSize) { int res = 0, l = 0; while (l < seatsSize && seats[l] == 0) { ++l; } res = fmax(res, l); while (l < seatsSize) { int r = l + 1; while (r < seatsSize && seats[r] == 0) { ++r; } if (r == seatsSize) { res = fmax(res, r - l - 1); } else { res = fmax(res, (r - l) / 2); } l = r; } return res; }
[sol1-Python]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defmaxDistToClosest(self, seats: List[int]) -> int: res, l = 0, 0 while l < len(seats) and seats[l] == 0: l += 1 res = max(res, l) while l < len(seats): r = l + 1 while r < len(seats) and seats[r] == 0: r += 1 if r == len(seats): res = max(res, r - l - 1) else: res = max(res, (r - l) // 2) l = r return res
funcmaxDistToClosest(seats []int)int { res := 0 l := 0 for l < len(seats) && seats[l] == 0 { l++ } res = max(res, l) for l < len(seats) { r := l + 1 for r < len(seats) && seats[r] == 0 { r++ } if r == len(seats) { res = max(res, r - l - 1) } else { res = max(res, (r - l) / 2) } l = r } return res }
funcmax(a, b int)int { if a > b { return a } return b }
var maxDistToClosest = function(seats) { let res = 0, l = 0; while (l < seats.length && seats[l] === 0) { l++; } res = Math.max(res, l); while (l < seats.length) { let r = l + 1; while (r < seats.length && seats[r] === 0) { r++; } if (r === seats.length) { res = Math.max(res, r - l - 1); } else { res = Math.max(res, parseInt((r - l) / 2)); } l = r; } return res; };