Sort by

recency

|

943 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

    Here is my Python solution! I think it can probably be done much faster, but I was too lazy to try to create a more efficient solution.

    def workbook(n, k, arr):
        special = 0
        current = 1
        sub = 0
        for chapter in range(len(arr)):
            for problem in range(arr[chapter]):
                if sub == k:
                    current += 1
                    sub = 0
                if problem + 1 == current:
                    special += 1
                sub += 1
            if sub != 0:
                current += 1
                sub = 0
        return special
    
  • + 0 comments

    Here is my rust submission:

    It is long but this is the only way I was able to understand properly. Try running it, the println! gives very useful output for figuring the problem out.

    fn workbook(n: i32, k: i32, arr: &[i32]) -> i32 {
        let mut special_problems = 0;
        let mut page_no = 0;
    
        for chapter in 0..n {
            let mut problem_count = arr[chapter as usize];
            let mut exitLoop = false;
            let mut start = 0;
            loop {
                let mut problems_in_page: Vec<i32> = Vec::new();
                if (problem_count > k) {
                    problem_count -= k;
    
                    let range = start..start + k;
    
                    problems_in_page = range.collect();
                    start += k;
                } else {
                    let range = start..start + problem_count;
                    problems_in_page = range.collect();
                    exitLoop = true;
                }
    
                for i in &problems_in_page {
                    if page_no == *i {
                        special_problems += 1;
                    }
                }
    
                println!("Chapter: {chapter}, Page: {page_no}, Value: {problems_in_page:?}");
                page_no += 1;
                if exitLoop {
                    break;
                }
            }
        }
    
        special_problems
    }
    
  • + 0 comments

    python3

    def workbook(n, k, arr):
        # Write your code here
      
        ch = []
        
        # step 1: create chapters
        for i in arr:
            c = list(range(1,i+1))
            for j in range(1,i//k+1):
                ch.append(c[:k])
                c = c[k:] # this might add some empty chapters
            ch.append(c)
            
        ch = list(filter(lambda i: len(i) != 0, ch)) # remove empty chapters
    
        #step 2: find special problems
        count = 0
        for i,j in enumerate(ch):
            if i + 1 in j:
                count +=1
            
        return count
    
  • + 0 comments

    Python3, but iterates over all problems N = sum(arr), so it's O(N). I belive it can be done much better

    def workbook(n, k, arr):
        page_index = 1
        page_count = k
        special_count = 0
        for problems in arr:
            for problem_index in range(1, problems+1):
                if problem_index == page_index:
                    special_count += 1
                page_count -= 1
                if page_count == 0:
                    page_index += 1
                    page_count = k
            if page_count != k:
                page_count = k
                page_index += 1
        return special_count