#98. 孤立元素替换

    ID: 98 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatArray-2gesp3一维数组字符串

孤立元素替换

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 valval. 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 22. The element 22 at index 11. Its left is 11, and its right is 33. Both 11 and 33 are different from 22, so 22 is alone. max(1,3)=3max(1, 3) = 3. Replace 22 with 33. The resulting array is [1, 3, 3].

Sample 2: For array [1, 2, 3, 2, 5, 2] and value 22.

  1. 22 at index 11: Left 11, right 33. Alone. max(1,3)=3max(1, 3) = 3. Array becomes [1, 3, 3, 2, 5, 2].
  2. 22 at index 33: Left 33, right 55. Alone. max(3,5)=5max(3, 5) = 5. Array becomes [1, 3, 3, 5, 5, 2].
  3. 22 at index 55: No right element. Not alone. The resulting array is [1, 3, 3, 5, 5, 2].

Sample 3: For array [3, 4] and value 33. 33 at index 00: No left element. Not alone. The resulting array is [3, 4].

Constraints

Time limit is 11 second and memory limit is 10241024 KiB for each test case.