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#949. Largest Time for Given Digits
ProblemGiven an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.
24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.
Return the latest 24-hour time in "HH:MM" format. If no valid time can be made, return an empty string.
Example 1:
1234Input: arr = [1,2,3,4]Output: "23:41"Explanation: The valid 24-hour times ...
Leetcode#134. Gas Station
ProblemThere are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas and cost, return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a s ...
Leetcode#322. Coin Change
ProblemYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
1234Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1
Example 2:
123Input: coins = [2], ...
Leetcode#380. Insert Delete GetRandom O(1)
ProblemImplement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). ...
Leetcode#274. H-Index
ProblemGiven an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher’s h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
Example 1:
12345Input: citations = [3,0,6,1,5]Output: 3Explanation: [3,0,6,1,5] means the researcher has 5 papers in total an ...
Leetcode#122. Best Time to Buy and Sell Stock II
ProblemYou 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 one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
123456Input: prices = [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy ...
Leetcode#189. Rotate Arra
ProblemGiven an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
1234567Input: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
123456Input: nums = [-1,-100,3,99], k = 2Output: [3,99,-1,-100]Explanation:rotate 1 steps to the right: [99,-1,-100,3]rotate 2 steps to the right: [3,99,-1,-100]
...
Leetcode#169. Majority Element
ProblemGiven an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Example 1:
123Input: nums = [3,2,3]Output: 3
Example 2:
123Input: nums = [2,2,1,1,1,2,2]Output: 2
Constraints:
n == nums.length
1 <= n <= 5 * 104
109 <= nums[i] <= 109
Follow-up:
Could you solve the problem in linear time and in
1O(1)
space?
Solve解112345678910class Solutio ...
Leetcode#80. Remove Duplicates from Sorted Array II
ProblemGiven an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result ...