๐ช๐ป programmers Lv1(24.10.17 - 3๋ฌธ์ )
programmers Lv1(24.10.17 - 3๋ฌธ์ )
52) ๋น๋ฐ์ง๋
๋ต์์ ์ฐธ๊ณ ํจ.
๐ ํ๋ ค๊ณ ์๋ ํ ๋ฐฉ์
1) n์ด ํ, ์ด ํฌ๊ธฐ๋ฅผ ๋ํ๋. ๋ ์ ์ ๋ฐฐ์ด์ 2์ง์๋ก ๋ณํํ๊ธฐ
- 2)
|
์ฐ์ฐ์ ํตํด ๊ณตํต๋ ๋ถ๋ถ ์ถ์ถํ๊ธฐ - 3) 1์
#
์ผ๋ก ๋ณํํด์ ๊ฒฐ๊ณผ ์ถ๋ ฅํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
class Solution {
public String[] solution(int n, int[] arr1, int[] arr2) {
String[] answer = new String[n];
for(int i=0;i<n;i++)
{
String st = Integer.toBinaryString(arr1[i] | arr2[i]);
st = String.format("%" + n + "s", st).replace(' ', '0');
answer[i] = st.replace('1','#').replace('0',' ');
}
return answer;
}
}
Integer.toBinaryString()
: 10์ง์๋ฅผ 2์ง์๋ก ๋ณํ|
์ฐ์ฐ : ํด๋น ์ฐ์ฐ์ ํตํด ๊ณตํต๋ ๋ถ๋ถ ์ถ์ถString.format("%"+n + "s", st)
:%s
๋ ๋ฌธ์์ด์ ์ถ๋ ฅํ ๋ ์ฌ์ฉํ๋๋ฐ, n์ด ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ฏ๋ก ๋ง์ฝ 5๋ผ๊ณ ํ๊ณ st = โ101โ์ผ ๋, 5์๋ฆฌ๊ฐ ๋์ด์ผ๋๋ฏ๋ก ์์ ๊ณต๋ฐฑ์ด 2๊ฐ ์ถ๊ฐ๋๋ ๊ฒ์ด๋ค. -> __101.replace()
: replace ๋ฉ์๋๋ฅผ ํตํด ๋ฌธ์์ด์ ๋ฐ๊ฟ ์ ์๋ค.
53) ๋ช ์์ ์ ๋น
ArrayList
ArrayList๋ Java์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ๋์ ๋ฐฐ์ด ํด๋์ค ์ค ํ๋๋ก ํฌ๊ธฐ๊ฐ ๊ฐ๋ณ์ ์ธ ๋ฐฐ์ด์ด๋ค. ํฌ๊ธฐ๊ฐ ์๋์ผ๋ก ์กฐ์ ๋๊ณ ๋ฐฐ์ด๊ณผ ๋ฌ๋ฆฌ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ ๋ ์ ์ฉํ๊ฒ ์ฐ์ธ๋ค.
๋ฐฐ์ด์ ์ค๊ฐ์ ์์๋ฅผ ์ฝ์ ํ๊ฑฐ๋ ์ญ์ ํ๋ ค๋ฉด ์ง์ ์์๋ฅผ ์ด๋ํด์ผ ํจ. ArrayList๋ ์ฝ์ , ์ญ์ ๊ฐ ์๋์ด์ง๋ง ์๋์ ์ผ๋ก ๋๋ฆฌ๋ค.
๋ฐฐ์ด์ ํน์ ํ์ ๋ง ์ ์ฅํ ์ ์์. ArrayList๋ ์ ๋ค๋ฆญ์ ์ฌ์ฉํด์ ์ฌ๋ฌ ํ์ ์ ์ฅ ๊ฐ๋ฅ
๋ฐฐ์ด์ ๊ณ ์ ๋ ํฌ๊ธฐ, ArrayList๋ ์๋์ผ๋ก ํฌ๊ธฐ๊ฐ ๋์ด๋จ.
์ ์ธ ๋ฐ ์์ฑ
1
2
List<Integer> li = new ArrayList<>();
List<String> strLi = new ArrayList<>();
- ์์๋ฅผ ๋ฆฌ์คํธ์ ์ถ๊ฐ
1
li.add();
- ํน์ ์ธ๋ฑ์ค์ ์์๋ฅผ ๊ฐ์ ธ์ด
1
li.get(i);
- ํน์ ์ธ๋ฑ์ค์ ์๋ ์์ ์ ๊ฑฐ
1
li.remove(i);
- ๋ฆฌ์คํธ์ ํฌ๊ธฐ ๋ฐํ
1
li.size();
- ๋ฆฌ์คํธ์ ๋ชจ๋ ์์ ์ ๊ฑฐ
1
li.clear();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
class Solution {
public int[] solution(int k, int[] score) {
int[] answer = new int[score.length];
List<Integer> li = new ArrayList<>();
for(int i=0;i<score.length;i++)
{
li.add(score[i]);
if(li.size()>k)
{
li.remove(Collections.min(li));
}
answer[i] = Collections.min(li);
}
return answer;
}
}
PriorityQueue
PriorityQueue๋ java์์ ์ ๊ณตํ๋ ์ฐ์ ์์ ํ๋ฅผ ๊ตฌํํ ํด๋์ค์ด๋ค. ์ด ํ๋ ์ฐ์ ์์์ ๋ฐ๋ผ ์์๋ฅผ ์ ๋ ฌํ์ฌ ์ ์ฅํ๋ฉฐ, ๊ธฐ๋ณธ์ ์ผ๋ก๋ ์ต์๊ฐ์ด ๋จผ์ ๋์จ๋ค. ์ฆ, ๊ฐ์ฅ ์์ ๊ฐ์ด ํ์ ๋งจ ์์ ์์นํ๋ค.
์์๊ฐ ์ถ๊ฐ๋ ๋๋ง๋ค ๋ด๋ถ์ ์ผ๋ก ์๋์ผ๋ก ์ ๋ ฌ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ด ๋๋ฉฐ, ์ฌ์ฉ์ ์ ์ ๊ฐ์ฒด์ ๊ฒฝ์ฐ Comparable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ฑฐ๋ Comparator๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
PriorityQueue๋ null ๊ฐ์ ํ์ฉํ์ง ์๋๋ค.
์ ์ธ
1
PriorityQueue<Integer> pq = new PriorityQueue<>();
- ์์ ์ถ๊ฐ
1
pq.add();
- ๊ฐ์ฅ ์์ ๊ฐ ์กฐํ
1
pq.peek();
- ๊ฐ์ฅ ์์ ๊ฐ ์ ๊ฑฐ
1
pq.poll();
- ํ์ ํฌ๊ธฐ
1
pq.size();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;
class Solution {
public int[] solution(int k, int[] score) {
int[] answer = new int[score.length];
PriorityQueue<Integer> pq = new PriorityQueue<>(); //์ฐ์ ์์ ํ ์ ์ธ
for(int i=0;i<score.length;i++)
{
pq.add(score[i]); //pq์ ์์ ์ ์ฅ
if(pq.size()>k) //pq์ ํฌ๊ธฐ๊ฐ k๋ณด๋ค ํฌ๋ฉด
{
pq.poll(); //๊ฐ์ฅ ์์ ๊ฐ ์ ๊ฑฐ
}
answer[i] = pq.peek(); // ๊ฐ์ฅ ์์ ๊ฐ ์กฐํ
}
return answer;
}
}
54) ์ถ์ต ์ ์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
for(int i=0;i<photo.length;i++)
{
for(int j=0;j<photo[i].length;j++)
{
for(int k=0;k<name.length;k++)
{
if(name[k].equals(photo[i][j]))
{
answer[i] += yearning[k];
}
}
}
}
return answer;
}
}
HashMap
HashMap์ java์์ ์ ๊ณตํ๋ ํค-๊ฐ ์์ ์ ์ฅํ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ด๋ค. ํด์ ํ ์ด๋ธ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉฐ, ํค๋ฅผ ํตํด ๋น ๋ฅด๊ฒ ๊ฐ์ ์กฐํํ ์ ์๋ค.
HashMap์ ๊ฐ ํค์ ๋ํด ํ๋์ ๊ฐ์ ์ ์ฅํ๋ค. ๋์ผํ ํค๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ์ถ๊ฐํ๋ ค๋ฉด ๊ธฐ์กด ๊ฐ์ด ๋ฎ์ด์์์ง๋ค.
์ค๋ณต๋ ํค๋ฅผ ํ์ฉํ์ง ์์ง๋ง, ๊ฐ์ ๊ฐ์ ์ฌ๋ฌ ๊ฐ ์ ์ฅํ ์ ์๋ค.
ํ๊ท ์ ์ผ๋ก ์์์ ๋ํ ์กฐํ, ์ถ๊ฐ, ์ญ์ ๊ฐ O(1)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค.
ํ๋์ null ํค์ ์ฌ๋ฌ ๊ฐ์ null ๊ฐ์ ํ์ฉํ๋ค.
์์์ ์ ์ฅ ์์๋ฅผ ๋ณด์ฅํ์ง ์๋๋ค. ์์๊ฐ ํ์ํ ๊ฒฝ์ฐ LinkedHashMap์ ์ฌ์ฉํ ์ ์๋ค.
์ ์ธ
1
HashMap<String, Integer> map = new HashMap<>();
- ๊ฐ ์ถ๊ฐ
1
map.put("",);
- ๊ฐ ์กฐํ
1
map.get();
- ๊ฐ ์์
1
map.put("", );
- ํค ์กด์ฌ ํ์ธ
1
map.containsKey();
- ํค-๊ฐ ์ ์ญ์
1
map.remove();
- ์ ์ฒด ํฌ๊ธฐ ํ์ธ
1
map.size();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
class Solution {
public int[] solution(String[] name, int[] yearning, String[][] photo) {
int[] answer = new int[photo.length];
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<name.length;i++)
{
map.put(name[i],yearning[i]);
}
for(int i=0;i<photo.length;i++)
{
for(int j=0;j<photo[i].length;j++)
{
if(map.containsKey(photo[i][j]))
{
answer[i] += map.get(photo[i][j]);
}
}
}
return answer;
}
}