最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 6929. 數(shù)組的最大美麗值

2023-07-16 12:43 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?array?nums?and a?non-negative?integer?k.

In one operation, you can do the following:

  • Choose an index?i?that?hasn't been chosen before?from the range?[0, nums.length - 1].

  • Replace?nums[i]?with any integer from the range?[nums[i] - k, nums[i] + k].

The?beauty?of the array is the length of the longest subsequence consisting of equal elements.

Return?the?maximum?possible beauty of the array?nums?after applying the operation any number of times.

Note?that you can apply the operation to each index?only once.

A?subsequence?of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.

?

Example 1:

Input: nums = [4,6,1,2], k = 2

Output: 3

Explanation: In this example, we apply the following operations: - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2]. - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4]. After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3). It can be proven that 3 is the maximum possible length we can achieve.

Example 2:

Input: nums = [1,1,1,1], k = 10

Output: 4

Explanation: In this example we don't have to apply any operations. The beauty of the array nums is 4 (whole array).

-------------------------------------

給你一個(gè)下標(biāo)從 0 開(kāi)始的整數(shù)數(shù)組 nums 和一個(gè) 非負(fù) 整數(shù) k 。


在一步操作中,你可以執(zhí)行下述指令:


在范圍 [0, nums.length - 1] 中選擇一個(gè) 此前沒(méi)有選過(guò) 的下標(biāo) i 。

將 nums[i] 替換為范圍 [nums[i] - k, nums[i] + k] 內(nèi)的任一整數(shù)。

數(shù)組的 美麗值 定義為數(shù)組中由相等元素組成的最長(zhǎng)子序列的長(zhǎng)度。


對(duì)數(shù)組 nums 執(zhí)行上述操作任意次后,返回?cái)?shù)組可能取得的 最大 美麗值。


注意:你 只 能對(duì)每個(gè)下標(biāo)執(zhí)行 一次 此操作。


數(shù)組的 子序列 定義是:經(jīng)由原數(shù)組刪除一些元素(也可能不刪除)得到的一個(gè)新數(shù)組,且在此過(guò)程中剩余元素的順序不發(fā)生改變。


?


示例 1:


輸入:nums = [4,6,1,2], k = 2

輸出:3

解釋?zhuān)涸谶@個(gè)示例中,我們執(zhí)行下述操作:

- 選擇下標(biāo) 1 ,將其替換為 4(從范圍 [4,8] 中選出),此時(shí) nums = [4,4,1,2] 。

- 選擇下標(biāo) 3 ,將其替換為 4(從范圍 [0,4] 中選出),此時(shí) nums = [4,4,1,4] 。

執(zhí)行上述操作后,數(shù)組的美麗值是 3(子序列由下標(biāo) 0 、1 、3 對(duì)應(yīng)的元素組成)。

可以證明 3 是我們可以得到的由相等元素組成的最長(zhǎng)子序列長(zhǎng)度。

示例 2:


輸入:nums = [1,1,1,1], k = 10

輸出:4

解釋?zhuān)涸谶@個(gè)示例中,我們無(wú)需執(zhí)行任何操作。

數(shù)組 nums 的美麗值是 4(整個(gè)數(shù)組)。

?


提示:


1 <= nums.length <= 105

0 <= nums[i], k <= 105

-------------------------

以2k為區(qū)間,找區(qū)間內(nèi)元素的最大值,題目信息一轉(zhuǎn)換,就知道怎么做了,沒(méi)錯(cuò),我還是一如既往的笨;

執(zhí)行用時(shí):39 ms, 在所有?Java?提交中擊敗了100.00%的用戶(hù)

內(nèi)存消耗:57 MB, 在所有?Java?提交中擊敗了100.00%的用戶(hù)


LeetCode 6929. 數(shù)組的最大美麗值的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
建阳市| 安多县| 顺平县| 台安县| 巨野县| 星子县| 剑川县| 信阳市| 静海县| 鹰潭市| 时尚| 浏阳市| 西平县| 滨海县| 廊坊市| 子长县| 临高县| 原阳县| 肥乡县| 仁寿县| 固原市| 镶黄旗| 基隆市| 靖州| 四川省| 龙井市| 宁武县| 寿宁县| 慈利县| 高邑县| 龙南县| 三原县| 宜昌市| 将乐县| 米易县| 新河县| 洪湖市| 浙江省| 简阳市| 雅安市| 卓尼县|