
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].
Example 1
Input: screen_width = 360, min_card_width = 100, gap = 10, item_count = 7
Output: [3, 113, 3]
Explanation: 3 columns fit because 3 * 100 + 2 * 10 = 320 <= 360. The final width is (360 - 20) // 3 = 113, and 7 items need 3 rows.
Example 2
Input: screen_width = 320, min_card_width = 150, gap = 20, item_count = 4
Output: [2, 150, 2]
Explanation: 2 columns fit exactly: 2 * 150 + 1 * 20 = 320.
0 <= item_count <= 10^51 <= min_card_width <= screen_width <= 10^90 <= gap <= 10^6item_count > 0.screen_width = 360, min_card_width = 100, gap = 10, item_count = 7Output[3, 113, 3]WhyThree columns fit, since `3 * 100 + 2 * 10 = 320 <= 360`, but four do not. The remaining width gives each card `113` pixels, and `7` items need `3` rows.screen_width = 320, min_card_width = 150, gap = 20, item_count = 4Output[2, 150, 2]WhyTwo columns fit exactly across the screen. With `4` items and `2` columns, the layout uses `2` rows.screen_width = 400, min_card_width = 120, gap = 16, item_count = 2Output[2, 192, 1]WhyAlthough more width is available, only `2` columns are needed because there are only `2` items. The card width becomes `(400 - 16) // 2 = 192`.0 <= item_count <= 10^51 <= min_card_width <= screen_width <= 10^90 <= gap <= 10^6At least one column is always possible when item_count > 0def optimal_layout(screen_width, min_card_width, gap, item_count):