Image for post
Image for post
Photo by Aaron Burden on Unsplash

Description

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Solution

Every time when you face a problem where you need to go through a tree level by level you should think about BFS — Breadth-first search.

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a search key) and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level — wiki. …


Image for post
Image for post
Photo by Pisit Heng on Unsplash

Description

In an alien language, surprisingly they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.

Example 1:

Example 2:


Image for post
Image for post
Photo by Maxim Melnikov on Unsplash

Description

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Example 1:

Example 2:

Constraints:

  • 1 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 104

Solution

In this problem, we can sort the intervals in advance and after that we can go through the sorted array and try to merge somehow these intervals. …


Image for post
Image for post
Photo by Upgraded Points on Unsplash

Description

Given an m x n 2d grid map of '1's (land) and '0's (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Example 2:

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is '0' or '1'.

Solution

Let’s take a look at Example 1.


Image for post
Image for post
Photo by Beth Jnr on Unsplash

Description

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:

Solution

The first solution that comes to my mind was two nested for loops:


Image for post
Image for post
Photo by Benjamin Child on Unsplash

Description

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine if a person could attend all meetings.

Example 1:

Example 2:

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti < endi <= 106

Solution

I came up with a brute force solution when I had seen this question the first time. We can go through the intervals array and create a Set with all busy minutes. …


Image for post
Image for post
Photo by Iva Rajović on Unsplash

Description

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Example 1:

Example 2:

Example 3:

Example 4:

Example 5:

Constraints:

  • 1 <= s.length <= 104
  • s consists of parentheses only '()[]{}'.

Solution

The best data structure here is stack. Because we need to check the right order of these parentheses. …


Image for post
Image for post
Photo by Adarsh Kummur on Unsplash

Description

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Example 1:


Image for post
Image for post
Photo by Mathew Schwartz on Unsplash

Description

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. …


Image for post
Image for post
Photo by Cristina Gottardi on Unsplash

Description

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XIIwhich is simply X + II. The number twenty-seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. …

About

Anatolii Kurochkin

Software Engineer | Frontend Developer | React Developer | JavaScript Developer https://anatolii.tech/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store