国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 849. Maximize Distance to Closest Perso

JerryC / 3328人閱讀

Problem

In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to closest person.

Example 1:

Input: [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.
Example 2:

Input: [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
Note:

1 <= seats.length <= 20000
seats contains only 0s or 1s, at least one 0, and at least one 1.

Solution
class Solution {
    public int maxDistToClosest(int[] seats) {
        int max = 0, left = -1, len = seats.length;
        for (int i = 0; i < len; i++) {
            if (seats[i] == 1) {
                if (left == -1) max = Math.max(max, i);
                else max = Math.max(max, (i-left)/2);
                left = i;
            }
        }
        if (seats[len-1] == 0) max = Math.max(max, len-1-left);
        return max;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/71956.html

相關文章

  • [LintCode] K Closest Points

    Problem Given some points and a point origin in two dimensional space, find k points out of the some points which are nearest to origin.Return these points sorted by distance, if they are same with di...

    沈儉 評論0 收藏0
  • [Leetcode] Closest Binary Search Tree Value 最近二叉搜索

    摘要:遞歸法復雜度時間空間思路根據二叉樹的性質,我們知道當遍歷到某個根節點時,最近的那個節點要么是在子樹里面,要么就是根節點本身。因為我們知道離目標數最接近的數肯定在二叉搜索的路徑上。 Closest Binary Search Tree Value I Given a non-empty binary search tree and a target value, find the va...

    AlphaWallet 評論0 收藏0
  • Leetcode PHP題解--D29 973. K Closest Points to Origi

    摘要:題目鏈接題目分析給一個坐標數組,從中返回個離最近的坐標。其中,用歐幾里得距離計算。思路把距離作為數組的鍵,把對應坐標作為數組的值。用函數排序,再用函數獲取前個即可。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 973. K Closest Points to Origin 題目鏈接 973. K Closest Points to Origin 題目分析 給一個坐標數組points...

    Sanchi 評論0 收藏0
  • LeetCode[270] Closest Binary Search Tree Value

    摘要:復雜度思路用一個變量來記錄當前的值,并且在每次之前,比較得到目前的最大值。注意變量的比較不要用代碼 LeetCode[270] Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the value in the BST that is close...

    pumpkin9 評論0 收藏0
  • leetcode16 3Sum Closest

    摘要:返回這三個值的和。思路一三指針這里的思路和是一樣的,就是用三個指針獲得三個值并計算他們的和。 題外話 鑒于這一題的核心思路和leetcode15的思路相同,可以先寫一下15題并參考一下我之前的一篇博客 題目要求 Given an array S of n integers, find three integers in S such that the sum is closest to...

    Blackjun 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<