From a0ec2d53806c72320729504691323432b22e0eb8 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 6 Sep 2021 12:49:13 +0800 Subject: [PATCH] =?UTF-8?q?455:=E5=88=86=E5=8F=91=E9=A5=BC=E5=B9=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/AssignCookies.java | 74 +++++++++++++++++++ .../editor/cn/doc/content/AssignCookies.md | 37 ++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/AssignCookies.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/AssignCookies.md diff --git a/src/main/java/leetcode/editor/cn/AssignCookies.java b/src/main/java/leetcode/editor/cn/AssignCookies.java new file mode 100644 index 0000000..04052b5 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/AssignCookies.java @@ -0,0 +1,74 @@ +//假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。 +// +// 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[ +//i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。 +// +// +// 示例 1: +// +// +//输入: g = [1,2,3], s = [1,1] +//输出: 1 +//解释: +//你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。 +//虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。 +//所以你应该输出1。 +// +// +// 示例 2: +// +// +//输入: g = [1,2], s = [1,2,3] +//输出: 2 +//解释: +//你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。 +//你拥有的饼干数量和尺寸都足以让所有孩子满足。 +//所以你应该输出2. +// +// +// +// +// 提示: +// +// +// 1 <= g.length <= 3 * 10⁴ +// 0 <= s.length <= 3 * 10⁴ +// 1 <= g[i], s[j] <= 2³¹ - 1 +// +// Related Topics 贪心 数组 排序 👍 375 👎 0 + +package leetcode.editor.cn; + +import java.util.Arrays; + +//455:分发饼干 +class AssignCookies { + public static void main(String[] args) { + //测试代码 + Solution solution = new AssignCookies().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int findContentChildren(int[] g, int[] s) { + Arrays.sort(g); + Arrays.sort(s); + int count = 0; + int gi = 0; + int si = 0; + while (gi < g.length && si < s.length) { + if(s[si]>=g[gi]){ + count++; + si++; + gi++; + }else{ + si++; + } + } + return count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/AssignCookies.md b/src/main/java/leetcode/editor/cn/doc/content/AssignCookies.md new file mode 100644 index 0000000..5ce5f96 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/AssignCookies.md @@ -0,0 +1,37 @@ +

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。

+ +

对每个孩子 i,都有一个胃口值 g[i]这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

+  + +

示例 1:

+ +
+输入: g = [1,2,3], s = [1,1]
+输出: 1
+解释: 
+你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
+虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
+所以你应该输出1。
+
+ +

示例 2:

+ +
+输入: g = [1,2], s = [1,2,3]
+输出: 2
+解释: 
+你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
+你拥有的饼干数量和尺寸都足以让所有孩子满足。
+所以你应该输出2.
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 贪心
  • 数组
  • 排序

  • 👍 375
  • 👎 0
  • \ No newline at end of file