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 fba9295

Browse files
solves replace elements with greatest on right
1 parent bc13e2e commit fba9295

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
## In-Place Operations
4040
| Question | Solution | Youtube |
4141
|------|:--------:|:----------------------:|
42-
| [Replace Elements with Greatest Element on Right Side](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3259/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
42+
| [Replace Elements with Greatest Element on Right Side](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3259/) | [![Java](assets/java.png)](java/src/ReplaceElementsWithGreatestOnTheRightSide.java) [![Python](assets/python.png)](python/replace_elements_with_greatest_on_right.py) |
4343
| [Remove Duplicates from Sorted Array](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3258/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
4444
| [Move Zeros](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3157/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
4545
| [Sort Array By Parity](https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3260/) | [![Java](assets/java.png)](java/src/) [![Python](assets/python.png)](python/) |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class ReplaceElementsWithGreatestOnTheRightSide {
2+
public int[] replaceElements(int[] array) {
3+
int[] result = new int[array.length];
4+
result[result.length - 1] = -1;
5+
for (int index = array.length - 2, max = array[array.length - 1]; index >= 0 ; index--) {
6+
result[index] = max;
7+
max = Math.max(max, array[index]);
8+
}
9+
return result;
10+
}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def replaceElements(self, array: List[int]) -> List[int]:
6+
result = [0] * len(array)
7+
result[len(result) - 1] = -1
8+
index, greatest = len(array) - 2, array[len(array) - 1]
9+
while index >= 0:
10+
result[index] = greatest
11+
greatest = max(greatest, array[index])
12+
index -= 1
13+
return result

0 commit comments

Comments
(0)

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