diff --git a/dynamic_programming/coin_change.ts b/dynamic_programming/coin_change.ts index 0734fb9a..2e9e7b0d 100644 --- a/dynamic_programming/coin_change.ts +++ b/dynamic_programming/coin_change.ts @@ -17,12 +17,12 @@ export const coinChange = (money: number, coins: number[]): CoinChange => { minCoins[0] = 0 // Fill in the DP table - for (let i = 0; i < coins.length; i++) { + for (const coin of coins) { for (let j = 0; j <= money; j++) { - if (j >= coins[i]) { - if (minCoins[j] > 1 + minCoins[j - coins[i]]) { - minCoins[j] = 1 + minCoins[j - coins[i]] - lastCoin[j] = coins[i] + if (j >= coin) { + if (minCoins[j] > 1 + minCoins[j - coin]) { + minCoins[j] = 1 + minCoins[j - coin] + lastCoin[j] = coin } } } diff --git a/graph/prim.ts b/graph/prim.ts index 3261c633..b3f565c7 100644 --- a/graph/prim.ts +++ b/graph/prim.ts @@ -51,8 +51,7 @@ const add_children = ( priorityQueue: PriorityQueue, node: number ) => { - for (let i = 0; i < graph[node].length; ++i) { - const out_edge = graph[node][i] + for (const out_edge of graph[node]) { // By increasing the priority, we ensure we only add each vertex to the queue one time, and the queue will be at most size V. priorityQueue.increasePriority( out_edge[0], diff --git a/sorts/counting_sort.ts b/sorts/counting_sort.ts index cedad89f..49e76232 100644 --- a/sorts/counting_sort.ts +++ b/sorts/counting_sort.ts @@ -13,7 +13,7 @@ export const countingSort = (inputArr: number[], min: number, max: number) => { const count = new Array(max - min + 1).fill(0) - for (let i = 0; i < inputArr.length; i++) count[inputArr[i] - min]++ + for (const element of inputArr) count[element - min]++ count[0] -= 1