Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
After doing so, return the array.
Example 1:
Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]
Explanation:
- index 0 --> the greatest element to the right of index 0 is index 1 (18).
- index 1 --> the greatest element to the right of index 1 is index 4 (6).
- index 2 --> the greatest element to the right of index 2 is index 4 (6).
- index 3 --> the greatest element to the right of index 3 is index 4 (6).
- index 4 --> the greatest element to the right of index 4 is index 5 (1).
- index 5 --> there are no elements to the right of index 5, so we put -1.
Example 2:
Input: arr = [400]
Output: [-1]
Explanation: There are no elements to the right of index 0.
Constraints:
func replaceElements(arr []int) []int {
le := len(arr)
if le < 2 {
arr[0] = -1
return arr
}
for i := 0; i < le; i++ {
big := -1
for y := i + 1; y < le; y++ {
if big < arr[y] {
big = arr[y]
}
}
arr[i] = big
}
return arr
}
https://go.dev/play/p/LyGSC7p7oBf
func replaceElements(arr []int) []int {
le := len(arr)
if le < 2 {
arr[0] = -1
return arr
}
lch := make(chan []int)
go standardValue(arr, lch, le)
return <-lch
}
func standardValue(arr []int, lch chan []int, le int) {
rch := make(chan int)
for i := 0; i < le; i++ {
go bigValue(arr, rch, i, le)
arr[i] = <-rch
}
lch <- arr
}
func bigValue(arr []int, rch chan int, i, le int) {
big := -1
for y := i + 1; y < le; y++ {
if big < arr[y] {
big = arr[y]
}
}
rch <- big
}
https://go.dev/play/p/VV_koDD_13T
func replaceElements(arr []int) []int {
greatest := -1
for i := len(arr) - 1; i >= 0; i-- {
arr[i], greatest = greatest, max(greatest, arr[i]) // has to be the same line b4 arr[i] get updated
}
return arr
}
func max(i,j int)int{
if i > j {
return i
}
return j
}
func replaceElements(arr []int) []int {
max, current := arr[len(arr)-1], 0
arr[len(arr)-1] = -1
for i := len(arr) - 2; i >= 0; i-- {
current, arr[i] = arr[i], max
if current > max {
max = current
}
}
return arr
}
func replaceElements(arr []int) []int {
max, current := -1, -1
for i := len(arr) - 1; i >= 0; i-- {
current, arr[i] = arr[i], max
if current > max {
max = current
}
}
return arr
}
func replaceElements(arr []int) []int {
max := -1
if len(arr) == 1 {
return []int{-1}
}
for i := len(arr) - 1; i >= 0; i-- {
temp := arr[i]
arr[i] = max
if temp > max {
max = temp
}
}
return arr
}
func replaceElements(arr []int) []int {
for i := 0; i < len(arr)-1; i++ {
index := i+1
for j := i+1; j < len(arr); j++ {
if arr[j] > arr[index] {
index = j
}
}
arr[i]=arr[index]
}
if len(arr) > 0 {
arr[len(arr)-1]= -1
}
return arr
}
func replaceElements(arr []int) []int {
l := len(arr)
for i := 0; i < l; i++ {
if i == l - 1 {
arr[i] = -1
break
}
h := arr[i+1]
for j := i + 2; j < l; j++ {
if arr[j] > h {
h = arr[j]
}
}
arr[i] = h
}
return arr
}
func Max (x, y int) int {
if x > y {
return x
}
return y
}
func replaceElements(arr []int) []int {
maxRight := arr[len(arr)-1]
arr[len(arr)-1] = -1
for i := len(arr) -2 ; i >= 0; i-- {
temp := arr[i]
arr[i] = maxRight
maxRight = Max(maxRight, temp)
}
return arr
}
func replaceElements(arr []int) []int {
m, t := -1, 0
for i := len(arr) - 1; i >= 0; i-- {
t, arr[i] = arr[i], m
if m < t {
m = t
}
}
return arr
}
Array101 Remove Duplicates from Sorted Array (0) | 2022.03.06 |
---|---|
Arrays 101 Code from Java to Go(A Better Repeated Deletion Algorithm - Intro) (0) | 2022.03.06 |
Arrays 101 Code from Java to Go(In-Place Array Operations Introduction) (0) | 2022.03.05 |
Arrays101 Valid Mountain Array in GO (0) | 2022.03.04 |
Arrays101 Check If N and Its Double Exist in go (0) | 2022.03.02 |
댓글 영역