#98. 孤立元素替换
孤立元素替换
Replace Alone Elements
Background
Problem Description
We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.
Input Format
Input is given from Standard Input in the following format.
The first line contains a string representation of an integer array, with elements space-separated and enclosed in square brackets, followed by an integer . For example:
[1 2 3] 2.
Output Format
Output is printed to Standard Output in the following format.
A string representation of the modified array, with elements separated by commas and spaces, enclosed in square brackets.
Sample
[1 2 3] 2
[1, 3, 3]
[1 2 3 2 5 2] 2
[1, 3, 3, 5, 5, 2]
[3 4] 3
[3, 4]
Sample Explanation
Sample 1:
For array [1, 2, 3] and value .
The element at index . Its left is , and its right is . Both and are different from , so is alone.
. Replace with .
The resulting array is [1, 3, 3].
Sample 2:
For array [1, 2, 3, 2, 5, 2] and value .
- at index : Left , right . Alone. . Array becomes
[1, 3, 3, 2, 5, 2]. - at index : Left , right . Alone. . Array becomes
[1, 3, 3, 5, 5, 2]. - at index : No right element. Not alone.
The resulting array is
[1, 3, 3, 5, 5, 2].
Sample 3:
For array [3, 4] and value .
at index : No left element. Not alone.
The resulting array is [3, 4].
Constraints
Time limit is second and memory limit is KiB for each test case.