Difficulty: Easy
Jimmy puts Ann's birthday present in a cuboid box. The dimensions of its edges are positive integers and the sum of its , , and is .
Jimmy puts Ann's birthday present in a cuboid box. The dimensions of its edges are positive integers and the sum of its , , and is .
What is the maximum volume Ann's present box can have?
Input Format
A single integer, (the sum of the box's , , and ).
Constraint
Output Format
Print the maximum possible volume of the box.
Sample Input 0
4
Sample Output 0
2
Sample Input 1
8
Sample Output 1
18
Explanation
Sample 0
Here, our only possible dimensions are some combination of , , and . .
Here, our only possible dimensions are some combination of , , and . .
Sample 1
Here are all possible edge dimensions:
, .
, .
, .
, .
, .
Here are all possible edge dimensions:
, .
, .
, .
, .
, .
We print the maximum volume, which is .
Runtime Analysis:
Given the only constraint that N is between 3 and 103, we know that pretty much most algorithms besides a pure brute force can solve this problem. An entirely brute force approach O(n3), involving going over every possible length, width, height from 1 to N and saving the l * w * h when l + w + h = n will fail as 109 would cause a time out. This problem can be solved O(n2) or O(1).
Runtime: O(n2) or O(1)
Algorithmic Approach:
O(n2): This question involves some basic mathematical reasoning. There is really no need to loop for all possibly length, widths, heights. If you simply loop:
1. length from 1 to N
2. width from 1 to N - length
You can infer the height from (N - length - width). You would just have to keep track of the max value for:
Thus, making an O(n2) solution.
Java Code: O(n2)
1. length from 1 to N
2. width from 1 to N - length
You can infer the height from (N - length - width). You would just have to keep track of the max value for:
Length * width * (N - length - width)
Thus, making an O(n2) solution.
Java Code: O(n2)
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int ans = 0; for (int i=1;i<=n;i++) for (int j=1;j<=n && i+j<=n;j++) ans = Math.max(ans, i*j*(n-i-j)); System.out.println(ans); } }
O(1): If you think a bit further and walk through a couple examples of this problem with smaller numbers, you will notice that the largest volumes are always made when the length, width, and height values are as close to each other as possible. In the case that N is divisible by 3, your length, width, and height would all be equal to N/3.
In order to divide the sides as equally as possible:
Length = N/3
Width = (N - N/3) / 2
Height = (N - Length - Width)
Java Code: O(1)
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int a = n/3; int b = (n - n/3) / 2; int c = n - (a + b); System.out.println(a * b * c); } }
No comments:
Post a Comment