Last active
November 4, 2019 23:37
-
-
Save Abdillah/9b6164362d17fa825d5be74e51f28cd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: | |
For i <= k < j, A[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even; | |
OR, for i <= k < j, A[k] > A[k+1] when k is even, and A[k] < A[k+1] when k is odd. | |
That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. | |
Return the length of a maximum size turbulent subarray of A. | |
Example 1: | |
Input: [9,4,2,10,7,8,8,1,9] | |
Output: 5 | |
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5]) | |
Example 2: | |
Input: [4,8,12,16] | |
Output: 2 | |
Example 3: | |
Input: [100] | |
Output: 1 | |
Note: | |
1 <= A.length <= 40000 | |
0 <= A[i] <= 10^9 | |
*/ | |
var assert = require("assert"); | |
// [1,7,6,7,6,7,6,1,1,1,1,1,7,6,7] | |
// [1,1,7,6,7,6,7,6] | |
/** | |
* @param {number[]} A | |
* @return {number} | |
*/ | |
/** | |
* @param {number[]} A | |
* @return {number} | |
*/ | |
var maxTurbulenceSize = function (A) { | |
var getSign = (a, b) => (a === undefined || b === undefined || a == b)? 0 : (a - b > 0? 1 : -1); | |
var counter = 1, maxCounter = 1; | |
var lastsign = 0; | |
for (var idx = 0; idx < (A.length - 1); idx++) { | |
var sign = getSign(A[idx], A[idx+1]); | |
if ((lastsign == 0 || (-1 * lastsign) == sign) && sign != 0) { | |
lastsign = sign; | |
counter++; | |
} else if (sign != 0) { | |
counter = 2; | |
} else { | |
counter = 1; | |
} | |
maxCounter = Math.max(counter, maxCounter); | |
} | |
return maxCounter; | |
}; | |
assert.deepStrictEqual(maxTurbulenceSizeFaiz([9,4,2,10,7,8,8,1,9]), 5); | |
assert.deepStrictEqual(maxTurbulenceSizeFaiz([4,8,12,16]), 2); | |
assert.deepStrictEqual(maxTurbulenceSizeFaiz([100]), 1); | |
assert.deepStrictEqual(maxTurbulenceSizeFaiz([5,5,5,5,5,5]), 1); | |
// assert.equal(5, maxTurbulenceSizeFaiz([9,4,2,10,7,8,8,1,9])) | |
// [9,4,2,10,7,8,8,1,9] | |
// [9,4,2,10] [7,8,8,1,9] | |
/** | |
* @param {number[]} A | |
* @return {number} | |
*/ | |
var maxTurbulenceSizeEzzat = function(A) { | |
if (A.length === 1) return 1; | |
if (A.length === 2 && A[0] === A[1]) return 1; | |
if (A.length === 2 && A[0] !== A[1]) return 2; | |
if (A.every((el) => el === A[0])) return 1; | |
LARGER = 1; | |
SMALLER = 0; | |
ERR = 3; | |
let maxCounter = 2; | |
let counter = 2; | |
// x x v | |
// [9,4,2,10,7,8,8,1,9] | |
for (let i = 2; i < A.length; i++) { | |
let expected; | |
if (A[i - 2] > A[i - 1]) { | |
expected = LARGER; | |
} else if (A[i - 2] < A[i - 1]) { | |
expected = SMALLER; | |
} else { | |
expected = ERR; | |
} | |
if (expected === SMALLER && A[i] < A[i-1]) { | |
counter++; | |
} else if (expected === LARGER && A[i] > A[i-1]) { | |
counter++; | |
} else if (expected === SMALLER && A[i] >= A[i-1]) { | |
counter = 2; | |
} else if (expected === LARGER && A[i] <= A[i-1]) { | |
counter = 2; | |
} else if (expected === ERR) { | |
counter = 2; | |
} | |
maxCounter = Math.max(maxCounter, counter); | |
} | |
console.log(maxCounter); | |
return maxCounter; | |
}; | |
// count = 2 | |
// expected | |
// check expected from A[i-2] and A[i-1] | |
// check if current number as expected | |
// count++ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kodeku tak persimpel, tur readability ne dadi elek wkwk.