백준알고리즘
-
[JAVA] 9012번: 괄호Algorithm/백준 알고리즘 2021. 7. 4. 16:04
문제 설명 괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고 부른다. 한 쌍의 괄호 기호로 된 “( )” 문자열은 기본 VPS 이라고 부른다. 만일 x 가 VPS 라면 이것을 하나의 괄호에 넣은 새로운 문자열 “(x)”도 VPS 가 된다. 그리고 두 VPS x 와 y를 접합(concatenation)시킨 새로운 문자열 xy도 VPS 가 된다. 예를 들어 “(())()”와 “((()))” 는 VPS 이지만 “(()(”, “(())()))” , 그리고 “(()” 는 모두 VPS 가 아닌 문자열이다. 여러분은 입력으로 주어진 괄호 ..
-
[JAVA] 9093번: 단어 뒤집기Algorithm/백준 알고리즘 2021. 7. 4. 00:10
문제 설명 문장이 주어졌을 때, 단어를 모두 뒤집어서 출력하는 프로그램을 작성하시오. 단, 단어의 순서는 바꿀 수 없다. 단어는 영어 알파벳으로만 이루어져 있다. 입력 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 공백이 하나 있다. 입력 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 공백이 하나 있다. 풀이 1. 문장을 저장하기 위해, String형 배열 array를 선언하고 크기를 T만큼 설정하였다..
-
[JAVA] 10828번:스택Algorithm/백준 알고리즘 2021. 7. 3. 23:36
문제 설명 정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오. 명령은 총 다섯 가지이다. push X: 정수 X를 스택에 넣는 연산이다. pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다. size: 스택에 들어있는 정수의 개수를 출력한다. empty: 스택이 비어있으면 1, 아니면 0을 출력한다. top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다. 입력 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,0..
-
[JAVA/Level1] K번째수Algorithm/프로그래머스 2021. 6. 23. 11:13
문제 설명 배열 array의 i번재 숫자부터 j번째 숫자까지 자르고 정렬했을 때, K번째에 있는 수를 구하려 합니다. 예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면 array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다. 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다. 2에서 나온 배열의 3번째 숫자는 5입니다. 배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return하도록 solution함수를 작성해주세요. 제한 조건 array의 길이는 1이상 100이하입니다. arra..
-
[JAVA/Level1] 완주하지 못한 선수Algorithm/프로그래머스 2021. 6. 22. 17:45
문제 설명 수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return하도록 solution함수를 작성해주세요. 제한 조건 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다. completion의 길이는 participant의 길이보다 1작습니다. 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있습니다. 참가자 중에는 동명이인이 있을 수 있습니다. 입출력 예 풀이 HashMap자료형은 의 형태로 이루어진 자료형으로 처음 접하게 되었다. ..
-
[JAVA/Level2] 정렬된 수에서 하나의 수 위치 찾기Algorithm/프로그래머스 2021. 6. 11. 23:33
문제 설명 여러 개의 수가 정렬된 순서로 있을 때 특정한 수를 찾으세요. 수의 예: [12, 25, 31, 48, 54, 66, 70, 83, 95, 108] 단순 반복문을 이용하면 수의 개수에 따라 비교 횟수가 증가하는 O(n)의 수행이 이루어진다. 수가 정렬된 상태에서 이진 탐색을 활용할 경우 매번 비교되는 요소의 수가 절반으로 감소될 수 있으므로 O(logN)의 수행으로 원하는 수를 찾을 수 있다. 입출력 예 찾고자하는 수의 위치를 찾을 경우 배열 내 인덱스의 값을 출력하세요. 찾고자하는 수가 존재하지 않을 경우 "찾는 숫자가 존재하지 않습니다."를 출력하세요. 풀이 수가 정렬된 상태이므로 중간의 값을 하나 선택하고, 찾으려는 값이 그보다 크면 범위를 오른쪽으로 그보다 작으면 범위를 왼쪽으로 좁히며..
-
[JAVA/자료구조] 자료구조(Data Structure)Language/Java 2021. 6. 8. 12:56
자료구조란? 프로그램에서 사용할 많은 데이터를 메모리 상에서 관리하는 여러 구현 방법이다. 효율적인 자료구조가 성능 좋은 알고리즘의 기반이 된다. 자료의 효율적인 관리는 프로그램의 수행속도와 밀접한 관련이 있다. 여러 자료구조 중 구현하려는 프로그램에 맞는 최적의 자료구조를 활용해야하므로 자료구조에 대한 이해가 중요하다. 자료구조 종류 배열(Array) - 선형으로 자료를 관리, 정해진 크기의 메모리를 먼저 할당받아 사용한다. - 자료의 물리적 위치와 논리적 위치가 같다. 연결 리스트(LinkedList) - 선형으로 자료를 관리, 자료가 추가될 때마다 메모리를 할당 받고 자료는 링크로 연결된다. - 자료의 물리적 위치와 논리적 위치가 다를 수 있다. 큐(Queue) - 가장 먼저 입력 된 자료가 가장 ..
-
[JAVA/Level1] 문자열을 정수로 바꾸기Algorithm/프로그래머스 2021. 6. 1. 23:49
문제 설명 문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요. 제한 조건 s의 길이는 1 이상 5이하입니다. s의 맨앞에는 부호(+, -)가 올 수 있습니다. s는 부호와 숫자로만 이루어져있습니다. s는 "0"으로 시작하지 않습니다. 입출력 예 예를들어 str이 "1234"이면 1234를 반환하고, "-1234"이면 -1234를 반환하면 됩니다. str은 부호(+,-)와 숫자로만 구성되어 있고, 잘못된 값이 입력되는 경우는 없습니다. 풀이 1. String을 int로 변경하기 위하여 Integer.parseInt를 사용하였다. 2. 변경된 값을 answer에 저장하고, 해당 값을 리턴하였다. 코드