Created
February 2, 2018 06:56
-
-
Save jianminchen/ce4f7422862704bd6dc278c451fd6346 to your computer and use it in GitHub Desktop.
Float number and operators - mock interview - main bug is the max/ min value - Feb. 1, 2018 10:00 PM mock interview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given a list of float numbers, and four operators +, -, *, / with | |
flat preference, find the maximum value by inserting operator | |
between each consecutive pair of numbers. | |
For example, given the array [1, 12, -3], the maximum number 33 can | |
be found using 1 - 12 * (-3), since all operators have flat preference, | |
so 1 - 12 * (-3) will be handled from left to right, first operation is | |
1 - 12 with value -11, and then second operation is -11 * (-3) = 33. | |
class Solution { | |
public static void main(String[] args) { | |
} | |
public int findMax(float[] nums) { | |
if (nums.length == 0) | |
return 0; | |
return calc(nums[0], nums, 1); | |
} | |
private float calc(float inter, float[] nums, int idx) { | |
if (idx == nums.length) | |
return inter; | |
return Math.max(calc(inter + nums[idx], nums, idx+1) | |
calc(inter - nums[idx], nums, idx+1), | |
calc(inter * nums[idx], nums, idx+1), | |
calc(inter / nums[idx], nums, idx+1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment