1059. 좋은 구간
브루트포스 알고리즘
1) n과 같은 값이 있는 경우 = 0
2) 첫번째 배열부터 n보다 큰 경우
3) 집합 원소 사이에 들어갈 수 있는 경우
left n right
2) 0 arr[0]
3) arr[index-1] arr[index]
left와 n 사이의 개수 : L = n-left-1
right와 n 사이의 개수: R = right-n-1
n을 포함하여 만들수 있는 좋은 구간의 개수
= L*R + L + R
Scanner sc = new Scanner(System.in);
int len = sc.nextInt();
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int n = sc.nextInt();
int index = Arrays.binarySearch(arr, n);
if (index < 0) {
index = -(index + 1);
}
long result = 0;
if (index == 0 && arr[index] != n){
int left = 0;
int right = arr[index];
result = (long) (n - left-1) * (right - n-1) - left-1 + right-1;
}
else if (index > 0 && index < len && arr[index] != n) {
int left = arr[index - 1];
int right = arr[index];
result = (long) (n - left-1) * (right - n-1) - left-1 + right-1;
}
System.out.println(result);
2751. 수 정렬하기 2
빠른입력, 빠른출력, 빠른 sort 필요
1) 빠른입력
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine()
2) 빠른출력
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(arr[i]+"\n");
3) 빠른 sort
Arrays.parallelSort(arr);
요소가 많으면 parallelSort가 빠르고,
적은 양이라면 sort가 빠르다
2557, 10171, 10869, 10172, 2884, 1330, 2739
10718, 9498, 10951, 10430, 1152, 10818, 2588
2753, 2438, 2439, 4344