Coding Test
-
[LeetCode] 938. Range Sum of BST c++Coding Test/LeetCode 2022. 9. 26. 18:11
문제 Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. 이진탐색트리의 node중에 low와 high사이에 있는 값을 모두 더한 값을 구하는 문제이다. 문제 해결 class Solution { public: int nSum = 0; int rangeSumBST(TreeNode* root, int low, int high) { if(root == NULL) return nSum; if(root->val >= low && root-> val val; } rangeSumBST..
-
[LeetCode] 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree c++Coding Test/LeetCode 2022. 9. 26. 15:17
문제 Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree. Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree. original 트리에 target노드를 찾는 문제이다. 문제 해결 Tree..
-
[프로그래머스] 양궁대회 c++Coding Test/programmers 2022. 9. 26. 13:30
문제 설명 카카오배 양궁대회 결승전에 라이언과 어피치가 대결한다. 어피치가 먼저 화살을 모두 쏜 후, 라이언이 화살을 모두 쐈을 때, 라이언이 큰 점수 차이로 이기는 방법을 구하는 문제이다. (방법이 여러개라면, 가장 낮은 점수를 더 많이 맞히 경우가 정답이다.) 점수 계산법 - k점을 여러 발 맞혀도 k점 보다 많은 점수를 가져가는 게 아니고 k점만 가져감. 10점에 어피치가 3발, 라이언이 2발 쐈다면 어피치가 10점을 얻게 되고 라이언은 0점을 얻게된다. 라이언이 점수를 획득하기 위해서는 어피치보다 +1개를 쏴야 점수도 얻고 불필요한 화살 소모를 줄일 수 있다. 효율적으로 점수를 획득해야 한다. 불필요한 화살 소모는 다음과 같다. 1) K점 과녁에 a개 이하의 화살을 쏘는 경우 2) K점 과녁에 a..
-
[codility] MinAbsSum c++Coding Test/codility 2022. 9. 23. 21:15
문제 문제 해결 각 원소로 만들 수 있는 합의 개수 구하고, 그 합들을 이용해 전체 합에서 가장 작은 수가 구해질 때 까지 빼나간다. 1. A행렬의 원소를 모두 양수로 전환한다. A원소에 1 or -1 곱한 뒤 합을 구하는 거라 A의 원소가 양수여도 상관없다. 2. 양수로 변환된 A행렬의 원소의 합을 구하고 최댓값도 구한다. 최댓값을 구하는 이유는 각 원소의 개수를 구하는 것이고, 전체 합을 구하는 이유는 답의 가장 최악의 경우는 전체 합이기 때문 3. 각 원소마다 돌면서 가능한 부분 합들을 구한다. 이전에 가능한 부분합이 있었다면, 단순히 현재 원소의 개수를 넣어준다. 처음 만들어진 부분합이라면, 재사용한 것을 표기하기 위해 -1 한다. 4. 부분 합들을 전체합의 절반까지 계산하면서 전체 합에서 2배만..
-
[Codility] NumberSolitaire c++Coding Test/codility 2022. 9. 16. 19:19
문제 N개의 연속된 정사각형으로 구성된 보드에서 게임이 진행된다. 0번 정사각형의 조약돌을 N-1 정사각형으로 이동시켜야 하는 게임이다. 각 차례마다, 주사위를 던지고 주사위 윗면의 숫자 K를 고려한다. 그런 다음 현재 I에 서 있는 조약돌을 I + K로 이동시킨다. I + K가 존재하지 않으면 유효한 인덱스를 얻을 때까지 주사위를 다시 던진다. 게임이 끝난 후(조약돌이 N-1 로 이동했을 때), 이동한 모든 사각형에 쓰여진 숫자의 합을 구한다. 결론, A[0] → A[N-1] 로 이동할 때의 배열 원소의 최대 합을 구하는 문제이다. 문제 해결 int solution(vector &A) { vector temp(A.size(), -1); temp[0] = A[0]; for(unsigned int i = ..
-
[LeetCode] 27. Remove Element c++Coding Test/LeetCode 2022. 9. 15. 13:08
문제 설명 Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k el..
-
[LeetCode] 26. Remove Duplicates from Sorted Array c++Coding Test/LeetCode 2022. 9. 15. 10:28
문제 설명 Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elemen..
-
[LeetCode] 1. Two Sum c++Coding Test/LeetCode 2022. 9. 14. 17:59
문제 설명 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. 제한 사항 2