Created
January 31, 2018 06:34
-
-
Save jianminchen/b5d2ca54b9805371da247a113de026a9 to your computer and use it in GitHub Desktop.
float number and operator -
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
using System; | |
// To execute C#, please define "static void Main" on a class | |
// named Solution. | |
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. | |
Peer's analysis and code: | |
arr.Length-1 | |
float[] GetMax(float[] arr , int len) | |
arr ==null || len==0 :- return 0; | |
if len==1 return arr[0]; | |
float[] next = GetMax(arr, len-1); | |
float max = Double.MinValue; | |
float max = Double.MinValue; | |
max = Math.Max(max, next + arr[len]); | |
max = Math.Max(max, next - arr[len]); | |
max = Math.Max(max, next * arr[len]); // 1 - 12 | |
max = Math.Max(max, next / arr[len]); | |
return max; | |
[1,12,-3,-4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment