From ec686c58f133dc726c96bc3463afb343f7be2a14 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Fri, 6 Jan 2023 10:41:09 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=201802?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\344\272\214\345\210\206.md" | 1 + "Index/\346\225\260\345\255\246.md" | 1 + "Index/\346\236\204\351\200\240.md" | 1 + "Index/\346\250\241\346\213\237.md" | 1 + ...52\345\277\203\347\256\227\346\263\225.md" | 1 + ...10\344\270\255\347\255\211\357\274\211.md" | 107 ++++++++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 "LeetCode/1801-1810/1802. \346\234\211\347\225\214\346\225\260\347\273\204\344\270\255\346\214\207\345\256\232\344\270\213\346\240\207\345\244\204\347\232\204\346\234\200\345\244\247\345\200\274\357\274\210\344\270\255\347\255\211\357\274\211.md" diff --git "a/Index/\344\272\214\345\210\206.md" "b/Index/\344\272\214\345\210\206.md" index 1bab100b..a44a9eec 100644 --- "a/Index/\344\272\214\345\210\206.md" +++ "b/Index/\344\272\214\345\210\206.md" @@ -67,6 +67,7 @@ | [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/) | 困难 | 🤩🤩🤩 | | [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/) | 困难 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | +| [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\225\260\345\255\246.md" "b/Index/\346\225\260\345\255\246.md" index 9e26a06c..df4055e7 100644 --- "a/Index/\346\225\260\345\255\246.md" +++ "b/Index/\346\225\260\345\255\246.md" @@ -82,6 +82,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | +| [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/) | 中等 | 🤩🤩🤩🤩 | | [剑指 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/) | 中等 | 🤩🤩🤩🤩 | | [面试题 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/) | 中等 | 🤩🤩🤩🤩 | | [面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\236\204\351\200\240.md" "b/Index/\346\236\204\351\200\240.md" index fc81a371..662d936b 100644 --- "a/Index/\346\236\204\351\200\240.md" +++ "b/Index/\346\236\204\351\200\240.md" @@ -16,6 +16,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [1537. 最大得分](https://leetcode.cn/problems/get-the-maximum-score/) | [LeetCode 题解链接](https://leetcode.cn/problems/get-the-maximum-score/solution/by-ac_oier-ht78/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 困难 | 🤩🤩 | +| [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/) | 中等 | 🤩🤩🤩🤩 | | [2028. 找出缺失的观测数据](https://leetcode-cn.com/problems/find-missing-observations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-missing-observations/solution/by-ac_oier-x22k/) | 中等 | 🤩🤩🤩🤩🤩 | | [剑指 Offer II 115. 重建序列](https://leetcode.cn/problems/ur2n8P/) | [LeetCode 题解链接](https://leetcode.cn/problems/ur2n8P/solution/by-ac_oier-oqxs/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index c22e7bea..017df1b7 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -221,6 +221,7 @@ | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" "b/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" index e2d0d0db..6ad8a67f 100644 --- "a/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" +++ "b/Index/\350\264\252\345\277\203\347\256\227\346\263\225.md" @@ -41,6 +41,7 @@ | [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/) | 简单 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/1801-1810/1802. \346\234\211\347\225\214\346\225\260\347\273\204\344\270\255\346\214\207\345\256\232\344\270\213\346\240\207\345\244\204\347\232\204\346\234\200\345\244\247\345\200\274\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1801-1810/1802. \346\234\211\347\225\214\346\225\260\347\273\204\344\270\255\346\214\207\345\256\232\344\270\213\346\240\207\345\244\204\347\232\204\346\234\200\345\244\247\345\200\274\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..ffc12564 --- /dev/null +++ "b/LeetCode/1801-1810/1802. \346\234\211\347\225\214\346\225\260\347\273\204\344\270\255\346\214\207\345\256\232\344\270\213\346\240\207\345\244\204\347\232\204\346\234\200\345\244\247\345\200\274\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,107 @@ +### 题目描述 + +这是 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/)** ,难度为 **中等**。 + +Tag : 「二分」、「数学」、「构造」、「贪心」、「模拟」 + + + +给你三个正整数 `n`、`index` 和 `maxSum`。你需要构造一个同时满足下述所有条件的数组 `nums`(下标 从 `0` 开始 计数): + +* `nums.length == n` +* `nums[i]` 是 正整数 ,其中 `0 <= i < n` +* `abs(nums[i] - nums[i+1]) <= 1` ,其中 `0 <= i < n-1` +* `nums` 中所有元素之和不超过 `maxSum` +* `nums[index]` 的值被 最大化 +* 返回你所构造的数组中的 `nums[index]` + +注意:`abs(x)` 等于 `x` 的前提是 `x >= 0`;否则,`abs(x)` 等于 `-x`。 + +示例 1: +``` +输入:n = 4, index = 2, maxSum = 6 + +输出:2 + +解释:数组 [1,1,2,1] 和 [1,2,2,1] 满足所有条件。不存在其他在指定下标处具有更大值的有效数组。 +``` +示例 2: +``` +输入:n = 6, index = 1, maxSum = 10 + +输出:3 +``` + +提示: +* $1 <= n <= maxSum <= 10^9$ +* $0 <= index < n$ + +--- + +### 二分 + 贪心 + 数学 + +根据题意,容易想到以 `ans` 为分割点的正整数数组具有二段性,其中 `ans` 为最大的 $nums[idx]$。 + +小于等于 `ans` 的值均能通过直接调整 $nums[idx]$ 来构造,不会违反总和不超过 `max` 的限制;大于 `ans` 的值则无法满足 `max` 限制。基于此我们可通过「二分」的方式来找分割点。 + +假设当前二分到的值为 `x`,考虑如何实现一个 `check` 函数,该函数用于判断 `x` 能否作为 $nums[idx]$: + +为了令 $nums[idx] = x$ 时,数组总和 `sum` 不超过 `max` 限制,我们应当贪心构造 $nums$ 的剩余元素:从 $idx$ 开始往两侧构造,按照递减的方式进行逐个构造,若递减到 $1$ 则维持不变。 + +这样可确保构造出来的 $nums$ 既满足 $nums[idx] = x$ 同时元素总和最小。 + +![](https://pic.leetcode.cn/1672970207-OYdBZZ-image.png) + +位置 `idx` 的值为 `x`,其左边有 `idx` 个元素,其右边有 `n - idx - 1` 个元素。 + +利用「等差数列求和」公式分别从 `x - 1` 开始构造(注意:这里说的构造仅是计算 $nums$ 总和),若总和不超过 `max` 说明 $nums[idx] = x$ 满足要求,我们令 $l = mid$,否则令 $r = mid - 1$。 + +代码: +```Java +class Solution { + public int maxValue(int n, int index, int max) { + long l = 1, r = max; + while (l < r) { + long mid = l + r + 1 >> 1; + if (check(n, mid, index, max)) l = mid; + else r = mid - 1; + } + return (int) r; + } + boolean check(int n, long x, int idx, int max) { + long sum = x; + if (idx > x - 1) { + long an = x - 1, a1 = 1, cnt = x - 1; + sum += cnt * (a1 + an) / 2; + sum += idx - cnt; + } else { + long cnt = idx, an = x - 1, a1 = an - cnt + 1; + sum += cnt * (a1 + an) / 2; + } + if (n - idx - 1 > x - 1) { + long an = x - 1, a1 = 1, cnt = x - 1; + sum += cnt * (a1 + an) / 2; + sum += n - idx - 1 - cnt; + } else { + long cnt = n - idx - 1, an = x - 1, a1 = an - cnt + 1; + sum += cnt * (a1 + an) / 2; + } + return sum <= max; + } +} +``` +* 时间复杂度:$O(\log{n})$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1802` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +