We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly
1
.Given an integer array
nums
, return the length of its longest harmonious subsequence among all its possible subsequences.A subsequence of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.
1 <= nums.length <= 2 * 10^4
-10^9 <= nums[i] <= 10^9
For this question, I used a map just for the…
Given two strings
A
andB
of lowercase letters, returntrue
if you can swap two letters inA
so the result is equal toB
, otherwise, returnfalse
.Swapping letters is defined as taking two indices
i
andj
(0-indexed) such thati != j
and swapping the characters atA[i]
andA[j]
. For example, swapping at indices0
and2
in"abcd"
results in"cbad"
.
0 <= A.length <= 20000
0 <= B.length <= 20000
A
and B
consist of lowercase letters.For this problem, one of the…
Given
head
, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the
next
pointer. Internally,pos
is used to denote the index of the node that tail'snext
pointer is connected to. Note thatpos
is not passed as a parameter.Return
true
if there is a cycle in the linked list. Otherwise, returnfalse
.
I used a set…
Given the
root
of a binary search tree and the lowest and highest boundaries aslow
andhigh
, trim the tree so that all its elements lies in[low, high]
. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
I solved this problem…
Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).
Note:
Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3 above, the input represents the signed integer.
-3
.Follow up: If this…
Today, we will be looking at the following problem listed as hard with an acceptance rate of 38.6% at the time of writing.
The problem goes as follows:
Given the
root
of a binary tree, calculate the vertical order traversal of the binary tree.For each node at position
(row, col)
, its left and right children will be at positions(row + 1, col - 1)
and(row + 1, col + 1)
respectively. The root of the tree is at(0, 0)
.The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column…
Today, we will be looking at the following problem:
Given an integer
n
, return the decimal value of the binary string formed by concatenating the binary representations of1
ton
in order, modulo10^9 + 7
.
Here is my solution:
In order to figure out this problem, I had to learn about bitwise operations and how shifting bits to the left is equal to multiplying the number by 2 * (number of places shifted)
.
For example, if I had the number 25
which translates to 11001
. A left shift would result in 110010
which is just a…
Before diving into the AWS Certified Cloud Practitioner or CCPs, let me tell you more about why “the cloud” excites me.
As you can see “the cloud” is everywhere around us. We can see that GitHub is a cloud repository for our code and services such as MongoDB Atlas or Heroku are helping you host your applications on the cloud. It’s easy to take for granted that we can very easily host those applications on storage that we do not own.
This is part 3 of the Learning TypeScript series. In part 2, we looked into what interfaces in TypeScript are and how they describe the shape of our objects in JavaScript. In this article, we will be exploring functions in more detail.
TypeScript supports both named and anonymous functions just like in JavaScript. To add types to our functions, we could explicitly add types to our parameters and return values (optional).
Another way we can type check is by defining the type first before referencing that type with our function.
This is part 2 of the Learning TypeScript series. In part 1, we looked into what TypeScript is, how to install it, and its basic types. In this article, we will be exploring its interfaces.
Simply put, an interface is a way of describing the shape of an object. In TypeScript, we are only concerned with checking if the properties within an object have the types that are declared and not if it specifically came from the same object instance.
To emphasize how TypeScript only checks the shape of objects, we have thisObj
and thatObj
with the same property…
I have a passion for coding.