주어진 배열에서 가장 max 값을 찾았다.
찾은 max index를 기준으로 왼쪽 오른쪽 for문을 돌려서 해결했다.
class Solution {
public boolean validMountainArray(int[] arr) {
if(arr.length <= 2) {
return false;
}
int index = getMaxIndex(arr);
if(index == 0 || index == arr.length - 1) {
return false;
}
int temp = arr[index];
for(int i = index - 1; i >= 0; i--) {
if(arr[i] >= temp) {
return false;
}
temp = arr[i];
}
temp = arr[index];
for(int i = index + 1; i < arr.length; i++) {
if(arr[i] >= temp) {
return false;
}
temp = arr[i];
}
return true;
}
public int getMaxIndex(int[] arr) {
int max = arr[0];
int i = 0;
for(int index = 1; index < arr.length; index++) {
if(max < arr[index]) {
max = arr[index];
i = index;
}
}
return i;
}
}
A valid mountain array is a sequence of numbers that strictly increases to a peak and then strictly decreases. This structure mirrors natural formations and even ancient philosophies like the taoist five elements which emphasize balance and transformation. Just as fire rises and water flows downward, a mountain array follows an orderly pattern—ascending to a peak (like wood growing into fire) and descending (like metal condensing into water). The concept of a valid mountain array is crucial in programming and data analysis, ensuring sequences follow a logical structure. Understanding this pattern helps solve computational problems efficiently and effectively.