File tree 2 files changed +58
-0
lines changed
Contests/Weekly Contest 105
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public static int maxSubarraySumCircular (int [] A ) {
3
+ int max = kadane (A );
4
+ int wrap = 0 ;
5
+
6
+ for (int i =0 ; i < A .length ; i ++) {
7
+ wrap = wrap +A [i ];
8
+ A [i ] *= -1 ;
9
+ }
10
+
11
+ wrap = wrap + kadane (A );
12
+
13
+ return Math .abs (wrap ) < Math .abs (max ) ? max : wrap ;
14
+ }
15
+
16
+
17
+ private static int kadane (int [] a ){
18
+ int maxSoFar =a [0 ];
19
+ int maxGlobal =a [0 ];
20
+
21
+ for (int i =1 ; i <a .length ; i ++) {
22
+ maxSoFar = Math .max (a [i ], maxSoFar + a [i ]);
23
+ maxGlobal =Math .max (maxGlobal , maxSoFar );
24
+ }
25
+
26
+ return maxGlobal ;
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public static String reverseOnlyLetters (String S ) {
3
+ char [] chars = S .toCharArray ();
4
+ int start = 0 ;
5
+ int end = chars .length - 1 ;
6
+
7
+ while (start < end ) {
8
+ if (Character .isLetter (chars [start ]) && Character .isLetter (chars [end ])) {
9
+ char temp = chars [start ];
10
+ chars [start ] = chars [end ];
11
+ chars [end ] = temp ;
12
+
13
+ start ++;
14
+ end --;
15
+ }
16
+ else if (Character .isLetter (chars [start ]) && !Character .isLetter (chars [end ])) {
17
+ end --;
18
+ }
19
+ else if (!Character .isLetter (chars [start ]) && Character .isLetter (chars [end ])) {
20
+ start ++;
21
+ }
22
+ else {
23
+ start ++;
24
+ end --;
25
+ }
26
+ }
27
+
28
+ return String .valueOf (chars );
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments