uk
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 258
Підписники
Немає даних24 години
+17 днів
-730 день
Архів дописів
Understanding Time Complexity with Simple Examples Hey everyone! 👋 Today, we’re going to break down Time Complexity in the simplest way possible. Whether you're a beginner or just need a refresher, this guide will help you understand how algorithms perform as input sizes grow. Let’s dive in! 🚀 What is Time Complexity? Time Complexity is a way to describe how the runtime of an algorithm grows as the input size increases. It helps us compare algorithms and predict their performance without running them on actual machines. We use Big-O Notation (like O(n), O(log n), etc.) to express time complexity. It tells us the upper bound of an algorithm's runtime in the worst-case scenario. Real-Life Example: Finding a Pen in a Classroom Imagine you’re in a classroom with 100 students, and you’ve given your pen to one of them. You need to find it! Here’s how different approaches work: O(n²) Approach: Ask the first student if they have the pen. Also, ask them about the other 99 students. Repeat this for every student. This is inefficient and takes a lot of time. O(n) Approach: Go to each student one by one and ask if they have the pen. This is linear and much better than O(n²). O(log n) Approach: Divide the class into two groups. Ask: “Is the pen on the left or right side?” Repeat this process until you find the student with the pen. This is super efficient! Key Points About Time Complexity It’s Not Actual Runtime: Time Complexity doesn’t measure the exact time an algorithm takes to run. Instead, it measures how the runtime grows as the input size increases. Why Use Big-O? It helps us compare algorithms independent of hardware or programming language. For example, O(n) will always be faster than O(n²) for large inputs.
Examples to Understand Time Complexity

Example 1: Constant Time - O(1)
print("Hello World")
Explanation: The code runs in constant time because it prints "Hello World" only once, no matter the input size.
Time Complexity: O(1)

Example 2: Linear Time - O(n)
n = 8
for i in range(1, n + 1):
    print("Hello World !!!")
Explanation: The code prints "Hello World !!!" n times. As n grows, the runtime grows linearly.
Time Complexity: O(n)

Example 3: Logarithmic Time - O(log n)

n = 8
for i in range(1, n + 1, 2):
    print("Hello World !!!")
Explanation: The loop runs logarithmically because it skips half the steps each time.
Time Complexity: O(log n)

Example 4: Polynomial Time - O(n²)
n = 3
m = 3
arr = [[3, 2, 7], [2, 6, 8], [5, 1, 9]]
sum = 0

for i in range(n):
    for j in range(m):
        sum += arr[i][j]
print(sum)
Explanation: The code uses nested loops to sum all elements in a 2D array.
Time Complexity: O(n * m)
How to Compare Algorithms? To compare algorithms, we focus on: Growth Rate: How does the runtime grow as input size increases? Big-O Notation: Use it to express the upper bound of runtime. For example: O(1) < O(log n) < O(n) < O(n²) < O(2ⁿ) Why Does Time Complexity Matter? Efficiency: Helps us choose the best algorithm for large inputs. Scalability: Ensures our code performs well as data grows. Optimization: Guides us in writing faster and more efficient programs. Final Thoughts Understanding Time Complexity is crucial for writing efficient algorithms. By using Big-O Notation, we can predict how our code will perform and make better decisions when solving problems. Keep practicing, and soon you’ll be a pro at analyzing algorithms! 💪 Got questions? Drop them in the comments below! Let’s learn together. 🚀 Source: GeeksforGeeks and DeepSeek. Share to ur Friends

Understanding Time Complexity with Simple Examples Hey everyone! 👋 Today, we’re going to break down Time Complexity in the simplest way possible. Whether you're a beginner or just need a refresher, this guide will help you understand how algorithms perform as input sizes grow. Let’s dive in! 🚀 What is Time Complexity? Time Complexity is a way to describe how the runtime of an algorithm grows as the input size increases. It helps us compare algorithms and predict their performance without running them on actual machines. We use Big-O Notation (like O(n), O(log n), etc.) to express time complexity. It tells us the upper bound of an algorithm's runtime in the worst-case scenario. Real-Life Example: Finding a Pen in a Classroom Imagine you’re in a classroom with 100 students, and you’ve given your pen to one of them. You need to find it! Here’s how different approaches work: O(n²) Approach: Ask the first student if they have the pen. Also, ask them about the other 99 students. Repeat this for every student. This is inefficient and takes a lot of time. O(n) Approach: Go to each student one by one and ask if they have the pen. This is linear and much better than O(n²). O(log n) Approach: Divide the class into two groups. Ask: “Is the pen on the left or right side?” Repeat this process until you find the student with the pen. This is super efficient! Key Points About Time Complexity It’s Not Actual Runtime: Time Complexity doesn’t measure the exact time an algorithm takes to run. Instead, it measures how the runtime grows as the input size increases. Why Use Big-O? It helps us compare algorithms independent of hardware or programming language. For example, O(n) will always be faster than O(n²) for large inputs. Examples to Understand Time Complexity Example 1: Constant Time - O(1) bash Copy Edit print("Hello World") Explanation: The code runs in constant time because it prints "Hello World" only once, no matter the input size. Time Complexity: O(1) Example 2: Linear Time - O(n) go Copy Edit n = 8 for i in range(1, n + 1): print("Hello World !!!") Explanation: The code prints "Hello World !!!" n times. As n grows, the runtime grows linearly. Time Complexity: O(n) Example 3: Logarithmic Time - O(log n) go Copy Edit n = 8 for i in range(1, n + 1, 2): print("Hello World !!!") Explanation: The loop runs logarithmically because it skips half the steps each time. Time Complexity: O(log n) Example 4: Polynomial Time - O(n²) lua Copy Edit n = 3 m = 3 arr = [[3, 2, 7], [2, 6, 8], [5, 1, 9]] sum = 0 for i in range(n): for j in range(m): sum += arr[i][j] print(sum) Explanation: The code uses nested loops to sum all elements in a 2D array. Time Complexity: O(n * m) How to Compare Algorithms? To compare algorithms, we focus on: Growth Rate: How does the runtime grow as input size increases? Big-O Notation: Use it to express the upper bound of runtime. For example: O(1) < O(log n) < O(n) < O(n²) < O(2ⁿ) Why Does Time Complexity Matter? Efficiency: Helps us choose the best algorithm for large inputs. Scalability: Ensures our code performs well as data grows. Optimization: Guides us in writing faster and more efficient programs. Final Thoughts Understanding Time Complexity is crucial for writing efficient algorithms. By using Big-O Notation, we can predict how our code will perform and make better decisions when solving problems. Keep practicing, and soon you’ll be a pro at analyzing algorithms! 💪 Got questions? Drop them in the comments below! Let’s learn together. 🚀 Source: GeeksforGeeks and DeepSeek. Share to ur Friends

🌟 Understanding Time Complexity with Simple Examples 🌟 Hey everyone! 👋 Today, we’re breaking down Time Complexity simply. Perfect for beginners or refreshers! Let’s dive in! 🚀 🔍 What is Time Complexity? It describes how an algorithm's runtime grows as input size increases. Measured using Big-O Notation (O(n), O(log n), etc.) for worst-case scenarios. 🏫 Real-Life Example: Finding a Pen in a Classroom 100 students, 1 pen - how to find it? 1. O(n²) Approach: - Ask first student + all 99 others - Repeat for every student → Very inefficient 2. O(n) Approach: - Check each student one by one → Linear efficiency 3. O(log n) Approach: - Split class in half repeatedly → Super efficient! 💡 Key Points: 1. Measures growth rate, not actual runtime 2. Big-O allows hardware/language-agnostic comparisons 📊 Examples: 1. O(1) - Constant Time: print("Hello World") → Runs same time regardless of input 2. O(n) - Linear Time: for i in range(n): print("Hello") → Runtime grows linearly with input 3. O(log n) - Logarithmic: while n > 1: n = n/2 → Halves problem size each step 4. O(n²) - Polynomial: for i in range(n): for j in range(n): sum += i+j → Nested loops = quadratic growth ⚖️ Comparing Algorithms: O(1) < O(log n) < O(n) < O(n²) < O(2ⁿ) 🔑 Why It Matters: - Efficiency for large inputs - Scalability - Performance optimization 💬 Final Thoughts: Mastering time complexity helps write better algorithms. Keep practicing! 💪 Questions? Let’s discuss below! 👇 #Coding #Algorithms #ProgrammingTips Source: GeeksforGeeks & DeepSeek

class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -&gt; bool: num_indices = {} for i, num in enumera
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
       
        num_indices = {}
        for i, num in enumerate(nums):
            if num in num_indices and i - num_indices[num] <= k:
                return True
            num_indices[num] = i

        return False

class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: myset = set() l = 0 for i in range(len(nu
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        myset = set()
        l = 0
        for i in range(len(nums)):
            while(i-l>k):
                myset.remove(nums[l])
                l+=1
            if nums[i] in myset:
                return True
            myset.add(nums[i])
        return False

Let’s tackle "Contains Duplicate II"! Day 1 Q 2 Problem Statement: Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that: nums[i] == nums[j] abs(i - j) <= k Otherwise, return false. Examples to Get You Started: 🔹 Example 1: Input: nums = [1,2,3,1], k = 3 Output: true Explanation: The element 1 occurs at indices 0 and 3, and abs(0 - 3) <= 3. 🔹 Example 2: Input: nums = [1,0,1,1], k = 1 Output: true Explanation: The element 1 occurs at indices 2 and 3, and abs(2 - 3) <= 1. 🔹 Example 3: Input: nums = [1,2,3,1,2,3], k = 2 Output: false Explanation: No duplicates satisfy abs(i - j) <= 2. Your Task: Can you solve this efficiently? Think about using hash maps or sliding window techniques! 💡 Pro Tip: This problem is a great way to practice working with dictionaries or hash sets for tracking indices. Drop your solutions in the comments below! Let’s see who can come up with the most optimized approach. 🏆 Submit

Ans: Fist do by your self
class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        myset = set()
        for i in nums:
            if i in myset:
                return True
            myset.add(i)
        return False

Day 1 Q 1 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. Examples to Get You Started: 🔹 Example 1: Input: nums = [1,2,3,1] Output: true Explanation: The element 1 occurs at indices 0 and 3. 🔹 Example 2: Input: nums = [1,2,3,4] Output: false Explanation: All elements are distinct. 🔹 Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true Submit

Hey everyone! 👋 I have a problem: I keep skipping LeetCode practice. 😓 Maybe you do too? Let’s fix this together! Join me: Starting Monday, promise yourself to solve at least 1 question daily from the 3 I share. Small steps, big progress! How it works: 1️⃣ Daily Questions: I’ll post 3 questions every day. Promise yourself to solve at least 1. 2️⃣ Try First: Solve it alone. If stuck, I’ll share answers so we can learn. 3️⃣ Repeat Hard Ones: Save tricky questions. Try them again after 3-4 days. 4️⃣ Help Each Other: Ask for hints or share tips—no shame! what do u think guys?

🚀 Great Resource Alert! 👉 Mosh for Learning Check out this awesome channel for coding tutorials, tips, and more! It’s been super helpful for me, and I highly recommend it. Happy learning! 💻✨ NOT AN AD!

code with mosh join this channel if u want to get free mosh courses

u can see different road maps here https://roadmap.sh/
u can see different road maps here https://roadmap.sh/

full stack road map

"Can You Solve the 100 Locker Riddle?"

Your rich, eccentric uncle has left behind a mysterious will. You and 99 relatives are called to his mansion for the reading. Here’s the twist:

There are 100 lockers, each hiding a secret word. Each of you is assigned a number from 1 to 100. The rules are:

Heir 1 opens every locker.

Heir 2 closes every 2nd locker.

Heir 3 changes the state of every 3rd locker (opens if closed, closes if open).

This continues until Heir 100 changes the state of the 100th locker.

At the end, only the lockers that remain open will reveal the code to the safe.

Here’s the challenge:
Without going through all 100 steps, can you figure out which lockers will stay open?

If you solved it differently or have any questions, drop your approach or thoughts in the comments! Let’s learn and grow together. 💡

def maxSubArray(nums):
    max_current = max_global = nums[0]
    for i in range(1, len(nums)):
        max_current = max(nums[i], max_current + nums[i])
        max_global = max(max_global, max_current)
    return max_global

Solution: Kadane’s Algorithm The most efficient way to solve this problem is using Kadane’s Algorithm, which runs in O(n) time and uses O(1) space. Here’s how it works: Initialize two variables: max_current: Tracks the maximum sum of the subarray ending at the current position. max_global: Tracks the overall maximum sum found so far. Iterate through the array: For each element, update max_current to be the maximum of the current element itself or the sum of max_current and the current element. Update max_global to be the maximum of max_global and max_current. Return max_global as the result.

can u Write a Python function to check if a word is a palindrome?… but you can’t use loops or reversed().

🔥 Problem of the Day: "Maximum Subarray" (Medium) LeetCode #53 | Topic: Dynamic Programming / Greedy 📝 Problem Statement Given an integer array nums, find the contiguous subarray with the largest sum. Return the sum. Example: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 (Because [4,-1,2,1] sums to 6) 💡 Hints to Get Started Should you track the current subarray or just its sum? Pro Tip: Kadane’s Algorithm can solve this in O(n) time! ⏳ Time to Solve: 40 minutes! Drop your solution in the comments 💬. I’ll post the optimized answer tomorrow