DEV Community

Cover image for Daily DSA and System Design Journal - 16
I.K
I.K

Posted on

Daily DSA and System Design Journal - 16

πŸ”₯ 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
  1. Sort the array in ascending order.
  2. Initialize a variable prev = -inf to represent the last assigned value.
  3. 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 set prev = 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
Enter fullscreen mode Exit fullscreen mode

βš™οΈ 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):

  1. The CDN checks if it already has a cached copy.
  2. If not, it pulls it from your origin server.
  3. 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)