๐ช๐ป programmers Lv1(24.10.21 - 5๋ฌธ์ )
programmers Lv1(24.10.21 - 5๋ฌธ์ )
55) ์นด๋ ๋ญ์น
card1๊ณผ card2์ ์ ํ ๋จ์ด๋ค๋ก goal ๋ง๋ค๊ธฐ. ๊ฐ๋ฅํ๋ฉด โYesโ ์ถ๋ ฅ, ๋ถ๊ฐ๋ฅํ๋ฉด โNoโ ์ถ๋ ฅํ๊ธฐ
๐ ํ์ด
1) ํฅ์๋ for๋ฌธ์ ์ด์ฉํด goal์ word๋ก
2) idx1์ด cards1์ ๊ธธ์ด๋ณด๋ค ํฌ๊ณ word์ ๋ฌธ์์ด๊ณผ cards1์ idx1 ์ธ๋ฑ์ค์ ๋ฌธ์์ด์ด ๊ฐ์ ๊ฒฝ์ฐ ์ธ๋ฑ์ค ์ฆ๊ฐ
3) idx2๊ฐ cards2์ ๊ธธ์ด๋ณด๋ค ํฌ๊ณ words์ ๋ฌธ์์ด๊ณผ cards2์ idx2 ์ธ๋ฑ์ค์ ๋ฌธ์์ด์ด ๊ฐ์ ๊ฒฝ์ฐ ์ธ๋ฑ์ค ์ฆ๊ฐ
4) ๊ทธ ์ธ์ ๊ฒฝ์ฐ โNoโ ์ถ๋ ฅ, ๋ง์ ๊ฒฝ์ฐ โYesโ ์ถ๋ ฅ
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 String solution(String[] cards1, String[] cards2, String[] goal) {
int idx1 = 0;
int idx2 = 0;
for(String word : goal)
{
if(cards1.length > idx1 && word.equals(cards1[idx1]))
{
idx1++;
}
else if(cards2.length > idx2 && word.equals(cards2[idx2]))
{
idx2++;
}
else{
return "No";
}
}
return "Yes";
}
}
56) ํฌ์ผ๋ชฌ
๊ฐ์ฅ ๋ง์ ์ข ๋ฅ์ ํฌ์ผ๋ชฌ์ ์ ํํ๋ ๋ฐฉ๋ฒ์ ์ฐพ์ ๊ทธ๋ ํฌ์ผ๋ชฌ ์ข ๋ฅ ๋ฒํธ์ ๊ฐ์๋ฅผ ๋ฐํ
๐ ๋ด ํ์ด
1) [3, 1, 2, 3] ์ด ๋ค์ด์์ผ๋ฉด 3 , 3์ ํ ์ข ๋ฅ๋ก ๋ณด๊ธฐ ๋๋ฌธ์ ์ค๋ณต์ ํ์ฉํ์ง ์์ผ๋ฏ๋ก HashSet์ ์ฌ์ฉ
2) nums์ ๊ธธ์ด๋งํผ ๋ฐ๋ณตํด์ set์ ์์ ์ ์ฅ
3) nums์ ๊ธธ์ด๋ฅผ 2๋ก ๋๋์ด์ num์ด๋ผ๋ ๋ณ์ ์์ฑ ํ, num์ ๊ธธ๊ธฐ์ set์ ํฌ๊ธฐ๋ณด๋ค ์์ผ๋ฉด num์ ์ถ๋ ฅ, ์๋๋ผ๋ฉด set์ ํฌ๊ธฐ ์ถ๋ ฅ
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(int[] nums) {
int answer = 0;
int num = nums.length/2; //2
HashSet<Integer> set = new HashSet<>();
for(int i=0;i<nums.length;i++)
{
set.add(nums[i]);
}
if(num < set.size())
{
answer = num;
}
else{
answer = set.size();
}
return answer;
}
}
57) 2016๋
2016๋ 1์ 1์ผ์ ๊ธ์์ผ์ด๋ค. 2016๋ a์ b์ผ์ ๋ฌด์จ ์์ผ์ธ์ง ๊ตฌํ๊ธฐ
๋ต์ ์ฐธ๊ณ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
class Solution {
public String solution(int a, int b) {
String answer = "";
String[] day = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
int[] date = {31,29,31,30,31,30,31,31,30,31,30,31};
int allDate = 0;
for(int i=0;i<a-1;i++)
{
allDate += date[i];
}
allDate += (b-1); // 1์ 1์ผ์ ๊ธฐ์ค์ผ๋ก 0์ผ์ด ๊ธ์์ผ๋ก ์์
answer = day[allDate % 7];
return answer;
}
}
// a = 5, b = 24
// allDate = 31 + 29 + 31 + 30 = 121
// allDate = 121 + 23 = 144
// 144 % 7 = 4, day[4] = TUE
58) ๊ธฐ์ฌ๋จ์์ ๋ฌด๊ธฐ
- ๊ธฐ์ฌ๋จ์์ ์๋ฅผ ๋ํ๋ด๋ ์ ์ number์ ์ด์๋๋ผ์ ํ์ฝ์ผ๋ก ์ ํด์ง ๊ณต๊ฒฉ๋ ฅ์ ์ ํ์์น๋ฅผ ๋ํ๋ด๋ ์ ์ limit์ ์ ํ์์น๋ฅผ ์ด๊ณผํ ๊ธฐ์ฌ๊ฐ ์ฌ์ฉํ ๋ฌด๊ธฐ์ ๊ณต๊ฒฉ๋ ฅ์ ๋ํ๋ด๋ ์ ์ power๊ฐ ์ฃผ์ด์ก์ ๋, ๋ฌด๊ธฐ์ ์ ์ฃผ์ธ์ด ๋ฌด๊ธฐ๋ฅผ ๋ชจ๋ ๋ง๋ค๊ธฐ ์ํด ํ์ํ ์ฒ ์ ๋ฌด๊ฒ๋ฅผ ๊ตฌํ๋ผ.
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
class Solution {
public int solution(int number, int limit, int power) {
int answer = 0;
int[] cnt = new int[number+1];
for(int i=1;i<=number;i++)
{
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
cnt[i]++;
}
}
if(cnt[i]<=limit)
{
answer+=cnt[i];
}
else if(cnt[i]>limit)
{
answer+=power;
}
}
return answer;
}
}
ํ ์คํธ ์ผ์ด์ค 27๊ฐ์ค 2๊ฐ ์๊ฐ ์ด๊ณผ ๋ธ.
์ซ์ i์ ๋ํด 1๋ถํฐ i๊น์ง ๋ชจ๋ ์ซ์๋ฅผ ์ํํ์ฌ ์ฝ์๋ฅผ ์ฐพ๋ ์ด ๋ฐฉ์์ ์๊ฐ ๋ณต์ก๋๊ฐ ๋งค์ฐ ํผ.
์๊ฐ ๋ณต์ก๋. ์ด์ค๋ฐ๋ณต๋ฌธ์ผ๋ก :
O(n^2)
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
27
28
29
class Solution {
public int solution(int number, int limit, int power) {
int answer = 0;
int[] cnt = new int[number+1];
for(int i=1;i<=number;i++)
{
for(int j=1;j*j<=i;j++)
{
if(i%j==0)
{
cnt[i]++;
if(j!=i/j)
{
cnt[i]++;
}
}
}
if(cnt[i]<=limit)
{
answer+=cnt[i];
}
else if(cnt[i]>limit)
{
answer+=power;
}
}
return answer;
}
}
for (int j = 1; j * j <= i; j++)
๋ก1
๋ถํฐโi
๊น์ง๋ง ํ์ธํ๊ณ , ๋์นญ๋๋ ์ฝ์ (i / j
)๋ ํจ๊ป ์นด์ดํธํ์ฌ ์ค๋ณต ๊ณ์ฐ์ ํผํ๋ค.j!=i/j
๋ฅผ ํ๋ ์ด์ ๋ ์ค๋ณต๋ ์ฝ์๋ ๋ฐฉ์งํ๊ณ ๊ทธ ์ธ์ ๋์นญ๋๋ ์ฝ์์ ๊ฐ์๋ฅผ ์นด์ดํธ ํ๊ธฐ ์ํด์.์๊ฐ ๋ณต์ก๋๊ฐ
O(n^2) -> O(n*sqrt(n))
์ผ๋ก ์ค์๋ค.
59) ๋ชจ์๊ณ ์ฌ
๐ ๋ด ํ์ด
- 1) num1, num2, num3 ๋ฐฐ์ด์ ๋ง๋ค์ด 1๋ฒ, 2๋ฒ, 3๋ฒ ์ํฌ์์ ์ซ์ ๋ฐฐ์ด ์ ์ฅ
2) ์ ๋ต ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ๋ฐ๋ณต๋ฌธ, 1, 2, 3๋ฒ ์ํฌ์์ ์ซ์์ ๋น๊ตํด์ ๊ฐ์ ๊ฒฝ์ฐ ๊ฐ ์นด์ดํธ ์ฆ๊ฐ
- 3) ์ ์ผ ํฐ ๊ฐ maxScore์ ์ ์ฅ
4) ์ํฌ์ 1, 2, 3๋ฒ์ ๊ฐ์ด maxScore์ผ ๊ฒฝ์ฐ list์ ์์ ์ถ๊ฐ
- 5) ๋ฆฌ์คํธ ์์ ์ถ๋ ฅ
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] answer;
int[] num1 = {1,2,3,4,5};
int[] num2 = {2,1,2,3,2,4,2,5};
int[] num3 = {3,3,1,1,2,2,4,4,5,5};
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
for(int i=0;i<answers.length;i++){
if(answers[i] == num1[i%num1.length])
{
cnt1++;
}
if(answers[i] == num2[i%num2.length])
{
cnt2++;
}
if(answers[i] == num3[i%num3.length])
{
cnt3++;
}
}
int maxScore = Math.max(cnt1, Math.max(cnt2,cnt3));
ArrayList<Integer> list = new ArrayList<>();
if(cnt1 == maxScore)
{
list.add(1);
}
if(cnt2 == maxScore)
{
list.add(2);
}
if(cnt3 == maxScore)
{
list.add(3);
}
answer = new int[list.size()];
for(int i=0;i<list.size();i++)
{
answer[i] = list.get(i);
}
return answer;
}
}