[NeetCode150] Binary Search

0.0(0)
studied byStudied by 3 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/3

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 12:24 AM on 10/30/24
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

4 Terms

1
New cards

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

2
New cards

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

3
New cards

draft

draft

4
New cards

draft

draft

Explore top flashcards