You are on page 1of 6

Given a sorted array nums, remove the duplicates in-place such that each element appears only once

and returns the new


length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra
memory.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means a modification to the input array will be known to the
caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)


int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.


// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

Example 1:

Input: nums = [1,1,2]


Output: 2, nums = [1,2]
Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2
respectively. It doesn't matter what you leave beyond the returned length.

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]


Output: 5, nums = [0,1,2,3,4]
Explanation: Your function should return length = 5, with the first five elements of nums being modified
to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.

Constraints:

0 <= nums.length <= 3 * 104


-104 <= nums[i] <= 104
nums is sorted in ascending order.

********************
53. Maximum Subarray
********************

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and
return its sum.
Example 1:

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


Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: nums = [1]


Output: 1

Example 3:

Input: nums = [5,4,-1,7,8]


Output: 23

Constraints:

1 <= nums.length <= 3 * 104


-105 <= nums[i] <= 105

Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach,
which is more subtle.

*******************
70. Climbing Stairs
*******************

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

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:

1 <= n <= 45

**************************************
83. Remove Duplicates from Sorted List
**************************************

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list
sorted as well.

Example 1:

Input: head = [1,1,2]


Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]


Output: [1,2,3]

Constraints:

The number of nodes in the list is in the range [0, 300].


-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.

*********************************
104. Maximum Depth of Binary Tree
*********************************

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf
node.

Example 1:

Input: root = [3,9,20,null,null,15,7]


Output: 3

Example 2:

Input: root = [1,null,2]


Output: 2

Example 3:

Input: root = []
Output: 0

Example 4:

Input: root = [0]


Output: 1

Constraints:

The number of nodes in the tree is in the range [0, 104].


-100 <= Node.val <= 100
*********************************
111. Minimum Depth of Binary Tree
*********************************

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Example 1:

Input: root = [3,9,20,null,null,15,7]


Output: 2

Example 2:

Input: root = [2,null,3,null,4,null,5,null,6]


Output: 5

Constraints:

The number of nodes in the tree is in the range [0, 105].


-1000 <= Node.val <= 1000

*************
112. Path Sum
*************

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up
all the values along the path equals targetSum.
A leaf is a node with no children.

Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22


Output: true

Example 2:

Input: root = [1,2,3], targetSum = 5


Output: false

Example 3:

Input: root = [1,2], targetSum = 0


Output: false

Constraints:

The number of nodes in the tree is in the range [0, 5000].


-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000

You might also like