https://www.acmicpc.net/problem/13144
13144번: List of Unique Numbers
길이가 N인 수열이 주어질 때, 수열에서 연속한 1개 이상의 수를 뽑았을 때 같은 수가 여러 번 등장하지 않는 경우의 수를 구하는 프로그램을 작성하여라.
www.acmicpc.net
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] input1 = br.readLine().split(" ");
int[] arr = new int[n];
for(int i=0; i<n;i++){
arr[i] = Integer.parseInt(input1[i]);
}
int right=0;
long count=0;
HashSet<Integer> set = new HashSet();
for(int left=0; left<n;left++){
while(right<n && !set.contains(arr[right])){
set.add(arr[right++]);
}
count+=(right-left);
set.remove(arr[left]);
}
System.out.println(count);
}
}
'Problem Solving > 투포인터' 카테고리의 다른 글
BOJ13422 도둑 (2) | 2024.03.05 |
---|---|
BOJ10025 게으른 백곰 (0) | 2024.03.05 |
슬라이딩 윈도우 개념 (0) | 2024.03.05 |
BOJ14465 소가 길을 건너간 이유 5 (0) | 2024.03.05 |
BOJ16472 고냥이 (0) | 2024.03.04 |