Skip to content

jaepyo99 / 5월 4주차 / 16문제 #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions jaepyo99/[PGS] 보석 쇼핑.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.*;
class Solution {
public int[] solution(String[] gems) {
int kind=new HashSet<>(Arrays.asList(gems)).size(); // 주어진 보석의 종류
int[]answer=new int[2]; //범위를 위한 배열
int length=Integer.MAX_VALUE,start=0; // length는 진열대의 길이(비교를 위해 큰숫자), start는 진열대의 시작인덱스
Map<String,Integer>map=new HashMap<>(); // 선택한 진열대에 있는 각 보석의 개수
for(int end=0;end<gems.length;end++){
map.put(gems[end],map.getOrDefault(gems[end],0)+1); // 내가 선택한 진열대의 보석 업데이트
while(map.get(gems[start])>1){ // 방금 선택으로 가장 왼쪽에 있는 보석이 여러개가 되었다면 보석의 개수를 줄이기 위해 1감소
map.put(gems[start],map.get(gems[start])-1);
start++;
}
if(map.size()==kind && length>(end-start)){
// 현재까지 선택한 진열대에 모든 보석이 있고, 길이가 짧다면 인덱스 업데이트
length=end-start;
answer[0]=start+1;
answer[1]=end+1;
}
}
// for문을 통해 가장 왼쪽에서 보석의 종류가 채워지는 최초의 배열을 찾은뒤, 이후의 조합중에 개수가 더 적은 배열이 있는지 찾는 방식
return answer;
}
}
49 changes: 49 additions & 0 deletions jaepyo99/[PGS] 불량 사용자.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;
class Solution {
int answer;
Set<Set<String>>ans_set=new HashSet<>();
String[]userIds;
String[]banIds;
public boolean isBan(String user,String ban){
if(user.length()!=ban.length()){
return false;
}
for(int i=0;i<user.length();i++){
if(ban.charAt(i)=='*'){
continue;
}
if(!(ban.charAt(i)==user.charAt(i))){
return false;
}
}
return true;
}

public void dfs(Set<String>set,int depth){
if(depth==banIds.length){
ans_set.add(set);
return;
}
for(int i=0;i<userIds.length;i++){
if(set.contains(userIds[i])){
continue;
}
if(isBan(userIds[i],banIds[depth])){
set.add(userIds[i]);
dfs(new HashSet<>(set),depth+1);
set.remove(userIds[i]);
}
}
}


public int solution(String[] user_id, String[] banned_id) {
answer = 0;
boolean[]visit_user=new boolean[user_id.length];
userIds=user_id;
banIds=banned_id;
dfs(new HashSet<>(),0);

return ans_set.size();
}
}
32 changes: 32 additions & 0 deletions jaepyo99/[PGS] 숫자 게임.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 이기면 승점1
// 최대 승점을 계산하시오
// A는 A팀의 출전 순서
// B는 B팀의 번호목록

// A팀의 순서가 의미가 있나?
// 5 1 3 7
// 2 2 6 8 / 0,3
import java.util.*;
class Solution {
boolean[]visit;
int max=0;
public int solution(int[] A, int[] B) {
Arrays.sort(A);
Arrays.sort(B);
int a=0;
int b=0;
int result=0;
for(int i=0;i<A.length;i++){
if(A[a]>B[b]){
b++;
}else if(A[a]==B[b]){
b++;
}else{
a++;
b++;
result++;
}
}
return result;
}
}
28 changes: 28 additions & 0 deletions jaepyo99/[PGS] 스티커 모으기(2).java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.*;
class Solution {
int max=0;
public int solution(int sticker[]) {
int answer = 0;
int l=sticker.length;
int[]arr=new int[l];
if(l==1){
return sticker[0];
}
int[]dp=new int[l];
dp[0]=sticker[0];
dp[1]=dp[0];
for(int i=2;i<l-1;i++){
dp[i]=Math.max(dp[i-1],dp[i-2]+sticker[i]);
}
int first=dp[l-2];
dp=new int[l];
dp[0]=0;
dp[1]=sticker[1];
for(int i=2;i<l;i++){
dp[i]=Math.max(dp[i-1],dp[i-2]+sticker[i]);
}
int second=dp[l-1];

return Math.max(first,second);
}
}
69 changes: 69 additions & 0 deletions jaepyo99/[PGS][1차] 셔틀버스.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.*;
class Solution {
public String solution(int n, int t, int m, String[] timetable) {
PriorityQueue<String>pq=new PriorityQueue<>();
for(int i=0;i<timetable.length;i++){
pq.offer(timetable[i]);
}
int cm=0;
String lastTimeTable=timetable[0];
String time="09:00";
while(n>0){
// System.out.println("===");
n--;
cm=0;
for(int i=0;i<m;i++){
String now;
if(!pq.isEmpty()){
now=pq.peek();
// System.out.println("now : "+now);

}else{
break;
}
if(calTime(now)<=calTime(time)){
cm++;
lastTimeTable=pq.poll();
// System.out.println("changeLastTimeTable : "+lastTimeTable);
}else{
break;
}
}
if(n>0){
time=changeTime(time,t);
}
// System.out.println("time : "+time);
// System.out.println("lastTimeTable : "+lastTimeTable);

}
if(cm<m){
return time;
}else{
return changeTime(lastTimeTable,-1);
}
}
public String changeTime(String now,int change){
String[] time=now.split(":");
String h=time[0];
String m=time[1];
Integer ih=Integer.parseInt(h);
Integer im=Integer.parseInt(m);
im=im+change;
if(im<0){
ih=ih-1;
im=im+60;
}else{
ih=ih+im/60;
im=im%60;
}
return String.format("%02d:%02d",ih,im);
}
public Integer calTime(String time){
String[] t=time.split(":");
String h=t[0];
String m=t[1];
Integer ih=Integer.parseInt(h);
Integer im=Integer.parseInt(m);
return ih*60+im;
}
}
22 changes: 22 additions & 0 deletions jaepyo99/[PGS]가장 긴 팰린드롬.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.*;
class Solution{
public int solution(String s){
int n=s.length();
for(int i=n;i>=1;i--){
for(int j=0;j+i<=n;j++){
if(isPal(s,j,i+j-1)){
return i;
}
}
}
return 0;
}
public boolean isPal(String s,int start,int end){
while(start<=end){
if(s.charAt(start++)!=s.charAt(end--)){
return false;
}
}
return true;
}
}
71 changes: 71 additions & 0 deletions jaepyo99/[PGS]경주로 건설.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.*;
class Solution {
static class Point{
int x,y,dir,cost;
Point(int x,int y,int dir, int cost){
this.x=x;
this.y=y;
this.dir=dir;
this.cost=cost;
}
}
static boolean[][] visit;
static int answer;
static int n;
static int[][][]arr;
public int solution(int[][] board) {
answer=Integer.MAX_VALUE;
n=board.length;
arr=new int[4][n][n];
for(int i=0;i<4;i++){
for(int x=0;x<n;x++){
for(int y=0;y<n;y++){
arr[i][x][y]=Integer.MAX_VALUE;
}
}
}
visit=new boolean[n][n];
bfs(0,0,-1,0,board);
return answer;
}
private void bfs(int x,int y,int dir,int cost,int[][]board){
int[]dx={-1,1,0,0};
int[]dy={0,0,-1,1};
Queue<Point>q=new LinkedList<>();
q.add(new Point(x,y,dir,cost));
visit[x][y]=true;
while(!q.isEmpty()){
Point point=q.poll();
int px=point.x;
int py=point.y;
int pdir=point.dir;
int pcost=point.cost;
if(px==n-1 && py==n-1){
answer=Math.min(answer,pcost);
continue;
}
for(int i=0;i<4;i++){
int nx=px+dx[i];
int ny=py+dy[i];
int ndir=i;
int ncost=pcost;
if(nx<0 ||nx>=n||ny<0||ny>=n||board[nx][ny]==1){
continue;
}
if(pdir==-1){
ncost+=100;
}else if(pdir==ndir){
ncost+=100;
}else{
ncost+=600;
}

if(!visit[nx][ny] || arr[ndir][nx][ny]>=ncost){
visit[nx][ny]=true;
arr[ndir][nx][ny]=ncost;
q.add(new Point(nx,ny,ndir,ncost));
}
}
}
}
}
20 changes: 20 additions & 0 deletions jaepyo99/[PGS]기지국 설치.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.*;
class Solution {
public int solution(int n, int[] stations, int w) {
int answer=0;
Queue<Integer>queue=new LinkedList<>();
int idx=0;
int now=1;
int l=stations.length;
while(now<=n){
if(idx<l&&stations[idx]-w<=now){
now=stations[idx]+w+1;
idx++;
}else{
answer+=1;
now+=(2*w)+1;
}
}
return answer;
}
}
36 changes: 36 additions & 0 deletions jaepyo99/[PGS]단속카메라.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;
class Solution {
public int solution(int[][] routes) {
int answer = 0;
Arrays.sort(routes,new Comparator<>(){
@Override
public int compare(int[]o1,int[]o2){
if(o1[0]==o2[0]){
return o1[1]-o2[1];
}else{
return o1[0]-o2[0];
}
}
});
// for(int i=0;i<routes.length;i++){
// System.out.print("1 :"+routes[i][0]+", 2 :"+routes[i][1]);
// System.out.println();
// }
int cnt=1;
int left=routes[0][0];
int right=routes[0][1];
for(int i=1;i<routes.length;i++){
if(left<=routes[i][0] && routes[i][0]<=right){
left=routes[i][0];
if(routes[i][1]<=right){
right=routes[i][1];
}
}else{
left=routes[i][0];
right=routes[i][1];
cnt++;
}
}
return cnt;
}
}
19 changes: 19 additions & 0 deletions jaepyo99/[PGS]두 원 사이의 정수 쌍.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.*;
class Solution {
public long solution(int r1, int r2) {
long answer = 0;
long left=(long)r1*r1;
long right=(long)r2*r2;
long sum=0;
for(long i=-1*r2;i<=0;i++){
long l=left-(i*i);
long r=right-(i*i);
if(l<0){
l=0;
}
sum+=(long)Math.floor(Math.sqrt(r))-(long)Math.ceil(Math.sqrt(l))+1;
}
answer=(sum-(r2-r1+1))*4;
return answer;
}
}
Loading