Sort by

recency

|

936 Discussions

|

  • + 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
    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
    
  • + 0 comments

    Instead of iterating over each problem, we can check if current_page falls within the range of problem_number to last_problem_on_page.

    This approach reduces the time complexity for each page from O(k) to O(1) , where k is the maximum number of problems per page. Because it performs a single comparison per page rather than multiple comparisons for each problem on the page.

    def work_book(n, k, arr):
        special_problems = 0
        current_page = 1
    
        for chapter in range(n):
            total_problems = arr[chapter]
            problem_number = 1
    
            while problem_number <= total_problems:
                # Calculate the last problem on the current page
                last_problem_on_page = min(problem_number + k - 1, total_problems)
    
                # Check if the current page contains a special problem
                if problem_number <= current_page <= last_problem_on_page:
                    special_problems += 1
    
                # Move to the next page and update the problem number
                current_page += 1
                problem_number = last_problem_on_page + 1
    
        return special_problems
    
  • + 0 comments

    Haskell

    module Main where
    
    import Data.List.Split (chunksOf)
    
    solve :: Int -> [Int] -> Int
    solve k arr =
        length -- count the True
            . filter id -- only the Trues
            . zipWith elem [1 ..] -- is the page number in the page?
            $ concatMap (chunksOf k . flip take [1 ..]) arr -- list of pages' contents
    
    main :: IO ()
    main = do
        [n, k] <- map read . words <$> getLine :: IO [Int]
        arr <- map read . words <$> getLine :: IO [Int]
        print $ solve k arr
    
  • + 0 comments

    O(n) where n number of pages

    def workbook(n, k, arr):
        m=1
        n=1
        o=0
        acc=0
        while (o != len(arr)):
            le = min(m+k-1, arr[o])
            if (m <= n and n <= le):
                acc+=1
            m = le + 1
            if (m > arr[o]):
                o+=1
                m=1
            n+=1
        return acc