1635291540

Hello! Today’s problem comes from Leetcode’s Top Interview Questions — Easy under the *Others* chapter.

In **Pascal’s Triangle**:

Given an integer numRows, return the first numRows of

Pascal's triangle.

In

Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

1624874940

Hello, hello! As I continue my job search, I am practicing my algorithm questions on Leetcode. So, I thought I would blog about some Leetcode problems as I solve them.

As a Bootcamp graduate, I did not get much practice in algorithms, so it has been a very fun and sometimes frustrating trial and error in recognizing patterns, optimizing my code to be faster (looking at you Big-O), learning how to break down a problem, and sometimes implementing neat math tricks to solve these algorithm problems. I have been practicing them using JavaScript.

As I’m relatively new to algorithm problems, I’ve started out with their Easy Collection of Top Interview Questions. So in **Contains Duplicate:**

Given an integer array `nums`

, return `true`

if any value appears **at least twice** in the array, and return `false`

if every element is distinct.

Examples:

```
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
```

Simply, if any numbers in the array appear more than once in the array, I need to return `true`

. Now, I will definitely need to figure out a way to compare a number to the other numbers. Additionally, I will most likely need to loop through the array so I have access to each number to then compare it to others.

First, I thought that I could iterate through the array, and at each instance, I iterate through another loop of the remaining integers and compare them to see if any are equal (===). However, per my beginner understanding of Big-O, this would yield me an unfavorable time complexity of O(n²).

Then, I thought that I could create an object and count how many times a number in the array appears. By doing this, I could then check to see if any of the numbers had a count greater than 1. If so, then I could return `true`

else I would return `false`

.

#computer-science #leetcode #javascript #programming #algorithms #leetcodes algorithm

1596429120

A palindrome is a word that reads the same forwards and backwards. In my universe, there are two kinds of palindrome: ** “Odd Pal”** and

Even Pal is a palindrome which has an even length, for example: “ZYXXYZ”. In the visualization below you can see how we can split it into 2 parts that have size 3: “ZYX” and “XYZ” . This palindrome will always have two centres called **“ Left Center”** at index2 and

“Even Pal”

Notice that:

- character at index2 = character at index3
*(mirror 1)* - character at index1 = character at index4
*(mirror 2)* - character at index0 = character at index5
*(mirror 3)*

It is because of the above equality of characters at their respective indexes that the string is a palindrome and the two parts look like a sequence of *3 mirrors.*

Odd Pal is a palindrome which has an odd length, example: “ZYXWXYZ”. In the visualization below you can see how we can split the string into 2 parts that have size 3: “ZYX” and “XYZ”, while “W” at index3 becomes the center.

“Odd Pal”

Notice that:

- character at index2 = character at index4
*(mirror 1)* - character at index1 = character at index5
*(mirror 2)* - character at index0 = character at index6
*(mirror 3)*

Notice how again the two parts look like a sequence of *3 mirrors.*

The problem states that we have to find the longest substring that is a palindrome in the string. A substring is a section of a string that consists of contiguous elements. Some examples are:

Input1: “BABAD” | Output1: “BAB” or “ABA”

Input2: “CBBD” | Output2: “BB”

Input3: “A” | Output3: “A”

The simplest way to solve for this is to take every index as 1) a center 2) a Left Center and *“**expand outwards**”* to make character comparisons and find mirrors. The sequence with the maximum number of mirrors would be returned. Seems very simple and intuitive but the complexity is O(n²) because expanding a palindrome around a center could take O(n) time and we will explore every index in the string to be a center and left center, so O(n²) would be the time complexity.

We can improve this runtime if we think of a way where we can leverage the previous palindromes found (as we navigate through the string) and reduce the “expanding outwards” part. Luckily, this is possible with ** “Manachar’s algorithm”**.

Before diving into the algorithm itself, we need to learn a few things first. For simplicity we will be focusing on Odd Pals for the most part. So let’s learn a few things in order:

#palindrome #leetcode #algorithms #substring #coding #algorithms

1596487260

It’s a website where people–mostly software engineers–practice their coding skills. It’s pretty similar to sites like HackerRank & Topcoder which will rank your code written for a particular problem against the ones submitted by other users

Leetcode gets primarily used for two use cases:

- Solve interesting data structure problems
- Prepare and practice for coding interviews 🤔

I never had a clear understanding when it comes to data structures and algorithms, so being a software engineer for the last 12 years, I thought it’s not a bad idea to at least start now. So as of Dec 2019, I became a premium member in Leetcode and committed myself to get as much value out of 150$ that I spent on the membership fees. Leetcode is like an ocean; it has closer to 2000+ questions, which keeps growing day by day.

In fact, this was my exact reaction when I open the website for the first time after looking at the number of questions and categories it had

In this post, let me explain some of the methods I developed over some time, which helped me to understand and learn about these data structures & algorithms.

Most people new to Leetcode directly jump into solving the problems without getting a proper understanding of the underlying data structures like a linked list, stack, queue, tree, etc. Trust me this is a bad idea

I would recommend spending some time understanding these data structures, which are the building blocks for solving any problems.

Even if you are not able to go through all the data structures, at least get a strong understanding of the *linked list* and _binary trees. _Fortunately, there are some excellent materials created by Standford, which can help you with this:

- http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
- http://cslibrary.stanford.edu/105/LinkedListProblems.pdf

Leetcode has many categories and a whole lot of questions in each of them, so it’s essential to come up with a method that could help you to understand these concepts. Learning with LeetCode is a long process, and it will take time, so one thing that I clearly understood is you have to keep the enthusiasm and eagerness to learn high if you want to go through the journey.

If you are a beginner, then use the below flowchart to learn things and gradually increase the complexity of the problems that you solve. It also helps in understanding the concepts well so you can reapply those techniques in the subsequent problems.

Sequence for solving problems

Stages are numbers in red circles, and dotted lines are entirely optional for proceeding to the next stage. When you are done with stage 3 depending upon your motivation, you will end up either doing “*Monthly coding challenges*” or “*attending interviews*” (both are fun)

#coding #leetcode #data-structures #problem-solving #algorithms #algorithms

1597352220

In older phones, the keypads are used to type both numbers and letters, as follows:

Given a string of digits, write a function to return **all possible combinations** of letters that can be generated from pressing those digits on the phone’s keypad.

**Input:**

The string of digits.

```
25
```

**Output:**

A list of combinations, separated by space. We will sort the output for you so you can easily compare it with our expected output.

```
aj ak al bj bk bl cj ck cl
```

- This is a straightforward recursion question that asks to search for all combinations.
- The idea is, for each digit, we give it 1 of the 3 or 4 matching letters.
- Then, we recurse on the next digit, giving it a letter, and moving on until we reach the terminal condition, that is the end of the
**digits**string. - The question asks to return a list of combinations, which can be tricky and unoptimized if we are not careful.

Stay away from heavy array operations such as shallow copying. Concatenating arrays, in some languages, do it the heavy way where it shallow copies the arrays and reconstructs the merged one.

- Create a
**findCombinations**recursion function that takes in**index**and iterates through the matching letters for the digit at**index**. For each letter, recursively call**findCombinations**for**index+1**. - Terminal condition: we reach the end of the
**digits**string. - The output array is passed by reference to
**findCombinations**. We push any found results directly to this array, so no extra array operations are required. - Because each output contains the letter replacement for all the digits, we need to pass this data along as we recurse.

#javascript #algorithms #frontend #java #leetcode #algorithms

1624970880

As more data, better algorithms, and higher computing power continue to shape the future of artificial intelligence (AI), reliable machine learning models have become paramount to optimise outcomes. OpenAI’s meta-learning algorithm, Reptile, is one such model designed to perform a wide array of tasks.

For those unaware, meta-learning refers to the idea of ‘learning to learn by solving multiple tasks, like how humans learn. Using meta-learning, you can design models that can learn new skills or adapt to new environments rapidly with a few training examples.

In the recent past, the meta-learning algorithm has had a fair bit of success as it can learn with limited quantities of data. Unlike other learning models like reinforcement learning, which uses reward mechanisms for each action, meta-learning can generalise to different scenarios by separating a specified task into two functions.

The first function often gives a quick response within a specific task, while the second function includes the extraction of information learned from previous tasks. It is similar to how humans behave, where they often gain knowledge from previous unrelated tasks or experiences.

Typically, there are three common approaches to meta-learning.

- Metric-based: Learn an efficient distance metric
- Model-based: Use (recurrent) network with external or internal memory
- Optimisation-based: Optimise the model parameters explicitly for fast learning

For instance, the above image depicts the model-agnostic meta-learning algorithm (MAML) developed by researchers at the University of California, Berkeley, in partnership with OpenAI. The MAML optimises for a representation θ that can quickly adapt to new tasks.

On the other hand, Reptile utilises a stochastic gradient descent (SGD) to initialise the model’s parameters instead of performing several computations that are often resource-consuming. In other words, it also reduces the dependency of higher computational hardware requirements, if implemented in a machine learning project.

#developers corner #how reptile works #meta learning algorithm #meta-learning algorithm #algorithm