Posts

Showing posts with the label medium

198. House Robber

Blog You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night . Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police . Example: 1 Input: nums = [1,2,3,1] Output: 4 Example: 2 Input: nums = [2,7,9,3,1] Output: 12 Constraints: 1 0 ...

96. Unique Binary Search Trees

Blog Given an integer n , return the number of structurally unique BST 's (binary search trees) which has exactly n nodes of unique values from 1 to n . Example: 1 Input: n = 3 Output: 5 Example: 2 Input: n = 1 Output: 1 Constraints: 1 Important observation for solving the problem: If we keep K as our root, then, all numbers lower than K will come in left subtree and all numbers greater than 1 will come in right subtree. Also, if we have X ways of forming left subtree and Y ways of forming right subtree, then, total number of trees rooted at K can be calculated as : ways(K) = ways(X) * ways(Y...

1834. Single-Threaded CPU

Blog You are given n ​​​​​​ tasks labeled from 0 to n - 1 represented by a 2D integer array tasks , where tasks[i] = [enqueueTime i , processingTime i ] means that the i ​​​​​​th​​​​ task will be available to process at enqueueTime i and will take processingTime i to finish processing. You have a single-threaded CPU that can process at most one task at a time and will act in the following way: If the CPU is idle and there are no available tasks to process, the CPU remains idle. If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time . If multiple tasks have the same shortest processing time, it will choose the task with the smallest index. Once a task is started, the CPU will process the entire task without stopping. The CPU can finish a task then start a new one instan...

1824. Minimum Sideway Jumps

Blog There is a 3 lane road of length n that consists of n + 1 points labeled from 0 to n . A frog starts at point 0 in the second lane and wants to jump to point n . However, there could be obstacles along the way. You are given an array obstacles of length n + 1 where each obstacles[i] (ranging from 0 to 3) describes an obstacle on the lane obstacles[i] at point i . If obstacles[i] == 0 , there are no obstacles at point i. There will be at most one obstacle in the 3 lanes at each point. For example, if obstacles[2] == 1 , then there is an obstacle on lane 1 at point 2. The frog can only travel from point i to point i + 1 on the same lane if there is not an obstacle on the lane at point i + 1 . To avoid obstacles, the frog can also perform a side jump to jump to another lane (even if they are not adjacent) at the same p...

91. Decode Ways

Blog A message containing letters from A-Z can be encoded into numbers using the following mapping: 'A' -> "1" 'B' -> "2" ... 'Z' -> "3" To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, "11106" can be mapped into: "AAJF" with the grouping (1 1 10 6) "KJF" with the grouping (11 10 6) Note that the grouping (1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06" . Given a string s containing only digits, return the number of ways to dec...

1814. Count Nice Pairs in an Array

Blog You are given an array nums that consists of non-negative integers. Let us define rev(x) as the reverse of the non-negative integer x . For example, rev(123) = 321 , and rev(120) = 21 . A pair of indices (i, j) is nice if it satisfies all of the following conditions: 0 nums[i] + rev(nums[j]) == nums[j] + rev(nums[i]) Return the number of nice pairs of indices. Since that number can be too large, return it modulo 10 9 + 7 . Example: 1 Input: nums = [42,11,1,97] Output: 2 Example: 2 Input: nums = [13,10,35,24,76] Output: 4 Constraints: 1 0 Step I (Cha...

1798. Maximum Number of Consecutive Values You Can Make (Leetcode)

Blog You are given an integer array coins of length n which represents the n coins that you own. The value of the i th coin is coins[i] . You can make some value x if you can choose some of your n coins such that their values sum up to x . Return the maximum number of consecutive integer values that you can make with your coins starting from and including 0 . Note that you may have multiple coins of the same value. Example: 1 Input: coins = [1,3] Output: 2 Example: 2 Input: coins = [1,1,1,4] Output: 8 Example: 3 Input: [1,4,10,3,1] Output: 20 Constraints: ...

1792. Maximum Average Pass Ratio (Leetcode)

There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes , where classes[i] = [pass i , total i ] . You know beforehand that in the i th class, there are total i total students, but only pass i number of students will pass the exam. You are also given an integer extraStudents . There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes. The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes. ...

1791. Find Center of Star Graph (Leetcode)

Image
There is an undirected star graph consisting of n nodes labeled from 1 to n . A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node. You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi . Return the center of the given star graph. Example: 1 Input: edges = [[1,2],[2,3],[4,2]] Output: 2 Explanation: As shown in the figure above, node 2 is connected to every other node, so 2 is the center. Example: 2 Input: edges = [[1,2],[5,1],[1,3],[1,4]] Output: 1 Constraints: 3 ed...