Skip to content

Commit

Permalink
242nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 25, 2024
1 parent 7330056 commit b67c99d
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@ Ace Coding Interview with 75 Qs
| 215. Kth Largest Element in an Array | [Solution][215] | Medium |
| 2336. Smallest Number in Infinite Set | [Solution][2336] | Medium |
| 2542. Maximum Subsequence Score | [Solution][2542] | Medium |
| 2462. Total Cost to Hire K Workers | Solution | Medium |
| 2462. Total Cost to Hire K Workers | [Solution][2462] | Medium |

[215]: ./src/page-2/215.%20Kth%20Largest%20Element%20in%20an%20Array/findKthLargest.ts
[2336]: ./src/page-22/2336.%20Smallest%20Number%20in%20Infinite%20Set/SmallestInfiniteSet.ts
[2542]: ./src/page-24/2542.%20Maximum%20Subsequence%20Score/maxScore.ts
[2462]: ./src/page-23/2462.%20Total%20Cost%20to%20Hire%20K%20Workers/totalCost.ts

| Binary Search | | |
| -------------------------------------------- | ---------------- | ------ |
Expand Down
2 changes: 1 addition & 1 deletion algorithms/2.linked-list/CircularLinkedList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ListNode } from './ListNode';
import { ListNode } from './ListNode.ts';

export class CircularLinkedList<T> {
head: ListNode<T> | null;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/2.linked-list/DoublyLinkedList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DoublyListNode } from './DoublyListNode';
import { DoublyListNode } from './DoublyListNode.ts';

export class DoublyLinkedList<T> {
head: DoublyListNode<T> | null;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/2.linked-list/LinkedList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ListNode } from './ListNode';
import { ListNode } from './ListNode.ts';

export class LinkedList<T> {
head: ListNode<T> | null;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/3.stack/LinkedListStack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ListNode } from '../linked-list/ListNode';
import { ListNode } from '../2.linked-list/ListNode.ts';

export class LinkedListStack<T> {
private head: ListNode<T> | null = null;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/9.trie/Trie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TrieNode } from './TrieNode';
import { TrieNode } from './TrieNode.ts';

export class Trie {
root: TrieNode;
Expand Down
6 changes: 4 additions & 2 deletions src/page-1/1. Two Sum/twoSum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export const twoSum2: TwoSum = (nums, target) => {
const map = new Map<number, number>();

for (let i = 0; i < nums.length; i++) {
const index = map.get(target - nums[i]);
if (typeof index === 'number') return [index, i];
// nums[i] + x = target
// x = target − nums[i]
const complement = map.get(target - nums[i]);
if (complement !== undefined) return [complement, i];
map.set(nums[i], i);
}

Expand Down
5 changes: 1 addition & 4 deletions src/page-10/994. Rotting Oranges/orangesRotting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export const orangesRotting: OrangesRotting = (grid) => {
let rottenInThisMinute = false;

for (let i = 0; i < currentLevelSize; i++) {
const current = queue.shift();
if (current === undefined) continue; // Safely handle empty queue

const [row, col] = current;
const [row, col] = queue.shift() as [number, number];

// Check all 4-directionally adjacent cells
for (const [dr, dc] of directions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ export const nearestExit: NearestExit = (maze, entrance) => {
maze[startRow][startCol] = '+'; // Mark the entrance as visited by converting it to a wall.

while (queue.length > 0) {
const current = queue.shift();
if (current === undefined) continue; // Safely handle empty queue

const [row, col, steps] = current;
const [row, col, steps] = queue.shift() as [number, number, number];

// Check all four possible directions.
for (const [dRow, dCol] of directions) {
Expand Down
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 src/page-23/2462. Total Cost to Hire K Workers/totalCost.ts
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;
};

0 comments on commit b67c99d

Please sign in to comment.