Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,557 members, 7,816,346 topics. Date: Friday, 03 May 2024 at 09:56 AM

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

Nairaland Forum / Science/Technology / Programming / Algorithm And Data Structure Study Section (6920 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 qtguru(m): 9:38pm On Feb 26, 2023
No vex, been busy with life, as election dey go, seems like we will resume this thread grin

4 Likes

Re: Algorithm And Data Structure Study Section by Tenrack: 10:31am On Mar 06, 2023
truthCoder:


Anytime you apply for any role this year, ask yourself this simple question?

Who is going to review my application?

You never know. You just never know.

I own you Andrew.
bros..can I talk to you in private please? Good morning
Re: Algorithm And Data Structure Study Section by truthCoder: 10:41am On Mar 06, 2023
Tenrack:
bros..can I talk to you in private please? Good morning

truthcoder@protonmail.com
Re: Algorithm And Data Structure Study Section by Tenrack: 10:43am On Mar 06, 2023
truthCoder:


truthcoder@protonmail.com
thanks boss
Re: Algorithm And Data Structure Study Section by Tenrack: 10:47am On Mar 06, 2023
truthCoder:


truthcoder@protonmail.com
already sent you a mail sir.
Re: Algorithm And Data Structure Study Section by richebony: 4:04pm On Mar 15, 2023
Please can anyone come up with an optimized solution for solving this PS?

Design an algorithm to find all the common elements in two sorted lists of
numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output
should be 2, 5, 5.

Time complexity should be 0(n) or better
NB : am terrible with algos so any contribution would be highly appreciated

namikaze
truthcoder
qtguru
tensazangetsu20
Re: Algorithm And Data Structure Study Section by namikaze: 8:21pm On Mar 15, 2023
richebony:
Please can anyone come up with an optimized solution for solving this PS?

Design an algorithm to find all the common elements in two sorted lists of
numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output
should be 2, 5, 5.

Time complexity should be 0(n) or better
NB : am terrible with algos so any contribution would be highly appreciated

namikaze
truthcoder
qtguru
tensazangetsu20
Have you tried using an Hashmap/Set?

For example:

from collections import Counter

def common_elements(A, B):
res, map = [], Counter(A)
for num in B:
if map[num]:
res.append(num)
map[num] -= 1
return res


def test():
listA = [2, 5, 5, 5]
listB = [2, 2, 3, 5, 5, 7]
got = common_elements(listA, listB)
want = [2, 5, 5]
assert got == want, f'got {got} want {want}'

test()

1 Like

Re: Algorithm And Data Structure Study Section by richebony: 7:49am On Mar 16, 2023
namikaze:

Have you tried using an Hashmap/Set?

For example:

from collections import Counter

def common_elements(A, B):
res, map = [], Counter(A)
for num in B:
if map[num]:
res.append(num)
map[num] -= 1
return res


def test():
listA = [2, 5, 5, 5]
listB = [2, 2, 3, 5, 5, 7]
got = common_elements(listA, listB)
want = [2, 5, 5]
assert got == want, f'got {got} want {want}'

test()


Thanks a lot man !!


function commonElements(first, second) {
const res = [];
const map = {};

for (let element of first) {
map[element] = (map[element] || 0) + 1;
}

for (element of second) {
if (map[element]) {
res.push(element);
map[element]--;
}
}
return res
}


Hope u don't mind my asking, but how many languages do you actually know because I have seen your code in golang, I once confused you with another handle ( decide )

1 Like

Re: Algorithm And Data Structure Study Section by namikaze: 10:14am On Mar 16, 2023
richebony:



Thanks a lot man !!


function commonElements(first, second) {
const res = [];
const map = {};

for (let element of first) {
map[element] = (map[element] || 0) + 1;
}

for (element of second) {
if (map[element]) {
res.push(element);
map[element]--;
}
}
return res
}


Hope u don't mind my asking, but how many languages do you actually know because I have seen your code in golang, I once confused you with another handle ( decide )
I mostly use golang for my side projects, js(nodejs) because I gotta get a job cry, python for DSA wink. I also know Java, C and C++ but I don't use them anymore.

2 Likes

Re: Algorithm And Data Structure Study Section by Ayrastarr(f): 9:12pm On May 30, 2023
@qtguru

Hi!
Please, how do I join your class?
Re: Algorithm And Data Structure Study Section by qtguru(m): 9:24pm On May 30, 2023
Ayrastarr:
@qtguru

Hi!
Please, how do I join your class?

Do you write PHP ?
Re: Algorithm And Data Structure Study Section by Edipet(m): 10:05pm On May 30, 2023
Tobedated:
Well if someone is studying JavaScript and want to learn dsa he should check out codevolution on YouTube the guy is amazing teaches you all the basics you'll need


If you want to prepare for interview

Then I'll recommend Blind75 it's the holy grail of leetcode you'll find it and solutions at neetcode YouTube channel or neetcode.io

That's my own 5cent I have to offer good luck to all of us
please guide me on javascript
Re: Algorithm And Data Structure Study Section by Ayrastarr(f): 3:10am On May 31, 2023
qtguru:


Do you write PHP ?

Still learning
Re: Algorithm And Data Structure Study Section by qtguru(m): 4:25am On May 31, 2023
Ayrastarr:


Still learning

that's enough I'll share the resources here and meeting for the next class

1 Like

Re: Algorithm And Data Structure Study Section by Ayrastarr(f): 5:14am On May 31, 2023
qtguru:


that's enough I'll share the resources here and meeting for the next class

Alright.

Thank you
Re: Algorithm And Data Structure Study Section by Tobedated(m): 8:55pm On Jun 01, 2023
Edipet:
please guide me on javascript
sorry boss me sef still dey learn am i'll recommend you check out a youtube video or a book on it
Re: Algorithm And Data Structure Study Section by drDoom3(m): 2:03am On Jul 30, 2023
richebony:
Please can anyone come up with an optimized solution for solving this PS?

Design an algorithm to find all the common elements in two sorted lists of
numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output
should be 2, 5, 5.

Time complexity should be 0(n) or better
NB : am terrible with algos so any contribution would be highly appreciated

namikaze
truthcoder
qtguru
tensazangetsu20

The most efficient way to solve this is two pointers in O(n) time and O(k) space where k is the number of common elements. Using a set or hashmap might be O(n) time, but O(n) space so is less efficient.
Re: Algorithm And Data Structure Study Section by drDoom3(m): 3:20am On Jul 31, 2023
Longest Palindromic Substring


Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"

Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"

Difficulty:

Medium

Textbook dynamic programming problem. The key is recognizing that for any palindrome s, adding the same letter to its start and end also gives a palindrome.

For instance, "aba" is a palindrome. Add "c" to it's beginning and end gives "cabac", which is also a palindrome.

Now, for the base case, every string of length 1 is a palindrome. Also, every string of length two with the same letters (i.e "bb"wink is also a palindrome.

Then, the recurrence is given two indices i and j in string s such that s[i] and s[j] contain the same letters, find the longest palindrome in the substring s[i + 1 : j - 1], then add 2 to the length of this longest palindrome to find the length of the longest palindrome in the substring s[0: n] where n is the size of string s.

In the case where s[i] != s[j], the longest palindrome must be from either increasing i or decreasing j. This is because picking i and j together does not form a palindrome, therefore we must make a choice to drop one.

Thus, the full recurrence relation becomes:

s[i:j] = 2 + s[i+1:j-1] if s[i] == s[j]
s[i:j] = max(s[i+1 : j], s[i : j-1] if s[i] != s[j]

Knowing the base case(s) and recurrence relation for a dp problem makes the code implementation trivial, so I will not bother to write it down here.
Re: Algorithm And Data Structure Study Section by drDoom3(m): 3:31am On Jul 31, 2023
drDoom3:


The most efficient way to solve this is two pointers in O(n) time and O(k) space where k is the number of common elements. Using a set or hashmap might be O(n) time, but O(n) space so is less efficient.

In python:

def common_elements(arr1, arr2):
i, j = 0, 0 # pointers for arr1 and arr2 respectively
res = [] # result array

while i < len(arr1) and j < len(arr2):
# first skip all recurring values in both arrays
while i + 1 < len(arr1) and arr1[i] == arr1[i + 1]:
i += 1
while j + 1 < len(arr2) and arr2[j] == arr2[j + 1]:
j += 1

if arr1[i] == arr2[j]: # both are the same, so add to res
res.append(arr1[i])
i, j += 1
elif arr[i] < arr2[j]: # move i pointer
i += 1
else: # move j pointer
j += 1

return res

This should solve it with O(n) time and O(k) extra space where k is the size of res. We don't need a hashmap/set to store every single value in this case.

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

New Private Spamming Tutorials 2016 + Tools + Video / What Softwares Are Used To Push Codes To A Live Website / Machine Learning/data Science/analytics

(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. 34
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.