본문 바로가기

Algorithm

(177)
[LeetCode] 130. Surrounded Regions (BFS, DFS, TDD, 코테, 릿코드, tech interview) 간만에 미디움 풀었더니 빡셌음 퍼포먼스는 딱히.. 리팩토링 하느라 테스트코드만 매우 꼼꼼하게 작성하게 됨 테스트코드 import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class LeetCode_0130_SurroundedRegionsTest { @DisplayName("BFS, leetcode 130. Surrounded Regions Test 1.") @Test void surroundedRegionsTest_1() { LeetCode_013..
[LeetCode] 1742. Maximum Number of Balls in a Box (TDD, 코테, 릿코드, tech interview) TDD로 알고리즘 풀이 후 리팩토링 리팩토링해서 함수만들고 하면 성능이 좀 떨어짐 테스트코드 package algorithm_sites.leetcode; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; public class LeetCode_1742_MaximumNumberOfBallsInABoxTest { private LeetCode_1742_MaximumNumberOfBallsInABox test = new LeetCode_1742_MaximumNumberOfBallsInABox(); @Test public void countBallsTest() { Assertions.assertThat(test.countBall..
[LeetCode] 7. Reverse Integer (코딩테스트, 릿코드, tech interview) 프로그래밍 처음 시작할 때, 막 풀었던 것보다 코드도 깨끗해지고 퍼포먼스도 훨씬 올라가서 나름 좋았다. 3번에 걸쳐서 리팩토링 했고, 최대 퍼포먼스 뽑아냄 하지만 개인적으로는 2번째 코드가 좀 더 정리하면 가장 읽기 편할 것으로 보여서 좋아보인다. package algorithm_sites.leetcode; public class LC_0007_ReverseInteger { public int reverse_3(int x) { if (x == 0) { return 0; } boolean isPositive = x > 0; int result = 0; int pop; try { while (x != 0) { pop = x % 10; x /= 10; if (isPositive && result > (Inte..
[LeetCode] 1. Two Sum (코딩테스트, 릿코드, tech interview) 릿코드 하나씩 공부하면서 열심히 풀기로 1, 2, 3 세번에 걸쳐서 퍼포먼스를 올렸고, 거의 최대치로 나옴 Process // 1. Input // 2. Iterate all with hashmap (i) // 2.1. Check target-nums[i] // 2.1.1. If so -> return answer // 3. Finish package algorithm_sites.leetcode; import java.util.*; public class LC_00001_TowSum { public int[] twoSum_1(int[] nums, int target) { if (nums.length == 2) { return new int[] {0,1}; } for (int i = 0; i < nums...
[Codility] MaxNonoverlappingSegments, 코딜리티 그리디 greedy 문제는 다음과 같다. 풀이 방법으로 생각했던 것은 다음과 같다. 모든 세그먼트를 하나의 평면으로 통합시키면, 중복은 전부 연결이 된다. 이 때 떨어져있는 세그먼트들의 수를 세면 된다. 통과되는 답은 다음과 같다. 계속 50프로만 되다가 다른 블로그를 보고 참고했는데.. public class MaxNonoverlappingSegments { public static void main(String args[]) { int count = solution(new int[] {1, 3, 7, 9, 9, 10}, new int[] {5, 6, 8, 9, 10, 10}); System.out.println(count); } public static int solution(int[] A, int[] B) { if (..
[프로그래머스] 전화번호 목록, 알고리즘 문제풀이, Hash, 해시 이전에 파이썬으로 풀었던 방식은, 정렬 후에 두개의 for 문을 사용하되 해당 순서의 길이보다 긴 것들만 찾아보는 식으로 풀었었다. 21년 3월 효율성 테스트케이스 추가되면서 안되는 듯해서 연습하다가 java로 다시 풀었다. Hash 문제라서 HashSet을 사용했는데, 단어 하나하나에서 각 전체의 substring을 구하는건 비효율적으로 보였으나, 문자열 자체를 비교하는 것 보다는, substring 에서 잘라낸 문자열을 hash 에 있는지 없는지 확인하는 것으로 찾는 것이 더 빨랐다. package algorithm_sites.programmers; import java.util.HashSet; public class hash_02_phone_number_prefix { public static vo..
Python sandglass 파이썬 모래시계 * 찍기 알고리즘 파이써닉하지 않은 모래시계 생성 코드 def calculate_star_count(input_number, row_index): return abs((input_number//2 - row_index) * 2) + 1 def calculate_space_count(input_number, star_count): return (input_number - star_count) // 2 def draw_space(space_count): for j in range(0, space_count): print(' ', end='') def draw_star(star_count): for j in range(0, star_count): print('*', end='') def new_line(): print() if..
백준 16236 - 아기상어,시뮬레이션,우선순위큐, 그리디,알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, Leetcode, search, greedy,acmicpc,bfs,dfs, priority queue // Process // 1. Input n of nxn, nxn matrix // 2. 먹을 수 있는 물고기가 있으면 반복한다. // 2.1. 먹을 수 있는 물고기 중, 가장 가깝고, 맨 위의 왼쪽에 있는 물고기와의 거리를 센다. // 2.2. 해당 위치로 상어를 이동시킨다. // 2.3. 총 거리에 센 거리를 더한다. // 2.4. 사이즈업까지 남은 고기수를 줄인다. // 2.5. 빈칸으로 만든다. // 3. 총 거리를 반환한다. import java.io.*; import java.util.*; class BabyShark { static List eatable; public static void main(String[] args) throws IOException { // 1. BufferedRe..