fa
Feedback
Leetcode with dani

Leetcode with dani

رفتن به کانال در Telegram

Join us and let's tackle leet code questions together: improve your problem-solving skills Preparing for coding interviews learning new algorithms and data structures connect with other coding enthusiasts

نمایش بیشتر
1 257
مشترکین
-124 ساعت
-17 روز
-1130 روز
آرشیو پست ها
so let's tackle this question. so how can we know the element is maority element ,if it is the most frequent element in the array yea . so we can calculate each element freaquency by creating a dictionary like this my_dicti = {} for i in nums: if nums[i] in my_dict: my_dict[i] +=1 else: my_dict[i] = 1 and we can print the find the maximum frequent number in the dictionary and return it but there is also another efficient method to do it by declearing two variabes one to contain the number and the second to cotain it's occurence like this
nums = [1,2,3,4,5,4,4,3]
contain = nums[0]
occurence = 0
for i in nums:
    if i == contain:
        occurence += 1
    else:
        if occurence == 0:
            contain = i
            occurence =1
        else:
            occurence -= 1

so let's tackle this question. so how can we know the element is maority element ,if it is the most frequent element in the array yea . so we can calculate each element freaquency by creating a dictionary like this my_dicti = {} for i in nums: if nums[i] in my dict:

give me your ideas about this question. how can we solve it???

Repost from Leetcode with dani
it's good. we did better from 78 % of submitted code in python by running time
it's good. we did better from 78 % of submitted code in python by running time

Let's tackle the question of finding the number that appears only once in a list. if you want to figure it out your self dont see the answer? We could iterate through the list, comparing each element to all others to count occurrences. This approach would work, but it has a time complexity of n^2, exceeding the given constraint of linear time (O(n)). Since the question guarantees that all other elements appear twice, we can exploit this property. Here's how: Convert the list to a set. Sets eliminate duplicates, leaving only the unique element. Calculate the sum of the elements in the original list and the set. Unique number = Sum of list elements - (2 * Sum of set elements) This method efficiently finds the unique element in linear time. There's an even better approach called the XOR method. If you'd like an explanation of how it works, I can provide the code and break it down for you class Solution: def singleNumber(self, nums: List[int]) -> int: result = 0 for i in nums: result^=i return result

Let's tackle the question of finding the number that appears only once in a list. We could iterate through the list, comparing each element to all others to count occurrences. This approach would work, but it has a time complexity of n^2, exceeding the given constraint of linear time (O(n)). Since the question guarantees that all other elements appear twice, we can exploit this property. Here's how: Convert the list to a set. Sets eliminate duplicates, leaving only the unique element. Calculate the sum of the elements in the original list and the set. Unique number = Sum of list elements - (2 * Sum of set elements) This method efficiently finds the unique element in linear time. There's an even better approach called the XOR method. If you'd like an explanation of how it works, I can provide the code and break it down for you class Solution: def singleNumber(self, nums: List[int]) -> int: result = 0 for i in nums: result^=i return result

Let's tackle the question of finding the number that appears only once in a list. We could iterate through the list, comparing each element to all others to count occurrences. This approach would work, but it has a time complexity of n^2, exceeding the given constraint of linear time (O(n)). Since the question guarantees that all other elements appear twice, we can exploit this property. Here's how:
Convert the list to a set. Sets eliminate duplicates, leaving only the unique element. Calculate the sum of the elements in the original list and the set. Unique number = Sum of list elements - (2 * Sum of set elements) This method efficiently finds the unique element in linear time. There's an even better approach called the XOR method. If you'd like an explanation of how it works, I can provide the code and break it down for you
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        result = 0 
        for i in nums:
            result^=i
        return result

#leet_code_Q2 #Array Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example 1: Input: nums = [3,2,3] Output: 3 Example 2: Input: nums = [2,2,1,1,1,2,2] Output: 2

lets do another Question on Leet code

Level Up Problem-Solving: LeetCode trains your brain to break down problems, a skill used by 80% of software engineers according to a [Payscale survey]( Indeed salary survey for Software Engineer).

it's good. we did better from 78 % of submitted code in python by running time
it's good. we did better from 78 % of submitted code in python by running time

it good. we are better than 78% of submitted code in python by running time
it good. we are better than 78% of submitted code in python by running time

lets submit our code

Let's tackle the question of finding the number that appears only once in a list. We could iterate through the list, comparing each element to all others to count occurrences. This approach would work, but it has a time complexity of n^2, exceeding the given constraint of linear time (O(n)). Since the question guarantees that all other elements appear twice, we can exploit this property. Here's how: Convert the list to a set. Sets eliminate duplicates, leaving only the unique element. Calculate the sum of the elements in the original list and the set. Unique number = Sum of list elements - (2 * Sum of set elements) This method efficiently finds the unique element in linear time. There's an even better approach called the XOR method. If you'd like an explanation of how it works, I can provide the code and break it down for you
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        result = 0 
        for i in nums:
            result^=i
        return result

let's do the question the question above that asks two find the number which appears only once , so we can compare each element vs the list it self and try to count it and we can return the number that only appers once but the question restricts us to find in linear time but in the code is n^2 time complexity so we can use it ,so let's find another method so the question says the list contain one unique element and the others are exits twice so by this logic. we create empty list and copy the list to set then we will find the sum of the list and the set then the unique number = total element in the list - (2*total element in the set).so we can do the question by this method but there is also another method that is better than the prevouies it is XOR method i just write the code if u want explanation i will send it but first try to figure it out ur self how it works
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        result = 0 
        for i in nums:
            result^=i
        return result class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        result = 0 
        for i in nums:
            result^=i
        return result

Repost from Leetcode with dani
#leet_code_Q1 #Array Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Example 1: Input: nums = [2,2,1] Output: 1 Example 2: Input: nums = [4,1,2,1,2] Output: 4 Example 3: Input: nums = [1] Output: 1 Each element in the array appears twice except for one element which appears only once.

Hint2: try Xor operation

Hint: try to do it with one of the bitwise operationd

Try this question. i am not getting enough answers from you