Advertisement
Curious33

Day2#2 solution

Dec 2nd, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function isSafeRow(data: number[]) {
  2.     let inc = 0,
  3.         dec = 0
  4.     for (let i = 0; i < data.length - 1; i++) {
  5.         if (data[i] > data[i + 1]) dec++
  6.         else if (data[i] < data[i + 1]) inc++
  7.  
  8.         if (Math.abs(data[i] - data[i + 1]) > 3) {
  9.             return false
  10.         }
  11.     }
  12.  
  13.     const len = data.length - 1
  14.     if (inc === len || dec === len || inc === len - 1 || dec === len - 1) {
  15.         return true
  16.     }
  17.  
  18.     return false
  19. }
  20.  
  21. function getTotalSafeRow(arr: Array<number[]>) {
  22.     let total = 0
  23.     for (let i = 0; i < arr.length; i++) {
  24.         if (isSafeRow(arr[i])) {
  25.             total++
  26.         }
  27.     }
  28.  
  29.     return total
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement