✔ 알고리즘 문제 풀기
문제의 내용이 영 남 일같지 않을것은 기분 탓일까?
import java.util.*;
class Solution {
public List<Integer> solution(int[] a) {
int[][] b ={ {1,2,3,4,5},{2,1,2,3,2,4,2,5},{3,3,1,1,2,2,4,4,5,5} };
int[] c = new int[3] ;
int d=0;
List<Integer> e = new ArrayList<>();
for(int i=0;i<a.length;i++)
{
for(int j=0;j<3;j++)
{
if(b[j][i%b[j].length]==a[i])
c[j]++;
}
}
for(int i=0;i<3;i++)
if(c[i]>d) d=c[i];
for(int i=0;i<3;i++)
if(c[i]==d) e.add(i+1);
return e;
}
}
경험자로서 한 마디 하자면, 3점짜리 쉬운 문제부터 다 푼 뒤에, 끝나기 15분 전 서술형 찍먹좀 하다가, 끝나기 5분 전에 나온 번호의 수를 모두 세고 가장 적은 번호 하나로 통일해 찍는 것이 가장 효과가 좋았다.
-
소수 만들기
import java.util.*;
class Solution {
public int solution(int[] n) {
int a =0;
List<Integer> b= new ArrayList<Integer>();
for(int i=0;i<n.length-2;i++)
{for(int j=i+1;j<n.length-1;j++)
{for(int k=j+1;k<n.length;k++)
{
if(IsPrime(n[i]+n[j]+n[k],b))
a++;
}
}
}
return a;
}
public boolean IsPrime(int a, List<Integer> b)
{
if(b.contains(a)) return true;
for(int i=2;i<a;i++)
{
if(a%i==0) return false;
}
b.add(a);
return true;
}
}
문제가 읽기 귀찮은 관계로 예시로 문제를 유추
class Solution {
public int solution(int n, int m, int[] b) {
int a = 0;
int[] c= new int[n+1];
for(int i=0;i<b.length;i++)
{
if(c[b[i]]==0)
{
for(int j=b[i];j<b[i]+m;j++)
{
if(j>n)break;
c[j]=1;
}
a++;
}
}
return a;
}
}
아
class Solution {
public int solution(int n, int c, int d) {
int a=0;
int[] b= new int[n];
for(int i=0;i<n;i++)
{
b[i]=MeasureNum(i+1);
if(b[i]>c) a+=d;
else a+=b[i];
}
return a;
}
public int MeasureNum(int a)
{
int b=0;
for(int i=1;i*i<=a;i++)
{
if(i*i==a) b++;
else if(a%i==0) b+=2;
}
return b;
}
}
기사단원의 무기
갈수록 길어지는 비문학 지문..
class Solution {
public int[] solution(int[] c, int[] d) {
int[] a= {7,7} ;
for(int i=0;i<c.length;i++)
{
if(c[i]==0)
{
a[0]--;
continue;
}
for(int j=0;j<c.length;j++)
{
if(c[i]==d[j])
{
a[0]--;
a[1]--;
break;
}
}
}
a[0]= a[0]>6? 6: a[0];
a[1]= a[1]>6? 6: a[1];
return a;
}
}
옹알이(2)
응애
import java.util.*;
class Solution {
public int solution(String[] b) {
int d = 0;
List<String> a = new ArrayList<>();
a.add("aya");
a.add("ye");
a.add("woo");
a.add("ma");
for (int i = 0; i < b.length; i++){
String[] c = {"",""};
for (int j = 0; j < b[i].length(); j++){
c[0] += b[i].charAt(j);
if ( !c[1].equals(c[0]) && a.contains(c[0]))
{
c[1] = c[0];
c[0] = "";
}
}
if (c[0].isEmpty()) d ++;
}
return d;
}
}