[JavaScript] leetCode : Valid Mountain Array
728x90

๋ฌธ์ œ

Given an array of integers arr, return true if and only if it is a valid mountain array.
Recall that arr is a mountain array if and only if

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

๋ฐฐ์—ด์ด ์‚ฐ์ฒ˜๋Ÿผ ์˜ค๋ฆ„์ฐจ์ˆœ-๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ boolean์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ.
์กฐ๊ฑด์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 3 ์ด์ƒ์ด์–ด์•ผ ํ•˜๊ณ , ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ์˜ ๊ธฐ์ ์ด ๋˜๋Š” i์˜ ๊ฐ’์ด ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.
๋ฌธ์ œ์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ์•„๋ž˜ ์‚ฌ์ง„์„ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋” ์‰ฝ๋‹ค.

ํ’€์ด

const validMountainArray = function(arr) {
 // ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 3๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ฐ˜ํ™˜ํ•œ๋‹ค
  if (arr.length < 3) return false;

  // ์ขŒ,์šฐ ํฌ์ธํ„ฐ ํ•˜๋‚˜์”ฉ ๋‘”๋‹ค.
  let left = 0;
  let right = arr.length - 1;

  // ๋ฃจํ”„ 1: ์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ์ง€ ํ™•์ธ
  while (arr[left] < arr[left + 1]) {
      left++;
  }

  // ๋ฃจํ”„ 2: ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ์ง€ ํ™•์ธ
  while (arr[right] < arr[right - 1]) {
      right--;
  }

  return left === right && left !== 0 && right !== arr.length - 1;

};

ํฌ์ธํ„ฐ๋ฅผ ๋‹ค์–‘ํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„ ์ •๋ฆฌ๋ฅผ ํ•ด๋‘๋ ค๊ณ  ํ•œ๋‹ค.

์šฐ์„  ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 3๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋”์ด์ƒ ์ง„ํ–‰ ๋˜์ง€ ์•Š๊ณ  false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋‹ค.

์ฒ˜์Œ์—” ์ค‘๊ฐ„๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋”ฐ์ ธ๋ณด๋ ค ํ–ˆ๋Š”๋ฐ ์ž˜ ๋˜์ง€ ์•Š์•„์„œ ์–‘์ชฝ ๋์— ํฌ์ธํ„ฐ๋ฅผ ๋‘์–ด์„œ
์‚ฐ์ฒ˜๋Ÿผ ๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

์™ผ์ชฝ ํฌ์ธํ„ฐ๋Š” ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ์ง€,
์˜ค๋ฅธ์ชฝ ํฌ์ธํ„ฐ๋Š” ์—ญ์ˆœ์œผ๋กœ ๊ฐ€๋ฉฐ ์˜ค๋ฆ„์ฐจ์ˆœ์ธ์ง€ ํ™•์ธํ•˜๋ฉฐ ๋ถ€ํ•ฉํ•œ๋‹ค๋ฉด ์ธ๋ฑ์Šค๋ฅผ ๋”ํ•˜๊ฑฐ๋‚˜ ์ฐจ๊ฐํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด [0,3,4,2,1] ์ด๋ผ๋Š” ๋ฐฐ์—ด์ด ์žˆ๋‹ค๊ณ  ํ•˜์ž.

์–‘ ๋์— ํฌ์ธํ„ฐ๋ฅผ ๋‘๊ณ  ์ฒซ๋ฒˆ์งธ while๋ฌธ์— ๋“ค์–ด๊ฐ€๋ฉด, ๋ฐฐ์—ด์˜ ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’๋ณด๋‹ค ๋‹ค์Œ ์ธ๋ฑ์Šค์˜ ๊ฐ’์ด ํฌ๋‹ค๋ฉด ํฌ์ธํ„ฐ๋Š” ๊ณ„์† ๋‹ค์Œ ์ธ๋ฑ์Šค๋กœ ์ด๋™ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” arr[0]์ธ 0๋ณด๋‹ค arr[1]์ธ 3์ด ํฌ๋‹ˆ left๋Š” ์ฆ๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค.

๋”์ด์ƒ ๋ฐฐ์—ด์˜ ๋‹ค์Œ ์ธ๋ฑ์Šค ๊ฐ’์ด ํฌ์ง€ ์•Š์€ ์ง€์ ์—์„œ ๋น ์ ธ๋‚˜์˜จ๋‹ค.

์‚ฐ์ด๋ผ๋ฉด ๊ผญ๋Œ€๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

์ด์–ด์„œ ๋‘๋ฒˆ์งธ while๋ฌธ์€ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

๋ฐฐ์—ด์˜ ์ด์ „ ์ธ๋ฑ์Šค ๊ฐ’์ด ๋” ํฌ๋‹ค๋ฉด ํฌ์ธํ„ฐ๋ฅผ ๊ฐ์†Œ์‹œ์ผœ ์ด์ „ ์ธ๋ฑ์Šค๋กœ ํ–ฅํ•œ๋‹ค.

๊ทธ๋Ÿผ ๋‘ ํฌ์ธํ„ฐ๋Š” ๊ฐ€์žฅ ํฐ ๊ฐ’์ธ ์ง€์ ์—์„œ ๋งŒ๋‚˜์„œ ๋ฉˆ์ถ”๊ฒŒ ๋œ๋‹ค.

์ด ๋•Œ์˜ left๋Š” 2, right๋„ 2.

์ฒ˜์Œ์—๋Š” ๋‘ ํฌ์ธํ„ฐ์˜ ๊ฐ’์ด ๊ฐ™์„ ๋•Œ๋งŒ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋Š”๋ฐ,

ํฌ์ธํ„ฐ๊ฐ€ ์›€์ง์ด์ง€ ์•Š์€ ๊ฒฝ์šฐ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ํฌ์ธํ„ฐ๊ฐ€ ์ดˆ๊ธฐ์ƒํƒœ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ์กฐ๊ฑด์— ํฌํ•จ์‹œ์ผฐ๋‹ค.
ํˆฌํฌ์ธํ„ฐ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n).

 

 

๋‹ค๋ฅธ ํ’€์ด

var validMountainArray = function(arr, edge = 0) {
    for (let i = 1; i < arr.length-1; i++) {
        if (arr[i-1] < arr[i] && arr[i] > arr[i+1]) edge++;
        if (arr[i-1] >= arr[i] && arr[i] <= arr[i+1]) return false;
    }
    return edge == 1;
};

ํ•˜๋‚˜์˜ for๋ฌธ์„ ํ†ตํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.
edge๋ผ๋Š” ์ดˆ๊ธฐ๊ฐ’์ด 0์ธ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด์คฌ๋Š”๋ฐ, ๊ฐ€์žฅ ๊ผญ๋Œ€๊ธฐ์— ์žˆ๋Š” ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ์ฐพ๋Š” ์ฝ”๋“œ์ด๋‹ค.
๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ด์ „ ๊ฐ’์€ ๋” ์ž‘๊ณ , ์ดํ›„๊ฐ’๋„ ์ž‘์„ ๋•Œ๋ฅผ ์ฐพ์•„ edge๋ฅผ ๋”ํ•ด์ค€๋‹ค.
edge๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š”๊ฑด ๊ฒฐ๊ตญ ์‚ฐ ๋ชจ์–‘์ด ๋˜๋Š” ๊ธฐ์ ๊ฐ’์ด ์žˆ๋‹ค๋Š” ๊ฒƒ!

๋ฉ‹์ง„ ๋ฐœ์ƒ์ธ ๊ฒƒ ๊ฐ™๋‹ค.

 

์›๋ฌธ์€ ์•„๋ž˜์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

https://leetcode.com/problems/valid-mountain-array/discuss/1717503/Javascript-easy-simple-solution-5-lines-of-code-O(N)-time-O(1)-space

728x90

'๐Ÿ““ Algorithm > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] leetCode : Longest Common Prefix  (0) 2022.05.09
[JavaScript] leetCode : Linked List Cycle II  (3) 2022.05.03
[JavaScript] LeetCode : Add Binary  (0) 2022.04.22
[JavaScript] LeetCode : Plus One  (0) 2022.04.21
[JavaScript] LeetCode : Largest Number At Least Twice of Others  (1) 2022.04.20