Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
Once upon a time, in a village of coders, there lived a gardener named Prashant. He had two ways of planting flowers in his garden Greedy and Non-Greedy.
1. The Non-Greedy Way
One day, Prashant wanted to count all the even-numbered flowers in his garden.
let count = 0; for (let flower of flowers) { // Just counts every even flower if (flower % 2 === 0) count++; }2. The Greedy Twist
let count = 0; let lastEven = false; for (let flower of flowers) { // Greedy condition if (flower % 2 === 0 && !lastEven) { count++; lastEven = true; // Lock it in! } else { lastEven = false; // Move on } }Why was this greedy?
3. Another Greedy Trick
let maxReach = 0; for (let i = 0; i < stones.length; i++) { // Can't go further! if (maxReach < i) return false; // Take the biggest leap maxReach = Math.max(maxReach, i + stones[i]); } return true;4. The Gas Station Puzzle
let totalGas = 0, currentGas = 0, start = 0; for (let i = 0; i < gas.length; i++) { totalGas += gas[i] - cost[i]; currentGas += gas[i] - cost[i]; if (currentGas < 0) { // Oops, restart! start = i + 1; currentGas = 0; } } return totalGas >= 0 ? start : -1;Moral of the Story
Greedy algorithms make quick, local decisions
— Prashant, the Algorithmic Gardener 🌸