|
| 1 | +import React, { useState, useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +export default function CoinChangeVisualizer() { |
| 4 | + const [coins, setCoins] = useState([1, 2, 5]); |
| 5 | + const [amount, setAmount] = useState(11); |
| 6 | + const [speed, setSpeed] = useState(500); |
| 7 | + const [dp, setDp] = useState([]); |
| 8 | + const [steps, setSteps] = useState([]); |
| 9 | + const [currentStep, setCurrentStep] = useState(0); |
| 10 | + const [isRunning, setIsRunning] = useState(false); |
| 11 | + const intervalRef = useRef(null); |
| 12 | + |
| 13 | + // --- Algorithm Logic --- |
| 14 | + const generateSteps = (coins, amount) => { |
| 15 | + const dpTable = Array(coins.length + 1) |
| 16 | + .fill(null) |
| 17 | + .map(() => Array(amount + 1).fill(Infinity)); |
| 18 | + for (let i = 0; i <= coins.length; i++) dpTable[i][0] = 0; |
| 19 | + |
| 20 | + const stepsList = []; |
| 21 | + for (let i = 1; i <= coins.length; i++) { |
| 22 | + for (let j = 1; j <= amount; j++) { |
| 23 | + const withoutCoin = dpTable[i - 1][j]; |
| 24 | + const withCoin = |
| 25 | + j >= coins[i - 1] ? dpTable[i][j - coins[i - 1]] + 1 : Infinity; |
| 26 | + dpTable[i][j] = Math.min(withoutCoin, withCoin); |
| 27 | + |
| 28 | + stepsList.push({ |
| 29 | + i, |
| 30 | + j, |
| 31 | + coin: coins[i - 1], |
| 32 | + withoutCoin, |
| 33 | + withCoin, |
| 34 | + result: dpTable[i][j], |
| 35 | + dpSnapshot: dpTable.map((r) => [...r]), |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + return stepsList; |
| 40 | + }; |
| 41 | + |
| 42 | + // --- Visualization Control --- |
| 43 | + const startVisualization = () => { |
| 44 | + const newSteps = generateSteps(coins, amount); |
| 45 | + setSteps(newSteps); |
| 46 | + setDp(newSteps[0].dpSnapshot); |
| 47 | + setCurrentStep(0); |
| 48 | + setIsRunning(true); |
| 49 | + }; |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + if (isRunning && steps.length > 0) { |
| 53 | + intervalRef.current = setInterval(() => { |
| 54 | + setCurrentStep((prev) => { |
| 55 | + if (prev >= steps.length - 1) { |
| 56 | + clearInterval(intervalRef.current); |
| 57 | + setIsRunning(false); |
| 58 | + return prev; |
| 59 | + } |
| 60 | + return prev + 1; |
| 61 | + }); |
| 62 | + }, speed); |
| 63 | + } |
| 64 | + return () => clearInterval(intervalRef.current); |
| 65 | + }, [isRunning, steps, speed]); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + if (steps[currentStep]) setDp(steps[currentStep].dpSnapshot); |
| 69 | + }, [currentStep, steps]); |
| 70 | + |
| 71 | + const current = steps[currentStep]; |
| 72 | + |
| 73 | + // --- Color Logic --- |
| 74 | + const getCellColor = (i, j, cell) => { |
| 75 | + if (cell === 0) return "bg-green-600 text-white font-bold"; |
| 76 | + if (current && current.i === i && current.j === j) |
| 77 | + return "bg-yellow-400 text-black animate-pulse shadow-lg shadow-yellow-400/40"; |
| 78 | + if (i === coins.length && j === amount) |
| 79 | + return "bg-purple-600 text-white font-bold shadow-md"; |
| 80 | + if (cell === Infinity) return "bg-gray-800 text-gray-400"; |
| 81 | + return "bg-blue-600/70 text-white"; |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <div className="p-4 md:p-8 bg-gray-950 min-h-screen text-white font-sans"> |
| 86 | + <h1 className="text-3xl font-bold mb-8 text-center text-indigo-400"> |
| 87 | + Coin Change Visualizer (Dynamic Programming) |
| 88 | + </h1> |
| 89 | + |
| 90 | + {/* Input Controls */} |
| 91 | + <div className="flex flex-wrap justify-center gap-4 mb-8"> |
| 92 | + <input |
| 93 | + type="text" |
| 94 | + value={coins.join(",")} |
| 95 | + onChange={(e) => setCoins(e.target.value.split(",").map(Number))} |
| 96 | + placeholder="Coins (e.g., 1,2,5)" |
| 97 | + className="p-2 rounded-md bg-gray-800 border border-gray-700 text-center w-48" |
| 98 | + /> |
| 99 | + <input |
| 100 | + type="number" |
| 101 | + value={amount} |
| 102 | + onChange={(e) => setAmount(Number(e.target.value))} |
| 103 | + placeholder="Amount" |
| 104 | + className="p-2 rounded-md bg-gray-800 border border-gray-700 text-center w-32" |
| 105 | + /> |
| 106 | + <input |
| 107 | + type="range" |
| 108 | + min="200" |
| 109 | + max="1500" |
| 110 | + value={speed} |
| 111 | + onChange={(e) => setSpeed(Number(e.target.value))} |
| 112 | + className="w-40 accent-indigo-500" |
| 113 | + /> |
| 114 | + <button |
| 115 | + onClick={startVisualization} |
| 116 | + className="px-4 py-2 bg-indigo-600 rounded-md hover:bg-indigo-500 transition" |
| 117 | + > |
| 118 | + Start |
| 119 | + </button> |
| 120 | + <button |
| 121 | + onClick={() => { |
| 122 | + setIsRunning(false); |
| 123 | + clearInterval(intervalRef.current); |
| 124 | + setSteps([]); |
| 125 | + setDp([]); |
| 126 | + }} |
| 127 | + className="px-4 py-2 bg-gray-700 rounded-md hover:bg-gray-600 transition" |
| 128 | + > |
| 129 | + Reset |
| 130 | + </button> |
| 131 | + </div> |
| 132 | + |
| 133 | + {/* DP Table */} |
| 134 | + <div className="overflow-x-auto flex justify-center mb-10"> |
| 135 | + <table className="border-separate border-spacing-1 text-center"> |
| 136 | + <thead> |
| 137 | + <tr> |
| 138 | + <th className="p-2 bg-gray-800 border border-gray-700 rounded-md text-gray-300"> |
| 139 | + Coins ↓ / Amount → |
| 140 | + </th> |
| 141 | + {Array.from({ length: amount + 1 }, (_, j) => ( |
| 142 | + <th |
| 143 | + key={j} |
| 144 | + className="p-2 bg-gray-800 border border-gray-700 rounded-md text-sm" |
| 145 | + > |
| 146 | + {j} |
| 147 | + </th> |
| 148 | + ))} |
| 149 | + </tr> |
| 150 | + </thead> |
| 151 | + <tbody> |
| 152 | + {dp.map((row, i) => ( |
| 153 | + <tr key={i} className="even:bg-gray-900/40"> |
| 154 | + <td className="px-3 py-2 bg-gray-800 border border-gray-700 rounded-md text-sm text-gray-300"> |
| 155 | + {i === 0 ? "∅" : `[${coins[i - 1]}]`} |
| 156 | + </td> |
| 157 | + {row.map((cell, j) => ( |
| 158 | + <td |
| 159 | + key={j} |
| 160 | + className={`w-12 h-12 border border-gray-700 rounded-md text-center transition-all duration-300 ${getCellColor( |
| 161 | + i, |
| 162 | + j, |
| 163 | + cell |
| 164 | + )}`} |
| 165 | + > |
| 166 | + {cell === Infinity ? "∞" : cell} |
| 167 | + </td> |
| 168 | + ))} |
| 169 | + </tr> |
| 170 | + ))} |
| 171 | + </tbody> |
| 172 | + </table> |
| 173 | + </div> |
| 174 | + |
| 175 | + {/* Explanation Panel */} |
| 176 | + {current && ( |
| 177 | + <div className="text-center bg-gray-900 border border-gray-700 rounded-xl p-6 shadow-lg max-w-2xl mx-auto"> |
| 178 | + <p className="text-indigo-300 font-semibold mb-2"> |
| 179 | + Step {currentStep + 1} / {steps.length} |
| 180 | + </p> |
| 181 | + <p className="text-gray-200"> |
| 182 | + Computing{" "} |
| 183 | + <span className="text-yellow-400 font-medium"> |
| 184 | + dp[{current.i}][{current.j}] |
| 185 | + </span>{" "} |
| 186 | + using coin{" "} |
| 187 | + <span className="text-cyan-400 font-medium">{current.coin}</span> |
| 188 | + </p> |
| 189 | + <p className="mt-3 text-gray-300"> |
| 190 | + Without coin ={" "} |
| 191 | + <span className="text-red-400"> |
| 192 | + {current.withoutCoin === Infinity ? "∞" : current.withoutCoin} |
| 193 | + </span>{" "} |
| 194 | + | With coin ={" "} |
| 195 | + <span className="text-green-400"> |
| 196 | + {current.withCoin === Infinity ? "∞" : current.withCoin} |
| 197 | + </span> |
| 198 | + </p> |
| 199 | + <p className="mt-3 text-green-400 font-semibold"> |
| 200 | + dp[{current.i}][{current.j}] = min( |
| 201 | + {current.withoutCoin === Infinity ? "∞" : current.withoutCoin},{" "} |
| 202 | + {current.withCoin === Infinity ? "∞" : current.withCoin}) ={" "} |
| 203 | + {current.result === Infinity ? "∞" : current.result} |
| 204 | + </p> |
| 205 | + </div> |
| 206 | + )} |
| 207 | + </div> |
| 208 | + ); |
| 209 | +} |
0 commit comments