Leetcode#45. Jump Game II
ProblemYou 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. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
Example 1:
1234Input: nums = [2,3,1,1,4]Output: 2Explanation: T ...
Leetcode#55. Jump Game
ProblemYou 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 true if you can reach the last index, or false otherwise.
Example 1:
1234Input: nums = [2,3,1,1,4]Output: trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
1234Input: nums = [3,2,1,0,4]Output: falseExplanation: You will always arrive at index 3 no matter what. Its m ...
Leetcode#34. Find First and Last Position of Element in Sorted Array
ProblemGiven 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 1:
123Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]
Example 2:
123Input: nums = [5,7,7,8,8,10], target = 6Output: [-1,-1]
Example 3:
123Input: nums = [], target = 0Output: [-1,-1]
Constraints:
0 <= nums.length <= 10^5
10^9 & ...
Leetcode#105. Construct Binary Tree from Preorder and Inorder Traversal
ProblemGiven two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/tree.jpg
123Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]Output: [3,9,20,null,null,15,7]
Example 2:
123Input: preorder = [-1], inorder = [-1]Output: [-1]
Constraints:
1 <= preorder.length <= 3000
inorder.length == pr ...
Leetcode#92. Reverse Linked List II
ProblemGiven the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg
123Input: head = [1,2,3,4,5], left = 2, right = 4Output: [1,4,3,2,5]
Example 2:
123Input: head = [5], left = 1, right = 1Output: [5]
Constraints:
The number of nodes in the list is n.
1 <= n <= 500
500 <= Node.val &l ...
Leetcode#221. Maximal Square
ProblemGiven an m x n binary matrix filled with 0‘s and 1‘s, find the largest square containing only 1‘s and return its area.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg
123Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0& ...
Leetcode#79. Word Search
進階leetcode212
ProblemGiven 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 cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/04/word2.jpg
123Input: board = [["A","B","C","E"],["S","F","C" ...
Leetcode#100. Same Tree
ProblemGiven 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 nodes have the same value.
Example 1:
!https://assets.leetcode.com/uploads/2020/12/20/ex1.jpg
123Input: p = [1,2,3], q = [1,2,3]Output: true
Example 2:
!https://assets.leetcode.com/uploads/2020/12/20/ex2.jpg
123Input: p = [1,2], q = [1,null,2]Output: false
Example 3:
!https://assets.leetcode.com/upload ...
Leetcode#62. Unique Paths
123456789101112- [Leetcode]- [Python]- [medium]- [💡]- Math- Dynamic Programming- Combinatoricscover: /img/cover/leetcode.jpgcategories: Leetcode
ProblemThere 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]). 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 c ...
Leetcode#120. Triangle
leetcode62
ProblemGiven a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
Example 1:
123456789Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]Output: 11Explanation: The triangle looks like: 2 3 4 6 5 74 1 8 3The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Exa ...