• + 0 comments

    my answer in ts

    function workbook(n: number, k: number, arr: number[]): number {
      let counter = 0;
    
      let page = 1;
      let chapter = 1;
      let problem = 1;
      while (chapter <= n) {
        const ps = Array(Math.min(problem + k - 1, arr[chapter - 1]) - problem + 1)
          .fill(0)
          .map((_, i) => i + problem);
    
        if (problem > arr[chapter - 1]) {
          chapter++;
          problem = 1;
          continue;
        }
    
        if (ps.includes(page)) counter++;
    
        page++;
        problem = ps[ps.length - 1] + 1;
        continue;
      }
    
      return counter;
    }