- ํ๊ธ ํ์ฅ
- ์๋๊ทธ๋จ(HashMap)
- ๋งค์ถ์ก์ ์ข ๋ฅ(Hash, sliding window)
- ๋ชจ๋ ์๋๊ทธ๋จ ์ฐพ๊ธฐ ( Hash, sliding window : ์๊ฐ๋ณต์ก๋ O(n))
- K๋ฒ์งธ ํฐ ์
1. ํ๊ธํ์ฅ
import java.util.*;
public class Main {
public char solution(int n,String str){
char answer=' ';
HashMap<Character, Integer> map=new HashMap<>();
for(char x:str.toCharArray()){
map.put(x,map.getOrDefault(x,0)+1);
}
int max=Integer.MIN_VALUE;
for(char key:map.keySet()){
if(map.get(key)>max){
max=map.get(key);
answer=key;
}
}
return answer;
}
public static void main(String[] args){
Main T=new Main();
Scanner kb =new Scanner(System.in);
int n=kb.nextInt();
String str=kb.next();
System.out.print(T.solution(n, str));
}
}
2. ์๋๊ทธ๋จ ( HashMap)
import java.util.*;
public class Main {
public String solution(String s1, String s2){
String answer="YES";
HashMap<Character,Integer> map = new HashMap<>(); // ํด์๋งต ๊ฐ์ฒด map ์์ฑ
for(char x : s1.toCharArray()){
map.put(x, map.getOrDefault(x, 0)+1); // x ํค๊ฐ ์์ผ๋ฉด 0 return
}
for(char x : s2.toCharArray()){
if(!map.containsKey(x)||map.get(x)==0) return "NO";
map.put(x, map.get(x)-1);
}
return answer;
}
public static void main(String[] args){
Main T = new Main();
Scanner kb = new Scanner(System.in);
String n=kb.next();
String m=kb.next();
System.out.print(T.solution(n,m));
}
}
3. ๋งค์ถ์ก์ ์ข ๋ฅ ( Hash, sliding window )
import java.util.*;
public class Main {
public ArrayList<Integer> solution(int n, int k,int[] arr){
ArrayList<Integer> answer=new ArrayList<>();
HashMap<Integer, Integer> map=new HashMap<>();
for(int i=0; i<k-1; i++){
map.put(arr[i], map.getOrDefault(arr[i],0)+1);
}
int lt=0;
for(int rt=k-1; rt<n; rt++){
map.put(arr[rt], map.getOrDefault(arr[rt],0)+1);
answer.add(map.size());
map.put(arr[lt],map.get(arr[lt])-1);
if(map.get(arr[lt])==0) map.remove(arr[lt]);
lt++;
}
return answer;
}
public static void main(String[] args){
Main T=new Main();
Scanner kb =new Scanner(System.in);
int n=kb.nextInt();
int k=kb.nextInt();
int[] arr=new int[n];
for(int i=0; i<n; i++) {
arr[i] = kb.nextInt();
}
for(int x:T.solution(n,k,arr)) System.out.print(x+" ");
}
}
4. ๋ชจ๋ ์๋๊ทธ๋จ ์ฐพ๊ธฐ ( Hash, sliding window : ์๊ฐ๋ณต์ก๋ O(n))
import java.util.*;
public class Main {
public int solution(String a, String b){
int answer=0;
HashMap<Character, Integer> am=new HashMap<>();
HashMap<Character, Integer> bm=new HashMap<>();
for(char x: b.toCharArray()) bm.put(x,bm.getOrDefault(x,0)+1); // b ๋ฏธ๋ฆฌ Key ์ธํ
int L=b.length()-1; // b๊ฐ abc, 3์ด๋ฉด L = 2
for(int i=0; i<L; i++) am.put(a.charAt(i), am.getOrDefault(a.charAt(i),0)+1);
int lt=0;
for(int rt=L; rt<a.length(); rt++){
am.put(a.charAt(rt), am.getOrDefault(a.charAt(rt),0)+1);
if(am.equals(bm)) answer++;
am.put(a.charAt(lt), am.get(a.charAt(lt))-1);
if(am.get(a.charAt(lt))==0) am.remove(a.charAt(lt));
lt++;
}
return answer;
}
public static void main(String[] args) {
Main T=new Main();
Scanner kb=new Scanner(System.in);
String a=kb.next();
String b=kb.next();
System.out.print(T.solution(a,b));
}
}
5. K๋ฒ์งธ ํฐ ์
import java.util.*;
public class Main {
public int solution(int[] arr, int n, int k){
int answer=-1;
TreeSet<Integer> Tset=new TreeSet<>(Collections.reverseOrder()); // reverseOrder = ๋ด๋ฆผ์ฐจ์ / default : ์ค๋ฆ์ฐจ์
for(int i=0; i<n; i++){ // 3์ฅ ๋ฝ๊ธฐ
for(int j=i+1; j<n; j++){
for(int l=j+1; l<n; l++){
Tset.add(arr[i]+arr[j]+arr[l]);
}
}
}
int cnt=0;
for(int x: Tset){
cnt++;
if(cnt==k) return x;
}
return answer; // cnt==k ๊ฐ์ด ์์ผ๋ฉด -1 return
}
public static void main(String[] args) {
Main T=new Main();
Scanner kb=new Scanner(System.in);
int n=kb.nextInt();
int k=kb.nextInt();
int[] arr=new int[n];
for(int i=0; i<n; i++){
arr[i]=kb.nextInt();
}
System.out.println(T.solution(arr, n, k));
}
}
'๐ ์ฝ๋ฉํ ์คํธ > Inflearn' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์น์ 6. Sorting and Searching(์ ๋ ฌ, ์ด๋ถ๊ฒ์๊ณผ ๊ฒฐ์ ์๊ณ ๋ฆฌ์ฆ) (0) | 2022.12.07 |
---|---|
์น์ 5. Stack, Queue(์๋ฃ๊ตฌ์กฐ) (0) | 2022.10.16 |
์น์ 3. Two points, Sliding window[ํจ์จ์ฑ : O(n^2)-->O(n)] (0) | 2022.09.16 |
์น์ 1. String(๋ฌธ์์ด) (0) | 2022.08.29 |
์น์ 2. Array( 1,2์ฐจ์ ๋ฐฐ์ด ) (0) | 2022.08.29 |