Welcome to the Python screen.
The question is on your right: Greedy Mobile Grid Layout. Read through the requirements first.
Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?
At PixelFlow, a mobile screen must display identical rectangular cards in a grid. Write a function that calculates the optimal layout for a given screen width, card minimum width, gap size, and number of cards.
The goal is to place as many columns as possible while ensuring each card is at least min_card_width pixels wide. Among all valid layouts, choose the one with the maximum number of columns. Then compute the resulting card width and the number of rows needed.
Implement a function that receives:
screen_width: integer total width of the screen in pixelsmin_card_width: integer minimum allowed width for each cardgap: integer horizontal gap between adjacent cardsitem_count: integer number of cards to placeReturn a list [columns, card_width, rows] where:
columns is the chosen number of columnscard_width is the integer width of each card after distributing available space evenlyrows is the number of rows required to place all cardsIf item_count is 0, return [0, 0, 0].
def optimal_layout(screen_width, min_card_width, gap, item_count):