Cache Shared Image Pipeline Prefixes
Problem statement
An image is a rectangular matrix of grayscale pixel values. Several requested transformation pipelines all start from the same source image. Each pipeline is an ordered list of transform names.
Execute the batch while caching every intermediate image by its complete transform prefix. If two pipelines share a prefix, compute that intermediate image only once. Supported transforms are:
ROTATE_CW: rotate the image 90 degrees clockwise.FLIP_HORIZONTAL: reverse every row.INVERT: replace each pixelxwith255 - x.ENLARGE_2X: replace each pixel by a2by2block of that value.SHRINK_2X: replace each non-overlapping2by2block with the floor of its four-value average.
Return a string report. The first string is steps=k, where k is the number of distinct non-empty prefixes actually computed. Each remaining string encodes one final image in pipeline order: comma-separate pixels within a row and separate rows with /. An empty pipeline returns the unchanged source image and adds no step.
Function
runCachedImagePipelines(image: int[][], pipelines: String[][]) → String[]Examples
Example 1
image = [[1,2],[3,4]]pipelines = [["ROTATE_CW","INVERT"],["ROTATE_CW","FLIP_HORIZONTAL"],["ROTATE_CW","INVERT"]]return = ["steps=3","252,254/251,253","1,3/2,4","252,254/251,253"]The shared rotation, rotation-plus-inversion, and rotation-plus-flip are the three distinct non-empty prefixes. The repeated first pipeline reuses its cached final image.
Example 2
image = [[0,255]]pipelines = [[],["INVERT"],["INVERT","INVERT"]]return = ["steps=2","0,255","255,0","0,255"]The empty pipeline uses the source directly. The two inversion prefixes are each computed once.
Example 3
image = [[10,20],[30,40]]pipelines = [["ENLARGE_2X","SHRINK_2X"],["ENLARGE_2X"]]return = ["steps=2","10,20/30,40","10,10,20,20/10,10,20,20/30,30,40,40/30,30,40,40"]Both requests share the enlarged image. Shrinking that nearest-neighbor enlargement reconstructs the source.
Constraints
1 <= image.length, image[i].length <= 40.- The image is rectangular, and every pixel is between
0and255. 0 <= pipelines.length <= 1000, and each pipeline contains at most20transforms.- Every
SHRINK_2Xreceives an image with even positive height and width. - Every intermediate image has at most
160000pixels.