File tree Expand file tree Collapse file tree 1 file changed +27
-11
lines changed
src/main/kotlin/hackerrank/search Expand file tree Collapse file tree 1 file changed +27
-11
lines changed Original file line number Diff line number Diff line change 1
1
package hackerrank .search ;
2
2
3
3
import java .util .Arrays ;
4
- import java .util .HashSet ;
5
- import java .util .Set ;
6
4
7
5
public class TripleSum {
8
6
9
7
static long triplets (int [] a , int [] b , int [] c ) {
10
8
11
- // 예제로만 보면 주어진 배열들이 정렬되어 있음. 하지만 설명에는 명시되어 있지 않음.
12
9
Arrays .sort (a );
13
10
Arrays .sort (b );
14
11
Arrays .sort (c );
15
12
13
+ long aCount = 0 ;
14
+ long cCount = 0 ;
15
+ int aIndex = 0 ;
16
+ int cIndex = 0 ;
16
17
long count = 0L ;
17
- final Set < Integer > calculated = new HashSet <>() ;
18
+ int bCurrent = 0 ;
18
19
19
- // 배열 안에 중복된 숫자가 있을 수 있음.
20
- for (int number : b ) {
20
+ for (int n : b ) {
21
21
22
- if (calculated .contains (number )) continue ;
23
- calculated .add (number );
22
+ // 같은 값이 존재할 수 있음.
23
+ if (n <= bCurrent ) continue ;
24
+ bCurrent = n ;
24
25
25
- final long left = Arrays .stream (a ).filter (x -> x <= number ).count ();
26
- final long right = Arrays .stream (c ).filter (x -> x <= number ).count ();
27
- count += left * right ;
26
+ while (aIndex < a .length && a [aIndex ] <= n ) {
27
+ // 같은 값이 존재할 수 있음.
28
+ if (aIndex == 0 || a [aIndex - 1 ] != a [aIndex ]) {
29
+ aCount ++;
30
+ }
28
31
32
+ aIndex ++;
33
+ }
34
+
35
+ while (cIndex < c .length && c [cIndex ] <= n ) {
36
+ // 같은 값이 존재할 수 있음.
37
+ if (cIndex == 0 || c [cIndex - 1 ] != c [cIndex ]) {
38
+ cCount ++;
39
+ }
40
+
41
+ cIndex ++;
42
+ }
43
+
44
+ count += aCount * cCount ;
29
45
}
30
46
31
47
return count ;
You can’t perform that action at this time.
0 commit comments