Leetcode#135. Candy
ProblemThere are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
1234Input: ratings = [1,0,2]Output: 5Explanation: You can allocate to the fi ...
Leetcode#74. Search a 2D Matrix
ProblemYou 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 1:
!https://assets.leetcode.com/uploads/2020/10/05/mat.jpg
123Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3Outpu ...
Leetcode#139. Word Break
ProblemGiven a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
1234Input: s = "leetcode", wordDict = ["leet","code"]Output: trueExplanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
12345Input: s = "applepenap ...
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#52. N-Queens II
ProblemThe 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 the n-queens puzzle.
Example 1:
![(https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
!https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
1234Input: n = 4Output: 2Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
Example 2:
123Input: n = 1Output: 1
Constraints:
...
Leetcode#530. Minimum Absolute Difference in BST
ProblemGiven the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg
123Input: root = [4,2,6,1,3]Output: 1
Example 2:
!https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg
123Input: root = [1,0,48,null,null,12,49]Output: 1
Constraints:
The number of nodes in the tree is in the range [2, 10^4].
0 <= Node.val <= 10^5
Solve法一由於有有序性質、且正整 ...