Develop / Leetcode April 8, 2024

Daily Question – 1700. Number of Students Unable to Eat Lunch

Description: https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/description/

Notes:

Time Complexity = O(n2), can be simplify to O(n)

Code:

class Solution {
    public int countStudents(int[] students, int[] sandwiches) {
        Stack<Integer> sand = new Stack<>();
        List<Integer> stu = new ArrayList<>();
        boolean check;
        int x, i;
        for(i = sandwiches.length-1; i>=0; i--) sand.push(sandwiches[i]);
        for(i = 0; i < students.length; i++) stu.add(students[i]);
        while (!sand.isEmpty()) {
            check = false;
            x = sand.pop();
            for (i=0;i<stu.size();i++) if (stu.get(i) == x) {
                stu.remove(i);
                check = true;
                break;
            }
            if (!check) return stu.size();
        }
        return 0;
    }
}

You may also like...

Apr
05
2024
0

Daily Question – 1544. Make The String Great

Post Views: 30 Description: https://leetcode.com/problems/make-the-string-great/description/?envType=daily-question&envId=2024-04-05 Note: One direct way is to continue iterating the array until...

Feb
19
2024
0

Daily Question – 0231. Power of Two

Post Views: 34 Descroption: https://leetcode.com/problems/power-of-two/ Note

Mar
02
2024
0

Daily Question – 0977. Squares of a Sorted Array

Post Views: 30 Description: https://leetcode.com/problems/squares-of-a-sorted-array/description Code: