Ponchimeow/Practice

耶誕老人到你家 ( 75分 )

Opened this issue · 0 comments

Lidemy, OJ 耶誕老人到你家

目前 75 分,待檢修

function pd(a, b, c) {
  let num = [];
  let gift = [];
  let box = [];
  let count = 0;
  a.split(' ').forEach(item => {
    num.push(parseInt(item))
  });
  b.split(' ').forEach(item => {
    gift.push(parseInt(item))
  });
  c.split(' ').forEach(item => {
    box.push(parseInt(item))
  });

  let gifts = (gift.sort((a, b) => {
    return a - b
  }));

  let boxes = (box.sort((a, b) => {
    return a - b
  }));

  if (num[0] >= 1 && num[1] >= 1 && num[0] <= 50000 && num[1] <= 50000) {
    for (let i = num[1] - 1; i >= 0; i -= 1) {
      if (typeof boxes[i] === 'undefined' || (boxes[i] >= 1 && boxes[i] <= Math.pow(2, 31) - 1)) {
        if (typeof boxes[i] === 'undefined') {
          continue;
        }
        for (let j = num[0] - 1; j >= 0; j -= 1) {
          if (typeof gifts[j] === 'undefined' || (gifts[j] >= 1 && gifts[j] <= Math.pow(2, 31) - 1)) {
            if (typeof boxes[i] === 'undefined' || typeof gifts[j] === 'unfefined') {
              continue;
            }
            if (boxes.length === 0 || gifts.length === 0) {
              return count;
            }
            if (gifts[j] <= boxes[i]) {
              count += 1;
              gifts.splice(j, 1);
              boxes.splice(i, 1);
            } else {
              gifts.splice(j, 1);
            }
          }
        }
      }
    }
    if (count === 0 && boxes.length === 0 || gifts.length === 0) {
      return 'Santa Claus wishes you get AC in the next submission.'
    }
    return count;
  }
}