Skip to content

Commit 41fd0ee

Browse files
authored
Leetcode Problem #1299: Greatest Element To The Right (#29)
1 parent 07aad7b commit 41fd0ee

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Problem: https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/
2+
package main
3+
4+
func replaceElements(arr []int) []int {
5+
max := 0
6+
7+
for i := len(arr) - 1; i >= 0; i-- {
8+
temp := max
9+
10+
if arr[i] > max {
11+
max = arr[i]
12+
}
13+
14+
arr[i] = temp
15+
}
16+
17+
arr[len(arr)-1] = -1
18+
19+
return arr
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_replaceElements(t *testing.T) {
9+
type args struct {
10+
arr []int
11+
}
12+
13+
tests := []struct {
14+
name string
15+
args args
16+
want []int
17+
}{
18+
{
19+
name: "first case",
20+
args: args{arr: []int{17, 18, 5, 4, 6, 1}},
21+
want: []int{18, 6, 6, 6, 1, -1},
22+
},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
if got := replaceElements(tt.args.arr); !reflect.DeepEqual(got, tt.want) {
28+
t.Errorf("replaceElements() = %v, want %v", got, tt.want)
29+
}
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)