Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
  1. 타임아웃..

    Code Block
    public int firstBadVersion(int n) {
    		final Boolean[] booleans = IntStream.rangeClosed(1, n)
                    .mapToObj(i -> isBadVersion(i))
                    .toArray(Boolean[]::new);
            
            return Arrays.binarySearch(booleans, false);
        }
    
    
    // 회고  13 ms, faster than 37.95%
    
    public int firstBadVersion(int n) {
            int left = 1;
            int right = n;
    
            while (left <= right) {
                int mid = (left + right) >>> 1;
    
                if (isBadVersion(mid)) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            return left;
        }
    
    
  2. 실패

    Code Block
    public int[] searchRange(int[] nums, int target) {
        
    //         int last = Arrays.binarySearch(nums, target);
    //         if (last < 0) {
    //             return new int[]{-1, -1};
    //         }
    
    //         final Integer[] reverseNums = IntStream.of(nums)
    //                 .boxed()
    //                 .sorted(Comparator.reverseOrder())
    //                 .toArray(Integer[]::new);
    
    //         final int second = Arrays.binarySearch(reverseNums, target);
    
    //         int first = nums.length - 1 - second;
    //         return new int[]{first, last};
            
            final int i = Arrays.binarySearch(nums, 0, nums.length / 2 + 1, target);
            final int j = Arrays.binarySearch(nums, nums.length / 2, nums.length, target);
            
            if (i < 0 && j < 0) {
                return new int[]{-1, -1};
            }
            
            return new int[]{i, j};
        }