π§© Day 15 β Sum of Good Subsequences & CDN Fundamentals
π§ DSA Problems [1 hr]
Problem: 3351. Sum of Good Subsequences
π‘ Approach: Dynamic Programming on Value Links
π§ Intuition
We define two key state trackers:
-
count[a]β number of good subsequences ending witha. -
res[a]β total sum of all subsequences ending witha.
For each number a in the array:
- We can start a new subsequence with
a. - Or extend subsequences ending with
a - 1anda + 1.
Hence,
count[a] = count[a-1] + count[a+1] + 1
Each of these subsequences contributes additional sums based on their totals and the value a:
res[a] = res[a-1] + res[a+1] + a * (count[a-1] + count[a+1] + 1)
We take modulo 10^9 + 7 to keep values bounded.
Finally, the total sum of all subsequences is sum(res.values()).
π» Code Implementation
class Solution:
def sumOfGoodSubsequences(self, A: List[int]) -> int:
count = Counter()
res = Counter()
mod = 10 ** 9 + 7
for a in A:
count[a] += count[a - 1] + count[a + 1] + 1
count[a] %= mod
res[a] += res[a - 1] + res[a + 1] + a * (count[a - 1] + count[a + 1] + 1)
res[a] %= mod
return sum(res.values()) % mod
βοΈ Complexity
| Measure | Complexity |
|---|---|
| β± Time | O(n) |
| πΎ Space | O(n) |
π§© Key Insight
- The solution uses local adjacency relationships (
a-1,a+1) to form global subsequence sums. - This is a clean example of DP with aggregation via counters instead of full DP arrays β space-efficient and value-oriented.
- The transitions mimic propagation through neighboring states β a pattern common in DP, graph traversal, and even distributed systems.
π SYSTEM DESIGN β Roadmap.sh [1 hr]
π Content Delivery Networks (CDNs)
A CDN is a globally distributed network that serves web content from servers closer to users, drastically improving latency and scalability.
When you load a site like www.netflix.com, your assets (images, CSS, JS, videos) are often fetched from a nearby CDN edge node β not from Netflixβs origin servers.
βοΈ How It Works
- A client requests a resource (e.g., image or video).
- DNS resolves the domain to the nearest CDN edge server.
- If cached, content is served immediately.
- If not, the edge server fetches it from the origin server, caches it, and serves the user.
This reduces latency, distributes load, and saves bandwidth at the origin.
π§± Types of CDNs
πΉ Push CDN
- Content is pushed manually or via automation to CDN nodes when it changes.
- You control cache invalidation and content updates.
- Best for sites with low traffic or infrequent updates.
- β Pros: Less redundant traffic, predictable storage.
- β οΈ Cons: Manual management, risk of stale content.
πΉ Pull CDN
- CDN fetches content on demand when requested for the first time.
- Cached locally afterward for future users.
- Ideal for high-traffic or dynamic sites.
- β Pros: Easier setup, automatic caching.
- β οΈ Cons: First-request latency, possible redundancy if TTL is too short.
β³ TTL (Time To Live)
Each cached item has a TTL β after which it expires and must be re-fetched from the origin.
Finding the right TTL is crucial for balancing freshness vs. efficiency.
βοΈ CDN Trade-offs
| Advantage | Disadvantage |
|---|---|
| β‘ Faster content delivery | π° Extra cost based on bandwidth usage |
| π§ Global scalability | π Risk of stale content if updates happen before TTL expires |
| π§± Reduced load on origin servers | π Requires rewriting URLs for CDN routing |
π Example Providers
- Cloudflare β strong free tier, DDoS protection
- AWS CloudFront β deep AWS integration
- Akamai, Fastly, Google Cloud CDN β high-performance enterprise-grade CDNs
π§ Reflection
Todayβs DSA and System Design topics beautifully align:
| DSA Concept | CDN Analogy |
|---|---|
| Local adjacency propagation | Content caching propagation across edge nodes |
| Dynamic aggregation via neighbors | Dynamic content refresh via TTL and caching rules |
| O(1) lookups via Counter | Constant-time retrieval via global CDN caches |
Both rely on localized computation to achieve global efficiency.
β Day 15 Summary:
From subsequences to CDNs β efficiency grows when we connect adjacent states and cache intelligently. π
Top comments (0)