Skip to content

Latest commit

 

History

History
27 lines (25 loc) · 900 Bytes

1700. 无法吃午餐的学生数量.md

File metadata and controls

27 lines (25 loc) · 900 Bytes
/**
 * @param {number[]} students
 * @param {number[]} sandwiches
 * @return {number}
 */
var countStudents = function (students, sandwiches) {
    // 我想知道有多少个学生不喜欢栈顶的三明治
    let numberOfFailures = 0;
    // 结束的条件要么是三明治被拿光了,要么所有的学生都不喜欢栈顶的三明治
    while (sandwiches.length && numberOfFailures < students.length) {
        if (sandwiches[0] === students[0]) {
            // 重新统计不喜欢栈顶三明治的学生数量
            numberOfFailures = 0;
            sandwiches.shift();
            students.shift();
        } else {
            ++numberOfFailures;
            students.push(students.shift());
        }
    }
    return students.length;
};