본문 바로가기
반응형

전체 글131

[코딩인터뷰완전분석] 21일차. 수학 및 논리 퍼즐 6.5 public class Question { public static int breakingPoint = 89; public static int countDrops = 0; public static boolean willBreak(int floor) { countDrops++; return floor >= breakingPoint; } public static int findBreakingPoint(int floors) { int interval = 14; int previousFloor = 0; int egg1 = interval; /* Drop egg1 at decreasing intervals. */ while (!willBreak(egg1) && egg1 max ? countDrops : m.. 2023. 9. 30.
[코딩인터뷰완전분석] 13일차. 스택과 큐 3.4 스택으로 큐 import java.util.Stack; public class MyQueue { Stack stackNewest, stackOldest; public MyQueue() { stackNewest = new Stack(); stackOldest = new Stack(); } public int size() { return stackNewest.size() + stackOldest.size(); } public void add(T value) { stackNewest.push(value); } private void shiftStacks() { if (stackOldest.isEmpty()) { while (!stackNewest.isEmpty()) { stackOldest.push(s.. 2023. 9. 28.
[코딩인터뷰완전분석] 20일차. 수학 및 논리 퍼즐 아리스토테네스의 체 public class SieveOfEratosthenes { public static void crossOff(boolean[] flags, int prime) { for (int i = prime * prime; i < flags.length; i += prime) { flags[i] = false; } } public static int getNextPrime(boolean[] flags, int prime) { int next = prime + 1; while (next < flags.length && !flags[next]) { next++; } return next; } public static void init(boolean[] flags) { flags[0] = fals.. 2023. 9. 28.
[코딩인터뷰완전분석] 18일차. 비트조작 🐛 비트조작을 해보자 1. 0110 + 0110 은 0110 * 2 와 같고, 0110 을 왼쪽으로 한번 shift 하는것과 같다. 0100 = 4, 0100 * 4 는 왼쪽으로 두번 shift 하는것과 같다. 따라서 0011 을 왼쪽으로 두 번 shift 하면 1100 이 된다. 이 연산은 두개를 따로 생각해야 한다. a ^ (~a) 는 항상 1이 된다. ~0 은 모두 1이 된다. (1111) 이 값은 and 연산하면 1000 이다. 🐛 비트조작을 할 때 알아야 할 사실과 트릭들 1s 는 1111 이고, 0s 는 0000 이다. 편의상 4자리 2진수라 가정한다 🐛 2의 보수와 음수 컴퓨터는 정수를 저장할 때 2의 보수 형태로 저장한다. 양수는 문제가 없지만 음수는 절대값에 부호비트를 1로 셋팅한 뒤, .. 2023. 9. 26.
[코딩인터뷰완전분석] 19일차. 비트조작 5.5 디버거 ((n & (n-1)) == 0 ) 주어진 코드 ((n & (n-1)) == 0)는 주어진 정수 n이 2의 거듭제곱인지를 확인하는 코드입니다. 이 코드는 비트 연산을 사용하여 정수 n이 2의 거듭제곱인 경우 true를 반환하고, 그렇지 않은 경우 false를 반환합니다. n - 1을 계산합니다. 이렇게 하면 n의 이진 표현에서 가장 오른쪽에 있는 1 비트가 0으로 바뀝니다. 예를 들어, n이 8이라면 n - 1은 7이 됩니다. 이진 표현으로는 8이 "1000"이고 7은 "0111"이 됩니다. `n & (n - 1)`을 계산합니다. 이는 `n`과 `n - 1`의 비트 단위 AND 연산을 수행합니다. 이 과정에서 `n`의 이진 표현에서 오른쪽에서부터 처음으로 나타나는 1 이하의 비트들은 모두 .. 2023. 9. 26.
[코딩인터뷰완전분석] 17일차. 트리와 그래프 4.7 순서 정하기 import java.util.ArrayList; import java.util.HashMap; public class Project { public enum State {COMPLETE, PARTIAL, BLANK}; private ArrayList children = new ArrayList(); private HashMap map = new HashMap(); private String name; private State state = State.BLANK; public Project(String n) { name = n; } public String getName() { return name; } public void addNeighbor(Project node) { if (!.. 2023. 9. 26.
[Algorithm] Maximum Depth of Binary Tree 01. 문제내용 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree - LeetCode Can you solve this real interview question? Maximum Depth of Binary Tree - Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf leetcode.com Given the root of a b.. 2023. 6. 15.
[Algorithm] 이상한 문자 만들기 (5.79ms -> 0.15ms개선) 01. 문제 https://school.programmers.co.kr/learn/courses/30/lessons/12930 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 문자열 s는 한 개 이상의 단어로 구성되어 있다. 각 단어는 하나 이상의 공백문자로 구분되어 있고, 각 단어의 짝수번째 알파벳은 대문자로, 홀수번째 알파벳은 소문자로 바꾼 문자열을 리턴하는 함수, solution을 완성하는 문제이다. 02. 문제 풀이로 배운점 이번 문제는 String을 사용하여 풀이하였지만 runtime이 생각보다 오래 걸려서 저번에 사용해봤던 StringBuilde.. 2023. 6. 12.
[Algorithm]3진법 뒤집기(2.87ms -> 0.05ms) 01. 문제내용 https://school.programmers.co.kr/learn/courses/30/lessons/68935 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 02. 문제풀이로 배운점 10진법으로 제공된 수를 3진법으로 바꾼 후 reverse하여 다시 10진법으로 반환하는 알고리즘이다. 초반 풀이에는 stack을 쓰면 유용할 것 같아 Java에서 제공하는 Stack을 사용하여 문제를 풀이하였다. 다른 사람들의 풀이에 StringBuilder가 있어 코드를 개선해보았다. 최대 2.87ms -> 0.05ms 까지 runtime을 개선할 수 .. 2023. 6. 11.
반응형