File tree Expand file tree Collapse file tree 1 file changed +13
-13
lines changed
src/main/java/g1601_1700/s1679_max_number_of_k_sum_pairs Expand file tree Collapse file tree 1 file changed +13
-13
lines changed Original file line number Diff line number Diff line change 11package g1601_1700 .s1679_max_number_of_k_sum_pairs ;
22
33// #Medium #Array #Hash_Table #Sorting #Two_Pointers
4- // #2022_04_21_Time_22_ms_(85.31%)_Space_67.1_MB_(70.80 %)
4+ // #2022_04_21_Time_20_ms_(91.22%)_Space_52.7_MB_(87.98 %)
55
66import java .util .Arrays ;
77
88public class Solution {
99 public int maxOperations (int [] nums , int k ) {
1010 Arrays .sort (nums );
11- int i = 0 ;
12- int c = 0 ;
13- int j = nums . length - 1 ;
14- while (i < j ) {
15- int sum = nums [i ] + nums [j ];
11+ int start = 0 ;
12+ int end = nums . length - 1 ;
13+ int count = 0 ;
14+ while (start < end ) {
15+ int sum = nums [start ] + nums [end ];
1616 if (sum == k ) {
17- c ++;
18- i ++;
19- j --;
20- } else if (sum > k ) {
21- j -- ;
17+ count ++;
18+ start ++;
19+ end --;
20+ } else if (sum < k ) {
21+ start ++ ;
2222 } else {
23- i ++ ;
23+ end -- ;
2424 }
2525 }
26- return c ;
26+ return count ;
2727 }
2828}
You can’t perform that action at this time.
0 commit comments