Here you can see the trade-offs of different sorting algorithms π
ππ @AceCoding Presents! ππ
ππ @AceCoding Presents! ππ
Data Structures Using C++.pdf
5.3 MB
Data Structures Using C++
Data Structure Worksheet.docx
20.4 KB
Data Structure Worksheet
β
Optimized version of bubble sort, which will give a TC of O(n) best case (array is already sorted)
void bubbleSortOptimized(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}β When do we encounter a Time Complexity of O(n * log n)? To be more explicit O(n * log2 n) the base is 2 not 10.
Whenever we repeatedly divide the problem by 2 or multiply the input by 2 (as seen in algorithms like merge sort or heap sort), we often deal with quasi-linear time complexity, which is O(n * log n). This pattern is common in efficient sorting and searching algorithms, so keep it in mind when breaking down problems!
#DSA #TimeComplexity #CodingTips
@AceCoding
π₯2
DSA MID TEST 2023-ANSWERSHEET.pdf
700.7 KB
ππ DSA AASTU 2023 Mid Exam
With Solution Answers ππ‘
#AASTU #DSA #DSAmidexam #softwaremidexam
ππ @AceCoding Presents! ππ
With Solution Answers ππ‘
#AASTU #DSA #DSAmidexam #softwaremidexam
ππ @AceCoding Presents! ππ
Basic_data_structures_and_time_and_space_complexity.pdf
3.9 MB
From a2sv
ππ @AceCoding Presents! ππ
ππ @AceCoding Presents! ππ
β‘1π1
< 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