Array manipulation
Problem Statement
Starting with 1-indexed slice of zeros and a list of operations, for each operation add a value to each the slice element between two indices inclusive. Once all the operations has been performed, return the maximum value in the slice.
The algorithm O(m * n)
- The function
arrayManipulation
takes two parameters:n
andqueries
.n
is the size of the array, andqueries
is a two dimensional slice. Each slice inqueries
represents an operation that adds a certain value to the array within a certain range[(1, 2) 3 ]
. - The function first transforms the two dimensional slice
queries
into a single dimension slice ofOperation
structs. EachOperation
struct has two fields:addition
andindexes
.addition
is the value that is added to the array, andindexes
is a struct that contains the start and end indices of the range. - The function then creates a new slice of int32s with the size of
n
. This slice will store the final result of the array manipulation operations. - Next, the function iterates over the
operations
slice. For each operation, it iterates over the range of indices and adds theaddition
value to the corresponding elements in theresult
slice. - After all the operations have been applied, the function sorts the
result
slice in descending order. - Finally, the function returns the first element in the
result
slice, which holds the maximum value inside the slice.
But my solution was not perfect and perform poorly so have take a look another algorithms in order to improve mine 😊. Thanks to Mirefly, Firegoblin and anupam_codecs I could came with a better algorithm.
But still it didn’t feel like that was my code but theirs.
The final implementation O(n)
- The function
arrayManipulation
takes two parameters:n
andqueries
.n
is the size of the array, andqueries
is a two dimensional slice. Each slice inqueries
represents an operation that adds a certain value to the array within a certain range. - The function first initializes a slice of int64s with the size of
n
. This slice will store the final result of the array manipulation operations. - Next, the function converts the two dimensional slice
queries
into a single dimension slice ofOperation
structs. EachOperation
struct has three fields:a
,b
, andk
.a
is the start index of the range,b
is the end index of the range, andk
is the value that is added to the array within the range. - The function then iterates over the
operations
slice. For each operation, it adds thek
value to the element at indexa
in theresult
slice. Ifb
is less thann
(the last position at the slice), the function also subtracts thek
value from the element at indexb + 1
in theresult
slice. - After all the operations have been applied, the function iterates over the
result
slice and adds each element to the element at the next index. This ensures that the final element in theresult
slice contains the maximum value in the array. - Right after, the function sorts the
result
slice in descending order - Finally returns the first element (Max value in the slice).
Disclaimer
I thought it was fun to implement the ascending order function with partial applying and closures BUT of course I KNOW it was a little too much.