Skip to content

Commit d010df5

Browse files
Merge pull request SharingSource#764 from SharingSource/ac_oier
✨feat: add 1802
2 parents a4b25fd + ec686c5 commit d010df5

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

Index/二分.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
| [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/) | 困难 | 🤩🤩🤩 |
6868
| [1751. 最多可以参加的会议数目 II](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/solution/po-su-dp-er-fen-dp-jie-fa-by-ac_oier-88du/) | 困难 | 🤩🤩🤩 |
6969
| [1760. 袋子里最少数目的球](https://leetcode.cn/problems/minimum-limit-of-balls-in-a-bag/) | [LeetCode 题解链接](https://acoier.com/2022/12/23/1760.%20%E8%A2%8B%E5%AD%90%E9%87%8C%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E7%90%83%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
70+
| [1802. 有界数组中指定下标处的最大值](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [LeetCode 题解链接](https://acoier.com/2023/01/06/1802.%20%E6%9C%89%E7%95%8C%E6%95%B0%E7%BB%84%E4%B8%AD%E6%8C%87%E5%AE%9A%E4%B8%8B%E6%A0%87%E5%A4%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
7071
| [1818. 绝对差值和](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/solution/gong-shui-san-xie-tong-guo-er-fen-zhao-z-vrmq/) | 中等 | 🤩🤩🤩🤩🤩 |
7172
| [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/solution/gong-shui-san-xie-cong-mei-ju-dao-pai-xu-kxnk/) | 中等 | 🤩🤩🤩 |
7273
| [1894. 找到需要补充粉笔的学生编号](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-kpqsk/) | 中等 | 🤩🤩🤩🤩 |

Index/数学.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
| [1759. 统计同构子字符串的数目](https://leetcode.cn/problems/count-number-of-homogenous-substrings/) | [LeetCode 题解链接](https://acoier.com/2022/12/26/1759.%20%E7%BB%9F%E8%AE%A1%E5%90%8C%E6%9E%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%95%B0%E7%9B%AE%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
8383
| [1775. 通过最少操作次数使数组的和相等](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
8484
| [1780. 判断一个数字是否可以表示成三的幂的和](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/) | [LeetCode 题解链接](https://acoier.com/2022/12/12/1780.%20%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E8%A1%A8%E7%A4%BA%E6%88%90%E4%B8%89%E7%9A%84%E5%B9%82%E7%9A%84%E5%92%8C%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
85+
| [1802. 有界数组中指定下标处的最大值](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [LeetCode 题解链接](https://acoier.com/2023/01/06/1802.%20%E6%9C%89%E7%95%8C%E6%95%B0%E7%BB%84%E4%B8%AD%E6%8C%87%E5%AE%9A%E4%B8%8B%E6%A0%87%E5%A4%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
8586
| [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/by-ac_oier-wgr8/) | 中等 | 🤩🤩🤩🤩 |
8687
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
8788
| [面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/) | 困难 | 🤩🤩🤩🤩 |

Index/构造.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| [1441. 用栈操作构建数组](https://leetcode.cn/problems/build-an-array-with-stack-operations/) | [LeetCode 题解链接](https://leetcode.cn/problems/build-an-array-with-stack-operations/solution/by-ac_oier-q37s/) | 中等 | 🤩🤩🤩🤩 |
1717
| [1537. 最大得分](https://leetcode.cn/problems/get-the-maximum-score/) | [LeetCode 题解链接](https://leetcode.cn/problems/get-the-maximum-score/solution/by-ac_oier-ht78/) | 困难 | 🤩🤩🤩🤩 |
1818
| [1719. 重构一棵树的方案数](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/solution/gong-shui-san-xie-gou-zao-yan-zheng-he-f-q6fc/) | 困难 | 🤩🤩 |
19+
| [1802. 有界数组中指定下标处的最大值](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [LeetCode 题解链接](https://acoier.com/2023/01/06/1802.%20%E6%9C%89%E7%95%8C%E6%95%B0%E7%BB%84%E4%B8%AD%E6%8C%87%E5%AE%9A%E4%B8%8B%E6%A0%87%E5%A4%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
1920
| [2028. 找出缺失的观测数据](https://leetcode-cn.com/problems/find-missing-observations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-missing-observations/solution/by-ac_oier-x22k/) | 中等 | 🤩🤩🤩🤩🤩 |
2021
| [剑指 Offer II 115. 重建序列](https://leetcode.cn/problems/ur2n8P/) | [LeetCode 题解链接](https://leetcode.cn/problems/ur2n8P/solution/by-ac_oier-oqxs/) | 中等 | 🤩🤩🤩🤩🤩 |
2122

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
| [1785. 构成特定和需要添加的最少元素](https://leetcode.cn/problems/minimum-elements-to-add-to-form-a-given-sum/) | [LeetCode 题解链接](https://acoier.com/2022/12/16/1785.%20%E6%9E%84%E6%88%90%E7%89%B9%E5%AE%9A%E5%92%8C%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E6%9C%80%E5%B0%91%E5%85%83%E7%B4%A0%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
222222
| [1790. 仅执行一次字符串交换能否使两个字符串相等](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/solution/by-ac_oier-qeul/) | 简单 | 🤩🤩🤩🤩🤩 |
223223
| [1791. 找出星型图的中心节点](https://leetcode-cn.com/problems/find-center-of-star-graph/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-center-of-star-graph/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-qoix/) | 简单 | 🤩🤩🤩 |
224+
| [1802. 有界数组中指定下标处的最大值](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [LeetCode 题解链接](https://acoier.com/2023/01/06/1802.%20%E6%9C%89%E7%95%8C%E6%95%B0%E7%BB%84%E4%B8%AD%E6%8C%87%E5%AE%9A%E4%B8%8B%E6%A0%87%E5%A4%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
224225
| [1816. 截断句子](https://leetcode-cn.com/problems/truncate-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/truncate-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-l7gu/) | 简单 | 🤩🤩🤩🤩 |
225226
| [1822. 数组元素积的符号](https://leetcode.cn/problems/sign-of-the-product-of-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/sign-of-the-product-of-an-array/solution/by-ac_oier-qy0n/) | 简单 | 🤩🤩🤩🤩 |
226227
| [1823. 找出游戏的获胜者](https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game/solution/by-ac_oier-qsuq/) | 中等 | 🤩🤩🤩🤩 |

Index/贪心算法.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
| [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩🤩 |
4242
| [1775. 通过最少操作次数使数组的和相等](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
4343
| [1785. 构成特定和需要添加的最少元素](https://leetcode.cn/problems/minimum-elements-to-add-to-form-a-given-sum/) | [LeetCode 题解链接](https://acoier.com/2022/12/16/1785.%20%E6%9E%84%E6%88%90%E7%89%B9%E5%AE%9A%E5%92%8C%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E6%9C%80%E5%B0%91%E5%85%83%E7%B4%A0%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
44+
| [1802. 有界数组中指定下标处的最大值](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [LeetCode 题解链接](https://acoier.com/2023/01/06/1802.%20%E6%9C%89%E7%95%8C%E6%95%B0%E7%BB%84%E4%B8%AD%E6%8C%87%E5%AE%9A%E4%B8%8B%E6%A0%87%E5%A4%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
4445
| [1833. 雪糕的最大数量](https://leetcode-cn.com/problems/maximum-ice-cream-bars/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-ice-cream-bars/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yrhjx/) | 中等 | 🤩🤩🤩🤩🤩 |
4546
| [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yh9qt/) | 中等 | 🤩🤩🤩🤩🤩 |
4647
| [1877. 数组中最大数对和的最小值](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ru29y/) | 中等 | 🤩🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1802. 有界数组中指定下标处的最大值](https://acoier.com/2023/01/06/1802.%20%E6%9C%89%E7%95%8C%E6%95%B0%E7%BB%84%E4%B8%AD%E6%8C%87%E5%AE%9A%E4%B8%8B%E6%A0%87%E5%A4%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**
4+
5+
Tag : 「二分」、「数学」、「构造」、「贪心」、「模拟」
6+
7+
8+
9+
给你三个正整数 `n``index``maxSum`。你需要构造一个同时满足下述所有条件的数组 `nums`(下标 从 `0` 开始 计数):
10+
11+
* `nums.length == n`
12+
* `nums[i]` 是 正整数 ,其中 `0 <= i < n`
13+
* `abs(nums[i] - nums[i+1]) <= 1` ,其中 `0 <= i < n-1`
14+
* `nums` 中所有元素之和不超过 `maxSum`
15+
* `nums[index]` 的值被 最大化
16+
* 返回你所构造的数组中的 `nums[index]`
17+
18+
注意:`abs(x)` 等于 `x` 的前提是 `x >= 0`;否则,`abs(x)` 等于 `-x`
19+
20+
示例 1:
21+
```
22+
输入:n = 4, index = 2, maxSum = 6
23+
24+
输出:2
25+
26+
解释:数组 [1,1,2,1] 和 [1,2,2,1] 满足所有条件。不存在其他在指定下标处具有更大值的有效数组。
27+
```
28+
示例 2:
29+
```
30+
输入:n = 6, index = 1, maxSum = 10
31+
32+
输出:3
33+
```
34+
35+
提示:
36+
* $1 <= n <= maxSum <= 10^9$
37+
* $0 <= index < n$
38+
39+
---
40+
41+
### 二分 + 贪心 + 数学
42+
43+
根据题意,容易想到以 `ans` 为分割点的正整数数组具有二段性,其中 `ans` 为最大的 $nums[idx]$。
44+
45+
小于等于 `ans` 的值均能通过直接调整 $nums[idx]$ 来构造,不会违反总和不超过 `max` 的限制;大于 `ans` 的值则无法满足 `max` 限制。基于此我们可通过「二分」的方式来找分割点。
46+
47+
假设当前二分到的值为 `x`,考虑如何实现一个 `check` 函数,该函数用于判断 `x` 能否作为 $nums[idx]$:
48+
49+
为了令 $nums[idx] = x$ 时,数组总和 `sum` 不超过 `max` 限制,我们应当贪心构造 $nums$ 的剩余元素:从 $idx$ 开始往两侧构造,按照递减的方式进行逐个构造,若递减到 $1$ 则维持不变。
50+
51+
这样可确保构造出来的 $nums$ 既满足 $nums[idx] = x$ 同时元素总和最小。
52+
53+
![](https://pic.leetcode.cn/1672970207-OYdBZZ-image.png)
54+
55+
位置 `idx` 的值为 `x`,其左边有 `idx` 个元素,其右边有 `n - idx - 1` 个元素。
56+
57+
利用「等差数列求和」公式分别从 `x - 1` 开始构造(注意:这里说的构造仅是计算 $nums$ 总和),若总和不超过 `max` 说明 $nums[idx] = x$ 满足要求,我们令 $l = mid$,否则令 $r = mid - 1$。
58+
59+
代码:
60+
```Java
61+
class Solution {
62+
public int maxValue(int n, int index, int max) {
63+
long l = 1, r = max;
64+
while (l < r) {
65+
long mid = l + r + 1 >> 1;
66+
if (check(n, mid, index, max)) l = mid;
67+
else r = mid - 1;
68+
}
69+
return (int) r;
70+
}
71+
boolean check(int n, long x, int idx, int max) {
72+
long sum = x;
73+
if (idx > x - 1) {
74+
long an = x - 1, a1 = 1, cnt = x - 1;
75+
sum += cnt * (a1 + an) / 2;
76+
sum += idx - cnt;
77+
} else {
78+
long cnt = idx, an = x - 1, a1 = an - cnt + 1;
79+
sum += cnt * (a1 + an) / 2;
80+
}
81+
if (n - idx - 1 > x - 1) {
82+
long an = x - 1, a1 = 1, cnt = x - 1;
83+
sum += cnt * (a1 + an) / 2;
84+
sum += n - idx - 1 - cnt;
85+
} else {
86+
long cnt = n - idx - 1, an = x - 1, a1 = an - cnt + 1;
87+
sum += cnt * (a1 + an) / 2;
88+
}
89+
return sum <= max;
90+
}
91+
}
92+
```
93+
* 时间复杂度:$O(\log{n})$
94+
* 空间复杂度:$O(1)$
95+
96+
---
97+
98+
### 最后
99+
100+
这是我们「刷穿 LeetCode」系列文章的第 `No.1802` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
101+
102+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
103+
104+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
105+
106+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
107+

0 commit comments

Comments
 (0)