From 4aa8eba7350d02d50f9b4b75862ac33d459cbb9a Mon Sep 17 00:00:00 2001 From: yi-ge Date: Mon, 1 Jun 2020 05:14:21 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=8B=A5=E6=9C=89=E6=9C=80=E5=A4=9A?= =?UTF-8?q?=E7=B3=96=E6=9E=9C=E7=9A=84=E5=AD=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ src/array/kids-with-the-greatest-number-of-candies.js | 8 ++++++++ .../kids-with-the-greatest-number-of-candies.test.js | 7 +++++++ 3 files changed, 19 insertions(+) create mode 100644 src/array/kids-with-the-greatest-number-of-candies.js create mode 100644 test/array/kids-with-the-greatest-number-of-candies.test.js diff --git a/README.md b/README.md index b1bc0a2..aa044d8 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,10 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重 - LeetCode 198. 打家劫舍 - LintCode 392. 打劫房屋 +- [拥有最多糖果的孩子](src/array/kids-with-the-greatest-number-of-candies.js) + + - LeetCode 1431. 拥有最多糖果的孩子 + ## 栈 - [最大矩阵](src/stack/maximal-rectangle.js) diff --git a/src/array/kids-with-the-greatest-number-of-candies.js b/src/array/kids-with-the-greatest-number-of-candies.js new file mode 100644 index 0000000..98d8ad8 --- /dev/null +++ b/src/array/kids-with-the-greatest-number-of-candies.js @@ -0,0 +1,8 @@ +/** + * @param {number[]} candies + * @param {number} extraCandies + * @return {boolean[]} + */ +export const kidsWithCandies = (candies, extraCandies) => { + return candies.map(n => n + extraCandies >= Math.max(...candies)) +} diff --git a/test/array/kids-with-the-greatest-number-of-candies.test.js b/test/array/kids-with-the-greatest-number-of-candies.test.js new file mode 100644 index 0000000..949b781 --- /dev/null +++ b/test/array/kids-with-the-greatest-number-of-candies.test.js @@ -0,0 +1,7 @@ +import { kidsWithCandies } from '../../src/array/kids-with-the-greatest-number-of-candies' + +test('拥有最多糖果的孩子', () => { + expect(kidsWithCandies([2, 3, 5, 1, 3], 3)).toEqual([true, true, true, false, true]) + expect(kidsWithCandies([4, 2, 1, 1, 2], 1)).toEqual([true, false, false, false, false]) + expect(kidsWithCandies([12, 1, 12], 10)).toEqual([true, false, true]) +})