成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 849. Maximize Distance to Closest Perso

JerryC / 3350人閱讀

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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71956.html

相關(guān)文章

  • [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 最近二叉搜索

    摘要:遞歸法復(fù)雜度時間空間思路根據(jù)二叉樹的性質(zhì),我們知道當(dāng)遍歷到某個根節(jié)點時,最近的那個節(jié)點要么是在子樹里面,要么就是根節(jié)點本身。因為我們知道離目標(biāo)數(shù)最接近的數(shù)肯定在二叉搜索的路徑上。 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

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

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

    摘要:復(fù)雜度思路用一個變量來記錄當(dāng)前的值,并且在每次之前,比較得到目前的最大值。注意變量的比較不要用代碼 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

發(fā)表評論

0條評論

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