LeetCode Question · Apr 2024

Amazon OA

4 upvotes 2,063 views 7 replies

Question Details

public static int minimumNumberOfPages(List pages, int days) { \tint max_page_day = Integer.MIN_VALUE; \tfor(int i = 0; i < pages.sizes(); i++) \t{ \t\tif(max_page_day < pages.get(i)) \t\t{ \t\t\tmax_page_day = pages.get(i); \t\t} \t} \tint low = 1; \tint high = max_page_day; \t \tint current_days = 0; \tint...

Full Details

image
image
image
image

public static int minimumNumberOfPages(List<Integer> pages, int days)
{
\tint max_page_day = Integer.MIN_VALUE;
\tfor(int i = 0; i < pages.sizes(); i++)
\t{
\t\tif(max_page_day < pages.get(i))
\t\t{
\t\t\tmax_page_day = pages.get(i);
\t\t}
\t}
\tint low = 1;
\tint high = max_page_day;
\t
\tint current_days = 0;
\tint actual_days = Integer.MAX_VALUE;
\t
\twhile(low <= high)
\t{
\t\tint mid = low + (high-low)/2;
\t\t
\t\tint current_days = getDays(pages, mid);
\t\t
\t\tif(current_days <= days)
\t\t{
\t\t\tactual_days = Math.min(current_days, mid);
\t\t\thigh = mid-1;
\t\t}
\t\telse
\t\t{
\t\t\tlow = mid +1;
\t\t}
\t}
\t
\treturn actual_days == Integer.MAX_VALUE ? -1 : actual_days;
}

public static int getDays(List<Integer> pages, int capacity)
{
\tint total = 0;
\tint i = 0;
\twhile( i < pages.sizes())
\t{
\t\tint curr = pages.get(i);
\t\twhile( curr > 0)
\t\t{
\t\t\ttotal ++;
\t\t\tcurr -= capacity;
\t\t}
\t\ti++;
}
\t\t

About This Question

This is a reported interview question from a amazon interview for a swe role during the oa round reported in 2024.

It covers the following topics: General Experience .