[NeetCode150] Binary Search

studied byStudied by 0 people
0.0(0)
Get a hint
Hint

Binary Search

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

1 / 3

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

4 Terms

1

Binary Search

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

easiest way is to use 2 pointers startIdx and endIdx, and check middleIdx

New cards
2

Search a 2D matrix

ou are given an m x n integer matrix matrix with the following two properties:

  • Each row is sorted in non-decreasing order.

  • The first integer of each row is greater than the last integer of the previous row.

Given an integer target, return true if target is in matrix or false otherwise.

You must write a solution in O(log(m * n)) time complexity.

 

Example 1:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Example 2:

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false

think of the matrix as a 1D array, we have left = first item in matrix, right = last item in matrix. Find the middle item and use binary search to find target

New cards
3

draft

draft

New cards
4

draft

draft

New cards

Explore top notes

note Note
studied byStudied by 635 people
... ago
4.8(5)
note Note
studied byStudied by 11 people
... ago
5.0(1)
note Note
studied byStudied by 1 person
... ago
5.0(1)
note Note
studied byStudied by 11 people
... ago
4.5(2)
note Note
studied byStudied by 21 people
... ago
5.0(1)
note Note
studied byStudied by 93 people
... ago
5.0(1)
note Note
studied byStudied by 7 people
... ago
5.0(1)
note Note
studied byStudied by 13 people
... ago
5.0(1)

Explore top flashcards

flashcards Flashcard (32)
studied byStudied by 47 people
... ago
4.0(1)
flashcards Flashcard (22)
studied byStudied by 6 people
... ago
5.0(1)
flashcards Flashcard (500)
studied byStudied by 24 people
... ago
5.0(1)
flashcards Flashcard (90)
studied byStudied by 2 people
... ago
4.0(1)
flashcards Flashcard (118)
studied byStudied by 9 people
... ago
4.0(1)
flashcards Flashcard (25)
studied byStudied by 2 people
... ago
4.0(1)
flashcards Flashcard (30)
studied byStudied by 10 people
... ago
5.0(1)
flashcards Flashcard (88)
studied byStudied by 559 people
... ago
5.0(1)
robot