Play Dirty and Play Smart
One of the things that I discovered as a DM (and, admittedly, I'm not the best) is that D&D is heavily focused on advance preparation. Although there are definitely tools that can help you adapt on the fly to things that you won't necessarily find practical to use at the table.
One thing that I argue is to base the competence of any enemy commander/coordinator on the number of players present and adjust accordingly. This will mean that you adapt challenges somewhat in terms of number, but also that you accommodate other variables into your battlefield schema to ensure that combat can remain balanced even with unexpected player counts.
Some of this also depends on who your players are. If you wind up with a particularly bad balance of players (e.g. a solo Wizard with a disregard for combat spells), you might wind up not having a good way to balance an encounter.
One thing that I found with my unreliable group is that I had to steer away from big cinematic battles unless I was able to bring in an ally NPC or have the players cover for each others' characters. In some circles, that's controversial, in mine it was generally acknowledged that your character wouldn't die while in someone else's hands (though I rarely killed characters to begin with: my total PC death toll in several years of running games was around four) to make up for the fact that they might not be played totally responsibly.
Instead, I focused more on weak individual foes, the sort of creatures that can more easily be adapted on the fly: not only can you adjust the number of them, but relying on a basic knowledge of real tactics can make or break a fight involving a small number of individuals.
Number, Location, and Awareness
From a military science perspective, there are three main things that factor into how successful a character will be on the battlefield. The (in)famous story of Tucker's Kobolds illustrates this in a pretty simple manner from the AD&D days, and these principles remain to this day.
As such, all future examples for this answer will be given via kobolds. Enjoy.
Play your enemies with a varying degree of cleverness based on how many PCs show up for the session. You don't need to drop from eight kobolds to two, even: just make sure that the kobolds never really outnumber the PCs, which ensures that they get a more or less even share in the combat rotation.
This will allow most PCs to fight a good number of enemies, but if you really want to make the players sweat toss six or eight enemies at them in one go. Even just a few enemy combatants getting turns that the PCs don't have anyone to match will quickly make up for the difference.
Likewise, if the PCs are in a place where they could have low-level NPCs of their own, a little ablative armor or light fire support can even the odds for a band of heroes caught without important combat-focused PCs.
The general advice I give when playing with numbers is that if you are going to add characters to balance a larger group you should keep them tiny. This stands even for a boss fight: if a new guy shows up on the day of the bossfight, so do a couple CR 1/4 inconveniences. Even a mighty combatant can be brought low with an unfortunate critical at the right moment (though D&D makes it somewhat difficult for that to happen at high levels).
Give the players an advantageous location if they are underpowered or a disadvantageous location if they are too strong for the encounter. Consider their abilities: I made my DM unhappy with my archer ranger by playing a little too smart in combat and relying on cover all the time. Between heavy martial allies and a large boost to my AC, I was able to go a whole combat without taking a scratch.
Your NPCs can do that too: rely on terrain to slow or block PC movements, give Advantage or Disadvantage as needed, and redirect their motions.
If you are stuck with a couple PCs fighting a very tough boss, give them places to run to: temporary respites that allow them to apply restorative magic or take shots before the baddies can get into them (pillars work well for this when fighting 10x10 monsters: they can be brought down to allow access, but will allow characters an easy way out if they don't let themselves get cornered).
Likewise, if an extra PC shows up, the enemies are on the hospitable side of cover and they have no intention of leaving. Give them a little bit of a boost by leveling the playing field, rather than requiring you to consult the DMG or simply put extra figures on the field.
Sometimes there's not a whole lot you can do about your players' awareness of the world around them. I've played with a group that can be blissfully unaware or keenly cunning depending on the weather, as well as groups that are either chronically lacking in tactical savvy or obsessive planners.
One of the best things to happen to the combat system are surprise rules. They can be a DM's nightmare, but even having something as simple as a hidden kobold fire a crossbow from a cranny can cause major pains for the party.
On the other hand, having the PCs know of all possible threats, their locations, and the full battlefield schema will provide them with an edge against most NPCs.
Consider adding more active elements to your scenes: give players a way to transform or exploit the terrain in a way their enemies aren't expecting (or vice versa). Throw in a minor trap that can slow a character down or take them out of combat for a round or two (which also helps with balancing numbers, if necessary) to provide an element of surprise that aids NPCs' efforts to stay alive without dooming the party to failure.