As the covid-19 pandemic fades in the US, many companies are preparing their employees to return to the office. I work in a large tech company that is also going in that direction, and with a slow rollout, we can reasonably expect that the vast majority of employees will be back in the office by the end of the year.

There has been some pushback, as some workers would like to prolong the period that they were allowed to work from home, or would like to come to the office only for a few days per week. They contend that…


In this problem (https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/), we want to count the number of substring of length 3(i.e. 3 characters in a row in the string), such that these substring don’t contain duplicate characters.

Given n the length of the input string, we can solve the problem by testing all the substrings of length 3. The number of substrings of length 3 is n — 2, that is O(n). For each of the substring, we only need to compare the 3 characters with 3 comparisons, that is O(1).

class Solution {
public int countGoodSubstrings(String s) {…

In this problem, we are trying to optimize the “score” of “good subarrays defined this way:

You are given an array of integers nums (0-indexed) and an integer k.

The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). A good subarray is a subarray where i <= k <= j.

Return the maximum possible score of a good subarray.

Solution

The brute force approach for this would be to try all the pairs (i,j) such that i <= k <= j. Assuming that we can optimize and get the minimum…


In this Leetcode problem, we are given a list of edges representing a star graph. A star graph is a graph where all the edges contain the “center” node. This is the problem statement:

There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.

You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui


This problem is very classic. I find it helpful to see all the values in the array, as differences between values in a series, and to draw it on paper. Don’t try to find a complicated solution to this problem. You only need to remember a couple of things like the minimum and maximum sum you encountered for instance. Try to do it in one simple pass.

We apply a simple greedy method to find the maximum absolute sum of any subarray.

As we traverse the array, we keep track of the current sum.

We retain the max_sum and the…


In this Leetcode problem, we need to count the vowels in each half of the string in input. If the number of vowels is the same in both halves, we return true, we return false otherwise.

Here is the implementation for the all the main backend languages, in alphabetical order:

C++

class Solution {
public:
bool halvesAreAlike(string s) {
int n = s.length();
int count = 0;
set<char> vowels;
vowels.insert('a');
vowels.insert('e');
vowels.insert('i');
vowels.insert('o');
vowels.insert('u');
vowels.insert('A');
vowels.insert('E');
vowels.insert('I');
vowels.insert('O');
vowels.insert('U');
for (int i = 0; i < n/2; i++) {…

In this Leetcode problem, we are given an array nums of integers. In one operation, we can take any pair of integers adding up to k and take it out of the array. We want to know the maximum number of operations that we can do following that rule.

For any number num in nums, it can only be paired with k — numfor the sum to be equal tok. …


In this Leetcode problem, we are implementing a Queue from from with we can push and pop from the front, back, or middle.

Problem statement

Solution

Obviously, the pop and push from the middle are the core of the problem. Otherwise we could use a LinkedList.

To solve this problem, we break up the linked list into 2 linked lists. We split it in the middle, so that it is easier when we need to push or pop from the middle.

Now, the lists can become unbalanced, so we ensure that after each operations, the lists have the same sizes, or differ by…


In this Leetcode problem, we want to merge a list list2 into another list list1 and remove some elements of list1 where list2 is going.

Problem statement

Solution

We want to simply build the list simply by going to the node in list1 where list2 should be inserted. Then keep going in list1 to get to the node that should come after the last node of list2.

Let’s look at an example:

list1 = [1, 2, 3, 4, 5];
list2 = [6, 7, 8];
a = 3;
b = 4;

We setup a list current to go through list1. After we go through…


In this Leetcode problem, we are given a String representing a list of keys pressed on a keyboard. We are given the time for which each key was released in the array int[] releaseTimes. We assume that at the exact same time that a key is released, the next key is pressed, and that the first key started to be pressed at the time 0. We are asked to find the character corresponding to the key that was pressed the longest. In case of the same duration, we should return the key that is the greatest.

Here is the exact…

Pierre-Marie Poitevin

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