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#133. Clone Graph
ProblemGiven a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
12345class Node { public int val; public List<Node> neighbors;}
Test case format:
For simplicity, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the te ...
Leetcode#1319. Number of Operations to Make Network Connected
ProblemThere are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connecte ...
Leetcode#114. Flatten Binary Tree to Linked List
ProblemGiven the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg
123Input: root = [1,2,5,3,4,null,6]Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
1 ...
Leetcode#450. Delete Node in a BST
ProblemGiven a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.
Basically, the deletion can be divided into two stages:
Search for a node to remove.
If the node is found, delete the node.
Example 1:
!https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg
123456Input: root = [5,3,6,2,4,null,7], key = 3Output: [5,4,6,2,null,null,7]Explanation: Given key to delete is 3. So we find the node ...
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 ...