My name is Ifihan
Question on Leetcode - Easy
My first approach was to traverse through the student
array and compare with the sandwiches
array. If they're the same, pop the student. If it is not, remove and add to the end of the array. Continue like that until there are no matches left and then return with the length. I was left with an error, hmm. It was inacurrate. The sandwiches
were not accounted for.
Then the second approach was using a while loop and popping the students
and sandwiches
if they were the same. If they are not, I searched for the student in the sandwiches
array and pop and return the length. It worked but I got Time Limit Exceeded.
My final approach was to use a hashmap to store 0's and 1's. I first count the number of students with each preference using a dictionary. Then, I iterate over the sandwiches, checking if there are any students remaining with the same prefer
Question on Leetcode - Easy
The appraoch used is to creates two sets, distincts
and seen
. The distincts
set will store the unique elements of the input array arr, and the seen
set will store the elements that have already been seen.
We then iterate through the input array arr
. For each element num, the code checks if num is in the seen set. If it is, then the code removes num from the distincts set. Otherwise, the code adds num to both the distincts and seen sets.
After the loop has finished, the code checks if the length of the distincts set is less than the input k
. If it is, then the code returns the empty string. Otherwise, the code returns the k-th element of the distincts set.
Question on Leetcode - Easy
The question states there are two integers that serve as boundaries for the array (left
and right
). Taking that into condideration, we are to only search for vowels in that boundary.
What qualifies a word to be a vowel string? It starts with a vowel character and ends with a vowel character. And the vowels in the English dictionary are: a
, e
, i
, o
, and u
.
To go about this, I defined a res
that would serve as a counter. Then run a for loop over the array with the range being the boundaries left
and right
. Then I check if the current word first letter and last letter starts with any vowel. If it does, add to counter res
and if it does not, pass.
Question on Leetcode - Easy
My approach to this question was use a set to break down each string into a set and store their occurence to a hashmap. Then, I compare the values of each character to the length of the array.
For example, if a
appears three times in the array and the length of the array is four, a
is not going to be included in the result
printed.
Any edge cases thought of? Nope. Not yet.
Question on Leetcode - Easy
The approach to this question looks straighforward and simple at first. I'm returning the list pf squared arrays using sorted(). Should solve better tomorrow.
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
Question on Leetcode - Easy
The approach I took was to use two pointers. One moved the normal speed, while the other went twice as fast as the first pointer.
When the faster pointer reaches the end, it returns the value (node) of the value at that time.
Question on Leetcode - Easy
(high level summary)
The approach taken was to use two pointer method: one that takes account of the current value and the other to take the value ahead. Then I traversed through the linkedlist.
First, I use a while
loop to iterate over each node in the linked list. It will continue until the current variable reaches the end of the list (i.e., None). Inside this while
loop, another variable, temp
is initialized with the next node after the current node (curr). This temp variable is used to check for duplicate values.
Question on Leetcode - Easy
The approach taken was to use a pointer intialized at 0, for the sequence s
string. This pointer servers as a counter which the increases for every positive match against the main string.
Afterwards, I traversed through the main string t
and returned the a boolean of if the pointer value was the same as the length of the sequence s
.
class Solution:
Question: Leetcode - Easy
The approach to use two pointers: The first one was a dummy ListNode()
and the second is a tail. Then I traverse throught the
list by appending the smaller values of either lists in the tail pointer.
Once the end of the list is gotten to, I then append the remaining nodes to the linked list.