Similar Problems

Similar Problems not available

Categorize Box According To Criteria - Leetcode Solution

Companies:

LeetCode:  Categorize Box According To Criteria Leetcode Solution

Difficulty: Easy

Topics: math  

Problem Statement:

Given a list of n boxes, where each box can be represented as a list of three integers [length, width, height]. Implement a function that categorizes the boxes based on the following criteria:

  1. If the box can fit inside another box (i.e., all three dimensions of the box are strictly less than all three dimensions of another box), it is considered a "small box".
  2. If the box can be placed on top of another box (i.e., the length and width of the box are strictly less than the length and width of another box, but the height is greater or equal), it is considered a "medium box".
  3. If the box cannot fit inside or be placed on top of another box, it is considered a "large box".

The output should be three lists of box indices, one for each type of box.

Example:

Input: boxes = [[1, 2, 3], [3, 2, 1], [2, 3, 4], [4, 3, 2]] Output: [[0, 1], [2], [3]]

Solution:

To solve the problem, we first need to iterate over all the boxes and check for the criteria and categorize them accordingly. We can create three empty lists - small_boxes, medium_boxes, large_boxes - to store the indices of the boxes based on the criteria. Then, for each box, we can iterate over all the other boxes and check if it satisfies any of the criteria.

For the first criteria, we need to check if all three dimensions of the box are strictly less than all three dimensions of another box. We can use a nested loop for this and compare the dimensions of each box. If a box satisfies this criteria, we can add its index to the small_boxes list and break the loop since it cannot satisfy any other criteria.

For the second criteria, we need to check if the length and width of the box are strictly less than the length and width of another box, but the height is greater or equal. Again, we can use a nested loop for this and compare the dimensions of each box. If a box satisfies this criteria, we can add its index to the medium_boxes list and break the loop since it cannot satisfy the third criteria.

For the third criteria, we need to check if a box does not satisfy any of the previous criteria. We can add its index to the large_boxes list.

Finally, we can return the three lists of box indices.

Time Complexity:

The time complexity of the solution is O(n^2), where n is the number of boxes. We need to iterate over all the boxes and compare each box with every other box.

Space Complexity:

The space complexity of the solution is O(n), where n is the number of boxes. We need to store the indices of the boxes in the three separate lists.

Code:

Here is the Python code for the solution:

def categorize_boxes(boxes): small_boxes = [] medium_boxes = [] large_boxes = [] n = len(boxes) for i in range(n): for j in range(n): if i == j: continue if all([boxes[i][k] < boxes[j][k] for k in range(3)]): small_boxes.append(i) break elif boxes[i][0] < boxes[j][0] and boxes[i][1] < boxes[j][1] and boxes[i][2] >= boxes[j][2]: medium_boxes.append(i) break else: large_boxes.append(i) return [small_boxes, medium_boxes, large_boxes]

Test the solution

boxes = [[1, 2, 3], [3, 2, 1], [2, 3, 4], [4, 3, 2]] print(categorize_boxes(boxes)) # Output: [[0, 1], [2], [3]]

Categorize Box According To Criteria Solution Code

1