Home > Mobile >  Finding performance feasibility using array strings
Finding performance feasibility using array strings

Time:05-13

Here is the requirement details:

time = ["09-13", "12-14"]

getResult(time) = false

The first performance runs from 9 a.m. to 1 p.m. and the second one starts at 12 p.m. So you won't be able to see each performance in full.

time = ["07-09", "10-12", "15-19"]

getResult(time) = true

but i could not able to get the result. I am finding challange to get it done. any one help me?

here is my try:

const getResult = (time) => {
  const nums = [];
  let pre = 0;
  time.map((item,index) => {
    item.split('-').map((v,i) => {
      nums.push( v);//converting as number
    });
  });
  const result = nums.map((v,i) => {
    if(!i) return;
    console.log(v-pre)//but logically this is not works
    pre = v;
  })
}

//time = ["09-13", "12-14"]; //false
time = ["07-09", "10-12", "15-19"] //true
getResult(time); //should be false

Thanks in advance.

CodePudding user response:

Turn each range into each individual hour within the range, then:

  • if the hour already exists in the array, return false
  • else, push to the array

const getResult = (ranges) => {
  const filled = [];
  for (const range of ranges) {
    let [start, end] = range.split('-').map(Number);
    while (end !== start) { // Exclusive
      if (filled.includes(start)) return false;
      filled.push(start);
      start  ;
    }
  }
  return true;
};
console.log(getResult(["09-13", "12-14"]));
console.log(getResult(["07-09", "10-12", "15-19"]));

Your current approach doesn't look to have any notion of turning the ranges into their individual hours, or of identifying overlaps.

CodePudding user response:

  • Sort the time slots based on their start time.
  • Loop over all the time slots in the sorted array and if anytime the start of a slot is less than the end of the previous one, then return false.
  • If the whole loop is exhausted return true.

function getResult(time) {
  const sortedTime = [...time].sort((a, b) => {
    const slotA = Number(a.split("-")[0]);
    const slotB = Number(b.split("-")[0]);

    return slotA - slotB;
  });
  console.log("Sorted Time Slots", sortedTime);

  let last;

  for (let t of sortedTime) {
    const [start, end] = t.split("-").map(Number);
    if (last && start < last) {
      return false;
    }
    last = end;
  }

  return true;
}

console.log(getResult(["12-14", "09-13"]));
console.log(getResult(["10-12", "07-09", "15-19"]));

  • Related