Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit acaeeda

Browse files
feat: add ts solution to lc problem: No.1846 (doocs#1559)
1 parent ee6cae6 commit acaeeda

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

‎solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/README.md‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<b>输入:</b>arr = [2,2,1,2,1]
3131
<b>输出:</b>2
3232
<b>解释:</b>
33-
我们可以重新排列 arr 得到 <code>[1,2,2,2,1] ,该数组满足所有条件。</code>
33+
我们可以重新排列 arr 得到 [1,2,2,2,1] ,该数组满足所有条件。
3434
arr 中最大元素为 2 。
3535
</pre>
3636

@@ -41,10 +41,10 @@ arr 中最大元素为 2 。
4141
<b>输出:</b>3
4242
<b>解释:</b>
4343
一个可行的方案如下:
44-
1. 重新排列 <code>arr</code> 得到 <code>[1,100,1000] 。</code>
44+
1. 重新排列 arr< 得到 [1,100,1000] 。
4545
2. 将第二个元素减小为 2 。
4646
3. 将第三个元素减小为 3 。
47-
现在 <code>arr = [1,2,3] ,满足所有条件。</code>
47+
现在 arr = [1,2,3] ,满足所有条件。
4848
arr 中最大元素为 3 。
4949
</pre>
5050

@@ -150,6 +150,22 @@ func max(a, b int) int {
150150
}
151151
```
152152

153+
### **TypeScript**
154+
155+
```ts
156+
function maximumElementAfterDecrementingAndRearranging(arr: number[]): number {
157+
arr.sort((a, b) => a - b);
158+
arr[0] = 1;
159+
let ans = 1;
160+
for (let i = 1; i < arr.length; ++i) {
161+
const d = Math.max(0, arr[i] - arr[i - 1] - 1);
162+
arr[i] -= d;
163+
ans = Math.max(ans, arr[i]);
164+
}
165+
return ans;
166+
};
167+
```
168+
153169
### **...**
154170

155171
```

‎solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/README_EN.md‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<strong>Input:</strong> arr = [2,2,1,2,1]
2828
<strong>Output:</strong> 2
2929
<strong>Explanation:</strong>
30-
We can satisfy the conditions by rearranging <code>arr</code> so it becomes <code>[1,2,2,2,1]</code>.
31-
The largest element in <code>arr</code> is 2.
30+
We can satisfy the conditions by rearranging arr so it becomes [1,2,2,2,1].
31+
The largest element in arr is 2.
3232
</pre>
3333

3434
<p><strong class="example">Example 2:</strong></p>
@@ -38,11 +38,11 @@ The largest element in <code>arr</code> is 2.
3838
<strong>Output:</strong> 3
3939
<strong>Explanation:</strong>
4040
One possible way to satisfy the conditions is by doing the following:
41-
1. Rearrange <code>arr</code> so it becomes <code>[1,100,1000]</code>.
41+
1. Rearrange arr so it becomes [1,100,1000].
4242
2. Decrease the value of the second element to 2.
4343
3. Decrease the value of the third element to 3.
44-
Now <code>arr = [1,2,3], which </code>satisfies the conditions.
45-
The largest element in <code>arr is 3.</code>
44+
Now arr = [1,2,3], which satisfies the conditions.
45+
The largest element in arr is 3.
4646
</pre>
4747

4848
<p><strong class="example">Example 3:</strong></p>
@@ -138,6 +138,22 @@ func max(a, b int) int {
138138
}
139139
```
140140

141+
### **TypeScript**
142+
143+
```ts
144+
function maximumElementAfterDecrementingAndRearranging(arr: number[]): number {
145+
arr.sort((a, b) => a - b);
146+
arr[0] = 1;
147+
let ans = 1;
148+
for (let i = 1; i < arr.length; ++i) {
149+
const d = Math.max(0, arr[i] - arr[i - 1] - 1);
150+
arr[i] -= d;
151+
ans = Math.max(ans, arr[i]);
152+
}
153+
return ans;
154+
};
155+
```
156+
141157
### **...**
142158

143159
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function maximumElementAfterDecrementingAndRearranging(arr: number[]): number {
2+
arr.sort((a, b) => a - b);
3+
arr[0] = 1;
4+
let ans = 1;
5+
for (let i = 1; i < arr.length; ++i) {
6+
const d = Math.max(0, arr[i] - arr[i - 1] - 1);
7+
arr[i] -= d;
8+
ans = Math.max(ans, arr[i]);
9+
}
10+
return ans;
11+
};

‎solution/config.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
1754,
7070
1760,
7171
1797,
72+
1846,
7273
1850,
7374
1891,
7475
1899,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /