亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] 849. Maximize Distance to Closest Perso

JerryC / 3369人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.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...

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

    摘要:遞歸法復(fù)雜度時(shí)間空間思路根據(jù)二叉樹(shù)的性質(zhì),我們知道當(dāng)遍歷到某個(gè)根節(jié)點(diǎn)時(shí),最近的那個(gè)節(jié)點(diǎn)要么是在子樹(shù)里面,要么就是根節(jié)點(diǎn)本身。因?yàn)槲覀冎离x目標(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 評(píng)論0 收藏0
  • Leetcode PHP題解--D29 973. K Closest Points to Origi

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

    Sanchi 評(píng)論0 收藏0
  • LeetCode[270] Closest Binary Search Tree Value

    摘要:復(fù)雜度思路用一個(gè)變量來(lái)記錄當(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 評(píng)論0 收藏0
  • leetcode16 3Sum Closest

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

    Blackjun 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<