본문 바로가기

알고리즘15

[프로그래머스] (완전탐색) 모의고사 단순히 문제 그대로... 완전탐색 했다. 세명의 찍는 패턴을 바탕으로 정답수를 구해서 별도의 배열에 저장하고, 그 중에 최대 정답수와 일치하는 것을 정답배열에 다시 담았다. 수포자가 3명이라 가능한 방법같은데.. 여러명이면 다른방법으로 해야 될 것 같다. import java.util.*; class Solution { public int[] solution(int[] answers) { int[] supo1 = {1,2,3,4,5}; int[] supo2 = {2,1,2,3,2,4,2,5}; int[] supo3 = {3,3,1,1,2,2,4,4,5,5}; int supo1Correct = 0; int supo2Correct = 0; int supo3Correct = 0; ArrayList cnt = .. 2021. 3. 25.
[프로그래머스] (정렬) H-Index 논문배열을 역정렬하거나, Arrays.sort를 해서 뒤부터 찾도록 하자. 원소의 인용횟수(인자) vs 정렬한 배열에서 원소보다 크거나 같은 갯수(루프 반복횟수) 를 세어서 역전이 일어나기 바로 직전이 H-Index. import java.util.*; class Solution { public int solution(int[] citations) { int answer = 0; Arrays.sort(citations); int len = citations.length; for (int i = len - 1; i >= 0; i--) { int citation = citations[i]; int iy = len - i; if(iy 2021. 3. 25.
[프로그래머스] (정렬) 가장 큰 수 굉장히 쉽다고 생각했지만 생각보다 굉장히 오래걸린 풀이... numbers sort return [2, 23, 231] 23 231 2 232312 [3, 321, 32] 3 32 321 332321 [3, 312, 31] 3 31 312 331312 위 처럼 다양한 경우에 따라 정렬이 달라져야 하는데, 처음에생각한 방법은 number의 원소는 최대 1000 이므로 첫번째자리의 숫자로 오른쪽을 다 채운다음 단순정렬. numbers rpad sort return [2, 23, 231] [2(22), 23(2), 231] [23(2), 231, 2(22)] 232312 [3, 321, 32] [3(33), 321, 32(3)] [3(33), 32(3), 321] 332321 [3, 312, 31] [3(33.. 2021. 3. 25.
[프로그래머스] (스택/큐) 프린터 하도 막혀서 어떻게든 복잡하게라도 풀어내고 빨리 다른사람 풀이를 보고 싶었던 문제.. 결국 스택과 큐는 사용하지 않았다. 문서의 인덱스와 문서를 조건에 따라 재배치하고 문서의 인덱스를 저장하는 배열에 따로 최종본을 저장하고 그 배열에서 조회할 문서를 다시 찾아온다. import java.util.*; class Solution { public int solution(int[] priorities, int location) { int answer = 0; List docIdx = new ArrayList(); List doc = new ArrayList(); List reIdxDoc = new ArrayList(); for (int i = 0; i < priorities.length; i++) { int .. 2021. 3. 23.
[프로그래머스] (스택/큐) 기능개발 각 작업의 소요일자를 구한 후, 선작업 부터 같이 배포가능한 원소들을 뽑아서(소요일자가 자신보다 작은것) 배열에서 remove 시키는 방법으로 구현하였다. 배열에서 원소를 지워나가는 가장 안전한 방법은 iterator를 사용하는 것. import java.util.*; class Solution { public int[] solution(int[] progresses, int[] speeds) { int[] answer = {}; List temp = new ArrayList(); List tempAns = new ArrayList(); // 각 작업이 배포될 수 있는 일자를 별도로 담아 놓는다. for (int i = 0; i < progresses.length; i++) { int progress =.. 2021. 3. 23.
[LeetCode] Two Sum Two Sum - LeetCode 숫자의 배열, 목표 숫자를 입력받고 배열 중 두 수를 골라서 더하면 목표 숫자가 나오는 두 수의 인덱스를 반환하라. class Solution { public int[] twoSum(int[] nums, int target) { int[] answer = new int[2]; for (int i = 0; i < nums.length; i++) { int num1 = nums[i]; for (int j = i + 1; j < nums.length; j++) { int num2 = nums[j]; if(num1 + num2 == target) { answer[0] = i; answer[1] = j; break; } } } return answer; } } 출처: LeetCo.. 2021. 3. 22.
반응형