33// #Hard #Array #Dynamic_Programming #Divide_and_Conquer #Segment_Tree
44// #2024_06_02_Time_1927_ms_(87.75%)_Space_82.1_MB_(5.31%)
55
6- import java .util .Arrays ;
6+ import java .util .stream . Stream ;
77
88public class Solution {
9- private static int MOD = 1_000_000_007 ;
9+ private static final int MOD = 1_000_000_007 ;
1010
1111 public int maximumSumSubsequence (int [] nums , int [][] queries ) {
12- var n = nums .length ;
1312 int ans = 0 ;
1413 SegTree segTree = new SegTree (nums );
1514 for (int [] q : queries ) {
@@ -23,43 +22,44 @@ public int maximumSumSubsequence(int[] nums, int[][] queries) {
2322}
2423
2524class SegTree {
26- private class Record {
25+ private static class Record {
2726 int takeFirstTakeLast ;
2827 int takeFirstSkipLast ;
2928 int skipFirstSkipLast ;
3029 int skipFirstTakeLast ;
3130
3231 public Integer getMax () {
33- return Arrays . asList (
32+ return Stream . of (
3433 this .takeFirstSkipLast ,
3534 this .takeFirstTakeLast ,
3635 this .skipFirstSkipLast ,
3736 this .skipFirstTakeLast )
38- .stream ()
3937 .max (Integer ::compare )
4038 .orElse (null );
4139 }
4240
4341 public Integer skipLast () {
44- return Arrays . asList (this .takeFirstSkipLast , this .skipFirstSkipLast ). stream ( )
42+ return Stream . of (this .takeFirstSkipLast , this .skipFirstSkipLast )
4543 .max (Integer ::compare )
4644 .orElse (null );
4745 }
4846
4947 public Integer takeLast () {
50- return Arrays . asList (this .skipFirstTakeLast , this .takeFirstTakeLast ). stream ( )
48+ return Stream . of (this .skipFirstTakeLast , this .takeFirstTakeLast )
5149 .max (Integer ::compare )
5250 .orElse (null );
5351 }
5452 }
5553
5654 private static Record [] seg ;
57- private static int [] nums ;
55+ private final int [] nums ;
5856
5957 public SegTree (int [] nums ) {
6058 this .nums = nums ;
6159 seg = new Record [4 * nums .length ];
62- for (int i = 0 ; i < 4 * nums .length ; ++i ) seg [i ] = new Record ();
60+ for (int i = 0 ; i < 4 * nums .length ; ++i ) {
61+ seg [i ] = new Record ();
62+ }
6363 build (0 , nums .length - 1 , 0 );
6464 }
6565
0 commit comments