An extension of leetcode. Keep working on leetcode.
Daily Temperatures
Daily Temperatures: Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
Brute Force
1 | class Solution: |
However, the worst time complexity is $O(N^2)$. The code can not pass one of the complex test case which is (Time Limit Exceeded).
Advance Approach
Stack can be used to improve the efficiency of this issue. We use stack to store a key-value temperature with decreasing order where key is the index of temperature, and value is the temperature. The time complexity of this approache is $O(N)$. For example:
1 | Temperature: [73, 74, 75, 71, 69, 72, 76, 73] |
And implementation is:
1 | class Solution: |
Minimum Size Subarray Sum
Minimum Size Subarray Sum: Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, …, numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.
Example:
1 | Input: target = 7, nums = [2,3,1,2,4,3] |
Brute Force
The basic approach is to use two loops to solve that. The time complexity of this approach is $O(N^2)$. It will cause time limit exceeded.
1 | class Solution: |
Advance Approach
To solve this issue, we can use Sliding Window to solve this issue. This method is similar to the double pointer. The index
the left index of this array. When the sum between i
and index
is greater or equal to the target, then advance i
and index
to find other positions that sum is greater or equal to the target. The basic idea of sliding windows is shown in the Figure.
1 | class Solution: |
Valid Anagram
Given two strings s
and t
, return true if t
is an anagram of s
, and false otherwise.
1 | Input: s = "anagram", t = "nagaram" |
Brute Force
This issue can be solved by using two loop. But the time complexity is $O(N)$. In addition, it also can be done through sort algorithm, to check whether same. But it is too complex. Here we did not code for this approache.
Advance Approach
We can use hashmap
to record that how many times they appeared in the string. Therefore, the time complexity is $O(N)$.
1 | class Solution: |
Combinations
Given two integers n
and k
, return all possible combinations of k
numbers out of the range [1, n]
. You may return the answer in any order. For example
1 | Input: n = 4, k = 2 |
It is pretty hard to use loop to solve this issue. Beacause the k
determine the number of nested loop, when k=2
then it should have three nested loop. However, when k in not a const number, then we can not determine how many loops we need.
Therefore, we can use recursion to do this kind of issues. We can seen it as a tree as shown in the Figure. The root node is 1-n
and the deep is k
. Pick up the number from left to right without repeating. We can set up a path
to record the visited path, and when the length of path
is equal to k
then add the path
to result.
1 | class Solution: |
Combination Sum III
Combination Sum III: Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
- Only numbers 1 through 9 are used.
- Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order. For example:
1 | Input: k = 3, n = 7 |
This question is pretty similar with the previous one, just add one more constrain (sum = n).
1 | class Solution: |
Valid Palindrome
Valid Palindrome: Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
1 | Input: s = "A man, a plan, a canal: Panama" |
This one is pretty easy, we just need to ignore the char which is not belong to a-z, 0-9
. During the processing, we can use double pointer to do that. Set a index from the left and a index from the right.
1 | class Solution: |
Next Greater Node In Linked List
Next Greater Node In Linked List is a medium problem.
We are given a linked list with head
as the first node. Let’s number the nodes in the list: node_1, node_2, node_3, ... etc.
Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i
, node_j.val > node_i.val,
and j
is the smallest possible choice. If such a j does not exist, the next larger value is 0.
Return an array of integers answer, where answer[i] = next_larger(node_{i+1})
.
Note that in the example inputs (not outputs) below, arrays such as [2,1,5]
represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.
Approach
This problem is pretty similar with Daily Temperatures which can be solved by applying stack. For this issue, we can use stack
+ hashmap
to solve it with $O(n)$ time complexity.
- Init stack and the final result.
result
(hashmap) will recored the index as key and greater or 0 value as the value- When the
temp
node value is greater then the top value of the stack, update theresult
value and pop the top value of stack.
1 | # Definition for singly-linked list. |
We can use the following data to give an example:
1 | Input: [2,1,5] |
In the following part, we will display the result
and stack
storage.
1 | 1. stack.push((2, 0)) stack = [(2, 0)], result = {0: 0} |
How Many Numbers Are Smaller Than the Current Number
How Many Numbers Are Smaller Than the Current Number is an easy problem. Howerver, it is more interesting than other questions.
Given the array nums, for each nums[i]
find out how many numbers in the array are smaller than it. That is, for each nums[i]
you have to count the number of valid j’s such that j != i
and nums[j] < nums[i]
.
Example 1:
1 | Input: nums = [8,1,2,2,3] |
Approach
Naive approach is brute force. Just use two nest loop can get the result.
1 | result = [] |
The time complexity is O(n^2)