Agnes  Sauer

Agnes Sauer

1596487260

Graduating from Leetcode

What is LeetCode.com?

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

People who use them

Leetcode gets primarily used for two use cases:

Purpose of this post

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

Image for post

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.

Get the basics right — Don’t run too fast

Image for post

Most people new to Leetcode directly jump into solving the problems without getting a proper understanding of the underlying data structures like a linked liststack, 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:

Linked List:

Binary tree

Method to madness

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.

Image for post

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

What is GEEK

Buddha Community

Graduating from Leetcode
Agnes  Sauer

Agnes Sauer

1596487260

Graduating from Leetcode

What is LeetCode.com?

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

People who use them

Leetcode gets primarily used for two use cases:

Purpose of this post

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

Image for post

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.

Get the basics right — Don’t run too fast

Image for post

Most people new to Leetcode directly jump into solving the problems without getting a proper understanding of the underlying data structures like a linked liststack, 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:

Linked List:

Binary tree

Method to madness

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.

Image for post

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

Leetcode Problem: Partition Labels

I’ve been gaining confidence in my algorithm solving skills. So much so that I’ve graduated to the medium difficulty Leetcode problems. Yay! Can I effortlessly solve every problem I see? Hell no! I’m at the stage where given the problem I can normally come up with a working brute force solution. However, when it comes to finding the most optimized answer, I usually look for a few hints. But that’s ok! As long as I spend time fully understanding the solution before I move on, I will have learned a new trick or two and can apply them to the next problem.

The following is one such problem:

Partition Labels Problem

A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.

Note:

  • S will have a length in the range [1, 500].
  • S will consist of lowercase English letters ('a' to 'z') only.

Example:

Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation: The partition is "ababcbaca", "defegde", "hijhklij".

Algorithm

To solve the problem, we will use a two pointer solution. See steps below:

Steps:

  1. First we create a hashmap of the last index of each unique character in the string. This is done by looping through the string and storing the index at which each letter occurs. If a letter is repeated, the hash will update with the greater index and only the last occurrence will be saved.
  2. Next comes the two pointer part. First we will initialize our partitions array (this is what we will be returning) and we initialize our two pointers to the start of the string (set end and start to 0).
  3. Then we loop through the string, setting the end pointer. The end pointer is the bigger value between the index we are on, and the last index of the character we are on (use the hashmap!).
  4. Next we compare the position of our end pointer against where we are in terms of iterating over the string. If the current index is not the same as the end index, we have to keep moving and do nothing.
  5. However, if the end and current index are the same then we must be at the end of a partition. Basically, this character wont appear again so you should make a cut in the string to ensure we get the maximum number of partitions.
  6. To make a cut, push the length of the segment into the partitions array. Then we update our start pointer to one after the end of the last partition.
  7. After the end of the loop, we simply return the partitions array.

Solution Code

var partitionLabels = function(S) {

  let lastIndex = {}
  for (let i = 0; i < S.length; i++){
    lastIndex[S[i]]= i
  }
  let partitions = []
  let start = 0
  let end = 0
  for (let i = 0; i < S.length; i++) {
    end = Math.max(end, lastIndex[S[i]])
    if (i === end) {
      partitions.push(i — start + 1)
      start = i + 1
    }
  }
  return partitions
}

Space-Time Complexity

Never move on from a problem until you complete the space-time complexity analysis. It can help trigger a signal to a brute force solution where a more optimized approach may exist.

For the solution code above:

Space Complexity: O(n) This has to do with the step where we store the last index of each character. In theory, you could have each character be unique, resulting in O(n) space needed for that hash.

Time Complexity: O(n) We loop through the array twice. Once to create the hash of the last indices and again to create the partitions. This results in a time complexity of 2*O(n) at the worst case. But we don’t care about coefficients and therefore we drop the 2 at the beginning.

#leetcode #leetcode-medium #algorithms

Beth  Nabimanya

Beth Nabimanya

1624874940

Leetcodes Algorithm Series: Contains Duplicate

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

Sid  Schuppe

Sid Schuppe

1615992780

Leetcode Problem Hacks: 881 Boats to Save People

I was doing some recreational programming tonight on LeetCode and I stumbled into an interesting performance case study.

The Problem
This is LeetCode Problem: 881 Boats to Save People.

Boats to Save People

The i-th person has weight people[i], and each boat can carry a maximum weight of limit.

Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.)

#python #leetcode-python #leetcode #recreational-programming

Namani Karthik

1610513150

How to Use LeetCode effectively | Best Strategy to Solve LeetCode Problems

In this video we will talk about how to use LeetCode Effectively.

Subscribe: https://www.youtube.com/channel/UCs6nmQViDpUw0nuIx9c_WvA

#leetcode #web-development