0% found this document useful (0 votes)
28 views

Day 1,2

Uploaded by

Shivangi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Day 1,2

Uploaded by

Shivangi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Arrays - 1D & 2 D , Searching and Sorting, Recursion,

Backtracking

1. Two Sum [Easy]

Given an array of integers nums and an integer target , return indices of the two numbers such
that they add up to target You may assume that each input would have exactly one solution,
and you may not use the same element twice.
You can return the answer in any order.
Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Practice Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
https://leetcode.com/problems/two-sum

2. Best Time to Buy and Sell Stock [Easy]

Given an array of integers nums and an integer target , return indices of the two numbers such
that they add up to target You may assume that each input would have exactly one solution,
and you may not use the same element twice.
You can return the answer in any order.
Example:
Input: prices = [7,1,5,3,6,4]
Output: 5
Practice Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock

3. First Bad Version [Easy]

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.
Example:
Input: n = 5, bad = 4
Output: 4
Practice Link:https://leetcode.com/problems/first-bad-version
4. Climbing Stairs [Easy]

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1
or 2 steps. In how many distinct ways can you climb to the top?
Example:
Input: n = 2
Output: 2
Practice Link:https://leetcode.com/problems/climbing-stairs

5. Search insert position [Easy]

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 :

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

Output: 1
Practice Link: https://leetcode.com/problems/search-insert-position

6. Find First and Last Position of Element in Sorted Array [Medium]


Given an array of integers nums sorted in non-decreasing order, find the starting and ending
position of a given target value.If target is not found in the array, return [-1, -1]. You must write
an algorithm with O(log n) runtime complexity.

Example:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Practice Link:https://leetcode.com/problems/find-first-and-last-position-of-element-in-
sorted-array

7. Pascal Triangle [Easy]

Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each
number is the sum of the two numbers directly above it as shown:

Example :
Practice Link:https://leetcode.com/problems/pascals-triangle

8. Single Element in a Sorted Array [Medium]

You are given a sorted array consisting of only integers where every element appears exactly
twice, except for one element which appears exactly once. Return the single element that
appears only once. Your solution must run in O(log n) time and O(1) space.

Example :
Input: nums = [1,1,2,3,3,4,4,8,8]
Output: 2
Practice Link: https://leetcode.com/problems/single-element-in-a-sorted-array

9. Sort Colors [Medium]

Given an array nums with n objects colored red, white, or blue, sort them
in-place so that objects of the same color are adjacent, with the colors in the order red, white,
and blue. You must solve this problem without using the library's sort function.

Example:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Practice Link:https://leetcode.com/problems/sort-colors
10. Find the duplicate number [Medium]

Given an array of integers nums containing n + 1 integers where each integer is in the range [1,
n] inclusive. There is only one repeated number in nums, return this repeated number. You
must solve the problem without modifying the array nums and uses only constant extra space.
Example :
Input: nums = [1,3,4,2,2]
Output: 2
Practice Link: https://leetcode.com/problems/find-the-duplicate-number
11. Pow(x, n) [Medium]

Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
Practice Link:https://leetcode.com/problems/powx-n

12. 3Sum [Medium]

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j ,
i != k and j != k , and nums[i] + nums[j] + nums[k] == 0 Notice that the solution set must not
contain duplicate triplets.
Example:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Practice Link:https://leetcode.com/problems/3sum

13. Combination Sum [Medium]

Given an array of distinct integers candidates and a target integer candidates target return a list
of all unique combinations of candidates where the chosen numbers sum to target You may
return the combinations in any order.
Example:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Practice Link:https://leetcode.com/problems/combination-sum
14. Maximum SubArrays [Medium]

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Example :

Input: nums = [2,3,-2,,4]


Output: 6
Input: nums = [-2,0,1]
Output: 1
Input: nums = [2,3,-2,-5,6,-1,,4]
Output: 360
Practice Link:https://leetcode.com/problems/maximum-subarray

15. Maximum Product SubArrays [Medium]

Given an integer array nums, find a subarray that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

Example :

Input: nums = [2,3,-2,4]

Output: 6
Practice Link:https://leetcode.com/problems/maximum-product-subarray

16. Majority Element[Medium]


Given an array nums of size n, return the majority element.

The majority element is the element that appears more than ⌊n / 2⌋ times. You may
assume that the majority element always exists in the array.

Example 1:

Input: nums = [3,2,3]


Output: 3
Practice Link:https://leetcode.com/problems/majority-element
17. Search a 2D Matrix [Medium]

You 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:

Practice Link:https://leetcode.com/problems/search-a-2d-matrix

18. Coin Change [Medium]

You are given an integer array coins representing coins of different denominations and an
integer amount representing a total amount of money. Return the fewest number of coins that
you need to make up that amount. If that amount of money cannot be made up by any
combination of the coins, return -1. You may assume that you have an infinite number of each kind
of coin.
Example:
Input: coins = [1,2,5], amount = 11
Output: 3
Practice Link: https://leetcode.com/problems/coin-change

19. Letter Combinations of a Phone Number [Medium]


Given a string containing digits from 2-9 inclusive, return all possible letter combinations that
the number could represent. Return the answer in any order. A mapping of digits to letters (just
like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:

Practice Link: https://leetcode.com/problems/letter-combinations-of-a-phone-number

20. Product of Array except self [Medium]

Given an integer array nums return an array answer such that answer[i] is equal to the product
of all the elements of nums except nums[i] You must write an algorithm that runs in O(n) time
and without using the division operation.
Example:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Practice Link:https://leetcode.com/problems/product-of-array-except-self
21. Search in Rotated Sorted Array

There is an integer array nums sorted in ascending order (with distinct values). Given the array
nums after the possible rotation and an integer target return the index of target if it is in nums ,
or -1 is not in nums You must write an algorithm with O(log n) runtime complexity.
Example:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Practice Link:https://leetcode.com/problems/search-in-rotated-sorted-array

22. Permutations [Medium]

Given an array nums of distinct integers, return all the possible permutations. You can return
the answer in any order.
Example:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Practice Link:https://leetcode.com/problems/permutations

23. Merge Intervals [Medium]

Given an array of intervals where intervals[i] = [starti, endi] merge all overlapping intervals, and
return an array of the non-overlapping intervals that cover all the intervals in the input.
Example:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Practice Link:https://leetcode.com/problems/merge-intervals
24. Set Matrix Zero [Medium]

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.

You must do it in place.


Example:

Practice Link:https://leetcode.com/problems/set-matrix-zeroes

25. Spiral Matrix [Medium]

Given an m x n matrix return all elements of the matrix in spiral order.


Example:

Practice Link:https://leetcode.com/problems/spiral-matrix

26. Subsets [Medium]

Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Practice Link:https://leetcode.com/problems/subsets

27. Unique Paths [Medium]

There is a robot on an m x n grid. The robot is initially located at the top-left corner. The robot
tries to move to the bottom-right corner The robot can only move either down or right at any
point in time. Given the two integers m and n , return the number of possible unique paths that
the robot can take to reach the bottom-right corner.
Example:
Practice Link:https://leetcode.com/problems/unique-paths

28. Container with most water [Medium]

You are given an integer array height of length n . There are n vertical lines drawn such that the
two endpoints of the ith line are (i, 0) and (i, height[i]) Return the maximum amount of water a
container can store.
Example:
Practice Link: https://leetcode.com/problems/container-with-most-water

29. Maximum Profit in Job Scheduling[Hard]

We have n where every job is scheduled to be done from startTime[i] to endTime[i] , obtaining
a profit of profit[i] Return the maximum profit you can take such that there are no two jobs in
the subset with overlapping time range.
Example:

Practice Link: https://leetcode.com/problems/maximum-profit-in-job-scheduling/


30. Largest Rectangle in Histogram[Medium]

Given an array of integers heights representing the histogram's bar height where the width of
each bar is 1 , return the area of the largest rectangle in the histogram.
Example:

Practice Link: https://leetcode.com/problems/largest-rectangle-in-histogram

31. Max value of Equation [Hard]

You are given an array points containing the coordinates of points on a 2D plane, sorted by the
x-values, where points[i] = [xi, yi] such that xi < xj for all 1 <= i < j <= points.length. You are also
given an integer k. Return the maximum value of the equation yi + yj + |xi - xj| where |xi - xj|
<= k and 1 <= i < j <= points.length. It is guaranteed that there exists at least one pair of points
that satisfy the constraint |xi - xj| <= k.
Example:
Input: points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
Output: 4
Practice Link: https://leetcode.com/problems/max-value-of-equation

You might also like