From 17dde0d098c5452933b383c5eacc6913cff062f5 Mon Sep 17 00:00:00 2001 From: Phuong Lam Date: Wed, 18 Nov 2020 20:01:50 -0500 Subject: [PATCH 1/3] Add solution for problem 1658 in javascript --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 375411ca04..15d6eaff9d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★ | # | Title | Solutions | Video | Difficulty | Tag |-----|----------------|---------------|--------|-------------|------------- +|1658|[Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|[Javascript](./javascript/_1658.js)||Medium|Greedy| |1657|[Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1657.java) ||Medium|Greedy| |1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design| |1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array| From 1e6f8c760641f758f2b4db17bf4f4bc9db474355 Mon Sep 17 00:00:00 2001 From: Phuong Lam Date: Thu, 19 Nov 2020 10:41:16 -0500 Subject: [PATCH 2/3] Add missing file --- javascript/_1658.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 javascript/_1658.js diff --git a/javascript/_1658.js b/javascript/_1658.js new file mode 100644 index 0000000000..d308fb73a0 --- /dev/null +++ b/javascript/_1658.js @@ -0,0 +1,25 @@ +// Author: Phuong Lam + +/** + * @param {number[]} nums + * @param {number} x + * @return {number} + */ +var minOperations = function (nums, x) { + const total = nums.reduce((a, b) => a + b) + if (total === x) return nums.length + + var sum = 0 + var head = 0 + var max = -1 + for (var tail = 0; tail < nums.length; tail++) { + sum += nums[tail] + while (sum > total - x) { + sum -= nums[head] + head++ + } + if (sum === total - x) max = Math.max(max, tail - head + 1) + } + + return max === -1 ? -1 : nums.length - max +} From 112c6ff7a709d6a23449ba684b3dcc5d40c06e1d Mon Sep 17 00:00:00 2001 From: Phuong Lam Date: Mon, 23 Nov 2020 07:44:46 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index 8d88c4079c..843461f141 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,9 @@ _If you like this project, please leave me a star._ ★ | # | Title | Solutions | Video | Difficulty | Tag |-----|----------------|---------------|--------|-------------|------------- -<<<<<<< HEAD -|1658|[Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|[Javascript](./javascript/_1658.js)||Medium|Greedy| -======= |1663|[Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1663.java) ||Medium|Greedy| |1662|[Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1662.java) ||Easy|String| ->>>>>>> 45495dbcdb410961f03610c8b23d13dbba6851d5 +|1658|[Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|[Javascript](./javascript/_1658.js)||Medium|Greedy| |1657|[Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1657.java) ||Medium|Greedy| |1656|[Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1656.java) ||Easy|Array, Design| |1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|