FastPrepApply Affine Array Transformations

Apply Affine Array Transformations

ByteDance logoByteDance● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Transform every element of an integer array values using an ordered list transforms.

For this exercise, assume each transformation is a pair [a,b]. It replaces the current value x with a * x + b. Apply all pairs in their given order to every element, then return the final values in the original array order.

  • The same transformation list applies independently to every element.
  • An empty transformation list leaves the values unchanged.
  • A multiplier of 0 resets the current value to that step's offset; later transformations still apply.
  • An empty input array produces an empty output array.
  • Use exact signed 64-bit results. Do not change the input arrays or round, clamp or take a modulus.

Function

transformArray(values: int[], transforms: int[][]) → long[]

Examples

Example 1

values = [1,-2,0]transforms = [[2,3],[-1,4]]return = [-1,5,1]

The first step gives [5,-1,3]; the second gives [-1,5,1].

Example 2

values = [10,-20]transforms = [[2,1],[0,7],[-2,3]]return = [-11,-11]

The middle step replaces every value with 7. The last step therefore returns -11 for both entries.

Example 3

values = [4,4,-1]transforms = []return = [4,4,-1]

An empty transformation list preserves every input value and its position.

Constraints

  • 0 <= values.length <= 256.
  • -1000000 <= values[i] <= 1000000.
  • 0 <= transforms.length <= 20; every row contains exactly two integers [a,b].
  • -2 <= a <= 2 and -1000 <= b <= 1000.
  • All inputs satisfy these rules. Every intermediate value and result fits a signed 64-bit integer.

More ByteDance problems

See ByteDance hiring insights
public long[] transformArray(int[] values, int[][] transforms) {
    // write your code here
}
values[1,-2,0]
transforms[[2,3],[-1,4]]
expected[-1,5,1]
Checking account…