-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataSet.java
1173 lines (723 loc) · 24.5 KB
/
DataSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static void displayUnionAndIntersection(int[] arrayOne,int[] arrayTwo){
Set<Integer> obj = new HashSet<>();
int i,j;
for(i=0; i<arrayOne.length; i++){
obj.add(arrayOne[i]);
}
for(j=0; j<arrayTwo.length; j++){
obj.add(arrayTwo[j]);
}
System.out.println("The union of both the arrays is");
for(Integer I: obj){
System.out.print(I + " ");
}
System.out.println();
obj.clear();
System.out.println("The intersection of both the arrays is");
for(i=0; i<arrayOne.length; i++){
obj.add(arrayOne[i]);
}
for(j=0; j<arrayTwo.length; j++){
if(obj.contains(arrayTwo[j]))
System.out.print(arrayTwo[j] + " ");
}
}
public static int findMin(int[] list)
{ assert list != null && list.length > 0 : "failed precondition";
int indexOfMin = 0;
for(int i = 1; i < list.length; i++)
{ if(list[i] < list[indexOfMin])
{ indexOfMin = i;
}
}
return indexOfMin;
}
public static void badResize(int[] list, int newSize)
{ assert list != null && newSize >= 0 : "failed precondition";
int[] temp = new int[newSize];
int limit = Math.min(list.length, newSize);
for(int i = 0; i < limit; i++)
{ temp[i] = list[i];
}
// uh oh!! Changing pointer, not pointee. This breaks the
// relationship between the parameter and argument
list = temp;
}
public static void findAndPrintPairs(int[] list, int target)
{ assert list != null : "failed precondition";
for(int i = 0; i < list.length; i++)
{ for(int j = i + 1; j < list.length; j++)
{ if(list[i] + list[j] == target)
{ System.out.println("The two elements at indices " + i + " and " + j
+ " are " + list[i] + " and " + list[j] + " add up to " + target);
}
}
}
}
public static boolean isAscending( int[] list )
{ boolean ascending = true;
int index = 1;
while( ascending && index < list.length )
{ assert index >= 0 && index < list.length;
ascending = (list[index - 1] <= list[index]);
index++;
}
return ascending;
}
public static void addBonus(double[] array, double bonus)
{
for (int k = 0; k < array.length; k++)
{
array[k] = array[k] + bonus;
}
}
public static string GetSortedName(String[] names)
{
int n;
String temp;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
}
}
public static void SeperateArrayWithPosition(int a[], int loc)
{
int n, x, flag = 1, loc = 0, k = 0,j = 0;
int b[] = new int[n];
int c[] = new int[n];
for(int i = 0; i < loc; i++)
{
b[k] = a[i];
k++;
}
for(int i = loc; i < n; i++)
{
c[j] = a[i];
j++;
}
System.out.print("First array:");
for(int i = 0;i < k; i++)
{
System.out.print(b[i]+" ");
}
System.out.println("");
System.out.print("Second array:");
for(int i = 0; i < j; i++)
{
System.out.print(c[i]+" ");
}
}
}
static void segregate0and1(int array[], int size)
{
int left = 0, right = size-1;
while (left < right)
{
/* Increment left index while we see 0 at left */
while (array[left] == 0 && left < right)
left++;
/* Decrement right index while we see 1 at right */
while (array[right] == 1 && left < right)
right--;
/* If left is smaller than right then there is a 1 at left and a 0 at right. Exchange it */
if (left < right)
{
array[left] = 0;
array[right] = 1;
left++;
right--;
}
}
for(int i = 0 ; i < 6; i++)
{
System.out.print(array[i]+"\t");
}
}
public static void CountOccurrence(int a[], int x)
{
int n, x, count = 0, i = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
for(i = 0; i < n; i++)
{
if(a[i] == x)
{
count++;
}
}
System.out.println("Number of Occurrence of the Element:"+count);
}
static int[] fisherYatesShuffling(int []arr, int n)
{
int []a = new int[n];
int []ind = new int[n];
for(int i=0; i<n; i++)
ind[i] = 0;
int index;
Random rand = new Random();
for(int i=0; i<n; i++)
{
do
{
index = rand.nextInt(n);
} while(ind[index] != 0);
ind[index] = 1;
a[i] = arr[index];
}
return a;
}
public void FermatFactor(long N)
{
long a = (long) Math.ceil(Math.sqrt(N));
long b2 = a * a - N;
while (!isSquare(b2))
{
a++;
b2 = a * a - N;
}
long r1 = a - (long)Math.sqrt(b2);
long r2 = N / r1;
display(r1, r2);
}
/** func
public int[] RotatedArray(int[] A, int K) {
int [] rotatedA = new int[A.length];
for(int i=0; i<A.length; i++) {
//rotated index needs to "wrap" around end of array
int rotatedIndex = (i + K) % A.length;
rotatedA[rotatedIndex] = A[i];
}
return rotatedA;
}
public int MissingElement(int[] A) {
int max = A.length + 1;
//load array elements into array so would be quick to check if elements exist
Set incompleteSet = new HashSet();
for(int i=0; i<A.length; i++) {
incompleteSet.add(A[i]);
}
for(int i=1; i<max+1; i++) {
if(!incompleteSet.contains(i)) {
return (i);
}
}
throw new RuntimeException("shouldn't reach here");
}
public int MinSumOfTwoSubArray(int[] A) {
long sumAllElements = 0;
for(int i=0; i<A.length; i++) {
sumAllElements += A[i];
}
int minDifference = Integer.MAX_VALUE;
int currentDifference = Integer.MAX_VALUE;
long sumFirstPart = 0;
long sumSecondPart = 0;
for(int p=0; p<A.length-1; p++) {
sumFirstPart += A[p];
sumSecondPart = sumAllElements - sumFirstPart;
currentDifference = (int) Math.abs(sumFirstPart - sumSecondPart);
minDifference = Math.min(currentDifference, minDifference);
}
return minDifference;
}
public int[] MaxCounters(int N, int[] A) {
int [] counters = new int[N];
int maxCounter = 0; //for the next re-set will know what high score to set all values
int lastResetCounter = 0; //for setting values that were never explicitly set - at the end
for(int i=0; i<A.length; i++) {
if(A[i] <= N) {
if(counters[A[i]-1] < lastResetCounter) { counters[A[i]-1] = lastResetCounter; //bring it up to last reset value } counters[A[i]-1]++; //store max counter in case need to set all counters to this value on next reset if(counters[A[i]-1] > maxCounter) {
maxCounter = counters[A[i]-1];
}
}
else {
//keep track of last reset counter
lastResetCounter = maxCounter;
}
}
//set all values to last reset value that was never explicitly changed after last reset
for(int i=0; i<counters.length; i++) {
if(counters[i] < lastResetCounter) {
counters[i] = lastResetCounter;
}
}
return counters;
}
public int PassingCars(int[] A) {
int zeros = 0;
int carPasses = 0;
for(int i=0; i<A.length; i++) { if(A[i] == 0) { zeros++; } else if(A[i] == 1)
{
//for every 1 - there will be an extra car pass for ALL the 0's that came before it carPasses += zeros; if(carPasses > 1000000000) {
return -1;
}
}
else throw new RuntimeException("shouldn't reach here");
}
return carPasses;
}
public int[] GenomeRangeQuery(String S, int[] P, int[] Q) {
int [] answers = new int[P.length];
int stringLength = S.length();
int [][] occurrences = new int [stringLength][4];
//step 1 - for each row, count occurrences of each nucleotide (can only have 1 occurrence / row)
//e.g. if S=CAGCCTA array will be
//{
// {0,1,0,0} C
// {1,0,0,0} A
// {0,0,1,0} G
// {0,1,0,0} C
// {0,1,0,0} C
// {0,0,0,1} T
// {1,0,0,0} A
// }
for(int i=0; i<occurrences.length; i++) {
char c = S.charAt(i);
if(c == 'A') occurrences[i][0] = 1;
else if(c == 'C') occurrences[i][1] = 1;
else if(c == 'G') occurrences[i][2] = 1;
else if(c == 'T') occurrences[i][3] = 1;
}
//step 2 - for each row (starting from 2nd row), add up occurrences of each nucleotide by adding
//occurrences from previous row to current row
//now have running sum of each nucleotide for any row
//e.g. if S=CAGCCTA array will be
//{
// {0,1,0,0} C
// {1,1,0,0} A
// {1,1,1,0} G
// {1,2,1,0} C
// {1,3,1,0} C
// {1,3,1,1} T
// {2,3,1,1} A
// }
for(int i=1; i<stringLength; i++) {
for(int j=0; j<4; j++) {
occurrences[i][j] += occurrences[i-1][j];
}
}
//check each slice for min by simple subtraction
for(int i=0; i<P.length; i++) {
int index1 = P[i];
int index2 = Q[i];
for(int j=0; j<4; j++) { int lowerIndexCount = 0; //when index1 not at beginning of String - need to get occurrences from just before //beginning of slice - to see if that nucleotide occurred within slice //e.g. if slice is (2, 4), need to check for occurrences of A, C, G, T from index 1 to 4 if(index1-1 >= 0) {
lowerIndexCount = occurrences[index1-1][j];
}
if(occurrences[index2][j] - lowerIndexCount > 0) {
answers[i] = j+1; //nucleotide value is 1 more than loop value (A=1, C=2, G=3, T=4)
//no need to keep checking since always checking from smallest impact factor
//as soon as find occurrence, that has to be minimum, cause subsequent nucleotides have
//larger impact factor
break;
}
}
}
return answers;
}
public int MinAverageTwoSlice(int[] A) {
//main idea: will find min average by checking only 2 and 3 contiguous elements at a time
int sum1, sum2 = 0;
double minAverage = Double.MAX_VALUE;
double currentAverage1 = Double.MAX_VALUE;
double currentAverage2 = Double.MAX_VALUE;
int minAverageSliceIndex = 0; //for size 2 arrays, this will always be true
//if array is > 2 elements
for(int i=0; i<A.length-2; i++) {
sum1 = A[i] + A[i+1];
currentAverage1 = sum1 / 2.0d;
if(currentAverage1 < minAverage) {
minAverage = currentAverage1;
minAverageSliceIndex = i;
}
sum2 = sum1 + A[i+2];
currentAverage2 = sum2 / 3.0d;
if(currentAverage2 < minAverage) {
minAverage = currentAverage2;
minAverageSliceIndex = i;
}
}
//check last 2 contiguous elements from the end - they won't otherwise be checked because
//when checking 2 and 3 contiguous elements at a time, will stop checking 3 elements from the end
currentAverage1 = (A[A.length-2] + A[A.length-1]) / 2.0d;
if(currentAverage1 < minAverage) {
minAverage = currentAverage1;
minAverageSliceIndex = A.length-2;
}
return minAverageSliceIndex;
}
public int IsTrianglePossible(int[] A) {
if(A.length < 3) return 0;
List aList = new ArrayList();
for(int i=0; i<A.length; i++) { aList.add(A[i]); } Collections.sort(aList); //made long array because each int element can be as high as Integer.MAX_VALUE so when add them //can overflow int long [] aOrdered = new long[A.length]; int index = 0; for(Integer i : aList) { aOrdered[index++] = i; } //start from the end (largest) //if previous 2 elements have sum > current element, found a triangle
for(int i=aOrdered.length-1; i>=2; i--) {
if(aOrdered[i-1] + aOrdered[i-2] > aOrdered[i]) {
return 1;
}
}
return 0;
}
public int MaximumProfitOfStockTicker(int[] A) {
if(A.length < 2) return 0; //for empty array or 1 element array, no profit can be realized
//convert profit table to delta table so can use max slice technique
int [] deltaA = new int[A.length];
deltaA[0] = 0;
for(int i=1; i<A.length; i++) {
deltaA[i] = A[i] - A[i-1];
}
int absoluteMax = deltaA[0];
int localMax = deltaA[0];
int nextSum = 0;
int currentElement = 0;
for (int i = 1; i < deltaA.length; i++) {
currentElement = deltaA[i];
nextSum = localMax + currentElement;
localMax = Math.max(deltaA[i], nextSum);
absoluteMax = Math.max(absoluteMax, localMax);
}
if(absoluteMax > 0) return absoluteMax;
return 0;
}
public int MaxDoubleSliceSum(int[] A) {
int[] slice1LocalMax = new int[A.length];
int[] slice2LocalMax = new int[A.length];
//start from i=1 because slice can't start at index 0
for(int i = 1; i < A.length-1; i++) { slice1LocalMax[i] = Math.max(slice1LocalMax[i-1] + A[i], 0); } //start from i=A.length-2 because slice can't end at index A.length-1 for(int i = A.length-2; i > 0; i--){
slice2LocalMax[i] = Math.max(slice2LocalMax[i+1]+A[i], 0);
}
int maxDoubleSliceSum = 0;
//compute sums of all slices to find absolute max
for(int i = 1; i < A.length-1; i++) {
maxDoubleSliceSum = Math.max(maxDoubleSliceSum, slice1LocalMax[i-1] + slice2LocalMax[i+1]);
}
return maxDoubleSliceSum;
}
public int[] ArePrime(int N) {
//make size N+1 so will have direct mapping from array index
boolean [] arePrimes = new boolean[N+1];
arePrimes[0] = false; //0 is never prime
arePrimes[1] = false; //1 is never prime
for(int i=2; i<arePrimes.length; i++) {
arePrimes[i] = true;
}
int nSquareRoot = (int) Math.sqrt(N);
for(int i=2; i<=nSquareRoot; i++) {
if(arePrimes[i]) {
//start checking from i^2 because lower numbers will have already been checked
//keep checking very multiple of i
for(int j=i*i; j<=N; j+=i) {
arePrimes[j] = false;
}
}
}
List primesList = new ArrayList();
for(int i=2; i<arePrimes.length; i++) {
if(arePrimes[i]) {
primesList.add(i);
}
}
//https://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
int[] primes = primesList.stream().mapToInt(i->i).toArray();
return primes;
}
public static int lonelyinteger(int[] a) {
int result = 0;
for (int i = 0; i < a.length; i++) {
result ^= a[i];
}
return result;
}
static int minimumLoss(long[] price) {
Map<Long,Integer> map=new HashMap<Long,Integer>();
for(int i=0;i<price.length;i++) {
map.put(price[i], i);
}
Arrays.sort(price);
long min=Long.MAX_VALUE;
for(int i=price.length-1;i>0;i--) {
long diff=price[i]-price[i-1];
if( diff<min && map.get(price[i])<map.get(price[i-1])) {
min=Math.abs(diff);
}
}
return (int) min;
}
public static int ToyContainerRequired(int[] w) {
Arrays.sort(w);
int containerCouter = 1, weightLimit = w[0];
for (int i = 0; i < w.length; i++) {
if (w[i] > weightLimit + 4) {
weightLimit = w[i];
containerCouter++;
}
}
return containerCouter;
}
public static int shiftCount(int[] a) {
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i; j > 0; j--) {
if (a[j] < a[j - 1]) {
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
count++;
}
}
}
return count;
}
static int birthdayCakeCandles(int[] ar) {
int maxCandleHeight = Integer.MIN_VALUE;
int maxCandleFreqCount = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i] == maxCandleHeight) {
maxCandleFreqCount++;
}
if (ar[i] > maxCandleHeight) {
maxCandleHeight = ar[i];
maxCandleFreqCount = 1;
}
}
return maxCandleFreqCount;
}
static int[] matchingStrings(String[] strings, String[] queries) {
Map<String, Integer> map = new HashMap<>();
int result[] = new int[queries.length];
for (int i = 0; i < strings.length; i++) {
String inputString = strings[i];
if (map.containsKey(inputString)) {
map.put(inputString, map.get(inputString) + 1);
} else {
map.put(inputString, 1);
}
}
for (int i = 0; i < queries.length; i++) {
String queryString = queries[i];
if (map.containsKey(queryString)) {
result[i] = map.get(queryString);
}
}
static int divisibleSumPairs(int n, int k, int[] ar) {
int[] bucket = new int[k];
int pairCounter = 0;
for (int element : ar) {
int remainder = element % k;
int complement = (k-remainder)%k;
pairCounter += ar[complement];
bucket[remainder]++;
}
return pairCounter;
}
public static int[] replaceMiddle(int[] arr) {
if (arr.length % 2 == 0) {
int[] sol = new int[arr.length - 1];
for (int i = 0, j = 0; i < arr.length; j++) {
if (i == arr.length / 2 - 1) {
sol[j] = arr[arr.length / 2] + arr[arr.length / 2 - 1];
i += 2;
} else {
sol[j] = arr[i];
i++;
}
}
return sol;
}
return arr;
public static int largestDistance(int[] a) {
int[] mx = new int[] { a[0], a[1] };
int[] mn = new int[] { a[a.length - 2], a[a.length - 1] };
for (int i = 0; i < a.length; i++) {
int k = i % 2;
if (a[i] > mx[k]) {
mx[k] = a[i];
} else if (a[i] < mn[k]) {
mn[k] = a[i];
}
}
return Math.max(mx[0] - mn[0], mx[1] - mn[1]);
}
private static double findMedian(int [] array, int start, int end) {
if ((end - start) % 2 == 0) { // odd number of elements
return (array[(end + start) / 2]);
} else { // even number of elements
int value1 = array[(end + start) / 2];
int value2 = array[(end + start) / 2 + 1];
return (value1 + value2) / 2.0;
}
}
static int[] reverseArray(int[] a) {
int[] reverseArr = new int[a.length];
for (int i = 0; i < a.length; i++) {
reverseArr[i] = a[a.length-1-i];
}
return reverseArr;
}
}
return result;
}
public int rob(int[] nums) {
if(nums.length == 0) {
return 0;
}
if(nums.length == 1) {
return nums[0];
}
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[1] = nums[0] > nums[1] ? nums[0] : nums[1];
for(int i = 2; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
}
return dp[dp.length - 1];
}
public boolean wordBreak(String s, Set<String> wordDict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for(int i = 1; i <= s.length(); i++) {
for(int j = 0; j < i; j++) {
if(dp[j] && wordDict.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] result = new int[n];
int left = 1;
for(int i = 0; i < nums.length; i++) {
if(i > 0) {
left *= nums[i - 1];
}
result[i] = left;
}
int right = 1;
for(int i = n - 1; i >= 0; i--) {
if(i < n - 1) {
right *= nums[i + 1];
}
result[i] *= right;
}
return result;
}
}
public int TrappingRainWater(int[] height) {
int water = 0;
int leftIndex = 0;
int rightIndex = height.length - 1;
int leftMax = 0;
int rightMax = 0;
while(leftIndex <= rightIndex) {
leftMax = Math.max(leftMax, height[leftIndex]);
rightMax = Math.max(rightMax, height[rightIndex]);
if(leftMax < rightMax) {
water += leftMax - height[leftIndex];
leftIndex++;
} else {
water += rightMax - height[rightIndex];
rightIndex--;
}