Leetcode#435. Non-overlapping Intervals
ProblemGiven an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Example 1:
1234Input: intervals = [[1,2],[2,3],[3,4],[1,3]]Output: 1Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
Example 2:
1234Input: intervals = [[1,2],[1,2],[1,2]]Output: 2Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
...
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#1. Two Sum
ProblemGiven 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, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
1234Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
123Input: nums = [3,2,4], target = 6Output: [1,2]
Example 3:
123Input: nums = [3,3 ...
Leetcode#337. House Robber III
ProblemThe thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.
Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police ...
Leetcode#53. Maximum Subarray
ProblemGiven an integer array nums, find the
subarray
with the largest sum, and return
its sum
.
Example 1:
1234Input: nums = [-2,1,-3,4,-1,2,1,-5,4]Output: 6Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
1234Input: nums = [1]Output: 1Explanation: The subarray [1] has the largest sum 1.
Example 3:
1234Input: nums = [5,4,-1,7,8]Output: 23Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 10^5
10^4 <= nums[i] <= 1 ...
Leetcode#202. Happy Number
ProblemWrite an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
12345678Input: n = 19Output: ...
Leetcode#172. Factorial Trailing Zeroes
ProblemGiven an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Example 1:
1234Input: n = 3Output: 0Explanation: 3! = 6, no trailing zero.
Example 2:
1234Input: n = 5Output: 1Explanation: 5! = 120, one trailing zero.
Example 3:
123Input: n = 0Output: 0
Constraints:
0 <= n <= 10^4
Follow up: Could you write a solution that works in logarithmic time complexity?
Solve這題先搞懂 trailing zeroes,是指尾巴的0
要計算尾巴有幾個0很簡單就是算其中乘了幾次(5*2)
由於每 ...
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 ...