Skip to content

Commit 41b0383

Browse files
committed
Add solution 1846
1 parent 185b1e1 commit 41b0383

File tree

8 files changed

+297
-4
lines changed

8 files changed

+297
-4
lines changed

leetcode/0218.The-Skyline-Problem/218. The Skyline Problem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (bit *BinaryIndexedTree) Query(index int) int {
8181
return sum
8282
}
8383

84-
// 解法三 线段树 Segment Tree,时间复杂度 O(n log n)
84+
// 解法二 线段树 Segment Tree,时间复杂度 O(n log n)
8585
func getSkyline1(buildings [][]int) [][]int {
8686
st, ans, lastHeight, check := template.SegmentTree{}, [][]int{}, 0, false
8787
posMap, pos := discretization218(buildings)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
func maximumElementAfterDecrementingAndRearranging(arr []int) int {
4+
n := len(arr)
5+
cnt := make([]int, n+1)
6+
for _, v := range arr {
7+
cnt[min(v, n)]++
8+
}
9+
miss := 0
10+
for _, c := range cnt[1:] {
11+
if c == 0 {
12+
miss++
13+
} else {
14+
miss -= min(c-1, miss)
15+
}
16+
}
17+
return n - miss
18+
}
19+
20+
func min(a, b int) int {
21+
if a < b {
22+
return a
23+
}
24+
return b
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1846 struct {
9+
para1846
10+
ans1846
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1846 struct {
16+
arr []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1846 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1846(t *testing.T) {
26+
27+
qs := []question1846{
28+
29+
{
30+
para1846{[]int{2, 2, 1, 2, 1}},
31+
ans1846{2},
32+
},
33+
34+
{
35+
para1846{[]int{100, 1, 1000}},
36+
ans1846{3},
37+
},
38+
39+
{
40+
para1846{[]int{1, 2, 3, 4, 5}},
41+
ans1846{5},
42+
},
43+
}
44+
45+
fmt.Printf("------------------------Leetcode Problem 1846------------------------\n")
46+
47+
for _, q := range qs {
48+
_, p := q.ans1846, q.para1846
49+
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumElementAfterDecrementingAndRearranging(p.arr))
50+
}
51+
fmt.Printf("\n\n\n")
52+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [1846. Maximum Element After Decreasing and Rearranging](https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/)
2+
3+
4+
## 题目
5+
6+
You are given an array of positive integers `arr`. Perform some operations (possibly none) on `arr` so that it satisfies these conditions:
7+
8+
- The value of the **first** element in `arr` must be `1`.
9+
- The absolute difference between any 2 adjacent elements must be **less than or equal to** `1`. In other words, `abs(arr[i] - arr[i - 1]) <= 1` for each `i` where `1 <= i < arr.length` (**0-indexed**). `abs(x)` is the absolute value of `x`.
10+
11+
There are 2 types of operations that you can perform any number of times:
12+
13+
- **Decrease** the value of any element of `arr` to a **smaller positive integer**.
14+
- **Rearrange** the elements of `arr` to be in any order.
15+
16+
Return *the **maximum** possible value of an element in* `arr` *after performing the operations to satisfy the conditions*.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: arr = [2,2,1,2,1]
22+
Output: 2
23+
Explanation:
24+
We can satisfy the conditions by rearrangingarr so it becomes[1,2,2,2,1].
25+
The largest element inarr is 2.
26+
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: arr = [100,1,1000]
33+
Output: 3
34+
Explanation:
35+
One possible way to satisfy the conditions is by doing the following:
36+
1. Rearrangearr so it becomes[1,100,1000].
37+
2. Decrease the value of the second element to 2.
38+
3. Decrease the value of the third element to 3.
39+
Nowarr = [1,2,3], whichsatisfies the conditions.
40+
The largest element inarr is 3.
41+
```
42+
43+
**Example 3:**
44+
45+
```
46+
Input: arr = [1,2,3,4,5]
47+
Output: 5
48+
Explanation: The array already satisfies the conditions, and the largest element is 5.
49+
50+
```
51+
52+
**Constraints:**
53+
54+
- `1 <= arr.length <= 10^5`
55+
- `1 <= arr[i] <= 10^9`
56+
57+
## 题目大意
58+
59+
给你一个正整数数组 arr 。请你对 arr 执行一些操作(也可以不进行任何操作),使得数组满足以下条件:
60+
61+
- arr 中 第一个 元素必须为 1 。
62+
- 任意相邻两个元素的差的绝对值 小于等于 1 ,也就是说,对于任意的 1 <= i < arr.length (数组下标从 0 开始),都满足 abs(arr[i] - arr[i - 1]) <= 1 。abs(x) 为 x 的绝对值。
63+
64+
你可以执行以下 2 种操作任意次:
65+
66+
- 减小 arr 中任意元素的值,使其变为一个 更小的正整数 。
67+
- 重新排列 arr 中的元素,你可以以任意顺序重新排列。
68+
69+
请你返回执行以上操作后,在满足前文所述的条件下,arr 中可能的 最大值 。
70+
71+
## 解题思路
72+
73+
- 正整数数组 arr 第一个元素必须为 1,且两两元素绝对值小于等于 1,那么 arr 最大值肯定不大于 n。采用贪心的策略,先统计所有元素出现的次数,大于 n 的元素出现次数都累加到 n 上。然后从 1 扫描到 n,遇到“空隙”(出现次数为 0 的元素),便将最近一个出现次数大于 1 的元素“挪”过来填补“空隙”。题目所求最大值出现在,“填补空隙”之后,数组从左往右连续的最右端。
74+
75+
## 代码
76+
77+
```go
78+
package leetcode
79+
80+
func maximumElementAfterDecrementingAndRearranging(arr []int) int {
81+
n := len(arr)
82+
count := make([]int, n+1)
83+
for _, v := range arr {
84+
count[min(v, n)]++
85+
}
86+
miss := 0
87+
for _, c := range count[1:] {
88+
if c == 0 {
89+
miss++
90+
} else {
91+
miss -= min(c-1, miss)
92+
}
93+
}
94+
return n - miss
95+
}
96+
97+
func min(a, b int) int {
98+
if a < b {
99+
return a
100+
}
101+
return b
102+
}
103+
```

website/content/ChapterFour/0200~0299/0218.The-Skyline-Problem.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (bit *BinaryIndexedTree) Query(index int) int {
162162
return sum
163163
}
164164

165-
// 解法三 线段树 Segment Tree,时间复杂度 O(n log n)
165+
// 解法二 线段树 Segment Tree,时间复杂度 O(n log n)
166166
func getSkyline1(buildings [][]int) [][]int {
167167
st, ans, lastHeight, check := template.SegmentTree{}, [][]int{}, 0, false
168168
posMap, pos := discretization218(buildings)

website/content/ChapterFour/1700~1799/1758.Minimum-Changes-To-Make-Alternating-Binary-String.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func min(a, b int) int {
7070
```
7171

7272

73-
7473
----------------------------------------------
74+
<div style="display: flex;justify-content: space-between;align-items: center;">
7575
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1752.Check-if-Array-Is-Sorted-and-Rotated/">⬅️上一页</a></p>
76-
76+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1800~1899/1846.Maximum-Element-After-Decreasing-and-Rearranging/">下一页➡️</a></p>
77+
</div>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [1846. Maximum Element After Decreasing and Rearranging](https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/)
2+
3+
4+
## 题目
5+
6+
You are given an array of positive integers `arr`. Perform some operations (possibly none) on `arr` so that it satisfies these conditions:
7+
8+
- The value of the **first** element in `arr` must be `1`.
9+
- The absolute difference between any 2 adjacent elements must be **less than or equal to** `1`. In other words, `abs(arr[i] - arr[i - 1]) <= 1` for each `i` where `1 <= i < arr.length` (**0-indexed**). `abs(x)` is the absolute value of `x`.
10+
11+
There are 2 types of operations that you can perform any number of times:
12+
13+
- **Decrease** the value of any element of `arr` to a **smaller positive integer**.
14+
- **Rearrange** the elements of `arr` to be in any order.
15+
16+
Return *the **maximum** possible value of an element in* `arr` *after performing the operations to satisfy the conditions*.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: arr = [2,2,1,2,1]
22+
Output: 2
23+
Explanation:
24+
We can satisfy the conditions by rearrangingarr so it becomes[1,2,2,2,1].
25+
The largest element inarr is 2.
26+
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: arr = [100,1,1000]
33+
Output: 3
34+
Explanation:
35+
One possible way to satisfy the conditions is by doing the following:
36+
1. Rearrangearr so it becomes[1,100,1000].
37+
2. Decrease the value of the second element to 2.
38+
3. Decrease the value of the third element to 3.
39+
Nowarr = [1,2,3], whichsatisfies the conditions.
40+
The largest element inarr is 3.
41+
```
42+
43+
**Example 3:**
44+
45+
```
46+
Input: arr = [1,2,3,4,5]
47+
Output: 5
48+
Explanation: The array already satisfies the conditions, and the largest element is 5.
49+
50+
```
51+
52+
**Constraints:**
53+
54+
- `1 <= arr.length <= 10^5`
55+
- `1 <= arr[i] <= 10^9`
56+
57+
## 题目大意
58+
59+
给你一个正整数数组 arr 。请你对 arr 执行一些操作(也可以不进行任何操作),使得数组满足以下条件:
60+
61+
- arr 中 第一个 元素必须为 1 。
62+
- 任意相邻两个元素的差的绝对值 小于等于 1 ,也就是说,对于任意的 1 <= i < arr.length (数组下标从 0 开始),都满足 abs(arr[i] - arr[i - 1]) <= 1 。abs(x) 为 x 的绝对值。
63+
64+
你可以执行以下 2 种操作任意次:
65+
66+
- 减小 arr 中任意元素的值,使其变为一个 更小的正整数 。
67+
- 重新排列 arr 中的元素,你可以以任意顺序重新排列。
68+
69+
请你返回执行以上操作后,在满足前文所述的条件下,arr 中可能的 最大值 。
70+
71+
## 解题思路
72+
73+
- 正整数数组 arr 第一个元素必须为 1,且两两元素绝对值小于等于 1,那么 arr 最大值肯定不大于 n。采用贪心的策略,先统计所有元素出现的次数,大于 n 的元素出现次数都累加到 n 上。然后从 1 扫描到 n,遇到“空隙”(出现次数为 0 的元素),便将最近一个出现次数大于 1 的元素“挪”过来填补“空隙”。题目所求最大值出现在,“填补空隙”之后,数组从左往右连续的最右端。
74+
75+
## 代码
76+
77+
```go
78+
package leetcode
79+
80+
func maximumElementAfterDecrementingAndRearranging(arr []int) int {
81+
n := len(arr)
82+
count := make([]int, n+1)
83+
for _, v := range arr {
84+
count[min(v, n)]++
85+
}
86+
miss := 0
87+
for _, c := range count[1:] {
88+
if c == 0 {
89+
miss++
90+
} else {
91+
miss -= min(c-1, miss)
92+
}
93+
}
94+
return n - miss
95+
}
96+
97+
func min(a, b int) int {
98+
if a < b {
99+
return a
100+
}
101+
return b
102+
}
103+
```
104+
105+
106+
----------------------------------------------
107+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1700~1799/1758.Minimum-Changes-To-Make-Alternating-Binary-String/">⬅️上一页</a></p>
108+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
bookCollapseSection: true
3+
weight: 20
4+
---

0 commit comments

Comments
 (0)