An important problem in neuronal computation is to discern how features of stimuli control the timing of action potentials. One aspect of this problem is to determine how an action potential, or spike, can be elicited with the least energy cost, e.g., a minimal amount of applied current. Here we show in the Hodgkin & Huxley model of the action potential and in experiments on squid giant axons that: 1) spike generation in a neuron can be highly discriminatory for stimulus shape and 2) the optimal stimulus shape is dependent upon inputs to the neuron. We show how polarity and time course of post-synaptic currents determine which of these optimal stimulus shapes best excites the neuron. These results are obtained mathematically using the calculus of variations and experimentally using a stochastic search methodology. Our findings reveal a surprising complexity of computation at the single cell level that may be relevant for understanding optimization of signaling in neurons and neuronal networks.