Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT
Children need greed factors [1, 2, 3, 5, 7]. Each child can receive at most one cookie, and a child is satisfied if the cookie size is at least their greed factor. Cookie sizes are [1, 3, 4, 6, 6] (each cookie used at most once). What is the maximum number of satisfied children?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Sort both lists and match greedily: give the smallest cookie that satisfies the least greedy child still unmatched. Matching the largest cookies to the largest children (in arbitrary pairs) can waste a cookie on a child who would have accepted a smaller one.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In one move you may increase ALL but one element of the array [1, 2, 4, 7] by 1. What is the minimum number of moves to make all elements…
- Each move changes an element of [1, 2, 9, 10, 4, 4, 3] by exactly 1 (up or down). What is the minimum number of moves needed to make all…
- Ropes of lengths [4, 3, 2, 6] are joined two at a time, and joining costs the sum of their lengths. You may choose the order of joins…
- Vertical lines have heights [1, 8, 6, 2, 5, 4, 8, 3, 7, 9, 1, 6] at positions 0 to 11. Choose two lines; the water they hold is…
- What is the maximum possible sum of entries of a non-empty contiguous rectangular submatrix of [[1, -2, 3, 0], [-1, 4, -2, 2], [2, -3, 5…
- A fence has 5 posts and each post is painted one of k = 3 colours. No three consecutive posts may share the same colour. How many…