๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/LeetCode

[LeetCode] Algorithm I - Day 1 Binary Search

by yunamom 2022. 10. 26.
๋ฐ˜์‘ํ˜•

Algorithm I - Day 1 Binary Search

In mathematics and computer science, an algorithm is defined as a process or set of rules to be followed in calculations or other problem-solving operations. This practical method is often used in calculations, data processing, and automatic reasoning because it contains clear and concise instructions and can be executed in limited time and space complexities.


704. 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.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

๐Ÿ“– ๋ฌธ์ œ. target์ด ๋ฐฐ์—ด์•ˆ์— ์žˆ์œผ๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
O(log n) ๋Ÿฐํƒ€์ž„ ๋ณต์žก๋„๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•ด์•ผ ํ•จ

class Solution {
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length-1;
        
        while(left <= right){
            int mid = (left + right) / 2;
            if(nums[mid] == target){
                return mid;
            } 
            else if(target > nums[mid]) {
                left = mid + 1;
            }
            else{
                right = mid - 1;
            }        
        }
        return -1;
    }
}

278. 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 after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.

Example 2:

Input: n = 1, bad = 1
Output: 1

๐Ÿ“– ๋ฌธ์ œ. n๊ฐœ์˜ ๋ฒ„์ „ [1, 2, ..., n] ์ค‘ ์ฒซ ๋ฒˆ์งธ ์ž˜๋ชป๋œ ๋ฒ„์ „์„ ์ฐพ์œผ์‹œ์˜ค.
๋ฒ„์ „ ๋ถˆ๋Ÿ‰ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” API bool is BadVersion(version)์ด ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.

/* The isBadVersion API is defined in the parent class VersionControl.
      boolean isBadVersion(int version); */

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int left = 1, right = n;
        
        while(left < right){
            int mid = left + (right - left) / 2;
            if(isBadVersion(mid)) right = mid;
            else left = mid+1;
        }
        return left;
    }
}

35. Search Insert Position

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4

 

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums contains distinct values sorted in ascending order.
  • -104 <= target <= 104

๐Ÿ“– ๋ฌธ์ œ. ์˜ค๋ฆ„์ฐจ์ˆœ ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์— target ์ด ์žˆ์œผ๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด ํ•ด๋‹น ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ€์•ผํ•  ์ž๋ฆฌ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ ํ•˜์‹œ์˜ค. O(log) ๋Ÿฐํƒ€์ž„ ๋ณต์žก์„ฑ์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length-1;
 
        while(left <= right){
            int mid = (left + right) / 2;
            if(nums[mid] == target){
                return mid;
            }
            else if(target > nums[mid]){
                left = mid + 1;
            }
            else{
                right = mid - 1;
            }   
        }
        return left;
    }
}

 

 

 

300x250

์ฝ”๋“œ