Home
Jose' Site
Cancel

Remove Duplicates from Sorted List

Problem 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. Examples Example 1: Input: head = [1...

Remove Duplicates from Sorted List II

Problem Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example...

Longest Subarray of 1's After Deleting One Element

Problem Given a binary array nums, you should delete one element from it. Return the size of the longest non-empty subarray containing only 1’s in the resulting array. Return 0 if there is no su...

Word Search

Problem Given an m x n grid of characters board and a string word, return true if word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent...

Subsets

Problem 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. Examp...

Combinations

Problem Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. You may return the answer in any order. Examples Example 1: Input: n = 4, k ...

Set Matrix Zeroes

Problem 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. Examples Example 1: Input: matrix = [[1,1,1],[1,0,1],[1,1...

Simplify Path

Problem Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. In a Unix-styl...

Sqrt(x)

Problem Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in expone...

Climbing Stairs

Problem 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? Examples Example 1: ...

Add Binary

Problem Given two binary strings a and b, return their sum as a binary string. Examples Example 1: Input: a = “11”, b = “1” Output: “100” Example 2: Input: a = “1010”, b = “1011” Outpu...

Plus One

Problem You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significa...

Minimum Path Sum

Problem Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. Note: You can only move either down...

Unique Paths II

Problem You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m -...

Unique Paths

Problem There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1])....

Rotate List

Problem Given the head of a linked list, rotate the list to the right by k places. Examples Example 1: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Example 2: Input: head =...

Spiral Matrix II

Problem Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. Examples Example 1: Input: n = 3 Output: [[1,2,3],[8,9,4],[7,6,5]] Example...

Length of Last Word

Problem Given a string s consisting of words and spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. Examples Exam...

Insert Interval

Problem You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending ...

Jump Game

Problem You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position. Return t...

Spiral Matrix

Problem Given an m x n matrix, return all elements of the matrix in spiral order. Examples Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2: ...

Maximum Subarray

Problem Given an integer array nums, find the subarray with the largest sum, and return its sum. Examples Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarra...

Edit Distance

Problem Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2. You have the following three operations permitted on a word: Insert a c...

Text Justification

Problem Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your wo...

Valid Number

Problem A valid number can be split up into these components (in order): A decimal number or an integer. (Optional) An 'e' or 'E', followed by an integer. A decimal number can be split up into t...

N-Queens II

Problem The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return the number of distinct solutions to th...

N-Queens

Problem The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens...

Pow(x, n)

Problem Implement pow(x, n), which calculates x raised to the power n (i.e., xn). Examples Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3...

Rotate Image

Problem You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D mat...

Permutations II

Problem Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order. Examples Example 1: Input: nums = [1,1,2] Output: [[1,1,2],...

Permutations

Problem Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Examples Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1...

Jump Game II

Problem You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0]. Each element nums[i] represents the maximum length of a forward jump from index i. ...

Trapping Rain Water

Problem Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. Examples Example 1: Input: height =...

Multiply Strings

Problem Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Note: You must not use any built-in BigInteger li...

Combination Sum II

Problem Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. Each number in ca...

Combination Sum

Problem Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return t...

Count And Say

Problem The count-and-say sequence is a sequence of digit strings defined by the recursive formula: countAndSay(1) = "1" countAndSay(n) is the way you would “say” the digit string from coun...

Sudoku Solver

Problem Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each ro...

Valid Sudoku

Problem Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each col...

Search Insert Position

Problem 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 w...

Find First and Last Position of Element in Sorted Array

Problem 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 ...

Longest Valid Parentheses

Problem Given a string containing just the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring. Examples Example 1: Input: s = “(()” Output: 2...

Next Permutation

Problem A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3], the following are all the permutations of arr: ...

Substring with Concatenation of All Words

Problem You are given a string s and an array of strings words. All the strings of words are of the same length. A concatenated substring in s is a substring that contains all the strings of any...

Divide Two Integers

Problem Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losin...

Find the Index of the First Occurrence in a String

Problem Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Examples Example 1: Input: haystack =...

Remove Element

Problem Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Since it is impossible to change the le...

Remove Duplicates from Sorted Array

Problem Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be ke...

First Missing Positive

Problem Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an algorithm that runs in O(n) time and uses constant extra space. Examples Exam...

Reverse Nodes in k-Group

Problem Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked li...

Swap Nodes in Pairs

Problem Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be ch...

Generate Parentheses

Problem Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Examples Example 1: Input: n = 3 Output: [”((()))”,”(()())”,”(())()”,”()((...

Merge Two Sorted Lists

Problem You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lis...

Longest Common Prefix

Problem Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Examples Example 1: Input: strs = [“...

Roman to Integer

Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

Remove Nth Node From End of List

Problem Given the head of a linked list, remove the nth node from the end of the list and return its head. Examples Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,4,5] Examp...

4Sum

Problem Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a, b, c, and d are distinc...

Letter Combinations of a Phone Number

Problem 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 lette...

3Sum Closest

Problem Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume t...

3Sum

Problem 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 ...

Integer to Roman

Problem Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

Regular Expression Matching

Problem Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character.​​​​ '*' Matches zero or more of ...

Palindrome Number

Problem Given an integer x, return true if x is a palindrome, and false otherwise. Examples Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from...

String to Integer (atoi)

Problem mplement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi function). The algorithm for myAtoi(string s) is as follows: Read in ...

Reverse Integer

Problem Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume t...

Zigzag Conversion

Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H ...

Longest Palindromic Substring

Problem Given a string s, return the longest palindromic substring in s. Examples Example 1: Input: s = “babad” Output: “bab” Explanation: “aba” is also a valid answer. Example 2: Inpu...

Median of Two Sorted Arrays

Problem Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Examples Exam...

Longest Substring Without Repeating Characters

Problem Given a string s, find the length of the longest substring without repeating characters. A substring is a contiguous non-empty sequence of characters within a string. Examples Example...

Best Time to Buy and Sell Stock With Transaction Fee

Problem You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee. Find the maximum profit you can ach...

Best Time to Buy and Sell Stock II

Problem You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only hold at most on...

Best Time to Buy and Sell Stock

Problem You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a diff...

Path Sum II

Problem Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a li...

Balanced Binary Tree

Problem Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than on...

Symmetric Tree

Problem Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Examples Example 1: Input: root = [1,2,2,3,4,4,3] Output: true Exa...

Same Tree

Problem Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the n...

Longest Repeating Character Replacement

Problem You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k time...

Sliding Window Maximum

Problem You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the windo...

Sliding Window Median

Problem The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values. For examples,...

Partition Labes

Problem You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. Note that the partition is done so that after co...

Container With Most Water

Problem 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]). Find two lines that togethe...

Top K Frequent Words

Problem Given an array of strings words and an integer k, return the k most frequent strings. Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequ...

Top K Frequent Elements

Problem Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Examples Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Out...

Remove Invalid Parentheses

Problem Given a string s that contains parentheses and letters, remove the minimum number of invalid parentheses to make the input string valid. Return a list of unique strings that are valid wi...

Merge Intervals

Problem 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 ...

Valid Parentheses

Problem Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the...

Find All Anagrams in a String

Problem Given two strings s and p, return an array of all the start indices of p’s anagrams in s. You may return the answer in any order. An Anagram is a word or phrase formed by rearranging the...

Number of Islands

Problem Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adja...

Valid Anagram

Problem Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typic...

Group Anagrams

Problem Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word ...

Two Sum

Problem 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, ...

Single Number

Problem Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only co...

Search in Rotated Sorted Array II

Problem There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values). Before being passed to your function, nums is rotated at an unknown pivot index k (0...

Find Minimum in Rotated Sorted Array

Problem Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated ...

Search in Rotated Sorted Array

Problem There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k &...

Search a 2D Matrix

Problem 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 in...

Guess Number Higher or Lower

Problem We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the numb...

Binary Search

Problem Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, r...

Reverse Linked List

Problem Given the head of a singly linked list, reverse the list, and return the reversed list. Examples Example 1: Input: [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1...

Add Two Numbers

Problem You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbe...

Linked List Cycle

Problem Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by co...

Merge k Sorted Lists

Problem You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Examples Example 1...

Shortest Subarray with Sum at Least K

Problem Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1. A subarray i...

Smallest Rotation with Highest Score

Problem You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]. A...

Subarray Sum Equals K

Problem Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k. A subarray is a contiguous non-empty sequence of elements within an array. ...

Continuous Subarray Sum

Problem Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise. A good subarray is a subarray where: its length is at least two, and the s...

Max Sum of Rectangle No Larger Than K

Problem Given an m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k. It is guaranteed that there will be a rectangle with...

Range Sum Query 2D - Immutable

Problem Given a 2D matrix matrix, handle multiple queries of the following type: Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1)...

Range Sum Query - Immutable

Problem Given an integer array nums, handle multiple queries of the following type: Calculate the sum of the elements of nums between indices left and right inclusive where left <= right. Im...

Product of Array Except Self

Problem 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]. The product of any prefix or suffix of nums is...

Minimum Size Subarray Sum

Problem Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray,...