-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7330056
commit b67c99d
Showing
11 changed files
with
82 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/page-23/2462. Total Cost to Hire K Workers/totalCost.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { totalCost } from './totalCost'; | ||
|
||
describe('2462. Total Cost to Hire K Workers', () => { | ||
test('totalCost', () => { | ||
expect(totalCost([17, 12, 10, 2, 7, 2, 11, 20, 8], 3, 4)).toBe(11); | ||
expect(totalCost([1, 2, 4, 1], 3, 3)).toBe(4); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
src/page-23/2462. Total Cost to Hire K Workers/totalCost.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { BinaryHeap } from '../../@std/data-structures'; | ||
|
||
type TotalCost = (costs: number[], k: number, candidates: number) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const totalCost: TotalCost = (costs, k, candidates) => { | ||
const n = costs.length; | ||
|
||
let totalCost = 0; | ||
|
||
const firstHeap = new BinaryHeap<[number, number]>((a, b) => a[0] - b[0]); // min-heap for (cost, index) | ||
const lastHeap = new BinaryHeap<[number, number]>((a, b) => a[0] - b[0]); // min-heap for (cost, index) | ||
|
||
// Initialize firstHeap with the first `candidates` workers | ||
for (let i = 0; i < Math.min(candidates, n); i++) { | ||
firstHeap.push([costs[i], i]); | ||
} | ||
|
||
// Initialize lastHeap with the last `candidates` workers | ||
for (let i = n - 1; i >= Math.max(n - candidates, candidates); i--) { | ||
lastHeap.push([costs[i], i]); | ||
} | ||
|
||
// Track the bounds for the remaining workers not in any heap | ||
let left = candidates; | ||
let right = n - candidates - 1; | ||
|
||
// Hire `k` workers based on the minimum cost in each session | ||
for (let i = 0; i < k; i++) { | ||
let chosen: [number, number]; | ||
|
||
const firstRoot = firstHeap.peek() as [number, number]; | ||
const lastRoot = lastHeap.peek() as [number, number]; | ||
|
||
if (firstHeap.isEmpty() || (!lastHeap.isEmpty() && lastRoot[0] < firstRoot[0])) { | ||
// Choose from lastHeap if it has the minimum cost | ||
chosen = lastHeap.pop() as [number, number]; | ||
totalCost += chosen[0]; | ||
|
||
// If there's any remaining candidate on the right side, add it to lastHeap | ||
if (right >= left) { | ||
lastHeap.push([costs[right], right]); | ||
right -= 1; | ||
} | ||
} else { | ||
// Choose from firstHeap | ||
chosen = firstHeap.pop() as [number, number]; | ||
totalCost += chosen[0]; | ||
|
||
// If there's any remaining candidate on the left side, add it to firstHeap | ||
if (left <= right) { | ||
firstHeap.push([costs[left], left]); | ||
left += 1; | ||
} | ||
} | ||
} | ||
|
||
return totalCost; | ||
}; |