{"id":10061,"date":"2025-09-11T14:30:41","date_gmt":"2025-09-11T09:00:41","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10061"},"modified":"2025-09-11T15:07:29","modified_gmt":"2025-09-11T09:37:29","slug":"assign-cookies","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/assign-cookies\/","title":{"rendered":"\u00a0Assign Cookies"},"content":{"rendered":"\n<!-- Greedy Assign Cookies: 3 -->\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_theme {\n  --primary: #E58C32;\n  --secondary: #030302;\n  --light-bg: #fef9f4;\n  --text-dark: #2d2d2d;\n  --tab-radius: 12px;\n  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n  --code-bg: #001f3f;\n  --code-text: #d4f1ff;\n}\n\n.wp_blog_container {\n  font-family: 'Segoe UI', sans-serif;\n  background: var(--light-bg);\n  margin: 0;\n  padding: 0;\n  color: var(--text-dark);\n}\n\n\/* Heading *\/\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: var(--primary);\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n\/* Explanation Card *\/\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  background: white;\n  border-radius: var(--tab-radius);\n  box-shadow: var(--shadow);\n}\n\n\/* Text and Visuals *\/\n.wp_blog_explanation h2 {\n  font-size: 1.4rem;\n  color: var(--primary);\n  margin-bottom: 0.5rem;\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n}\n\n.wp_blog_explanation code {\n  background: #fef9f4;   \/* light bg instead of dark blue *\/\n  color: #E58C32;        \/* brand orange *\/\n  padding: 3px 6px;\n  border-radius: 4px;\n  font-family: 'Courier New', monospace;\n  font-weight: 600;      \/* optional, makes it pop *\/\n}\n\n.wp_blog_explanation img {\n  max-width: 100%;\n  border-radius: var(--tab-radius);\n  margin-top: 1rem;\n  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);\n}\n\n\/* Tab Buttons *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid var(--primary);\n  background: white;\n  color: var(--primary);\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  transition: all 0.3s ease;\n}\n\n.wp_blog_code-tab-button:hover {\n  background: var(--secondary);\n}\n\n.wp_blog_code-tab-button.active {\n  background: var(--primary);\n  color: white;\n}\n\n\/* Code Content *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n}\n\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  background: var(--code-bg);\n  border-radius: var(--tab-radius);\n  color: var(--code-text);\n}\n\n\/* Dark mode variables *\/\n.wp_blog_theme.dark-mode {\n  --light-bg: #121212;\n  --text-dark: #f5f5f5;\n  --shadow: 0 4px 12px rgba(255, 255, 255, 0.08);\n  --code-bg: #1e1e1e;\n  --code-text: #c5f0ff;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_explanation {\n  background: #1e1e1e;\n}\n\n\/* Dark mode code highlight *\/\n.wp_blog_theme.dark-mode .wp_blog_explanation code {\n  background: #333;\n  color: #ffd27f;\n}\n\n.wp_blog_theme {\n  position: relative; \/* makes it the reference for absolute children *\/\n}\n\n.wp_blog_toggle-btn {\n  position: absolute;\n  top: 1rem;\n  right: 1rem;\n  z-index: 9999;\n  padding: 0.5rem 0.8rem;\n  border-radius: 10%;\n  background: var(--primary);\n  color: white;\n  font-weight: bold;\n  cursor: pointer;\n  border: none;\n  box-shadow: var(--shadow);\n  transition: background 0.3s, transform 0.2s;\n}\n\n.wp_blog_toggle-btn:hover {\n  background: #cc772e;\n}\n\n.wp_blog_theme.dark-mode .wp_blog_code-tabs-container {\n  background: #1e1e1e;\n}\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\">\n  <button id=\"blogNotesThemeToggle\" class=\"wp_blog_toggle-btn\">\ud83c\udf19<\/button>\n  <h1 class=\"wp_blog_main-heading\"><\/h1>\n\n  <div class=\"wp_blog_explanation\">\n    <h2>Problem Statement:<\/h2>\n    <p>\n        Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.\n    <\/p>\n\n    <p>\n        Each child <code>i<\/code> has a greed factor <code>g[i]<\/code>, which is the minimum size of a cookie that the child will be content with; and each cookie <code>j<\/code> has a size s[j]. If <code>s[j] >= g[i]<\/code>, we can assign the cookie <code>j<\/code> to the child <code>i<\/code>, and the child <code>i<\/code> will be content. Your goal is to maximize the number of your content children and output the maximum number.\n    <\/p>\n\n\n    <h3>Example 1:<\/h2>\n    <p><strong>Input:<\/strong> g = [1,2,3], s = [1,1]\n<\/p>\n    <p><strong>Output:<\/strong> 1<\/p>\n    <p><strong>Explanation:<\/strong>\n        You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. \n            And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.\n            You need to output 1.\n    <\/p>\n\n    <h3>Example 2:<\/h3>\n    <p><strong>Input: <\/strong>g = [1,2], s = [1,2,3]<\/p>\n    <p><strong>Output: <\/strong>2<\/p>\n    <p><strong>Explanation: <\/strong>\n    You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. \n    You have 3 cookies and their sizes are big enough to gratify all of the children, \n    You need to output 2.\n    <\/p>\n\n\n    <h3>Constraints<\/h3>\n    <ul>\n        <li><code>1 <= g.length <= 3 * 10<sup>4<\/sup><\/code><\/li>\n        <li><code>0 <= s.length <= 3 * 10<sup>4<\/sup><\/code><\/li>\n        <li><code>1 <= g[i], s[j] <= 2<sup>31<\/sup> - 1<\/code><\/li>\n    <\/ul>\n<h2>Approach:<\/h2>\n    <ul>\n        <li><strong>Sort<\/strong> both arrays <code>g<\/code> and <code>s<\/code> in <strong>ascending order<\/strong>.<\/li>\n        <li><strong>Use two pointers<\/strong>\n            <ul>\n                <li><code>i<\/code> \u2192 tracks children<\/li>\n                <li><code>j<\/code> \u2192 tracks cookies<\/li>\n            <\/ul>\n        <\/li>\n        <li><strong>Traverse<\/strong> through cookies (<code>s<\/code>):\n            <ul>\n                <li>If current cookie satisfies the child\u2019s greed (<code>s[j] >= g[i]<\/code>), assign it \u2192 move to next child (<code>i++<\/code>).<\/li>\n                <li>Always move to <strong>next<\/strong> cookie <code>(j++)<\/code>.<\/li>\n            <\/ul>\n        <\/li>\n        <li><strong>Return<\/strong> <code>i<\/code> \u2192 number of children satisfied.<\/li>\n    <\/ul>\n\n    <h2>Time &#038; Space Complexity:<\/h2>\n    <p><strong>Time Complexity:<\/strong> <code>O(n log n + m log m)<\/code><\/p>\n    <p><strong>Space Complexity:<\/strong> <code>O(1)<\/code><\/p>\n\n    \n<h2>Dry Run<\/h2>\n<div style=\"background: var(--light-bg); border-left: 4px solid var(--primary); padding: 1rem; border-radius: var(--tab-radius); margin: 1rem 0; color: var(--text-dark);\">\n  <p><strong>Input:<\/strong> <code>g = [1,2,3], s = [1,1]<\/code><\/p>\n  <pre style=\"white-space: pre-wrap; background: var(--code-bg); padding: 1rem; border-radius: 8px; overflow-x: auto; color: var(--code-text);\">\n\nStep 1: Sort both arrays\ng = [1, 2, 3]\ns = [1, 1]\n\nStep 2: Initialize\ni = 0 (index for g \u2192 children)\nj = 0 (index for s \u2192 cookies)\n\nStep 3: Start while loop (j < s.length)\nj = 0 \u2192 s[0] = 1, g[0] = 1\nSince 1 >= 1 \u2192 child gets cookie \u2192 i = 1\nj = 1 \u2192 s[1] = 1, g[1] = 2\nSince 1 < 2 \u2192 child not satisfied\nIncrement j = 2 (loop ends)\n\nStep 4: End\ni = 1 (number of content children)\n\n  <\/pre>\n  <p><strong>Output:<\/strong> <code>Result: 1<\/code><\/p>\n<\/div>\n\n\n<\/div>\n\n  <div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\nvar findContentChildren = function(g, s) {\n    s.sort((a, b) => a - b);\n    g.sort((a, b) => a - b);\n\n    let i = 0; \/\/ child index\n    let j = 0; \/\/ cookie index\n\n    while (i < g.length &#038;&#038; j < s.length) {\n        if (s[j] >= g[i]) {\n            ++i;\n            ++j;\n        } else {\n            ++j;\n        }\n    }\n    return i;\n};\n      <\/code><\/pre>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\ndef findContentChildren(g, s):\n    g.sort()\n    s.sort()\n    i = 0  \n    j = 0  \n    while i < len(g) and j < len(s):\n        if s[j] >= g[i]:\n            i += 1\n            j += 1\n        else:\n            j += 1\n    return i\n      <\/code><\/pre>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\nimport java.util.Arrays;\npublic class Solution {\n    public int findContentChildren(int[] g, int[] s) {\n        Arrays.sort(g);\n        Arrays.sort(s);\n        int i = 0; \n        int j = 0;\n\n        while (i < g.length &#038;&#038; j < s.length) {\n            if (s[j] >= g[i]) {\n                i++;\n                j++;\n            } else {\n                j++;\n            }\n        }\n        return i;\n    }\n}\n    <\/code><\/pre>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\nint findContentChildren(std::vector<int>& g, std::vector<int>& s) {\n    std::sort(g.begin(), g.end());\n    std::sort(s.begin(), s.end());\n    int i = 0; \n    int j = 0; \n\n    while (i < (int)g.size() &#038;&#038; j < (int)s.size()) {\n        if (s[j] >= g[i]) {\n            ++i;\n            ++j;\n        } else {\n            ++j;\n        }\n    }\n    return i;\n}\n<\/code><\/pre>\n    <\/div>\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n#include &lt;stdlib.h&gt;\nint cmp_int(const void *a, const void *b) {\n    int ia = *(const int*)a;\n    int ib = *(const int*)b;\n    return (ia > ib) - (ia < ib);\n}\nint findContentChildren(int *g, int gSize, int *s, int sSize) {\n    qsort(g, gSize, sizeof(int), cmp_int);\n    qsort(s, sSize, sizeof(int), cmp_int);\n    int i = 0; \n    int j = 0; \n\n    while (i < gSize &#038;&#038; j < sSize) {\n        if (s[j] >= g[i]) {\n            i++;\n            j++;\n        } else {\n            j++;\n        }\n    }\n    return i;\n}     <\/code><\/pre>\n    <\/div>\n\n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n      <pre><code class=\"language-csharp\">\nusing System;\npublic class Solution {\n    public int FindContentChildren(int[] g, int[] s) {\n        Array.Sort(g);\n        Array.Sort(s);\n\n        int i = 0; \n        int j = 0; \n        while (i < g.Length &#038;&#038; j < s.Length) {\n            if (s[j] >= g[i]) {\n                i++;\n                j++;\n            } else {\n                j++;\n            }\n        }\n        return i;\n    }\n}\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n<\/div>\n\n<script>\ndocument.addEventListener(\"DOMContentLoaded\", () => {\n  const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n  const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n  buttons.forEach((button) => {\n    button.addEventListener(\"click\", () => {\n      const lang = button.getAttribute(\"data-lang\");\n\n      buttons.forEach((btn) => btn.classList.remove(\"active\"));\n      contents.forEach((content) => content.classList.remove(\"active\"));\n\n      button.classList.add(\"active\");\n      document\n        .querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`)\n        .classList.add(\"active\");\n    });\n  });\n\n  const themeToggle = document.getElementById(\"blogNotesThemeToggle\");\n  const themeContainer = document.querySelector(\".wp_blog_theme\");\n\n  themeToggle.addEventListener(\"click\", () => {\n    themeContainer.classList.toggle(\"dark-mode\");\n    themeToggle.textContent =\n      themeContainer.classList.contains(\"dark-mode\") ? \"\u2600\ufe0f\" : \"\ud83c\udf19\";\n  });\n});\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udf19 Problem Statement: Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has<\/p>\n","protected":false},"author":108,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[322,260,176,175,211,811,810,174,172,173],"tags":[],"class_list":["post-10061","post","type-post","status-publish","format-standard","category-algorithms-and-data-structures","category-c-c-plus-plus","category-csharp","category-cplusplus","category-data-structures","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10061"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10061\/revisions"}],"predecessor-version":[{"id":10062,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10061\/revisions\/10062"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}