@@ -9,60 +9,58 @@ Credits:Special thanks to @ifanchu for adding this problem and creating all test
9
9
*/
10
10
11
11
int robx (int * nums , int numsSize , int * t ) {
12
- int k1 = 0 , k2 = 0 ;
13
-
14
- if (numsSize == 1 ) return nums [0 ];
15
- if (numsSize == 2 ) return nums [0 ] > nums [1 ] ? nums [0 ] : nums [1 ];
16
-
17
- if (t [2 ] == -1 ) {
18
- t [2 ] = robx (& nums [2 ], numsSize - 2 , & t [2 ]);
19
- }
20
- k1 = nums [0 ] + t [2 ];
21
- if (t [1 ] == -1 ) {
22
- t [1 ] = robx (& nums [1 ], numsSize - 1 , & t [1 ]);
23
- }
24
- k2 = t [1 ];
25
-
26
- return k1 > k2 ? k1 : k2 ;
12
+ int k1 = 0 , k2 = 0 ;
13
+
14
+ if (numsSize == 1 ) return nums [0 ];
15
+ if (numsSize == 2 ) return nums [0 ] > nums [1 ] ? nums [0 ] : nums [1 ];
16
+
17
+ if (t [2 ] == -1 ) {
18
+ t [2 ] = robx (& nums [2 ], numsSize - 2 , & t [2 ]);
19
+ }
20
+ k1 = nums [0 ] + t [2 ];
21
+ if (t [1 ] == -1 ) {
22
+ t [1 ] = robx (& nums [1 ], numsSize - 1 , & t [1 ]);
23
+ }
24
+ k2 = t [1 ];
25
+
26
+ return k1 > k2 ? k1 : k2 ;
27
27
}
28
-
28
+
29
29
int rob (int * nums , int numsSize ) {
30
30
#if 0
31
- int k = 0 ;
32
- int * t ;
33
-
34
- if (numsSize == 0 ) return 0 ;
35
-
36
- t = malloc (numsSize * sizeof (int ));
37
- if (!t ) return 0 ;
38
- memset (t , -1 , numsSize * sizeof (int ));
39
-
40
- k = robx (nums , numsSize , t );
41
-
42
- free (t );
43
-
44
- return k ;
31
+ int k = 0 ;
32
+ int * t ;
33
+
34
+ if (numsSize == 0 ) return 0 ;
35
+
36
+ t = malloc (numsSize * sizeof (int ));
37
+ if (!t ) return 0 ;
38
+ memset (t , -1 , numsSize * sizeof (int ));
39
+
40
+ k = robx (nums , numsSize , t );
41
+
42
+ free (t );
43
+
44
+ return k ;
45
45
#else
46
- int i ;
47
- int rob_p_p , rob_p , dont_rob_p ;
48
- int rob_this , dont_rob_this ;
49
-
50
- rob_p_p = rob_p = dont_rob_p = 0 ;
51
- rob_this = dont_rob_this = 0 ;
52
-
53
- for (i = 0 ; i < numsSize ; i ++ ) {
54
- rob_this = nums [i ] + ((rob_p_p > dont_rob_p ) ? rob_p_p : dont_rob_p );
55
- dont_rob_this = (rob_p > dont_rob_p ) ? rob_p : dont_rob_p ;
56
- rob_p_p = rob_p ;
57
- rob_p = rob_this ;
58
- dont_rob_p = dont_rob_this ;
59
- }
60
-
61
- return rob_this > dont_rob_this ? rob_this : dont_rob_this ;
46
+ int i ;
47
+ int rob_p , dont_rob_p ;
48
+ int rob_this , dont_rob_this ;
49
+
50
+ rob_p = dont_rob_p = 0 ;
51
+ rob_this = dont_rob_this = 0 ;
52
+
53
+ for (i = 0 ; i < numsSize ; i ++ ) {
54
+ rob_this = nums [i ] + dont_rob_p ;
55
+ dont_rob_this = (rob_p > dont_rob_p ) ? rob_p : dont_rob_p ;
56
+ rob_p = rob_this ;
57
+ dont_rob_p = dont_rob_this ;
58
+ }
59
+
60
+ return rob_this > dont_rob_this ? rob_this : dont_rob_this ;
62
61
#endif
63
62
}
64
63
65
-
66
64
/*
67
65
Difficulty:Easy
68
66
Total Accepted:145.8K
0 commit comments