Sort by

recency

|

946 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-lisas-workbook-problem-solution.html

  • + 0 comments

    Here is my c++ solution

    int workbook(int n, int k, vector arr) { vector> pages; int specialCount = 0;

    for (int chapter = 0; chapter < n; chapter++) {
        int problems = arr[chapter];
        int problemNum = 1;
    
        while (problemNum <= problems) {
            int end = min(problemNum + k - 1, problems);
            vector<int> page;
    
            for (int i = problemNum; i <= end; i++) {
                page.push_back(i);
            }
    
            pages.push_back(page);
            problemNum = end + 1;
        }
    }
    
    
    for (int i = 0; i < pages.size(); i++) {
        int pageNumber = i + 1;
        for (int j=0;j<pages[i].size();j++) {
            if (pages[i][j] == pageNumber) {
                specialCount++;
            }
        }
    }
    
    return specialCount;
    

    }

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

    Perl:

    sub workbook {
        my ($n, $k, $arr) = @_;
    
        my @tmp;
        my $cnt = 0;
        my $j = 0;
        for (my $i = 0; $i < scalar(@$arr); $i++) {
            if ($arr->[$i] > $k) {
                for ($j = 1; $j <= $arr->[$i] - ($arr->[$i] % $k); $j += $k) {
                    push(@tmp, [ $j..$j + $k - 1]);
                }
                push(@tmp, [$j..$arr->[$i]]) if ($arr->[$i] % $k != 0);
            } else { push(@tmp, [1..$arr->[$i]]); }
            
        }
        for (my $i = 0; $i <scalar(@tmp); $i++) {
            $cnt += grep { $_ == $i + 1} @{$tmp[$i]};
        }
        
        return $cnt;
    }
    
  • + 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