Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,529 members, 7,816,292 topics. Date: Friday, 03 May 2024 at 08:57 AM

Algorithm And Data Structure Study Section - Programming (4) - Nairaland

Nairaland Forum / Science/Technology / Programming / Algorithm And Data Structure Study Section (6917 Views)

Data Structure And Algorithm With Php / Is It Possible To Learn Algorithm And Data Structures? / Nigerian Developers How Did You Master Algorithm And Problem Solving (2) (3) (4)

(1) (2) (3) (4) (5) (6) (7) (Reply) (Go Down)

Re: Algorithm And Data Structure Study Section by Nobody: 1:16pm On Jan 08, 2023
namikaze:

fails some test cases, for example,
arr = [2,6,10,1,0];
correct answer should be [2,4,6,4,2], yours:

//Adjusted, error was from .sort() array Method. grin
link: https://www.jdoodle.com/ia/BXw

function medianStreams() {
var input = [2, 6, 10, 1, 0],
joiner = [],
output = [];
for (let num of input) {
joiner.push(num);
if (joiner.length === 1) {
output.push(joiner[0]);
} else {
if (joiner.length % 2 === 0) {
let evenMiddle = joiner.sort((a, b) => a - b).length / 2;
let average = joiner[evenMiddle] + joiner[evenMiddle - 1];
output.push(average / 2);
} else {
let oddMiddle = joiner.sort((a, b) => a - b).length - 1;
output.push(joiner[oddMiddle / 2]);
}
}
}
console.log(output);
}
medianStreams();

1 Like

Re: Algorithm And Data Structure Study Section by Nobody: 9:50pm On Jan 08, 2023
namikaze can u be posting like 1 question per day?
Re: Algorithm And Data Structure Study Section by Nobody: 12:22am On Jan 09, 2023
Following..
Re: Algorithm And Data Structure Study Section by Nobody: 12:53am On Jan 09, 2023
Let's solve all Easy and Medium Problems on Leetcode in a few Month's Time.

I will be posting questions from Leetcode daily by God's grace.

First will start with the 200+ Easy questions then the Medium then the Hard ones.

Basically, this is the only thing I will be doing in this section.


If you care to join

Here's a good online code editor to share your solutions:

https://www.jdoodle.com/execute-nodejs-online/
Re: Algorithm And Data Structure Study Section by Nobody: 12:54am On Jan 09, 2023
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Re: Algorithm And Data Structure Study Section by Nobody: 12:58am On Jan 09, 2023
GREATIGBOMAN:
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

checkout my solution:

https://www.jdoodle.com/ia/BZ8
Re: Algorithm And Data Structure Study Section by Nobody: 1:17am On Jan 09, 2023
2. Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Re: Algorithm And Data Structure Study Section by Nobody: 1:20am On Jan 09, 2023
GREATIGBOMAN:
2. Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

checkout my solution

https://jdoodle.com/ia/BZ9
Re: Algorithm And Data Structure Study Section by Nobody: 8:14am On Jan 09, 2023
if you're looking to improve your problem-solving skills https://neetcode.io/ is a really great platform.

2 Likes 1 Share

Re: Algorithm And Data Structure Study Section by qtguru(m): 8:23am On Jan 09, 2023
GREATIGBOMAN appreciate your contribution. smiley this is motivating.

3 Likes

Re: Algorithm And Data Structure Study Section by namikaze: 8:31am On Jan 10, 2023
GREATIGBOMAN:
namikaze can u be posting like 1 question per day?
sure

1 Like 1 Share

Re: Algorithm And Data Structure Study Section by AlchemyOfCodes: 10:04am On Jan 10, 2023
Wow great thread. Abeg u guys should comment your code or at least give us the approach u used in tackling the questions because of beginners like me interested in learning DSA. So we don't get scared off cheesy

I highly recommend the book " grokking algorithm" or "a common sense guide to DSA" for absolute beginners.
Re: Algorithm And Data Structure Study Section by Sleekcode: 3:52pm On Jan 10, 2023
greatigboman Banned.

Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

In a nutshell.
Remove the duplicate in a given sorted array without creating any extra array then return the length after modifying the original given array.
Re: Algorithm And Data Structure Study Section by Sleekcode: 3:56pm On Jan 10, 2023
Sleekcode:
greatigboman Banned.

Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

In a nutshell.
Remove the duplicate in a given sorted array without creating any extra array then return the length after modifying the original given array.


my Solution== https://www.jdoodle.com/ia/C3O
Re: Algorithm And Data Structure Study Section by Sleekcode: 4:08pm On Jan 10, 2023
Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.




Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Difficulty= Medium
Re: Algorithm And Data Structure Study Section by Devdevdev(f): 2:08pm On Jan 11, 2023
GREATIGBOMAN:


checkout my solution:

https://www.jdoodle.com/ia/BZ8

Your solution is a brute Force O of n squared solution that has you iterating over every element in the array. What if you have 1 million numbers in the array? This will take hours to compute.

The faster solution would be something like this:

I'm using python here.

Create a function. I'll call it findTwoSum
say the array of numbers is defined as myNumbers
And our Target value is defined as target

def find_Two_Sum() {

Create an empty dictionary and give it a name,

myDict = {}

for index, number in enumerate(myNumbers):
difference = target - number
if difference in myDict:
return [myDict[difference], index]
return myDict[number] = index
}
Re: Algorithm And Data Structure Study Section by Nobody: 4:40pm On Jan 11, 2023
Devdevdev:


Your solution is a brute Force O of n squared solution that has you iterating over every element in the array. What if you have 1 million numbers in the array? This will take hours to compute.

The faster solution would be something like this:

I'm using python here.

Create a function. I'll call it findTwoSum
say the array of numbers is defined as myNumbers
And our Target value is defined as target

def find_Two_Sum() {

Create an empty dictionary and give it a name,

myDict = {}

for index, number in enumerate(myNumbers):
difference = target - number
if difference in myDict:
return [myDict[difference], index]
return myDict[number] = index
}



Post it somewhere where u can share your answer
Re: Algorithm And Data Structure Study Section by sqlPAIN: 5:07pm On Jan 11, 2023
Devdevdev:


Your solution is a brute Force O of n squared solution that has you iterating over every element in the array. What if you have 1 million numbers in the array? This will take hours to compute.

The faster solution would be something like this:

I'm using python here.

Create a function. I'll call it findTwoSum
say the array of numbers is defined as myNumbers
And our Target value is defined as target

def find_Two_Sum() {

Create an empty dictionary and give it a name,

myDict = {}

for index, number in enumerate(myNumbers):
difference = target - number
if difference in myDict:
return [myDict[difference], index]
return myDict[number] = index
}


grin I just dey laugh but I swear I no know why I dey laugh grin
Re: Algorithm And Data Structure Study Section by Devdevdev(f): 7:00pm On Jan 11, 2023
GREATIGBOMAN:


Post it somewhere where u can share your answer

I tried to copy the link to my solution but I didn't find it. It is my first time using the site. So I took a screenshot of my solution. As you can see, mine compiled in 1.006 seconds, while yours compiled in 1.77 secs. This was just an array of length 4. If it was an array of 1 million numbers, it would literally take hours for yours to compile because you are iterating over the numbers linearly and comparing them one by one, whereas my solution will take seconds.

If you doubt me, try increasing the array length and compare both our solutions.

Re: Algorithm And Data Structure Study Section by Devdevdev(f): 7:20pm On Jan 11, 2023
I don't think it is wise to even use Javascript to solve DSAs. Javascript usually only allows you to arrive at brute force solutions, that is if you even arrive at the solution to begin with. This is because Javascript doesn't have support for a lot of data structures.

I hate python. Most of my learning time is spent heavily on Javascript, typescript and react. But I had to learn python just because of these annoying DSA questions.
Re: Algorithm And Data Structure Study Section by Nobody: 7:26pm On Jan 11, 2023
Devdevdev:


I tried to copy the link to my solution but I didn't find it. It is my first time using the site. So I took a screenshot of my solution. As you can see, mine compiled in 1.006 seconds, while yours compiled in 1.77 secs. This was just an array of length 4. If it was an array of 1 million numbers, it would literally take hours for yours to compile because you are iterating over the numbers linearly and comparing them one by one, whereas my solution will take seconds.

If you doubt me, try increasing the array length and compare both our solutions.

I didn't doubt you.

They're both correct.... Python is generally faster than JavaScript.

Not like yours will run exactly same time with array length of a million.
Re: Algorithm And Data Structure Study Section by Devdevdev(f): 7:39pm On Jan 11, 2023
Re: Algorithm And Data Structure Study Section by Devdevdev(f): 7:47pm On Jan 11, 2023
GREATIGBOMAN:


I didn't doubt you.

They're both correct.... Python is generally faster than JavaScript.

Not like yours will run exactly same time with array length of a million.


This isn't about whether python is faster than Javascript. The crux of the matter is that my solution is faster than yours. It is not about the language but about the solution. You could apply a better solution in Javascript that would compile in a faster time, that is if you swallow your ego, sit down and think of how to do that.

If you present your solution in an interview you will fail that interview woefully.

Java is way faster than python but if I implemented your solution in java it will still be slower than the implementation of my solution in python.

DSA questions don't test correctness, they test speed, memory management, efficiency and scalability. For someone who spends a lot of time insulting people that "cannot code", I thought this is something you should know by now.

1 Like

Re: Algorithm And Data Structure Study Section by namikaze: 7:54pm On Jan 11, 2023
Sleekcode:
Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.




Example:

Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Difficulty= Medium
with recursive dfs
jdoodle.com/ia/C7K

map = {
'2':"abc",'3':"def",'4':"ghi",
'5':"jkl", '6':"mno",'7':"pqrs",
'8':"tuv",'9':"wxyz"
}

def letterCombinations(s, cur="", res=[]):
if s == "":
res.append(cur)
return

n, s = s[0], s[1:]
for adj in map[n]:
letterCombinations(s, cur+adj, res)

return res


s = "23"
print(letterCombinations(s))
Re: Algorithm And Data Structure Study Section by Nobody: 8:28pm On Jan 11, 2023
Devdevdev:


This isn't about whether python is faster than Javascript. The crux of the matter is that my solution is faster than yours. It is not about the language but about the solution. You could apply a better solution in Javascript that would compile in a faster time, that is if you swallow your ego, sit down and think of how to do that.

If you present your solution in an interview you will fail that interview woefully.

Java is way faster than python but if I implemented your solution in java it will still be slower than the implementation of my solution in python.

DSA questions don't test correctness, they test speed, memory management, efficiency and scalability. For someone who spends a lot of time insulting people that "cannot code", I thought this is something you should know by now.

By your own logic if I was to provide a solution faster than yours that would render your current solution useless?

You should note that replying you is very stressful for me... You always sound like a loud entitled Karen or a 50 year old angry lady who's now trying to make something out of her failed life.
Re: Algorithm And Data Structure Study Section by qtguru(m): 8:45pm On Jan 11, 2023
Devdevdev:


I tried to copy the link to my solution but I didn't find it. It is my first time using the site. So I took a screenshot of my solution. As you can see, mine compiled in 1.006 seconds, while yours compiled in 1.77 secs. This was just an array of length 4. If it was an array of 1 million numbers, it would literally take hours for yours to compile because you are iterating over the numbers linearly and comparing them one by one, whereas my solution will take seconds.

If you doubt me, try increasing the array length and compare both our solutions.

"Premature optimization is the root of evil", the difference in speed is not that much to worry about,
Re: Algorithm And Data Structure Study Section by Devdevdev(f): 8:48pm On Jan 11, 2023
qtguru:


"Premature optimization is the root of evil", the difference in speed is not that much to worry about,

The difference in speed will be much if the length of the array increases exponentially.
Re: Algorithm And Data Structure Study Section by Devdevdev(f): 8:54pm On Jan 11, 2023
GREATIGBOMAN:


By your own logic if I was to provide a solution faster than yours that would render your current solution useless?

You should note that replying you is very stressful for me... You always sound like a loud entitled Karen or a 50 year old angry lady who's now trying to make something out of her failed life.

You still don't want to accept that your solution is slow and will give you a FAIL in an interview.

Ordinary this simple algorithm question and you can't provide an efficient solution. How will you now solve harder questions efficiently?

Set awon Nairaland troll. Maybe if you invest all the time you use in trolling and insulting people into practicing DSA questions, you'll get better. You are still at level zero. Providing brute force solution to ordinary Two Sum question.

It's clear that all my hard work is paying off. The more knowledgeable I become the more I realize just how much of a quack people like you are. Continue trolling Nairaland from morning till night while I invest my time improving my coding skills.

1 Like

Re: Algorithm And Data Structure Study Section by Nobody: 9:14pm On Jan 11, 2023
Devdevdev:


You still don't want to accept that your solution is slow and will give you a FAIL in an interview.

Ordinary this simple algorithm question and you can't provide an efficient solution. How will you now solve harder questions efficiently?

Set awon Nairaland troll. Maybe if you invest all the time you use in trolling and insulting people into practicing DSA questions, you'll get better. You are still at level zero. Providing brute force solution to ordinary Two Sum question.

It's clear that all my hard work is paying off. The more knowledgeable I become the more I realize just how much of a quack people like you are. Continue trolling Nairaland from morning till night while I invest my time improving my coding skills.

.
Re: Algorithm And Data Structure Study Section by qtguru(m): 9:22pm On Jan 11, 2023
Devdevdev:


The difference in speed will be much if the length of the array increases exponentially.

Oh I see, let's benchmark it with a high length of array and see what the time difference will be. Can you share a link ?

NOTE: @GREATIGBOMAN, I see devdevdev's point, the constant is n (Length of Array), if there are 5 million items, you will loop 5 million times, that's a brute force, but I don't think it's room for argument, it's an opportunity to provide alternative algo that is efficient.

Let's not argue and look at efficient runtime. I will join you guys soon, this week or next.

1 Like

Re: Algorithm And Data Structure Study Section by Nobody: 9:25pm On Jan 11, 2023
Devdevdev:


You still don't want to accept that your solution is slow and will give you a FAIL in an interview.

Ordinary this simple algorithm question and you can't provide an efficient solution. How will you now solve harder questions efficiently?

Set awon Nairaland troll. Maybe if you invest all the time you use in trolling and insulting people into practicing DSA questions, you'll get better. You are still at level zero. Providing brute force solution to ordinary Two Sum question.

It's clear that all my hard work is paying off. The more knowledgeable I become the more I realize just how much of a quack people like you are. Continue trolling Nairaland from morning till night while I invest my time improving my coding skills.


grin grin

It took you 4 months to prepare for this moment.

Yet those reading your pile of trash knows your spitting nonsense and disgracing your lineage as usual.

In what world slow = fail in a question where know one asked you to put time n space complexity into consideration?


You even cherry picked the most basic question to flex your amateur muscles yet you're still wrong.


I ask one more time, is this you solution the most effective/efficient solution of them all?

If I was to provide a more efficient solution would it render yours a fail and useless?

You're even using the run time of that online compiler to judge the speed of your code.


For your information the run time of that online compiler isn't constant and changes based upon the speed of the server... you can try running your code 3 or 4 times.



They're different approach to solving problem... The fact you provided one which was faster in a question which speed wasn't required or asked in the first place doesn't make the other person's solution which also arrived at the correct answer in all test cases wrong.



Test cases!

Provided I enter the solution I did on leetcode it'd still be correct rendering this you bias argument useless.

1 Like 1 Share

Re: Algorithm And Data Structure Study Section by qtguru(m): 9:30pm On Jan 11, 2023
Please no insults in this thread.

1 Like

(1) (2) (3) (4) (5) (6) (7) (Reply)

Hilarious Memes you can relate with as a Programmer / New Private Spamming Tutorials 2016 + Tools + Video / What Softwares Are Used To Push Codes To A Live Website

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 66
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.