Sort by

recency

|

939 Discussions

|

  • + 0 comments

    Typescript / Javascript solution:

    function workbook(n: number, k: number, arr: number[]): number {
        let usedPages = 0;
        let specialCount = 0;
        for (let i = 0; i < arr.length; i++) {
            const numOfQuestions = arr[i];
            const pagesNeeded = Math.ceil(numOfQuestions / k);
            for (let j = 0; j < pagesNeeded; j++) {
                 const pageNum = usedPages + j + 1;
                 const startIndex = j * k + 1;
                 const endIndex = Math.min(startIndex + k - 1, numOfQuestions);
                 if (pageNum >= startIndex && pageNum <= endIndex) {
                    specialCount++;
                 }
            } 
            usedPages += pagesNeeded;
        }
        return specialCount;
    }
    
  • + 0 comments
    int count = 0, p_r_p_c = 1; //p_r_p_c = page required per chapter
        for(int i = 0; i< arr.size();i++){
            int n = 1;
            while(n <= arr[i]){
                if(n == p_r_p_c) count++;
                
                if(n%k == 0 && n != arr[i]) p_r_p_c++;
                
                n++;
            }
            p_r_p_c++;
        }
        return count;
    
  • + 0 comments

    Here is my c++ solution , you can watch the explanation here : https://youtu.be/3L-haDbsjAg

    int workbook(int n, int k, vector<int> arr) {
        int page = 1, chapter = 1, fe = 1, res = 0;
        while(chapter <= arr.size()){
            int le = min(fe + k - 1, arr[chapter-1]);
            if(fe <= page && page <= le) res++;
            fe = le + 1;
            if(fe > arr[chapter-1]) {
                chapter++;
                fe = 1;
            }
            page ++;
        }   
        return res;
    }
    
  • + 0 comments
    def workbook(n, k, arr):
        book=[]
        ans=0
        for i in range(n):
            for h in range(arr[i]//k):
                book.append(list(range(h*k+1,h*k+k+1)))
            if arr[i]%k!=0:
                book.append(list(range((arr[i]//k)*k+1,arr[i]+1)))
        print(book)
        for d in range(len(book)):
            if d+1 in book[d]:
                ans+=1
        return ans
    
  • + 0 comments
    book = []
    page = -1
    ans = 0
    
    for i in range(n):
        for j in range(arr[i]):
            if j % k == 0:
                page += 1
                book.append([])
            book[page].append(j+1)
            if page == j:
                ans += 1
    return ans