@@ -6,46 +6,28 @@ public class Main {
6
6
7
7
public static class Solution {
8
8
9
- public int maxDistToClosest (int [] seats ) {
10
- int max = 0 ;
11
-
12
- int [] zone = new int [2 ];
13
- for (int index = 0 ; index < seats .length ; ) {
14
- Arrays .fill (zone , -1 );
15
- index = nextFree (seats , index , zone );
16
- if (zone [0 ] == 0 || zone [1 ] == seats .length - 1 ) {
17
- max = Math .max (max , zone [1 ] - zone [0 ] + 1 );
18
- } else {
19
- max = Math .max (max , (zone [1 ] - zone [0 ] + 2 ) / 2 );
20
- }
21
- }
22
-
23
- return max ;
9
+ public TreeNode constructMaximumBinaryTree (int [] nums ) {
10
+ return helper (nums , 0 , nums .length - 1 );
24
11
}
25
12
26
- private int nextFree (int [] seats , int start , int [] zone ) {
27
- boolean enter = false ;
28
- for (int i = start , j = 0 ; i <= seats .length ; i ++) {
29
- if (i < seats .length && seats [i ] == 0 ) {
30
- if (!enter ) {
31
- enter = true ;
32
- j = i ;
33
- }
34
- } else {
35
- if (enter ) {
36
- zone [0 ] = j ;
37
- zone [1 ] = i - 1 ;
38
- return i + 1 ;
39
- }
13
+ private TreeNode helper (int [] nums , int start , int end ) {
14
+ if (start > end ) {
15
+ return null ;
16
+ }
17
+ int index , max = start ;
18
+ for (index = start + 1 ; index <= end ; index ++) {
19
+ if (nums [index ] > nums [max ]) {
20
+ max = index ;
40
21
}
41
22
}
42
- return seats .length ;
23
+ TreeNode root = new TreeNode (nums [max ]);
24
+ root .left = helper (nums , start , max - 1 );
25
+ root .right = helper (nums , max + 1 , end );
26
+ return root ;
43
27
}
44
28
}
45
29
46
30
public static void main (String [] args ) {
47
- Solution solution = new Solution ();
48
- int s = solution .maxDistToClosest (new int []{1 ,0 ,0 ,0 ,1 ,0 ,1 });
49
- System .out .println (s );
31
+
50
32
}
51
33
}
0 commit comments