프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

Language: Java

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
    public String solution(String[] seoul) {
        String answer = "";
        for(int i=0; i<seoul.length; i++){
            if(seoul[i].equals("Kim")){
                answer = "김서방은 " + i + "에 있다";
                break;
            }
        }
        return answer;
    }
}
 
 

* for 반복문

 

 

😮 찾아본 풀이

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Arrays;
 
class Solution {
    public String solution(String[] seoul) {
        String answer = "";
        
        int x = Arrays.asList(seoul).indexOf("Kim");        
 
        return "김서방은 "+ x + "에 있다";
    }
}
 
 

* List + indexOf()

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

Language: Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public boolean solution(String s) {
        boolean answer = true;
        
        if(!(s.length()==4 || s.length()==6)){
            answer = false;
            return answer;
        }
        
        for(int i=0; i<s.length(); i++){
            if(!Character.isDigit(s.charAt(i))){
                answer = false;
                return answer;               
            }
        }
        
        return answer;
    }
}
 
 

1. if(!(s.length()==4 || s.length()==6))

    - 길이 확인

2. if(!Character.isDigit(s.charAt(i)))

    - 숫자 확인

 

 

😮 다른 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
  public boolean solution(String s) {
      if(s.length() == 4 || s.length() == 6){
          try{
              int x = Integer.parseInt(s);
              return true;
          } catch(NumberFormatException e){
              return false;
          }
      }
      else return false;
  }
}
 
 

* try-catch 활용

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

Language: Java

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class Solution {
    public int[] solution(String[] wallpaper) {
        int[] answer = new int[4];
        
        int east = wallpaper[0].length();
        int west = 0;
        int north = wallpaper.length;
        int south = 0;
        
        
        for(String row : wallpaper) {
            east = east > calcRowMin(row) ? calcRowMin(row) : east;
            west = west < calcRowMax(row) ? calcRowMax(row) : west;
        }
        
        for(int i=0; i<wallpaper.length; i++) {
            if(calcMinSharp(wallpaper[i])) {
                north = i;
                break;                
            }
        }
        
        for(int i=0; i<wallpaper.length; i++) {
            south = calcMaxSharp(wallpaper[i]) ? i : south;
        }
        
        answer[0= north;
        answer[1= east;
        answer[2= south+1;
        answer[3= west+1;
        
        
        return answer;
    }
 
    public static int calcRowMin(String row) {
        int min=row.length();
 
        for(int i=0; i<row.length(); i++) {
            if(row.charAt(i)=='#') {
                min = min > i ? i : min;
                break;
            }
        }
        
        return min;
        
    }
    
    public static int calcRowMax(String row) {
        int max=-1;
 
        for(int i=row.length()-1; i>=0; i--) {
            if(row.charAt(i)=='#') {
                max = max < i ? i : max;
                break;
            }
        }
        
        return max;
        
    }
    
    public static boolean calcMinSharp(String row) {
        boolean result = false;
 
        for(int i=0; i<row.length(); i++) {
            if(row.charAt(i)=='#') {
                result = true;
                break;
            }
        }
 
        return result;
        
    }
 
    public static boolean calcMaxSharp(String row) {
        boolean result = false;
 
        for(int i=row.length()-1; i>=0; i--) {
            if(row.charAt(i)=='#') {
                result = true;
                break;
            }
        }
 
        return result;
        
    }
 
}
 
 

1. int east, west, north, south

    - 상하좌우 최대, 최소를 담을 변수 선언

2. calcRowMin

    - 좌우 최소값 탐색

3. calcRowMax

    - 좌우 최대값 탐색

4. calcMinSharp

    - 상하 최소값(배열 중 첫번째 Sharp 위치) 탐색

5. calcMaxSharp

    - 상하 최대값(배열 중 마지막 Sharp 위치) 탐색

 

 

😮 찾아본 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public int[] solution(String[] wallpaper) {
        int minX = 50;
        int minY = 50;
        int maxX = -1;
        int maxY = -1;
        for(int i=0; i< wallpaper.length; i++){
            for(int j=0; j<wallpaper[i].length(); j++){
                if(wallpaper[i].charAt(j)=='#'){
                    minX = Math.min(minX,i);
                    minY = Math.min(minY,j);
                    maxX = Math.max(maxX,i);
                    maxY = Math.max(maxY,j);
                }
            }
        }
        return new int[]{minX,minY,maxX+1,maxY+1};
    }
}
 
 

* Min, Max 함수 활용

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

Language: Java

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
import java.util.Map;
import java.util.HashMap;
 
class Solution {
    static int[] answer;
    static Map<String, Integer> yearningScore;
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
 
        answer = new int[photo.length];
        
        yearningScore = new HashMap<>();
        
        for(int i=0; i<name.length; i++){
            yearningScore.put(name[i], yearning[i]);
        }
        
        int index=0;
        for(String[] peopleInPhoto : photo){
            answer[index++= calcYearningScore(peopleInPhoto);
        }
        
        return answer;
    }
    
    public int calcYearningScore(String[] peopleInPhoto){
        int sumYearning = 0;
        for(String personInPhoto : peopleInPhoto){
            if(yearningScore.get(personInPhoto)!=null)
                sumYearning += yearningScore.get(personInPhoto);
        }
        return sumYearning;
    }
}
 
 

1. yearningScore = new HashMap<>();

    - key: 인물, value: 그리움 점수

2. answer[index++] = calcYearningScore(peopleInPhoto);

    - 그리움 점수 계산

    - if(yearningScore.get(personInPhoto)!=null)

        - 사진 속 그리움 대상 인물이 아니라면, yearningScore로 부터 그리움 점수 합산

 

😮 찾아본 풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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(photo[i][j].equals(name[k])) answer[i] += yearning[k];
                }
            }
        }
        return answer;
    }
}
 
 

* 3중 for문

    - photo의 각 인물과 name 배열 비교

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

Language: Java

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
import java.util.*;
 
class Solution {
    public String[] solution(String[] players, String[] callings) {
        String[] answer = new String[players.length];
        
        Map<Integer, String> runningMap = new HashMap<>();
        for(int i=0; i<players.length; i++){
            runningMap.put(i, runningMap.getOrDefault(i, players[i]));
        };
        
        for(String runner : callings){
            for(int i=0; i<players.length; i++){
                if(runningMap.get(i).equals(runner)){
                    String catchUpRunner = runningMap.get(i-1);
                    runningMap.replace(i-1, runner);
                    runningMap.replace(i, catchUpRunner);
                    break;
                }
            }
        };
        
        
        for(int i = 0; i < players.length; i++){
            answer[i] = runningMap.get(i);
        }
        
        return answer;
    }
}
 
 

😖 시간 초과

* callings( 1백만 ) * players( 5만 ) = 500억

 

 

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
import java.util.*;
 
class Solution {
    public String[] solution(String[] players, String[] callings) {
        String[] answer = new String[players.length];
        
        Map<Integer, String> runningMapIntStr = new HashMap<>();
        for(int i=0; i<players.length; i++){
            runningMapIntStr.put(i, runningMapIntStr.getOrDefault(i, players[i]));
        };
        
        Map<String, Integer> runningMapStrInt = new HashMap<>();
        for(int i=0; i<players.length; i++){
            runningMapStrInt.put(players[i], runningMapStrInt.getOrDefault(players[i], i));
        };
 
        for(String runner : callings){
            int catchUpRank = runningMapStrInt.get(runner)-1;
            String catchUpRunner = runningMapIntStr.get(catchUpRank);
            runningMapIntStr.replace(catchUpRank, runner);
            runningMapIntStr.replace(catchUpRank+1, catchUpRunner);
            runningMapStrInt.replace(runner, catchUpRank);
            runningMapStrInt.replace(catchUpRunner, catchUpRank+1);
        };
        
        for(int i = 0; i < players.length; i++){
            answer[i] = runningMapIntStr.get(i);
        }
        
        return answer;
    }
}
 
 

1. runningMapIntStr, runningMapStrInt

    - key, value를 ranking과 runner 양방향으로 2개의 Map 선언

2. for(String runner : callings)

    - 순위 변동 시, 2개의 map 모두 replace하도록 유의

    * map은 중복제거되므로 replace 대신 put 사용 가능

 

 

😮 찾아본 풀이

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
import java.util.*;
 
class Solution {
    public String[] solution(String[] players, String[] callings) {
        String[] answer = new String[players.length];
        
        Map<String, Integer> runningMapStrInt = new HashMap<>();
        for(int i=0; i<players.length; i++){
            runningMapStrInt.put(players[i], i);
        };
 
        for(String runner : callings){
            int catchUpRank = runningMapStrInt.get(runner)-1;
            String catchUpRunner = players[catchUpRank];
            runningMapStrInt.put(catchUpRunner, catchUpRank+1);
            runningMapStrInt.put(runner, catchUpRank);
            
            players[catchUpRank] = runner;
            players[catchUpRank+1= catchUpRunner;
        };
        
        int index = 0;
        for(String player : players) {
            answer[index++= player;
        }
 
        return answer;
    }
}
 
 

* runningMapIntStr 대신 players 배열 직접 사용

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr