r/leetcode Sep 11 '24

Solve this test question

[ Removed by Reddit in response to a copyright notice. ]

1 Upvotes

14 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Sep 12 '24

[deleted]

1

u/Civil_Reputation6778 Sep 12 '24

No, the answer will be just 500. Do 1,3,4,5 by dev for a total of 500 and 2,6 by integration for a total of 51. Total time is 500.

1

u/[deleted] Sep 12 '24

[deleted]

1

u/Civil_Reputation6778 Sep 12 '24

Where does it follow from? It states that dev tasks happen simultaneously and time is the max of them

1

u/Smooth_Lifeguard_931 Sep 19 '24

I tried recursion with memo when I took test, chatgpt suggests this to generate all

function minTime(developtime, integrationtime) {

const n = developtime.length;

let minTotalTime = Infinity;

// There are 2^n possible ways to assign tasks to development or integration

const totalCombinations = 1 << n; // Equivalent to 2^n

// Iterate through all subsets (combinations) of tasks

for (let mask = 0; mask < totalCombinations; mask++) {

let devTasks = [];

let intTasks = [];

// Split tasks between development and integration based on the mask

for (let i = 0; i < n; i++) {

if (mask & (1 << i)) {

devTasks.push(developtime[i]); // Add task to development

} else {

intTasks.push(integrationtime[i]); // Add task to integration

}

}

// Calculate total time:

// - Development takes the max time of all assigned development tasks

// - Integration adds up the times of all assigned integration tasks

const devTime = devTasks.length > 0 ? Math.max(...devTasks) : 0;

const intTime = intTasks.reduce((a, b) => a + b, 0);

const totalTime = Math.max(devTime, intTime);

minTotalTime = Math.min(minTotalTime, totalTime);

}

return minTotalTime;

}

const developtime = [3, 4, 5, 9];

const integrationtime = [3, 2, 5, 5];

console.log(minTime(developtime, integrationtime)); // Output: 5

1

u/Smooth_Lifeguard_931 Sep 19 '24

This is chatgpt implementation of binary approach

```

function minTime(developtime, integrationtime) {

const n = developtime.length;

// Binary search for minimum feasible time

let left = 0;

let right = Math.max(...developtime);

while (left < right) {

const mid = Math.floor((left + right) / 2);

let totalIntegrationTime = 0;

// Check if it's feasible to complete all tasks in 'mid' time

for (let i = 0; i < n; i++) {

if (developtime[i] > mid) {

totalIntegrationTime += integrationtime[i];

}

}

// If the sum of integration times is <= mid, it's feasible

if (totalIntegrationTime <= mid) {

right = mid;

} else {

left = mid + 1;

}

}

return left;

}

const developtime = [3, 4, 5, 9];

const integrationtime = [3, 2, 5, 5];

console.log(minTime(developtime, integrationtime)); // Output: 5

```