Skip to content

Commit

Permalink
replaceElements
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuanleiGuo committed Jul 19, 2020
1 parent c1be9d2 commit fa00670
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions leetcode/replace_elements_with_greatest_element_on_right_side.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package leetcode

func replaceElements(arr []int) []int {
if len(arr) == 0 {
return arr
}

rMax := -1
for i := len(arr) - 1; i >= 0; i-- {
v := arr[i]
arr[i] = rMax
if v >= rMax {
rMax = v
}
}
return arr
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package leetcode

import (
"reflect"
"testing"
)

func TestReplaceElements(t *testing.T) {
cases := []struct {
input []int
expected []int
}{
{
input: []int{17, 18, 5, 4, 6, 1},
expected: []int{18, 6, 6, 6, 1, -1},
},
}

for _, c := range cases {
actual := replaceElements(c.input)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("err")
}
}
}

0 comments on commit fa00670

Please sign in to comment.