Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
196 photos
14 videos
56 files
240 links
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
Download Telegram
bubble sort
def bubbleSort(arr):
n = len(arr)

# Traverse through all array elements
for i in range(n):
swapped = False

# Last i elements are already in place
for j in range(0, n-i-1):

# Traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
if (swapped == False):
break
insertion sort
def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1

# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
Selection sort
def selection_sort(arr):
n = len(arr)
for i in range(n - 1):

# Assume the current position holds
# the minimum element
min_idx = i

# Iterate through the unsorted portion
# to find the actual minimum
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:

# Update min_idx if a smaller element is found
min_idx = j

# Move minimum element to its
# correct position
arr[i], arr[min_idx] = arr[min_idx], arr[i]
👍3
#Q21 #leet_codeQ1249 Medium noscript. Minimum Remove to Make Valid Parentheses
Given a string s of '(' , ')' and lowercase English characters.

Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.

Formally, a parentheses string is valid if and only if:

It is the empty string, contains only lowercase characters, or
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.


Example 1:

Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:

Input: s = "a)b(c)d"
Output: "ab(c)d"
Example 3:

Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.


Constraints:

1 <= s.length <= 105
s[i] is either '(' , ')', or lowercase English letter.
👍82
Forwarded from A2SV - Community
Hello fellow coders,

After a brief break, we've returned! Get ready for our fun weekly community coding contest happening every Saturday 🗓. It's your chance to compete with other awesome programmers, learn more about coding, and, most importantly, have a great time! 🥳

🏆 Contest Details:
📅 Date: November 2, 2024
Time: 6:00 PM
🔗 Link for Contest: Community Weekly Contest Return - Contest No 1

P.S. Remember to fill out this form to secure your spot for our upcoming community classes and lectures. It’s your golden ticket to enriching experiences you won’t want to miss. 📚
👍2🍌1
Forwarded from LeetCode VIP
🏆 LeetCode 490. The Maze (Premium) 🏆

There's a ball in a maze with:
- Empty spaces (0) and walls (1).

The ball can roll in any direction:
- Up, down, left, or right 🌐

But here's the catch:
- It won't stop until it hits a wall! 🚧
- Once it stops, it can choose another direction to roll.

### Task 🎯
Given:
- An m x n maze
- The ball's starting position and a destination

Determine:
- Can the ball stop at the destination?
- If yes, return true. Otherwise, return false.

📝 Assumption: All borders of the maze are walls.

---

### Examples 🔍

Example 1:

Input: 
maze = [
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,1,0],
[1,1,0,1,1],
[0,0,0,0,0]
],
start = [0,4],
destination = [4,4]

Output: true

Explanation: One possible path: left ➡️ down ➡️ left ➡️ down ➡️ right ➡️ down ➡️ right 🎯

---

Example 2:

Input:
maze = [
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,1,0],
[1,1,0,1,1],
[0,0,0,0,0]
],
start = [0,4],
destination = [3,2]

Output: false

Explanation: The ball can pass through the destination, but it cannot stop there.

---

Example 3:

Input:
maze = [
[0,0,0,0,0],
[1,1,0,0,1],
[0,0,0,0,0],
[0,1,0,0,1],
[0,1,0,0,0]
],
start = [4,3],
destination = [0,1]

Output: false


---

### Constraints 📏
- Maze Dimensions: 1 ≤ m, n ≤ 100
- Cells contain only 0 (empty) or 1 (wall).
- Start and destination are in empty spaces and won’t initially overlap.

Can you solve it? 🤔
👍321🤯1
Forwarded from LeetCode VIP
Forwarded from LeetCode VIP
🔥4👍1
Leetcode with dani
Photo
share ur answer in the comment section , i will check it
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.

Example:

Input : n =10
Output : 2 3 5 7


Input : n = 20
Output: 2 3 5 7 11 13 17 19
👍3
Sieve of Eratosthenes

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.

Example:

Input : n =10
Output : 2 3 5 7


Input : n = 20
Output: 2 3 5 7 11 13 17 19

The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so.

Following is the algorithm to find all the prime numbers less than or equal to a given integer n by the Eratosthene’s method:
When the algorithm terminates, all the numbers in the list that are not marked are prime.

Explanation with Example:
Let us take an example when n = 100. So, we need to print all prime numbers smaller than or equal to 100.


We create a list of all numbers from 2 to 100.
According to the algorithm we will mark all the numbers which are divisible by 2 and are greater than or equal to the square of it.
👍1
Now we move to our next unmarked number 3 and mark all the numbers which are multiples of 3 and are greater than or equal to the square of it.
We move to our next unmarked number 5 and mark all multiples of 5 and are greater than or equal to the square of it.
We continue this process, and our final table will look like below:
So, the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97.
👍1🆒1
🎯 Problem of the Day: Sort Colors

Problem Denoscription
You are given an array nums with \( n \) objects colored red, white, or blue. Sort the array in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
- Represent the colors using integers:
- 0 → Red
- 1 → White
- 2 → Blue

⚠️ Note: You must not use the library's sort function.

---

Example 1
Input:
nums = [2,0,2,1,1,0]

Output:
[0,0,1,1,2,2]


Example 2
Input:
nums = [2,0,1]

Output:
[0,1,2]


---

📝 Practice Questions for A2SV Preparation
1. Core Problem:
- Solve LeetCode 75: Sort Colors () using Dutch National Flag Algorithm for \( O(n) \) time complexity.

2. Related Problems:
- LeetCode 215 : Kth Largest Element in an Array
- LeetCode 347 : Top K Frequent Elements
- LeetCode 56 : Merge Intervals

3. Challenge Problem:
- LeetCode 88: Merge Sorted Array

---

💡 Tip: Use a two-pointer or three-pointer approach to keep track of boundaries for the different colors! Try to optimize your solution to \( O(n) \) in both time and space.

🧠 Share your solution and discuss your approach with the community! 🌟
👍4