< Ace Coding /> π
Basic_data_structures_and_time_and_space_complexity.pdf
Quote of the day
βKarsn Lamb
βA year from now you may wish you had
started todayβ
βKarsn Lamb
π3
time_complexity_Analysis_worked_examples.pdf
357.8 KB
ππ Understanding Time Complexity in Depth - Princeton University
#TimeComplexity #DSA #PrincetonUniversity #AceCoding
ππ @AceCoding Presents! ππ
#TimeComplexity #DSA #PrincetonUniversity #AceCoding
ππ @AceCoding Presents! ππ
π₯ Are U up for the Challenge? π₯
Here are some common binary search problems on LeetCode that will test your skills! ππ»
#LeetCode #BinarySearch #CodingChallenge #DSA
ππ @AceCoding Presents! ππ
Here are some common binary search problems on LeetCode that will test your skills! ππ»
#LeetCode #BinarySearch #CodingChallenge #DSA
ππ @AceCoding Presents! ππ
π₯4
β
34. Find First and Last Position of Element in Sorted Array
π’Using regular binary search
π‘Using the concept of lower bound and upper bound
π΄Using the bisect method (import bisect)
πΎC++ implementation using STL (lower_bound and upper_bound)
ππ @AceCoding Presents! ππ
π’Using regular binary search
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def first():
l, r = 0, len(nums)-1
first = -1
while l <= r:
mid = (l+r)//2
if nums[mid] == target:
first = mid
r = mid - 1
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return first
def last():
l, r = 0, len(nums)-1
last = -1
while l <= r:
mid = (l+r)//2
if nums[mid] == target:
last = mid
l = mid + 1
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return last
f = first()
if f == - 1: return [-1, -1]
la = last()
return [f, la]
π‘Using the concept of lower bound and upper bound
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
def lower_bound():
l, r = 0, len(nums)-1
ans = len(nums)
while l <= r:
mid = (l+r)//2
if nums[mid] >= target:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
def upper_bound():
l, r = 0, len(nums)-1
ans = len(nums)
while l <= r:
mid = (l+r)//2
if nums[mid] > target:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
lb = lower_bound()
up = upper_bound() - 1
if lb <= up:
return [lb, up]
return [-1, -1]
π΄Using the bisect method (import bisect)
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
lb = bisect.bisect_left(nums, target)
if lb >= len(nums) or nums[lb] != target:
return [-1, -1]
ub = bisect.bisect_right(nums, target)
return [lb, ub-1]
πΎC++ implementation using STL (lower_bound and upper_bound)
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
auto lb = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
if (lb >= nums.size() || nums[lb] != target)
return {-1, -1};
auto ub = upper_bound(nums.begin(), nums.end(), target) - nums.begin();
return {(int)lb, (int)ub-1};
}
};ππ @AceCoding Presents! ππ
β
74. Search a 2D Matrix
ππ @AceCoding Presents! ππ
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
row = len(matrix)
col = len(matrix[0])
l, r = 0, (row * col) - 1
while l <= r:
mid = (l+r)//2
# i = mid // col
# j = mid % col
i, j = divmod(mid, col) # one liner for the above
if matrix[i][j] == target:
return True
elif target >= matrix[i][j]:
l = mid + 1
else:
r = mid - 1
return False
ππ @AceCoding Presents! ππ
π₯ Hard Medium (1945) is nearly a hard question; binary search
π§ It took me around 35 min but I think I was lucky this time
@AceCoding
https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
π§ It took me around 35 min but I think I was lucky this time
Time complexity = O(nΓlog(maxDays)) ; Space complexity = O(1)
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
max_days = max(bloomDay)
min_days = -1
L, R = 0, max_days
while L <= R:
mid = (L+R)//2
count = 0
m_count = 0
for bloom in bloomDay:
if mid >= bloom:
count += 1
if count >= k:
m_count += 1
count = 0
else:
count = 0
if m_count >= m:
min_days = mid
R = mid - 1
else:
L = mid + 1
return min_days
@AceCoding
https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
LeetCode
Minimum Number of Days to Make m Bouquets - LeetCode
Can you solve this real interview question? Minimum Number of Days to Make m Bouquets - You are given an integer array bloomDay, an integer m and an integer k.
You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.β¦
You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.β¦
π‘ Easy Medium (1541) leetCode Question: 1283. Find the Smallest Divisor Given a Threshold
Solution: Straight-forward easy to understand
ππ @AceCoding Presents! ππ
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/
Solution: Straight-forward easy to understand
class Solution:
def smallestDivisor(self, nums: List[int], threshold: int) -> int:
max_num = max(nums)
divisor = max_num
L, R = 1, max_num # make sure to start from 1, if L = 0 we might encounter a runtime error, division by zero
while L <= R:
mid = (L+R)//2
total_sum = sum((num + mid - 1)//mid for num in nums)
# math.ceil(num / mid) is the same as (num+mid-1)//mid, the second on is more facter on CPU.
if total_sum <= threshold:
divisor = mid
R = mid - 1
else:
L = mid + 1
return divisor
ππ @AceCoding Presents! ππ
https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/
LeetCode
Find the Smallest Divisor Given a Threshold - LeetCode
Can you solve this real interview question? Find the Smallest Divisor Given a Threshold - Given an array of integers nums and an integer threshold, we will choose a positive integer divisor, divide all the array by it, and sum the division's result. Findβ¦
Forwarded from A2SV | Africa to Silicon Valley (A2SV)
Applications are Open for A2SV G6 Education!
The time has come for A2SV to welcome new members! Weβre looking for team-oriented individuals with a never-give-up mentality, ready to drive tech excellence and solve impactful challenges.
π Application opens: November 14, 2024
π Deadline: November 20, 2024, at 11:59 PM EAT
π Eligibility
Open to current students from Addis Ababa University (AAU), Addis Ababa Science and Technology University (AASTU), and Adama Science and Technology University (ASTU). If you're not from these schools or have already graduated, stay tuned for future remote applications!
π Requirements
- Familiarity with at least one programming language
- Experience with platforms like LeetCode or Codeforces
- Completed at least 40 problems on LeetCode or Codeforces
π€ Selection Process
- First Round Filtering: Initial application review
- Technical & Behavioral Interviews: For selected candidates, to assess skills and fit for the program
βοΈ Donβt wait! Start your application early to ensure a standout submission. π―
π Apply now: link
#A2SV #TechEducation #EmpoweringAfrica #ApplyNow
The time has come for A2SV to welcome new members! Weβre looking for team-oriented individuals with a never-give-up mentality, ready to drive tech excellence and solve impactful challenges.
π Application opens: November 14, 2024
π Deadline: November 20, 2024, at 11:59 PM EAT
π Eligibility
Open to current students from Addis Ababa University (AAU), Addis Ababa Science and Technology University (AASTU), and Adama Science and Technology University (ASTU). If you're not from these schools or have already graduated, stay tuned for future remote applications!
π Requirements
- Familiarity with at least one programming language
- Experience with platforms like LeetCode or Codeforces
- Completed at least 40 problems on LeetCode or Codeforces
π€ Selection Process
- First Round Filtering: Initial application review
- Technical & Behavioral Interviews: For selected candidates, to assess skills and fit for the program
βοΈ Donβt wait! Start your application early to ensure a standout submission. π―
π Apply now: link
#A2SV #TechEducation #EmpoweringAfrica #ApplyNow
Predict the output of the following code snippets! π€
π Code Snippets: ```java String input = new String("hello"); System.out.println(input == "hello"); String input = "hello"; System.out.println(input == "hello"); ```
π Code Snippets: ```java String input = new String("hello"); System.out.println(input == "hello"); String input = "hello"; System.out.println(input == "hello"); ```
Anonymous Quiz
9%
A) True, False
45%
B) False, True
36%
C) True, True
9%
D) False, False
π1
πFor the above question
String input = new String("hello");
System.out.println(input == "hello");
String input = "hello";
System.out.println(input == "hello");πΉHow can you correctly compare the contents of two strings in Java?
Anonymous Quiz
4%
A) string1 == string2
58%
B) string1.equals(string2)
4%
C) string1 != string2
0%
D) string1.compareTo(string2) == 0
35%
E) ALL
πΉ When comparing strings in Java, what does the method compareTo() return?
Anonymous Quiz
44%
A) A boolean value (true or false)
33%
B) An integer value indicating the difference between the strings
11%
C) A string with a comparison message
11%
D) A String object with the lexicographical order of both strings
In Java string literals (like "hello") are automatically stored in a special memory region called the string pool. When we use a string literal directly in your code, the JVM checks if that string already exists in the string pool. If it does, it reuses the same reference (i.e., the same object). If it doesn't, it adds the string to the pool.
What Happens When You Use new String()?
If you create a string using the new String() constructor, you explicitly create a new object in memory, even if the content of the string is the same.
In this case, "hello" in the string pool and the new String("hello") object are not the same object in memory, so == will return false.
ππ @AceCoding Presents! ππ
String input = "hello";
System.out.println(input == "hello"); // True
What Happens When You Use new String()?
If you create a string using the new String() constructor, you explicitly create a new object in memory, even if the content of the string is the same.
String input = new String("hello");
System.out.println(input == "hello"); // falseIn this case, "hello" in the string pool and the new String("hello") object are not the same object in memory, so == will return false.
ππ @AceCoding Presents! ππ
ALX Assessment Answers.pdf
726.5 KB
π¨ ALX Entrance Assessment Answer key! π¨
The ALX entrance Assessment has 30-40 questionsβtrivial but boring. π
Iβve put together a FREE PDF with all the questions and answers to help you breeze through it! π
π Download the FREE PDF now!
#ALX #EntranceAssessment #TechCareer π
ππ @AceCoding Presents! ππ
The ALX entrance Assessment has 30-40 questionsβtrivial but boring. π
Iβve put together a FREE PDF with all the questions and answers to help you breeze through it! π
π Download the FREE PDF now!
#ALX #EntranceAssessment #TechCareer π
ππ @AceCoding Presents! ππ
π¨ Exciting News π¨
ALX Ethiopia is NOW OPEN! π
Ready to kickstart your career with world-class tech training? π
π° Only $5/month βοΈ
β $200 πΈ
Here are the amazing programs available to you:
πΉ ALX Pathway
πΉ Virtual Assistant
πΉ Professional Foundations
πΉ AI Career Essentials
πΉ Front-End Web Development
πΉ Back-End Web Development
πΉ AWS Cloud Computing
πΉ Salesforce Administrator
πΉ Data Science
πΉ Data Analytics
Don't miss outβ Get started for only $5/month! π₯
π Learn More & Apply: ALX Programs
π Get the full ALX Entrance Assessment Answers: ALX Assessment key
#TechTraining #CareerGrowth #ALX #Ethiopia #FutureLeaders πβ¨
ππ @AceCoding Presents! ππ
ALX Ethiopia is NOW OPEN! π
Ready to kickstart your career with world-class tech training? π
π° Only $5/month βοΈ
β $200 πΈ
Here are the amazing programs available to you:
πΉ ALX Pathway
πΉ Virtual Assistant
πΉ Professional Foundations
πΉ AI Career Essentials
πΉ Front-End Web Development
πΉ Back-End Web Development
πΉ AWS Cloud Computing
πΉ Salesforce Administrator
πΉ Data Science
πΉ Data Analytics
Don't miss outβ Get started for only $5/month! π₯
π Learn More & Apply: ALX Programs
π Get the full ALX Entrance Assessment Answers: ALX Assessment key
#TechTraining #CareerGrowth #ALX #Ethiopia #FutureLeaders πβ¨
ππ @AceCoding Presents! ππ
π₯4
π A2SV Past Interview Questions π
Hey everyone! π’ If you're preparing for an A2SV interview, here are some
common questions you might encounter. Get ready to shine! β¨
Common Questions:
1.Tell me about yourself.
2. What do you know about A2SV and why do you want to join us?
3. Describe a time when you had to step out of your comfort zone to achieve something.
4. How do you give back to your community?
5. What are your strengths and weaknesses?
Additional Questions:
Introduce yourself π©.
What is the hardest decision you've had to make?
Tell us about a time when there was a disagreement in a group. How did you resolve it?
How did you hear about A2SV?
Are you committed to the cause?
Good luck with your preparations! You've got this. πͺπ
Feel free to reach out if you need more tips or have any questions. Let's conquer this together! ππ©βπ»π¨βπ»
I hope this will makes you feel more prepared and confident! If there's anything more you'd like to add or adjust, just let me know in the discussion group π.
Hey everyone! π’ If you're preparing for an A2SV interview, here are some
common questions you might encounter. Get ready to shine! β¨
Common Questions:
1.Tell me about yourself.
2. What do you know about A2SV and why do you want to join us?
3. Describe a time when you had to step out of your comfort zone to achieve something.
4. How do you give back to your community?
5. What are your strengths and weaknesses?
Additional Questions:
Introduce yourself π©.
What is the hardest decision you've had to make?
Tell us about a time when there was a disagreement in a group. How did you resolve it?
How did you hear about A2SV?
Are you committed to the cause?
Good luck with your preparations! You've got this. πͺπ
Feel free to reach out if you need more tips or have any questions. Let's conquer this together! ππ©βπ»π¨βπ»
I hope this will makes you feel more prepared and confident! If there's anything more you'd like to add or adjust, just let me know in the discussion group π.
π2
π‘ Medium leetcode question: 287. Find the Duplicate Number
π’ Using Cycle sort
π¨βπ» Optimized Cycle sort
π΄ Cycle detection Algorithm (Floyd's torties and hare / slow and fast pointer algorithm)
ππ @AceCoding Presents! ππ
https://leetcode.com/problems/find-the-duplicate-number/
π’ Using Cycle sort
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
i = 0
while i < len(nums):
correct_pos = nums[i] - 1
if nums[i] != nums[correct_pos]:
nums[i], nums[correct_pos] = nums[correct_pos], nums[i]
else:
i +=1
for i in range(len(nums)):
if nums[i] != i + 1:
return nums[i]
π¨βπ» Optimized Cycle sort
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
i = 0
while i < len(nums):
if nums[i] != i + 1:
correct_pos = nums[i] - 1
if nums[i] != nums[correct_pos]:
nums[i], nums[correct_pos] = nums[correct_pos], nums[i]
else:
return nums[i]
else:
i += 1
return -1
π΄ Cycle detection Algorithm (Floyd's torties and hare / slow and fast pointer algorithm)
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
slow = nums[nums[0]]
fast = nums[nums[nums[0]]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
slow = nums[0]
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
Time complexity : O(n) & Space Complexity: O(1)
ππ @AceCoding Presents! ππ
https://leetcode.com/problems/find-the-duplicate-number/
LeetCode
Find the Duplicate Number - LeetCode
Can you solve this real interview question? Find the Duplicate Number - Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.β¦
There is only one repeated number in nums, return this repeated number.β¦
π΄ Cycle detection Algorithm (Floyd's torties and hare / slow and fast pointer algorithm)
ππ @AceCoding Presents! ππ
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
slow = nums[nums[0]]
fast = nums[nums[nums[0]]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
slow = nums[0]
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slow
ππ @AceCoding Presents! ππ
π‘ 1561. Maximum Number of Coins You Can Get
π» This is a very good question on Greedy Algorithm and sorting
for those who don't know Greedy is a type of Algo or technique used to solve some problems efficiently ( By being GREEDY every time π that is why it's called greedy)
π Most Efficient Solution both in time and space complexity
ππ @AceCoding Presents! ππ
https://leetcode.com/problems/maximum-number-of-coins-you-can-get/
π» This is a very good question on Greedy Algorithm and sorting
for those who don't know Greedy is a type of Algo or technique used to solve some problems efficiently ( By being GREEDY every time π that is why it's called greedy)
π Most Efficient Solution both in time and space complexity
class Solution:
def maxCoins(self, piles: List[int]) -> int:
piles_per_person = len(piles) // 3
piles.sort(reverse=True)
sum = 0
for i in range(1, len(piles) - piles_per_person, 2):
sum += piles[i]
return sum
Time complexity : O(n*log n) & Space Complexity: O(1)
ππ @AceCoding Presents! ππ
https://leetcode.com/problems/maximum-number-of-coins-you-can-get/
π1