π₯ Day 16 β Greedy Growth & Global Pulls πβοΈ
Greedy algorithms and CDNs both thrive on balance β optimizing decisions locally to maximize global efficiency. Todayβs combo: maximizing distinctness in arrays and minimizing latency with pull-based caching.
π§© DSA Problems [1 hr]
Problem: 3176. Find Maximum Distinct Elements After Operations (the problem you described matches this one)
π‘ Approach: Greedy
π§ Intuition
We want to maximize the number of distinct elements in an array after at most one operation per element,
where each element nums[i] can be changed by any value in the range [nums[i] - k, nums[i] + k].
Key idea:
- After applying all operations, all numbers lie within
[min(nums) - k, max(nums) + k]. - To make as many unique numbers as possible, we should assign the smallest valid unused value to each number.
πͺ Step-by-Step Greedy Process
- Sort the array in ascending order.
- Initialize a variable
prev = -infto represent the last assigned value. - For each number in sorted order:
- Its valid range after operations is
[num - k, num + k]. -
Choose the smallest possible value thatβs still greater than
prev:
curr = min(max(num - k, prev + 1), num + k) If
curr > prev, increment the count and setprev = curr.
This ensures that every element takes the earliest distinct spot available.
π» Code Implementation
import math
class Solution:
def maxDistinctElements(self, nums: List[int], k: int) -> int:
nums.sort()
cnt = 0
prev = -math.inf
for num in nums:
curr = min(max(num - k, prev + 1), num + k)
if curr > prev:
cnt += 1
prev = curr
return cnt
βοΈ Complexity
| Measure | Complexity |
|---|---|
| β± Time | O(n log n) β due to sorting |
| πΎ Space | O(log n) β typical for in-place sorting |
π§ Insights
- This greedy algorithm ensures local optimality per element, leading to global optimal distinctness.
- It parallels interval scheduling β minimizing conflicts while maximizing utilization.
- Alternative view: weβre assigning each element a unique βslotβ within its valid range.
π SYSTEM DESIGN β Roadmap.sh [1 hr]
π§ Pull CDNs
Pull CDNs automatically fetch and cache your content on demand β that is, only when users request it for the first time.
When a user requests content (like an image or CSS file):
- The CDN checks if it already has a cached copy.
- If not, it pulls it from your origin server.
- The content is cached and served to future users until it expires (based on TTL).
βοΈ How It Works
- Clients access resources via CDN URLs (e.g.,
cdn.example.com/images/logo.png). - The CDN routes the request to the nearest edge node.
- If the content is cached and valid, itβs served instantly.
- Otherwise, itβs fetched from the origin, cached, and then served.
π TTL (Time To Live)
- Determines how long cached content remains before itβs revalidated or re-fetched.
- Choosing a good TTL balances freshness (short TTL) vs. efficiency (long TTL).
β Pros
- Hands-off caching β no need to push content manually.
- Great for dynamic or frequently accessed resources.
- Balances load across global edge servers.
β οΈ Cons
- First request latency β the initial user experiences a delay while the content is fetched.
- Redundant traffic β if TTLs expire too soon, the same files may be re-fetched repeatedly.
- Possible staleness β if content changes faster than cache invalidation.
π‘ Ideal Use Cases
| Use Case | Reason |
|---|---|
| High-traffic websites | Frequent access keeps caches warm |
| APIs serving static responses | Edge caching reduces origin hits |
| Video or image-heavy apps | Large content benefits from proximity caching |
π§© DSA β System Design Analogy
| DSA Concept | CDN Concept |
|---|---|
| Assigning smallest valid distinct value | Fetching content lazily, only when needed |
| Greedy range compression | Cache-space optimization |
Avoiding duplicates via prev tracking |
Avoiding redundant pulls via TTL |
Both operate on lazy evaluation principles β do the minimal necessary now to maximize global efficiency later. βοΈ
β Day 16 Summary
Greediness isnβt always bad β when used smartly, it leads to global harmony. Whether assigning numbers or caching content, optimal local choices build global distinctness and efficiency. π
Top comments (0)