Permutation Array Check in Python and C++ Codility Solutions

preview_player
Показать описание
This video describes the permutation permcheck lesson 4 of codility programming interview exercises, in C++ and in Python. A permutation array is described and 2 algorithms are detailed showing the difference with the efficient solution. Hope you will enjoy learning this example. python algorithms interview.

🍓 If you want to follow structured courses with more details and practice exercises check my "About" page for Discount Coupons on my Udemy courses covering: Python basics, Object Oriented Programming and Data Analysis with NumPy and Pandas, ... more courses are on the way drop me a message if you have a particular interesting topic! Good luck!

#python #cplusplusprogramming #codinginterview
Рекомендации по теме
Комментарии
Автор

Great explanation as usual.
This one was surprisingly easy given the nightmare FrogRiverOne problem that comes before it.
My solution uses set theory to do the comparison, but pretty much identical approach:

def solution(A):
n = len(A)
A = set(A)
B = set(x for x in range(1, n+1))
if len(A&B) == n:
return 1
return 0

wonderjaxe
Автор

My solution, using what I learned from the book "Python One-Liners"

def solution(A):
return 1 if sorted(A) == [*range(1, len(A)+1)] else 0

juandavidtorres
Автор

It doesn't mention whether duplicates are sent in. Convert to set, then sort, then compare the first and last items could result in a quicker algorithm.

TorstigHjalmr
Автор

Hey, great videos! Question though, does the sorting not increase the complexity of O(n)?

TonyBoy
Автор

Would it be right to order the array first and then see that for each element starting from index 0 if the next element is the current element + 1 then return true, else return false?

confluenceking
Автор

How is your solution O(n)? Youre using a sort method, none of which are linear.

gamesniper
welcome to shbcf.ru