8000 feat: solved 1299. Replace Elements with Greatest Element on Right Side Β· realtemirov/leetcode@dd3c76d Β· GitHub
[go: up one dir, main page]

Ski 8000 p to content

Commit dd3c76d

Browse files
committed
feat: solved 1299. Replace Elements with Greatest Element on Right Side
1 parent 177f75b commit dd3c76d

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 1299. Replace Elements with Greatest Element on Right Side
2+
3+
🟩 Easy
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func replaceElements(arr []int) []int {
11+
num := -1
12+
13+
for i:=len(arr)-1; i>=0; i-- {
14+
if arr[i] > num {
15+
num, arr[i] = arr[i], num
16+
} else {
17+
8000 arr[i] = num
18+
}
19+
}
20+
21+
return arr
22+
}
23+
```
24+
25+
![result](1299.png)
26+
27+
Leetcode: [link](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/description/)

β€ŽProblems/1299/1299.png

36.5 KB
Loading

β€ŽProblems/1299/solution.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package problem1299
2+
3+
func ReplaceElements(arr []int) []int {
4+
num := -1
5+
6+
for i := len(arr) - 1; i >= 0; i-- {
7+
if arr[i] > num {
8+
num, arr[i] = arr[i], num
9+
} else {
10+
arr[i] = num
11+
}
12+
}
13+
14+
return arr
15+
}

β€ŽProblems/1299/solution_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problem1299_test
2+
3+
import (
4+
"testing"
5+
6+
problem1299 "github.com/realtemirov/leetcode/Problems/1299"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases []int
14+
expected []int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: []int{17, 18, 5, 4, 6, 1},
19+
expected: []int{18, 6, 6, 6, 1, -1},
20+
},
21+
{
22+
name: "Test 2",
23+
cases: []int{400},
24+
expected: []int{-1},
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
result := problem1299.ReplaceElements(tc.cases)
31+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
32+
})
33+
}
34+
}

β€ŽProblems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* 🟩 Easy - [1232. Check If It Is a Straight Line](<./1232/1232. Check If It Is a Straight Line.md>)
6868
* 🟩 Easy - [1275. Find Winner on a Tic Tac Toe Game](<./1275/1275. Find Winner on a Tic Tac Toe Game.md>)
6969
* 🟩 Easy - [1295. Find Numbers with Even Number of Digits](<./1295/1295. Find Numbers with Even Number of Digits.md>)
70+
* 🟩 Easy - [1299. Replace Elements with Greatest Element on Right Side](<./1299/1299. Replace Elements with Greatest Element on Right Side.md>)
7071
* 🟩 Easy - [1342. Number of Steps to Reduce a Number to Zero](<./1342/1342. Number of Steps to Reduce a Number to Zero.md>)
7172
* 🟩 Easy - [1346. Check If N and Its Double Exist](<./1346/1346. Check If N and Its Double Exist.md>)
7273
* 🟩 Easy - [1480. Running Sum of 1d Array](<./1480/1480. Running Sum of 1d Array.md>)

β€ŽSUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
* [1232. Check If It Is a Straight Line](<./Problems/1232/1232. Check If It Is a Straight Line.md>)
123123
* [1275. Find Winner on a Tic Tac Toe Game](<./Problems/1275/1275. Find Winner on a Tic Tac Toe Game.md>)
124124
* [1295. Find Numbers with Even Number of Digits](<./Problems/1295/1295. Find Numbers with Even Number of Digits.md>)
125+
* [1299. Replace Elements with Greatest Element on Right Side](<./Problems/1299/1299. Replace Elements with Greatest Element on Right Side.md>)
125126
* [1342. Number of Steps to Reduce a Number to Zero](<./Problems/1342/1342. Number of Steps to Reduce a Number to Zero.md>)
126127
* [1346. Check If N and Its Double Exist](<./Problems/1346/1346. Check If N and Its Double Exist.md>)
127128
* [1480. Running Sum of 1d Array](<./Problems/1480/1480. Running Sum of 1d Array.md>)
@@ -221,7 +222,7 @@
221222
* [1287. Element Appearing More Than 25% In Sorted Array](<Problems/1287. Element Appearing More Than 25 In Sorted Array.md>)
222223
* [1291. Sequential Digits](<Problems/1291. Sequential Digits.md>)
223224
224-
* [1299. Replace Elements with Greatest Element on Right Side](<Problems/1299. Replace Elements with Greatest Element on Right Side.md>)
225+
225226
226227
227228
* [1422. Maximum Score After Splitting a String](<Problems/1422. Maximum Score After Splitting a String.md>)

0 commit comments

Comments
Β (0)
0