This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- Intuitvely, we can find the possible areas for each pair of heights | |
- For each pair, we check the area by multiplying the width to the height without spill over. We need to consider the minimum height between the pair. | |
- However, can we reduce the number of times we find the possible areas for every pair of the heights which is caused by the minimum height in each pair? | |
- Yes, we can. We can have two pointers at both endpoints and perform the area of the ractangle. | |
- When the height is minimum for a left pointer, we can move to the pointer to the right and when the height is minimum for a right pointer, we move the pointer to the left to the right. This is done to look for the next possible maximum height. | |
- TC: O(n), SC: O(1) | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- n = 19 --> 82 --> 68 --> 100 --> 1 | |
n // 10 = firstDigit | |
n % 10 = lastDigit | |
- n = 2 --> 4 --> 16 --> 37 --> 58 --> 89 --> 145 --> 42 --> 20 --> 4 | |
Since we've seen 4 before, then we know that the number is not happy. | |
Time Complexity: O(log n) - finding the sum square for a given number has a cost of O(logn) because we are processing each digit in the number, and the number of digits in a number is given by (log n): For n > 243, O(243 * 3 + log n + log log n + ....) = O(log n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- Note: Knowing which representation is the correct one for a particular number can be difficult. There are many possible ways of representing the number 120 for instance. | |
- So if num = 9, X and I are needed. Rule of roman numeral goes from largest to smallest. However, there are some special rules where the symbol with a smaller value can go before the symbol with a larger value. | |
- If there are no special rules, when num = 1500, | |
- We can divide 1500/M. i.e. 1500//1000 = 1. 1 tells us how many Ms in our final result. | |
- Then 1500 % 1000 = 500 | |
- 500 // 500 = 1. 1D in the final result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: | |
# Create an empty array to store your answer | |
res = [] | |
# Get the length of the nums array | |
n = len(nums) | |
# Slice the input array to get data from index 0 to index k | |
win_arr = nums[:k-1] |
NewerOlder