Algorithm/프로그래머스83 [Lv. 1] 2016년 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 select = 0; for(int i = 0; i < a-1; i++){ select += date[i]; } select += b-1; answer = day[select % 7]; return answer; } } 1. 요일을 나타내는 문자열 배열 day 생성 (시작일이 금요일이라고 명시되었기 떄문에 금요일부터 시작) 2. 각 월.. Algorithm/프로그래머스 2023. 10. 2. [Lv. 2] JadenCase 문자열 만들기 class Solution { public String solution(String s) { String answer = ""; StringBuilder sb = new StringBuilder(); String first = s.charAt(0) + ""; sb.append(first.toUpperCase()); for(int i = 1; i < s.length(); i++){ String now = s.charAt(i) + ""; if(now.equals(' ')) sb.append(" "); else if(s.charAt(i - 1) == ' ') sb.append(now.toUpperCase()); else sb.append(now.toLowerCase()); } answer = sb.toStri.. Algorithm/프로그래머스 2023. 9. 17. [Lv. 1] 로또의 최고 순위와 최저 순위 1. 구매한 로또 번호 중 알아볼 수 없는 번호/ 맞춘 번호 구하기 2. 최댓값 = 맞춘 번호 + 알아볼 수 없는 번호 //알아볼 수 없는 번호가 모두 맞았다 가정 3. 최솟값 = 맞춘번호 //알아볼 수 없는 번호가 모두 틀렸다 가정 4. 맞춘 개수로 최고 순위 최저 순위 산정 class Solution { public int[] solution(int[] lottos, int[] win_nums) { int[] answer = new int[2]; int win = 0; // 맞춘 번호 int zero = 0; // 알아볼 수 없는 번호 for(int i : lottos){ // 번호를 알아볼 수 없는 경우 if(i == 0) zero++; else { for(int j : win_nums){ if(i.. Algorithm/프로그래머스 2023. 9. 17. [Lv. 1] 명예의 전당(1) 두 가지 경우로 나눴다! 명예의 전당에 점수가 꽉 차지 않은 경우와 꽉 차있는 경우! 꽉 차있지 않은 경우는 그냥 선언한 리스트에 들어오는 점수를 추가해주고 정렬해서 최솟값 출력! 꽉 차있는 경우는 기존 명예의 전당에 있던 최솟값과 새로 추가 되는 i 번째 값을 비교한 후 새로 추가되는 값이 최솟값보다 크다면 기존값을 지우고 리스트에 새로운 값을 추가해주는 식으로 풀었다! 출력해야하는 값이 최솟값이므로 리스트의 0번째 값을 출력하도록 만들었다! import java.util.*; class Solution { public int[] solution(int k, int[] score) { int[] answer = new int[score.length]; int min = 0; List list = new.. Algorithm/프로그래머스 2023. 9. 7. [Lv. 1] 푸드 파이트 대회 food[0]은 무조건 물이므로 반복문을 돌 때 i = 1부터 돌게 만들었다 첫 루프에서 food 배열의 길이만큼 돌고 두 번째 루프에서 한 명이 i번째 음식을 몇개나 먹을 수 있는지 구했다 이 후부터는 문제에서 주어진 대로 물을 추가하고 그 문자를 그대로 뒤집어서 붙여주는 방식을 취했다! class Solution { public String solution(int[] food) { String answer = ""; String reverse = ""; for(int i = 1; i < food.length; i++){ for(int j = 0; j < food[i]/2 ; j++){ answer += i; } } StringBuilder sb = new StringBuilder(answer); re.. Algorithm/프로그래머스 2023. 9. 7. [Lv. 2] 점프와 순간 이동 배터리를 최소로 사용할 방법을 생각했다 -> 순간이동을 최대로 사용! 점프를 최소로 사용! n이 홀수 일 때만 1을 더해주는걸 n > 0일 때까지 반복했더니 탸댜~ 풀렸다 public class Solution { public int solution(int n) { int ans = 0; while(n > 0){ if(n % 2 != 0){ ans += 1; } n /= 2; } return ans; } } Algorithm/프로그래머스 2023. 8. 20. [Lv. 1] 바탕화면 정리 이것도 그냥 어차피 2차원 배열로 풀고 가장 큰 x,y 좌표를 저장하는 변수를 선언 해놓고 마지막에 이 좌표값에 그냥 + 1 하면 되는거 아니야??하고 냅다 풀어봤는데 그냥 풀렸다.. 넘쳐나는 if문의 향연... 분명 더 좋은 방법이 있을텐데 class Solution { public int[] solution(String[] wallpaper) { int[] answer = new int[4]; int lastX = wallpaper[0].length(); int lastY = wallpaper.length; int maxX = 0; int maxY = 0; for(int i = 0; i < wallpaper.length; i++){ for(int j = 0; j < wallpaper[0].length.. Algorithm/프로그래머스 2023. 8. 20. [Lv. 1] 신규 아이디 추천 사실 그냥 하라는대로 단계별로 했더니 풀렸다 물론...단계별로 하기 좀 귀찮길래 정규식 사용해서 풀었는데 이것 때문인지 몇몇 문제 시간이 오래 걸렸다 정규식 안쓰고 푸는 것도 해봐야겠다 class Solution { public String solution(String new_id) { String answer = ""; answer = new_id.toLowerCase(); //1단계 answer = answer.replaceAll("[^-_.a-z0-9]", ""); //2단계 answer = answer.replaceAll("[.]{2,}", "."); //3단계 answer = answer.replaceAll("^[.]|[.]$", ""); //4단계 if(answer.equals("")) { /.. Algorithm/프로그래머스 2023. 8. 20. [Lv. 2] 카펫 class Solution { public int[] solution(int brown, int yellow) { int[] answer = new int[2]; int w = 0; int h = 0; if(yellow == 1){ answer[0] = 3; answer[1] = 3; }else{ int temp = brown - 4; temp /= 2; for(int i = 1; i 앞에서 구한 값 /2 해서 가로+세로 타일 개수 구하기 -> yellow 타일 개수 가능한 N*M 조합을 찾는 방식으로 풀었는데 얘의 단점은 가로로 긴 경우에 구멍이 난다는 것...! class Solution { public int[] solution(int brown, int yellow) { int[] answer =.. Algorithm/프로그래머스 2023. 8. 8. [Lv. 1] 달리기 경주 class Solution { public String[] solution(String[] players, String[] callings) { String[] answer = new String[players.length]; HashMap Player = new HashMap(); HashMap Rank = new HashMap(); for (int i = 0; i < players.length; i++) { Player.put(players[i], i); Rank.put(i, players[i]); } for (String calling : callings) { int currentRank = Player.get(calling); String player = Rank.get(currentRank); .. Algorithm/프로그래머스 2023. 8. 8. [Lv. 1] 체육복 class Solution { public int solution(int n, int[] lost, int[] reserve) { int answer = n; int[] arr = new int[n]; Arrays.fill(arr, 1); for(int i : lost) arr[i - 1] -= 1; for(int i : reserve) arr[i - 1] += 1; for(int i = 0; i 0 && arr[i - 1] == 2){ //i의 왼쪽과 비교, 배열 내에 있어야 함 arr[i - 1]-=1; arr[i]++; } else if(i < n - 1 && arr[i + 1] == 2){ //i의 오른쪽과 비교, 배.. Algorithm/프로그래머스 2023. 8. 8. [Lv. 2] 영어 끝말잇기 1. 첫번째 단어부터 조건을 검색하도록 한다 2. 현재 단어가 이미 나온 단어인이거나 이전 단어의 마지막 글자와 현재 단어의 첫번째 글자가 일치하지 않는지 검사 - 조건이 참이면 잘못된 글자이므로 해당 위치를 answer에 저장하고 반환 - answer[0]: 해당 위치의 인덱스(i)를 한 줄에 들어가는 단어의 개수(n)로 나눈 나머지에 1을 더해 줄 번호 계산 - answer[1]: 해당 위치의 인덱스(i)를 한 줄에 들어가는 단어의 개수(n)로 나눈 몫에 1을 더해서 위치 계산 class Solution { public int[] solution(int n, String[] words) { int[] answer = new int[2]; Map map = new HashMap(); for(int i .. Algorithm/프로그래머스 2023. 7. 30. 이전 1 2 3 4 ··· 7 다음