Posts

Showing posts with the label trick

474. Ones and Zeroes

 https://leetcode.com/problems/ones-and-zeroes/description/ You are given an array of binary strings  strs  and two integers  m  and  n . Return  the size of the largest subset of  strs  such that there are  at most   m   0 's and  n   1 's in the subset . A set  x  is a  subset  of a set  y  if all elements of  x  are also elements of  y .   Example 1: Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3 Output: 4 Explanation: The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4. Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}. {"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3. Example 2: Input: strs = ["10","0","1"], m = 1, n = 1 Output: 2 Explanation: The largest subs...

2268. Minimum Number of Keypresses

Image
 https://leetcode.com/problems/minimum-number-of-keypresses/description/ You have a keypad with  9  buttons, numbered from  1  to  9 , each mapped to lowercase English letters. You can choose which characters each button is matched to as long as: All 26 lowercase English letters are mapped to. Each character is mapped to by  exactly   1  button. Each button maps to  at most   3  characters. To type the first character matched to a button, you press the button once. To type the second character, you press the button twice, and so on. Given a string  s , return  the  minimum  number of keypresses needed to type  s  using your keypad. Note  that the characters mapped to by each button, and the order they are mapped in cannot be changed.   Example 1: Input: s = "apple" Output: 5 Explanation: One optimal way to setup your keypad is shown above. Type 'a' by pressing button 1 once. Type 'p' b...

510. Inorder Successor in BST II

Image
 https://leetcode.com/problems/inorder-successor-in-bst-ii/description/ Given a  node  in a binary search tree, return  the in-order successor of that node in the BST . If that node has no in-order successor, return  null . The successor of a  node  is the node with the smallest key greater than  node.val . You will have direct access to the node but not to the root of the tree. Each node will have a reference to its parent node. Below is the definition for  Node : class Node { public int val; public Node left; public Node right; public Node parent; }   Example 1: Input: tree = [2,1,3], node = 1 Output: 2 Explanation: 1's in-order successor node is 2. Note that both the node and the return value is of Node type. Example 2: Input: tree = [5,3,6,2,4,null,null,1], node = 6 Output: null Explanation: There is no in-order successor of the current node, so the answer is null.   Constraints: The number of nodes in the ...