본문 바로가기

Algorithm/Binary Search

(43)
(C++, Rust) - LeetCode (easy) 222. Count Complete Tree Nodes https://leetcode.com/problems/fair-candy-swap/description/ Fair Candy Swap - LeetCode Can you solve this real interview question? Fair Candy Swap - Alice and Bob have a different total number of candies. You are given two integer arrays aliceSizes and bobSizes where aliceSizes[i] is the number of candies of the ith box of candy that Alice h leetcode.com 이분탐색 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 1. alice 가 ..
(C++) - LeetCode (easy) 744. Find Smallest Letter Greater Than Target https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/ Find Smallest Letter Greater Than Target - LeetCode Can you solve this real interview question? Find Smallest Letter Greater Than Target - You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters. Retu leetc..
(C++) - LeetCode (easy) 704. Binary Search https://leetcode.com/problems/binary-search/description/ Binary Search - LeetCode Can you solve this real interview question? Binary Search - Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. leetcode.com 이분탐색 문제였습니다. 📕 풀이방법 📔 풀이과정 target이상의 iterator를..
(C++) - LeetCode (easy) 455. Assign Cookies https://leetcode.com/problems/assign-cookies/description/ Assign Cookies - LeetCode Can you solve this real interview question? Assign Cookies - Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size o leetcode.com greedy 또는 이분탐색으로 푼 문제였습니다. 📕 풀이방법 📔 입력 및 초기화..
(C++) - LeetCode (easy) 278. First Bad Version https://leetcode.com/problems/first-bad-version/description/ First Bad Version - LeetCode First Bad Version - You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions leetcode.com 이분 탐색 문제였습니다. 📕 풀이방법 📔 풀이과정 처음으로 isBadV..
(C++) - LeetCode (easy) 374. Guess Number Higher or Lower https://leetcode.com/problems/guess-number-higher-or-lower/ Guess Number Higher or Lower - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 이분탐색 문제였습니다. 📕 풀이방법 📔 풀이과정 1. 1초는 1억연산이므로 n이 2^31승인 경우 O(n) 인 시간복잡도를 가지는 brute force로 해결할 수 없습니다. O(logn)인 이분탐색을 사용해야합니다. 2. 1 ~ n-1까지의 범위를 이분탐..
(C++) - LeetCode (easy) 35. Search Insert Position https://leetcode.com/problems/search-insert-position/ Search Insert Position - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 이분탐색 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 target이상의 값이 나온 원소의 iterator를 lower_bound함수를 이용해 구해줍니다. 이 값을 지역변수 idx 선언 후 저장합니다. 📔 풀이과정 1. target이상의 값이 num에 없으면 lower_boun..
(C++) - 백준(BOJ) 3151 : 합이 0 https://www.acmicpc.net/problem/3151 3151번: 합이 0 Elly는 예상치 못하게 프로그래밍 대회를 준비하는 학생들을 가르칠 위기에 처했다. 대회는 정확히 3명으로 구성된 팀만 참가가 가능하다. 그러나 그녀가 가르칠 학생들에게는 큰 문제가 있었다. www.acmicpc.net 이분탐색 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 학생 수 n, 정답을 출력할 변수 ans, 코딩 실력을 저장할 vector v를 선언한 후 적절히 입력받습니다. 이후 v를 오름차순으로 정렬해줍니다. 📔 풀이과정 n이 1만이므로 brute force로 3중 for loop를 수행하면 O(4억)이 넘어 4초 초과로 틀리게 됩니다. O(n^2log(n))으로 2개의 수를 결정했으면 나머지 한 개의 수..