Leetcode#300. Longest Increasing Subsequence
ProblemGiven an integer array nums, return the length of the longest strictly increasing
subsequence
.
Example 1:
1234Input: nums = [10,9,2,5,3,7,101,18]Output: 4Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2:
123Input: nums = [0,1,0,3,2,3]Output: 4
Example 3:
123Input: nums = [7,7,7,7,7,7,7]Output: 1
Constraints:
1 <= nums.length <= 2500
104 <= nums[i] <= 104
Follow up: Can you come up with an algorithm that runs in O(n log( ...
Leetcode#862. Shortest Subarray with Sum at Least K
ProblemGiven 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 is a contiguous part of an array.
Example 1:
123Input: nums = [1], k = 1Output: 1
Example 2:
123Input: nums = [1,2], k = 4Output: -1
Example 3:
123Input: nums = [2,-1,2], k = 3Output: 3
Constraints:
1 <= nums.length <= 10^5
10^5 <= nums[i] <= 10^5
1 <= k <= 10^9
Solve想破頭 簡單說就是 找到第一組解後 ...
Leetcode#33. Search in Rotated Sorted Array
ProblemThere 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 < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the ...
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#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#35. Search Insert Position
ProblemGiven 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 1:
123Input: nums = [1,3,5,6], target = 5Output: 2
Example 2:
123Input: nums = [1,3,5,6], target = 2Output: 1
Example 3:
123Input: nums = [1,3,5,6], target = 7Output: 4
Constraints:
1 <= nums.length <= 10^4
10^4 <= nums[i] <= 10^4
...