본문 바로가기

코딩테스트

(55)
[Python] 백준 1654. 랜선 자르기 https://www.acmicpc.net/problem/1654 1654번: 랜선 자르기 첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그 www.acmicpc.net 문제 설명: 만약 4개의 랜선이 주어지고 11개의 랜선을 만들려고 할 때 최대 길이 갯수를 구하는 문제이다. 4개의 랜선은 예를 들면 [802, 743, 457, 539] 랜선의 길이가 주어진다. 최대 길이가 200일때 802cm 랜선에서 4개, 743cm 랜선에서 3개, 457cm 랜선에서 2개, 539cm 랜선에서 2개를 잘라내 모두 11개를 만들 수 있다. 이진탐..
[Python][leetCode] List Sort(리스트 정렬) https://leetcode.com/problems/sort-list/description/ Sort List - LeetCode Can you solve this real interview question? Sort List - Given the head of a linked list, return the list after sorting it in ascending order. Example 1: [https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg] Input: head = [4,2,1,3] Output: [1, leetcode.com 문제설명 연결 리스트가 주어졌을때 퀵 정렬로 정렬하는 방식으로 구현해보았다. quick sort 방식은..
[Python][LeetCode] Insertion Sort List(삽입 정렬 리스트) https://leetcode.com/problems/insertion-sort-list/description/ 문제 설명: 연결리스트로 노드들이 주어졌을때 이것을 삽입정렬 방식으로 정렬하는 것이다. head는 링크들로 연결된 노드들로 이루어져있다. 1. 더미노드를 만든다. 2. 더미노드에 다음노드로 head가 가르키는 노드 값을 새로운 노드로 만들어 next로 추가한다. 3. 더미노드에 포인터 p를 두어 p가 증가하면서 새로운 노드들이 위치 할 곳을 찾는다. 예시 [4,6,5,9,1] test = ListNode(4, ListNode(6, ListNode(2, ListNode(9, ListNode(1))))) Solution().insertionSortList_2(test) 더미노드는 지금 빈 노드를 ..
[Python] [LeetCode] Balanced Binary Tree (균형 이진 트리) https://leetcode.com/problems/balanced-binary-tree/description/ Balanced Binary Tree - LeetCode Can you solve this real interview question? Balanced Binary Tree - Given a binary tree, determine if it is height-balanced. Example 1: [https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg] Input: root = [3,9,20,null,null,15,7] Output: true Exam leetcode.com 문제 설명: 이진트리가 주어지면 균형 잡힌 이진트리인지 확인하고..
[Python][LeetCode] Invert Tree https://leetcode.com/problems/invert-binary-tree/description/ Invert Binary Tree - LeetCode Can you solve this real interview question? Invert Binary Tree - Given the root of a binary tree, invert the tree, and return its root. Example 1: [https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg] Input: root = [4,2,7,1,3,6,9] Output: [4 leetcode.com 문제 설명 주어진 리스트를 트리로 바꿔 왼쪽과 오른쪽이 바뀐 값을 출력..
[Python] 백준 9095번. 1,2,3 더하기 https://www.acmicpc.net/problem/9095 9095번: 1, 2, 3 더하기 각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 출력한다. www.acmicpc.net 정수 n 에 대해 1,2,3으로 합을 만들 수 있는 경우의 수를 구하는 문제이다. 여기서 주의: 1이 처음 빠진다고 해서 두번째에도 1이 들어 갈 수 있다. for문을 n이 0이 될때까지 돌려주면 된다. 반복되는 과정이기 때문에 재귀함수로 구현가능 def sum(n): def dfs(n, arr): for i in [1,2,3]: if n-i == 0: result.append(arr+[i]) return else: dfs(n-i, arr+[i]) result = [] dfs(n, []) ret..
[Python] 백준 1795번. 암호 만들기 https://www.acmicpc.net/problem/1759 1759번: 암호 만들기 첫째 줄에 두 정수 L, C가 주어진다. (3 ≤ L ≤ C ≤ 15) 다음 줄에는 C개의 문자들이 공백으로 구분되어 주어진다. 주어지는 문자들은 알파벳 소문자이며, 중복되는 것은 없다. www.acmicpc.net 문제: 주어진 문자를 가지고 L자리 비밀번호를 조합하여 비밀번호를 풀려고 한다. 주의 할 점은 비밀번호 조합에는 최소 하나의 모음과, 최소 두개의 자음을 포함하고 있어야 한다. 문자가 4개 만들어지면 반환하도록 한다. 입력받은 문자열은 정렬이 되어야 한다. 출력결과를 보면 문자가 오름차순으로 정렬이 된 것으로 보인다. 결국 첫번째 자리 < 두번째 자리 < 세번째 자리 < 네번째 자리순으로 정렬되어야 한..
[LeetCode] Subsets(부분집합) https://leetcode.com/problems/subsets/description/ Subsets - LeetCode Can you solve this real interview question? Subsets - Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. Example 1: Input: n leetcode.com 문제 설명 부분집합을 구하는 문제이다. [1,2,3] 리스트가 주어졌을때 총 [],[1],[2],[3],[1,..