< Ace Coding /> πŸš€
337 subscribers
54 photos
2 videos
95 files
66 links
Welcome to Ace Coding! Join us for tips, tutorials, and insights on coding and software engineering. Stay updated with the latest content and elevate your programming skills!Let's learn and grow together in the world of software engineering!
Download Telegram
Here you can see the trade-offs of different sorting algorithms πŸ“Š

πŸŒŸπŸš€ @AceCoding Presents! πŸš€πŸŒŸ
Forwarded from AASTU Software Engineering (John Robi)
DSA Exams.rar
24.8 MB
Previous DSA exams
πŸ“Š Big O (worst case) Visualization.

πŸŒŸπŸš€ @AceCoding Presents! πŸš€πŸŒŸ
βœ… 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;
}
}
πŸŒŸπŸš€ @AceCoding Presents! πŸš€πŸŒŸ
πŸŒŸπŸš€ @AceCoding Presents! πŸš€πŸŒŸ
βœ… 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! πŸš€πŸŒŸ
Basic_data_structures_and_time_and_space_complexity.pdf
3.9 MB
From a2sv
πŸŒŸπŸš€ @AceCoding Presents! πŸš€πŸŒŸ
⚑1πŸ™1
< Ace Coding /> πŸš€
Basic_data_structures_and_time_and_space_complexity.pdf
Quote of the day
β€œ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! πŸš€πŸŒŸ
πŸ”₯ 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! πŸš€πŸŒŸ
πŸ”₯4
βœ… 34. Find First and Last Position of Element in Sorted Array

🟒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

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

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/
🟑 Easy Medium (1541) leetCode Question: 1283. 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/
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