코딩 문제 풀이/프로그래머스8 [프로그래머스] Lv3 문제풀이8(feat. JAVA) 가장 긴 팬린드롬 class Solution { public int palindrome(int right, int left, String s) { while(right = 0 && s.charAt(right) == s.charAt(left)) { right++; left--; } return right - left - 1; } public int solution(String s) { int answer = 0; for(int i = 0; i < s.length(); i++) { answer = Math.max(answer, palindrome(i+1, i-1, s)); answer = Math.max(answer, palindrome(i+1, i, s)); } re.. 2023. 10. 24. [프로그래머스] Lv3 문제풀이7(feat. JAVA) 디스크 컨트롤 import java.util.*; import java.util.stream.IntStream; class Solution { class Job { int start; int cost; Job(int start, int cost) { this.start = start; this.cost = cost; } } public int solution(int[][] jobs) { int answer = 0; PriorityQueue heap = new PriorityQueue((o1, o2) -> { if(o1.start != o2.start) return o1.start - o2.start; else return o1.cost - o2.cost; }); IntStream.range(0, jobs.. 2023. 10. 13. [프로그래머스] Lv3 문제풀이6(feat. JAVA) 가장 먼 노드 import java.util.*; import java.util.stream.IntStream; class Solution { public int solution(int n, int[][] edge) { int answer = 0; ArrayList[] list = new ArrayList[n+1]; for(int[] e : edge) { int x = e[0]; int y = e[1]; if(list[x] == null) list[x] = new ArrayList(); list[x].add(y); if(list[y] == null) list[y] = new ArrayList(); list[y].add(x); } int[] dist = new int[n+1]; Arrays.fill(dis.. 2023. 10. 11. [프로그래머스] Lv3 문제풀이5(feat. JAVA) 불량 사용자 import java.util.*; class Solution { public int cnt = 0; public Set lst = new HashSet(); public void permutation(String[] user_id,String[] banned_id, int depth, int n, int r) { if (depth == r) { boolean wrong = false; String[] slist = new String[r]; for(int i = 0; i < r; i++) { if(banned_id[i].length() != user_id[i].length()){ wrong = true; break; } for(int j = 0; j < banned_id[i].length(.. 2023. 10. 10. [프로그래머스] Lv3 문제풀이4(feat. JAVA) 기지국 설치 import java.util.*; class Solution { public int solution(int n, int[] stations, int w) { int answer = 0; int end = 0; ArrayList area = new ArrayList(); for(int i = 0; i end) { area.add(start - end); } end = stations[i] + w; } if(n - end > 0) { area.add(n - end); } int scope = w * 2 + 1; for(int a : area) { answer += a /.. 2023. 10. 10. [프로그래머스] Lv3 문제풀이3(feat. JAVA) 등굣길 - Dynamic Programming class Solution { public long[][] dp; public int solution(int m, int n, int[][] puddles) { int answer = 0; dp = new long[n+1][m+1]; dp[1][1] = 1; for(int[] a : puddles) dp[a[1]][a[0]] = -1; for(int i = 1; i o1.end - o2.end); ArrayList result = new ArrayList(); int last = -30_001; for(int i = 0; i last) { last = route.. 2023. 10. 6. [프로그래머스] Lv3 문제풀이2(feat. JAVA) 야근 지수 import java.util.*; class Solution { public long solution(int n, int[] works) { long answer = 0; Arrays.sort(works); long[] cnt = new long[works[works.length-1] + 1]; long sum = 0; for(int a : works) { sum += a; cnt[a]++; } if(sum 1; i--) { if(cnt[i] < n) { cnt[i-1] += cnt[i]; n -= cnt[i]; cnt[i] = 0; } else { cnt[i] -= n; cnt[i-1] += n; n = 0; break; } } if(n!=0) cnt[1] -= n; for(int i = .. 2023. 10. 6. [프로그래머스] Lv3 문제풀이1(feat. JAVA) 정수 삼각형 - Dynamic Programming import java.io.*; import java.util.*; class Solution { public int solution(int[][] triangle) { int answer = 0; int[][] dp = new int[triangle.length][triangle[triangle.length-1].length]; dp[0][0] = triangle[0][0]; for(int i = 1; i < triangle.length; i++) { for(int j = 0; j < i + 1; j++) { if(j == 0) { dp[i][j] = dp[i-1][j] + triangle[i][j]; } else if (j == i) { dp[i][.. 2023. 10. 4. 이전 1 다음 728x90 반응형