#๐Ÿ”’ Problems with leet code

175 messages ยท Page 1 of 1 (latest)

hybrid umbra
#

Im new to leet code

astral hedgeBOT
#

@hybrid umbra

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hybrid umbra
#

Im not entirely sure on how to output your answers in Leet code

neat coral
#

@hybrid umbra which question

hybrid umbra
#

Two sum

#

first one

neat coral
#

hmm

hybrid umbra
#

My Stdout gives correct answer always

#
print(Solution().twoSum(nums, target))
neat coral
#

you know return function ?

hybrid umbra
neat coral
#

you should not print them
you should return the answer in leetcode for the answer to be matched

#

check it

#

return it as a list

hybrid umbra
#

when i return a value the output is nothing

neat coral
#

yea, if you want output then you need to print it separately but to pass the test case you need to return the value

#

coz basically on the other end
the function is run

hybrid umbra
#
nums = input()
target = input()

class Solution(object):
    def twoSum(self, nums, target):
        for idx1 in range(0, len(nums)):
            val = nums[idx1]

            for idx2 in range(idx1 + 1, len(nums)):
                val2 = nums[idx2]
                if val + val2 == target:
                    return [idx1, idx2]




Solution().twoSum(nums, target)

this is my full code

neat coral
#

and the value is compared

#

why do you want the output though

#

@hybrid umbra

hybrid umbra
#

Oh wait

neat coral
#

also

gilded folio
hybrid umbra
#

I just called the function

Solution() and it worked

neat coral
#

the num and range is inputed by leetcode not you

hybrid umbra
#

ohhhhh

#

i thought we had to do it ourself

#

sorry

neat coral
#

and you dont need to run soutions

#

or whatever function you are defining

real sandal
#

calling Solution at the end wouldn't even call the method

#

so it's really doing nothing

hybrid umbra
#

Leet code does stuff in the background?

neat coral
real sandal
#

well yeah, cuz it has to test stuff

neat coral
#

so basically this is what you neeeded to do

hybrid umbra
#

ohh i never knew that, i thought we had to get all the inputs and call the functions

neat coral
#

how you get it

hybrid umbra
#

ty for the help ๐Ÿ™

#

oh ty

neat coral
#

and hunt after you run it

hybrid umbra
#

That makes al ot of sense thank you

neat coral
#

and if you are satisfied with how the code works

#

then you click on submit

hybrid umbra
#

yep ty

neat coral
#

so it runs all the xyz test cases, which include all the test cases and everything

#

edge cases and all

#

welcome

#

you are smart btw

hybrid umbra
neat coral
#

coz when i started leetcode i didnt know what a class was and how to exexute you own class funcs and what self was

neat coral
hybrid umbra
neat coral
hybrid umbra
#

as well as json

neat coral
#

okay, just telling you thing coz i would have wished for the same advice

#

learn some basics of what is time complexity and space complexity

hybrid umbra
#

in terms of optimising the code?

neat coral
#

yea

hybrid umbra
neat coral
#

and use neetcode .io to look for different solutions, for python there are videos

hybrid umbra
#

ill look into it ty

elfin viper
#

there is also rosetta code

neat coral
neat coral
elfin viper
#

often rosstta code has the alternate versions of code that are more effiecent

neat coral
#

sometimes i see some crazy code on leetcode like these

#

this one is for reversing a linked list

elfin viper
#

funniest one ive seen is sleep sort

neat coral
#

this is the least memory taxing sol
but how does one even think of these
and is it even useful learning these

neat coral
#

you got pics?

hybrid umbra
#

im trying to find a different way now

neat coral
#

yea, there is a really nice way to solve this, one you hear it (i this case read it) you wont be able to forget it

#

you wanna know or you wanna try for yourself?

#

@hybrid umbra

hybrid umbra
#

im thinking of enumerate

neat coral
#

there is a waaaaaaaaaaaaaaaaay better sol

hybrid umbra
#

how else would you do it?

neat coral
#

okay

#

we got target

#

and

#

wait let me read it once

elfin viper
#
list2 = [3,2,1,4,5,6,7,8,9,10]

def sleep_sort(val):
    import time
    import threading
    ## create 1 thread for each value
    for i in val:
        threading.Thread(target=lambda i=i: (time.sleep(i), print(i))).start()
    ## get the thread to wait for all threads to finish
    ## get 
    time.sleep(max(val)+1)


sleep_sort(list2)```
#

baiscaly you create a thread for each element, then sleep basied on the value of the element, the return will be naturaly sorted ;

neat coral
#

yea so you run a for loop across the list's range
then which ever element you are in ->you search for the number that is (target-element) you are in

hybrid umbra
#

but wouldnt that cause

#

problems

#

with duplicate numbers

elfin viper
#

that and it takes time N to work

hybrid umbra
#

and yo uwould need to prevent it from checking the same numbre

neat coral
#

yea, equal number check thing is true

#

one sec ill write the code and test it

elfin viper
#

so yeah it natrualy gives you unqiue sorted list

#

but honestly this is such a dumb algorithm for many many reasons

#

not only the fact its On time but all On threads

neat coral
#

im new to this too

elfin viper
#

O notation is used to denote complexity in both time and calualtion

#

n being the number of elments in the list

#

Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by German mathematicians Paul Bachmann, Edmund Landau, and others, collectively called Bachmannโ€“Landau notation or asymptotic notation. T...

#

the advantage i guess is it runs in liner time vs o^n time like going though a for loop for the lenght of N would

#

The AI alignment problem.

The alignment problem in AI refers to the challenge of designing AI systems with objectives, values, and actions that closely align with human intentions and ethical considerations.

One of AIโ€™s main alignment challenges is its black box nature (inputs and outputs are identifiable but the transformation process in be...

โ–ถ Play video
#

"general soultion in polynomial time" big O notation is baiscaly what hes talking about

neat coral
#

finally @hybrid umbra
i couldnt remember the prev logic

neat coral
#
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        ind=-1
        numlist=nums
        while len(nums)>1:
            ind=ind+1
            first=nums[0]
            numlist.pop(0)
            if (target-first) in numlist:
                return [ind, numlist.index(target-first)+ind+1]
        return None
#

still this is slow for some reason...

#

i remember it now

#

this is it

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        his={} #his as history
        for i, num in enumerate(nums):
            x= target - num #x is the valuw we have to find
            if x in his:
                return [his[x],i]
            his[num]=i
                
elfin viper
#

try this

def two_sum2(nums, target):
    nums.sort()
    half_target = target / 2
    mid_index = len(nums) // 2
    mid_value = nums[mid_index]
    
    if mid_value > half_target:
        search_range = nums[:mid_index]
    else:
        search_range = nums[mid_index:]
    
    for i in range(len(search_range)):
        for j in range(i+1, len(search_range)):
            if search_range[i] + search_range[j] == target:
                return [nums.index(search_range[i]), nums.index(search_range[j])]
    return None

print(two_sum2([2, 7, 11, 15], 9))```
#

run that though the profiler ๐Ÿ˜‰

neat coral
#

ohkay one sec

elfin viper
#

yeah i just realised its kinda a mental test

neat coral
#

can you solve for the invalid cases please, i wanna see how fast it runs

elfin viper
#
def two_sum2(nums, target):
    indexed_nums = list(enumerate(nums))
    indexed_nums.sort(key=lambda x: x[1])
    
    left, right = 0, len(indexed_nums) - 1
    while left < right:
        current_sum = indexed_nums[left][1] + indexed_nums[right][1]
        if current_sum == target:
            return [indexed_nums[left][0], indexed_nums[right][0]]
        elif current_sum < target:
            left += 1
        else:
            right -= 1
    return None
neat coral
# neat coral

@hybrid umbra also when you click on the bar graph's bar you can see their sample code

elfin viper
#

[0, 1] [1, 2] [0, 1]

neat coral
#

all solved

#

wtf

#

3ms

elfin viper
#

i am good at math

neat coral
#

3ms!!!!!!!!!!!!!!!!!!!

neat coral
#

how old are you dude

elfin viper
#

i was around with python 1

#

you want an explation of what it does?

neat coral
#

i would love to if you can spare some of your time?
you aint busy right?

elfin viper
#

ok so what it does, is take the taget value

#

half it

#

then it sorts the list

#

then searches from the midpoint of the list in steps of log2 from the middle of the list

#

then it has to check the poistion of the new sorted list vs the orginal

#

and return the values of the orginal list

#

poitional values

#

pythons good at list sorting timewise

#

so its a time effient but not memory effieent method

neat coral
#

im just awestruck that you have such knowledge of which takes how much time

elfin viper
#

experaince

#

and yeah its dealing with shit like leetcode and testing time

#

also i deal with big data, sooo i have spent way too much time optimizing code

neat coral
#

you work in a job or solo dev?

elfin viper
#

looking for work, just finished my AI masters

neat coral
#

oooh

#

i hope you get the job of your dreams ๐Ÿ‘Œ

neat coral
elfin viper
#

quite alot, however my issue was mainly with stuff like apache spark

elfin viper
#

there is a python rprofiler tool built in

#

spend some time with it you will see whats fast whats not

neat coral
#

mind if i chat with you for a while in DMS @elfin viper

elfin viper
#

sometime tommrow good? rn i am dealing with making a AI model for alot of data

neat coral
#

oh sure sure

#

ill just ping you in chat and type my stuff, reply to it whenever you can in this week

astral hedgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.