Leetcode with dani
1.29K subscribers
202 photos
14 videos
56 files
243 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
do u like this type of questions πŸ˜‹
❀6
πŸ”₯ Premium Question: Meeting Rooms II πŸ”₯
Find the minimum number of days required to schedule all meetings without conflicts.
πŸ”— Problem Link

β–ŽProblem Statement:

Given an array of meeting time interval objects consisting of start and end times [[start_1, end_1], [start_2, end_2], ...] (where start_i < end_i), determine the minimum number of days required to schedule all meetings without any overlaps.

β–ŽExample 1:

πŸ”Ή Input: intervals = [(0, 40), (5, 10), (15, 20)]
πŸ”Ή Output: 2
πŸ”Ή Explanation:

β€’ Day 1: (0, 40)

β€’ Day 2: (5, 10), (15, 20)

β–ŽExample 2:

πŸ”Ή Input: intervals = [(4, 9)]
πŸ”Ή Output: 1

βœ… Note:

β€’ (0, 8), (8, 10) is not considered a conflict at 8.

β–ŽConstraints:

β€’ 0 <= intervals.length <= 500

β€’ 0 <= intervals[i].start < intervals[i].end <= 1,000,000

πŸ’¬ Drop your solutions in the comments! πŸš€
✍3πŸ‘1
πŸ”Ή 2139. Minimum Moves to Reach Target Score
🟠 Medium | πŸ“Œ Topics: Greedy, Math πŸ˜‹

Problem Statement:
You are playing a game with integers. You start with 1 and want to reach target.

You can perform two types of moves:
βœ… Increment: Add 1 β†’ x = x + 1 (Unlimited)
βœ… Double: Multiply by 2 β†’ x = 2 * x (At most maxDoubles times)

Return the minimum number of moves needed to reach target starting from 1.

πŸ”Ή Examples:
πŸ”Ή Example 1:
πŸ”Ή Input: target = 5, maxDoubles = 0
πŸ”Ή Output: 4
πŸ”Ή Explanation: Only incrementing: 1 β†’ 2 β†’ 3 β†’ 4 β†’ 5

πŸ”Ή Example 2:
πŸ”Ή Input: target = 19, maxDoubles = 2
πŸ”Ή Output: 7
πŸ”Ή Explanation:
1 β†’ 2 β†’ 3 β†’ 4 β†’ 8 β†’ 9 β†’ 18 β†’ 19

πŸ”Ή Example 3:
πŸ”Ή Input: target = 10, maxDoubles = 4
πŸ”Ή Output: 4
πŸ”Ή Explanation:
1 β†’ 2 β†’ 4 β†’ 5 β†’ 10

πŸ”Ή Constraints:
βœ”οΈ 1 ≀ target ≀ 10⁹
βœ”οΈ 0 ≀ maxDoubles ≀ 100
Link
solution
class Solution:
def minMoves(self, target: int, maxDoubles: int) -> int:
count = 0
while target>1 and maxDoubles>0:
if target%2:
count+=1
target = target//2
maxDoubles-=1
count += 1
count += target -1
return count
UNRATED
Forwarded from Codeforces Official
XIX Open Olympiad in Informatics - Final Stage, Day 1 (Unrated, Online Mirror, IOI rules) will take place on the 7th of March at 08:05 UTC.
Please, join by the link https://codeforces.com/contests/2079

β–Ž976. Largest Perimeter Triangle

Difficulty: Easy

Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.

β–ŽExample 1:

Input:

nums = [2, 1, 2]


Output: 

5


Explanation: You can form a triangle with three side lengths: 1, 2, and 2.

β–ŽExample 2:

Input:

nums = [1, 2, 1, 10]


Output: 

0


Explanation:

β€’ You cannot use the side lengths 1, 1, and 2 to form a triangle.

β€’ You cannot use the side lengths 1, 1, and 10 to form a triangle.

β€’ You cannot use the side lengths 1, 2, and 10 to form a triangle.

As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.

β–ŽApproach

To solve this problem, we can follow these steps:

1. Sort the Array: Start by sorting the array in non-decreasing order.

2. Check Triplets: Iterate through the sorted array from the end to the beginning and check for valid triplets that can form a triangle using the Triangle Inequality Theorem.

3. Return the Perimeter: If a valid triplet is found, calculate and return their perimeter. If no valid triplet is found, return 0.

β–ŽImplementation

Here’s a Python implementation of the approach:

def largestPerimeter(nums):
# Sort the array in non-decreasing order
nums.sort()

# Iterate from the end of the sorted list to find a valid triangle
for i in range(len(nums) - 1, 1, -1):
# Check if nums[i-2], nums[i-1], nums[i] can form a triangle
if nums[i - 2] + nums[i - 1] > nums[i]:
# If they can, return their perimeter
return nums[i - 2] + nums[i - 1] + nums[i]

# If no valid triangle is found, return 0
return 0

# Example usage:
print(largestPerimeter([2, 1, 2])) # Output: 5
print(largestPerimeter([1, 2, 1, 10])) # Output: 0



β–ŽConclusion

This solution efficiently finds the largest perimeter of a triangle that can be formed from three lengths in the given array. If no such triangle exists, it correctly returns 0.
β–Ž860. Lemonade Change

Difficulty: Easy

At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by the array bills). Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

Note that you do not have any change in hand at first.

β–ŽProblem Statement

Given an integer array bills where bills[i] is the bill the i-th customer pays, return true if you can provide every customer with the correct change, or false otherwise.

β–ŽExamples

Example 1:
Input: bills = [5, 5, 5, 10, 20]
Output: true
Explanation:
- From the first 3 customers, we collect three $5 bills.
- From the fourth customer, we collect a $10 bill and give back a $5.
- From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got correct change, we output true.



Example 2:

Input: bills = [5, 5, 10, 10, 20]
Output: false
Explanation:
- From the first two customers, we collect two $5 bills.
- For the next two customers, we collect a $10 bill and give back a $5 bill.
- For the last customer, we cannot give the change of $15 back because we only have two $10 bills.
Since not every customer received the correct change, the answer is false.



β–ŽConstraints

β€’ 1 ≀ bills.length ≀ 10⁡

β€’ bills[i] is either 5, 10, or 20.

β–ŽSolution Code

from typing import List

class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
c5 = 0 # Count of $5 bills
c10 = 0 # Count of $10 bills

for i in bills:
if i == 5:
c5 += 1
elif i == 10:
c10 += 1
c5 -= 1
else: # i == 20
if c10 > 0:
c10 -= 1
c5 -= 1
else:
c5 -= 3

# Check if we have enough $5 bills to give change
if c5 < 0:
return False

return True
β–Ž2366. Minimum Replacements to Sort the Array

Difficulty: Hard

You are given a 0-indexed integer array nums. In one operation, you can replace any element of the array with any two elements that sum to it. For example, if nums = [5, 6, 7], you can replace nums[1] (which is 6) with two numbers, say 2 and 4, since 2 + 4 = 6. This transforms the array into [5, 2, 4, 7].

Return the minimum number of operations required to make the array sorted in non-decreasing order.

β–ŽExamples

Example 1:
Input: nums = [3, 9, 3]
Output: 2
Explanation:
- Operation 1: Replace 9 with [3, 6] β†’ Array becomes [3, 3, 6, 3]
- Operation 2: Replace 6 with [3, 3] β†’ Array becomes [3, 3, 3, 3, 3]
Thus, a total of 2 operations are required.



Example 2:

Input: nums = [1, 2, 3, 4, 5]
Output: 0
Explanation:
The array is already in non-decreasing order. Therefore, no operations are needed.



β–ŽConstraints

β€’ 1 ≀ nums.length ≀ 10⁡

β€’ 1 ≀ nums[i] ≀ 10⁹

β–ŽSolution Code

class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
operations = 0
n = len(nums)

# Start from the second last element and move backward
for i in range(n - 2, -1, -1):
if nums[i] > nums[i + 1]:
# Calculate how many parts we need to break nums[i] into
parts = (nums[i] + nums[i + 1] - 1) // nums[i + 1]
operations += parts - 1

# Set nums[i] to the maximum value allowed
nums[i] = nums[i] // parts

return operations
πŸ‘3πŸ‘1
Just a motivation πŸ˜„
Forwarded from Codeforces Official
Codeforces Round 1009 (Div. 3) will take place on the 11th of March at 14:35 UTC. 
Please, join by the link https://codeforces.com/contests/2074?locale=en
β–Ž678. Valid Parenthesis String

πŸ’» Difficulty: Medium

Given a string s containing only three types of characters: '(', ')', and '*', return true if s is valid.

β–ŽRules for a valid string:

βœ… Any left parenthesis '(' must have a corresponding right parenthesis ')'.
βœ… Any right parenthesis ')' must have a corresponding left parenthesis '('.
βœ… Left parenthesis '(' must go before the corresponding right parenthesis ')'.
βœ… '*' can be treated as '(', ')', or an empty string "".

β–ŽExamples

Example 1:
Input: s = "()"
Output: true

Example 2:
Input: s = "(*)"
Output: true

Example 3:
Input: s = "(*))"
Output: true


β–ŽConstraints:

β€’ 1 <= s.length <= 100

β€’ s[i] is '(', ')', or '*'.

---

β–ŽSolution (Greedy Approach)

def checkValidString(s: str) -> bool:
low = high = 0 # low: min open count, high: max open count

for char in s:
if char == '(':
low += 1
high += 1
elif char == ')':
low = max(0, low - 1) # Decrease open count but not below zero
high -= 1
else: # '*'
low = max(0, low - 1) # Treat '*' as ')'
high += 1 # Treat '*' as '('

if high < 0: # Too many ')', invalid
return False

return low == 0 # If low is zero, valid parentheses

# Test cases
print(checkValidString("()")) # True
print(checkValidString("(*)")) # True
print(checkValidString("(*))")) # True



β–ŽExplanation

We keep track of the possible range of open parentheses using low and high:

β€’ low (minimum open count) decreases when ')' appears but never goes below 0.

β€’ high (maximum open count) increases when '(' or '*' (treated as '(') appears.

β€’ If high becomes negative at any point, the string is invalid.

β€’ Finally, if low == 0, the string is valid.

πŸš€ Time Complexity: O(n)
πŸ”Ή Space Complexity: O(1)
πŸ‘3πŸ‘2
let me share one conversation with GPT
if i want to apply to FAANG what should i expect to know
ChatGPT said:
If you want to apply to FAANG (Facebook/Meta, Amazon, Apple, Netflix, Google), you need to be well-prepared in multiple technical and non-technical areas. Here’s a breakdown of what you should know:

1. Data Structures & Algorithms (DSA) – High Priority
You should be able to solve LeetCode Hard problems efficiently. Focus on:
βœ… Arrays, Strings, Linked Lists
βœ… Trees (Binary, BST, Trie)
βœ… Graphs (BFS, DFS, Dijkstra’s)
βœ… Dynamic Programming
βœ… Backtracking
βœ… Bit Manipulation
βœ… Heaps & Priority Queues
βœ… Two Pointers, Sliding Window, Prefix Sum

πŸ”₯ Recommended Resources:

LeetCode (Top 150 FAANG problems)
Codeforces (Div2 & Div1 contests)
NeetCode, Striver’s SDE Sheet
Cracking the Coding Interview by Gayle Laakmann McDowell
2. System Design (For Senior Roles)
FAANG interviews often test system design for mid/senior positions. You should know:
βœ… Scalability (Load Balancing, Sharding, Caching)
βœ… Microservices & Distributed Systems
βœ… Databases (SQL & NoSQL)
βœ… API Design, Rate Limiting, Consistency Models
βœ… Message Queues (Kafka, RabbitMQ)
βœ… CAP Theorem

πŸ”₯ Resources:

"Designing Data-Intensive Applications" by Martin Kleppmann
System Design Primer (GitHub)
Grokking the System Design Interview
3. Object-Oriented Design (OOD) & Design Patterns
βœ… SOLID Principles
βœ… Factory, Singleton, Observer, Decorator, Strategy Patterns

πŸ”₯ Resources:

Head First Design Patterns
Grokking the Object-Oriented Design Interview
4. Low-Level Programming & Optimization (For Performance-Critical Roles)
βœ… Memory Management (Heap vs Stack)
βœ… Threading & Concurrency
βœ… Cache Optimization
βœ… Assembly & Compiler Optimizations

5. Core CS Concepts
βœ… Operating Systems (Processes, Threads, Deadlocks, Virtual Memory)
βœ… Networking (TCP/IP, HTTP, Load Balancing, CDN)
βœ… Databases (Indexes, ACID, Transactions, Query Optimization)

πŸ”₯ Resources:

Computer Networking: A Top-Down Approach
Database System Concepts
6. Behavioral Interviews – Don't Ignore This!
FAANG uses STAR (Situation, Task, Action, Result) format for behavioral questions.
βœ… Leadership & Ownership
βœ… Conflict Resolution
βœ… Past Projects & Impact

πŸ”₯ Resources:

"The Amazon Leadership Principles"
"Decode & Conquer" by Lewis Lin
7. Programming Languages & Tech Stack
You should be fluent in at least one high-level language, such as:

Java, Python, C++, JavaScript, Go (most used in FAANG)
Knowledge of React, Node.js, Spring Boot, Django is a plus
8. Mock Interviews & Time Management
Start practicing mock interviews with peers or platforms like:
βœ… Pramp
βœ… Interviewing.io
βœ… Leetcode Contest + Codeforces
πŸ‘4❀1
Forwarded from Codeforces Official
Codeforces Round #1010 (Div. 1, Div. 2) will take place on the 15th of March at 05:35 UTC.
Please, join by the link https://codeforces.com/contests/2081,2082?locale=en
330. Patching Array
Difficulty: Hard

Problem Statement:

Given a sorted integer array nums and an integer n, add/patch elements to the array such that any number in the range [1, n] (inclusive) can be formed by the sum of some elements in the array.

Return the minimum number of patches required.

Example 1:

Input: nums = [1, 3], n = 6
Output: 1
Explanation:
The combinations of nums are [1], [3], [1, 3], which form possible sums of 1, 3, 4.
Now if we add/patch 2 to nums, the combinations become [1], [2], [3], [1, 3], [2, 3], [1, 2, 3], covering all sums 1, 2, 3, 4, 5, 6.
Thus, only 1 patch is needed.
Example 2:

Input: nums = [1, 5, 10], n = 20
Output: 2
Explanation:
The two patches can be [2, 4].
Example 3:

Input: nums = [1, 2, 2], n = 5
Output: 0
Constraints:

1 <= nums.length <= 1000
1 <= nums[i] <= 10^4
nums is sorted in ascending order.
1 <= n <= 2^31 - 1
Solution :

class Solution:
def minPatches(self, nums: List[int], n: int) -> int:
patches = 0
miss = 1
i = 0
while miss <= n:
if i < len(nums) and nums[i] <= miss:
miss += nums[i]
i += 1
else:
miss += miss
patches += 1
return patches
πŸ‘2