Day 1,2
Day 1,2
Backtracking
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
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
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
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 :
Output: 1
Practice Link: https://leetcode.com/problems/search-insert-position
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
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
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
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
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
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 :
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 :
Output: 6
Practice Link:https://leetcode.com/problems/maximum-product-subarray
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:
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
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
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
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
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.
Practice Link:https://leetcode.com/problems/set-matrix-zeroes
Practice Link:https://leetcode.com/problems/spiral-matrix
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
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
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
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:
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:
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