LeetCode - 150 - Pow(x, n)
The Problem Implement pow(x, n), which calculates x raised to the power n (i.e., xโฟ). Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3 Output: 9.26100 Example 3: Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2^-2 = 1/2^2 = 1/4 = 0.25 Constraints: -100.0 < x < 100.0 -2^31 <= n <= 2^31 - 1 n is an integer. Either x is not zero or n > 0. -10^4 <= x^n <= 10^4 Brute Force Solution Explanation The brute force way to solve this problem without using built-in functions is to use a while loop and multiply the x value n times. The problem with this solution is that it will not pass on LeetCode because it takes O(n) time. ...
LeetCode - 150 - Minimum Interval to Include Each Query
The problem You are given a 2D integer array intervals, where intervals[i] = [lefti, righti] describes the ith interval starting at lefti and ending at righti (inclusive). The size of an interval is defined as the number of integers it contains, or more formally righti - lefti + 1. You are also given an integer array queries. The answer to the jth query is the size of the smallest interval i such that lefti <= queries[j] <= righti. If no such interval exists, the answer is -1. ...
LeetCode - 150 - Valid Parenthesis String
The problem Given a string s containing only three types of characters: '(', ')', and '*', return true if s is valid. The following rules define a valid string: Any left parenthesis '(' must have a corresponding right parenthesis ')'. Any right parenthesis ')' must have a corresponding left parenthesis '('. A left parenthesis '(' must go before the corresponding right parenthesis ')'. '*' could be treated as a single right parenthesis ')', a single left parenthesis '(', or an empty string "". Example 1: ...
LeetCode - 150 - Partition Labels
LeetCode - 150 - Partition Labels The problem You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid. Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s. ...
LeetCode - 150 - Merge Triplets to Form Target Triplet
The problem A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [ai, bi, ci] describes the i-th triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain. To obtain target, you may apply the following operation on triplets any number of times (possibly zero): Choose two indices (0-indexed) i and j (i != j) and update triplets[j] to become [max(ai, aj), max(bi, bj), max(ci, cj)]. ...