ภาพรวม
เราต้องเขียนฟังก์ชันที่รับอาร์เรย์ตัวเลข และคืนค่าความยาวของซับอาร์เรย์ต่อเนื่องที่ยาวที่สุด ที่มีอิลิเมนต์เรียงเพิ่มขึ้นอย่างเคร่งครัด (Strictly Increasing)
ลำดับเพิ่มขึ้นอย่างเคร่งครัด หมายถึง อิลิเมนต์ถัดไปมากกว่าอิลิเมนต์ก่อนหน้าทุกคู่
ตัวอย่างโค้ด
const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89];
const findLongest = (arr) => {
if(arr.length === 0) {
return 0;
}
let max = 0;
let count = 0;
for(let i = 1; i < arr.length; i++) {
if(arr[i] > arr[i-1]) {
count++;
} else {
count = 0;
}
if(count > max) {
max = count;
}
}
return max + 1;
};
console.log(findLongest(arr));
ผลลัพธ์ในคอนโซล
4