public class Solution {
public String optimalDivision(int[] nums) {
StringBuilder builder = new StringBuilder();
builder.append(nums[0]);
for (int i = 1; i < nums.length; i++) {
if (i == 1 && nums.length > 2) {
builder.append("/(").append(nums[i]);
} else {
builder.append("/").append(nums[i]);
}
}
return nums.length > 2 ? builder.append(")").toString() : builder.toString();
}
}
O(n) very easy Java solution.



@Chengpengli You can check the leetcode article  https://leetcode.com/articles/optimaldivision/
It's about division and operator precedence. The result will always be of the forma/(b/c/d)
.While this is a really nice solution for this problem, I would still go ahead with the other solutions since that's more helpful in solving other similar problems for the interviews.

@rjtsdl Of course not. We aren't talking about integer division, we are talking about float division as written in the problem statement "Given a list of positive integers, the adjacent integers will perform the
float division
" .