Advertisement
shady_obeyd

05.Extract a Non-decreasing Subsequence from an Array

Jan 31st, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(nums) {
  2.     let result = [];
  3.     result.push(nums[0]);
  4.  
  5.     for (let i = 1; i < nums.length; i++) {
  6.         let currentLargestNum = Math.max(...result);
  7.  
  8.         if(currentLargestNum < nums[i]){
  9.             result.push(nums[i]);
  10.         }
  11.     }
  12.  
  13.     for(let num of result){
  14.         console.log(num);
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement