Leetcode 2525. Categorize Box According to Criteria
You are given four integers length
, width
, height
, and mass
that describe a box. The box can be categorized based on the following criteria:
The box could fall into one of the four following categories:
Write a function string categorizeBox(int length, int width, int height, int mass)
that returns the category of the box.
#include <iostream>
using namespace std;
string categorizeBox(int length, int width, int height, int mass) {
// Determine if the box is Bulky.
const long long volume = static_cast<long long>(length) * width * height; // Use long long to avoid overflow
bool isBulky = (volume >= 1000000000) || (length >= 10000) || (width >= 10000) || (height >= 10000);
// Determine if the box is Heavy.
bool isHeavy = (mass >= 100);
// Determine the category.
if (isBulky && isHeavy) {
return "Both";
} else if (isBulky) {
return "Bulky";
} else if (isHeavy) {
return "Heavy";
} else {
return "Neither";
}
}
// Sample Driver Code
int main() {
// Example cases
cout << categorizeBox(10000, 2000, 3000, 101) << endl; // Output: Both
cout << categorizeBox(10000, 2000, 300, 50) << endl; // Output: Bulky
cout << categorizeBox(10, 20, 30, 200) << endl; // Output: Heavy
cout << categorizeBox(10, 20, 30, 50) << endl; // Output: Neither
return 0;
}
The time complexity of the solution is (O(1)) since it involves a constant number of arithmetic operations and comparisons, regardless of the size of the input values. The operations themselves are basic and do not depend on input size.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?