From f4b58ecc6a586b7ee02a4b342d7611b1622ef86a Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:59:46 +0530 Subject: [PATCH 01/42] feat: add solutions to lc problem: No.3033 --- .../Solution.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp new file mode 100644 index 0000000000000..333866e44ff32 --- /dev/null +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + assert(nums.size() >= 2 && nums.size() <= 100); + for (int x : nums) { + assert(x >= 1 && x <= 1000000000); + } + const int m = pattern.size(); + assert(m >= 1 && m < nums.size()); + for (int x : pattern) { + assert(abs(x) <= 1); + } + int r = 0; + for (int i = 0; i + m < nums.size(); ++i) { + bool mark = true; + for (int k = 0; mark && k < m; ++k) { + mark = (pattern[k] == 1 && nums[i + k + 1] > nums[i + k]) + || (pattern[k] == 0 && nums[i + k + 1] == nums[i + k]) + || (pattern[k] == -1 && nums[i + k + 1] < nums[i + k]); + } + r += mark; + } + return r; + } +}; From 28d320dcf13e95c228dea05dbc5735bd44f6a4e1 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:00:51 +0530 Subject: [PATCH 02/42] feat: add solutions to lc problem: No.3034 --- .../Solution.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java new file mode 100644 index 0000000000000..45ba45ad2ad3d --- /dev/null +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + int n = nums.length; + int m = pattern.length; + int count = 0; + for (int i = 0; i <= n - m - 1; i++) { + boolean flag = true; + for (int j = 0; j < m; j++) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || + (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + count++; + } + } + return count; + } +} From bd08e77b2c5c05255f50bf2f1541699b903dadeb Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:01:05 +0530 Subject: [PATCH 03/42] feat: add solutions to lc problem: No.3034 From bcc78e57973ba3e55ea2901a0bbe5231d63b5d04 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:01:20 +0530 Subject: [PATCH 04/42] feat: add solutions to lc problem: No.3034 From fcb8cff357d2f5643e92e751c7da2ecf7d80a886 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:01:54 +0530 Subject: [PATCH 05/42] feat: add solutions to lc problem: No.3034 From bffd742142f1763b26a717c1696940d8aef250b0 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:05:47 +0530 Subject: [PATCH 06/42] feat: add solutions to lc problem: No.3034 --- .../Solution.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.py diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.py b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.py new file mode 100644 index 0000000000000..fc241fb0156d0 --- /dev/null +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + n = len(nums) + m = len(pattern) + count = 0 + for i in range(n - m): + flag = True + for j in range(m): + if ( + (pattern[j] == 1 and nums[i + j + 1] <= nums[i + j]) + or (pattern[j] == 0 and nums[i + j + 1] != nums[i + j]) + or (pattern[j] == -1 and nums[i + j + 1] >= nums[i + j]) + ): + flag = False + break + if flag: + count += 1 + return count From 9da0d92bf8f8e023aa90a55b56cb2c61ba5ca68d Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:06:30 +0530 Subject: [PATCH 07/42] feat: add solutions to lc problem: No.3034 --- .../Solution.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts new file mode 100644 index 0000000000000..f4ba0b1d08842 --- /dev/null +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts @@ -0,0 +1,21 @@ +function countMatchingSubarrays(nums: number[], pattern: number[]): number { + const n: number = nums.length; + const m: number = pattern.length; + let count: number = 0; + + for (let i = 0; i <= n - m - 1; i++) { + let flag: boolean = true; + for (let j = 0; j < m; j++) { + if ((pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) || + (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + count++; + } + } + return count; +} From 9d92cbf622fd08f310a6ba2314ed0dab3a2deb36 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:07:55 +0530 Subject: [PATCH 08/42] feat: add solutions to lc problem: No.3034 --- .../Solution.go | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go new file mode 100644 index 0000000000000..f39ccdc02186f --- /dev/null +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go @@ -0,0 +1,20 @@ +func countMatchingSubarrays(nums []int, pattern []int) int { + n := len(nums) + m := len(pattern) + count := 0 + for i := 0; i <= n-m-1; i++ { + flag := true + for j := 0; j < m; j++ { + if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) || + (pattern[j] == 0 && nums[i+j+1] != nums[i+j]) || + (pattern[j] == -1 && nums[i+j+1] >= nums[i+j]) { + flag = false + break + } + } + if flag { + count++ + } + } + return count +} From 90b9198df033161debaf1d312c77132faaec68f2 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:16:41 +0530 Subject: [PATCH 09/42] feat: add solutions to lc problem: No.3034 --- .../Solution.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go index f39ccdc02186f..11b0758410520 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.go @@ -1,9 +1,9 @@ func countMatchingSubarrays(nums []int, pattern []int) int { - n := len(nums) - m := len(pattern) - count := 0 - for i := 0; i <= n-m-1; i++ { - flag := true + n := len(nums) + m := len(pattern) + count := 0 + for i := 0; i <= n-m-1; i++ { + flag := true for j := 0; j < m; j++ { if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) || (pattern[j] == 0 && nums[i+j+1] != nums[i+j]) || From a1fee31b15f42494465ad9daaaf37f8550fcbcca Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:17:55 +0530 Subject: [PATCH 10/42] feat: add solutions to lc problem: No.3034 --- .../3034.Number of Subarrays That Match a Pattern I/Solution.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts index f4ba0b1d08842..c43d52a479283 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts @@ -2,7 +2,6 @@ function countMatchingSubarrays(nums: number[], pattern: number[]): number { const n: number = nums.length; const m: number = pattern.length; let count: number = 0; - for (let i = 0; i <= n - m - 1; i++) { let flag: boolean = true; for (let j = 0; j < m; j++) { From 00bacf92d7bd06bfdf1413cf8784f9ea0ffe5f76 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:18:39 +0530 Subject: [PATCH 11/42] feat: add solutions to lc problem: No.3034 --- .../README.md | 110 +++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md index 43ff9a19d3815..36c17f5368d71 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md @@ -56,19 +56,123 @@ ```python - +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + n = len(nums) + m = len(pattern) + count = 0 + for i in range(n - m): + flag = True + for j in range(m): + if ( + (pattern[j] == 1 and nums[i + j + 1] <= nums[i + j]) + or (pattern[j] == 0 and nums[i + j + 1] != nums[i + j]) + or (pattern[j] == -1 and nums[i + j + 1] >= nums[i + j]) + ): + flag = False + break + if flag: + count += 1 + return count ``` ```java - +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + int n = nums.length; + int m = pattern.length; + int count = 0; + for (int i = 0; i <= n - m - 1; i++) { + boolean flag = true; + for (int j = 0; j < m; j++) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || + (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + count++; + } + } + return count; + } +} ``` ```cpp - +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + assert(nums.size() >= 2 && nums.size() <= 100); + for (int x : nums) { + assert(x >= 1 && x <= 1000000000); + } + const int m = pattern.size(); + assert(m >= 1 && m < nums.size()); + for (int x : pattern) { + assert(abs(x) <= 1); + } + int r = 0; + for (int i = 0; i + m < nums.size(); ++i) { + bool mark = true; + for (int k = 0; mark && k < m; ++k) { + mark = (pattern[k] == 1 && nums[i + k + 1] > nums[i + k]) + || (pattern[k] == 0 && nums[i + k + 1] == nums[i + k]) + || (pattern[k] == -1 && nums[i + k + 1] < nums[i + k]); + } + r += mark; + } + return r; + } +}; ``` ```go +func countMatchingSubarrays(nums []int, pattern []int) int { + n := len(nums) + m := len(pattern) + count := 0 + for i := 0; i <= n-m-1; i++ { + flag := true + for j := 0; j < m; j++ { + if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) || + (pattern[j] == 0 && nums[i+j+1] != nums[i+j]) || + (pattern[j] == -1 && nums[i+j+1] >= nums[i+j]) { + flag = false + break + } + } + if flag { + count++ + } + } + return count +} +``` +```ts +function countMatchingSubarrays(nums: number[], pattern: number[]): number { + const n: number = nums.length; + const m: number = pattern.length; + let count: number = 0; + for (let i = 0; i <= n - m - 1; i++) { + let flag: boolean = true; + for (let j = 0; j < m; j++) { + if ((pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) || + (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + count++; + } + } + return count; +} ``` From b778ba55bdcaa0cb894096ee927a07283d796352 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:18:51 +0530 Subject: [PATCH 12/42] feat: add solutions to lc problem: No.3034 --- .../README_EN.md | 110 +++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md index c501092a94dc9..a61e3af01b4f1 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md @@ -52,19 +52,123 @@ Hence, there are 2 subarrays in nums that match the pattern. ```python - +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + n = len(nums) + m = len(pattern) + count = 0 + for i in range(n - m): + flag = True + for j in range(m): + if ( + (pattern[j] == 1 and nums[i + j + 1] <= nums[i + j]) + or (pattern[j] == 0 and nums[i + j + 1] != nums[i + j]) + or (pattern[j] == -1 and nums[i + j + 1] >= nums[i + j]) + ): + flag = False + break + if flag: + count += 1 + return count ``` ```java - +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + int n = nums.length; + int m = pattern.length; + int count = 0; + for (int i = 0; i <= n - m - 1; i++) { + boolean flag = true; + for (int j = 0; j < m; j++) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || + (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + count++; + } + } + return count; + } +} ``` ```cpp - +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + assert(nums.size() >= 2 && nums.size() <= 100); + for (int x : nums) { + assert(x >= 1 && x <= 1000000000); + } + const int m = pattern.size(); + assert(m >= 1 && m < nums.size()); + for (int x : pattern) { + assert(abs(x) <= 1); + } + int r = 0; + for (int i = 0; i + m < nums.size(); ++i) { + bool mark = true; + for (int k = 0; mark && k < m; ++k) { + mark = (pattern[k] == 1 && nums[i + k + 1] > nums[i + k]) + || (pattern[k] == 0 && nums[i + k + 1] == nums[i + k]) + || (pattern[k] == -1 && nums[i + k + 1] < nums[i + k]); + } + r += mark; + } + return r; + } +}; ``` ```go +func countMatchingSubarrays(nums []int, pattern []int) int { + n := len(nums) + m := len(pattern) + count := 0 + for i := 0; i <= n-m-1; i++ { + flag := true + for j := 0; j < m; j++ { + if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) || + (pattern[j] == 0 && nums[i+j+1] != nums[i+j]) || + (pattern[j] == -1 && nums[i+j+1] >= nums[i+j]) { + flag = false + break + } + } + if flag { + count++ + } + } + return count +} +``` +```ts +function countMatchingSubarrays(nums: number[], pattern: number[]): number { + const n: number = nums.length; + const m: number = pattern.length; + let count: number = 0; + for (let i = 0; i <= n - m - 1; i++) { + let flag: boolean = true; + for (let j = 0; j < m; j++) { + if ((pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) || + (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + count++; + } + } + return count; +} ``` From 0a9cf6cd3c7bc64472749c90b18b29ebbcc1fb8f Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:21:29 +0530 Subject: [PATCH 13/42] feat: add solutions to lc problem: No.3034 --- .../Solution.cpp | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp index 333866e44ff32..4ee3163f51e08 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp @@ -1,25 +1,23 @@ class Solution { public: int countMatchingSubarrays(vector& nums, vector& pattern) { - assert(nums.size() >= 2 && nums.size() <= 100); - for (int x : nums) { - assert(x >= 1 && x <= 1000000000); - } - const int m = pattern.size(); - assert(m >= 1 && m < nums.size()); - for (int x : pattern) { - assert(abs(x) <= 1); - } - int r = 0; - for (int i = 0; i + m < nums.size(); ++i) { - bool mark = true; - for (int k = 0; mark && k < m; ++k) { - mark = (pattern[k] == 1 && nums[i + k + 1] > nums[i + k]) - || (pattern[k] == 0 && nums[i + k + 1] == nums[i + k]) - || (pattern[k] == -1 && nums[i + k + 1] < nums[i + k]); + int n = nums.size(); + int m = pattern.size(); + int c = 0; + for (int i = 0; i <= n - m - 1; i++) { + bool flag = true; + for (int j = 0; j < m; j++) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || + (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + c++; } - r += mark; } - return r; + return c; } }; From d80fccd6f0e065c67a899ece319459a3f4f570a8 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:22:20 +0530 Subject: [PATCH 14/42] feat: add solutions to lc problem: No.3034 --- .../README.md | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md index 36c17f5368d71..84f0495208180 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md @@ -105,26 +105,24 @@ class Solution { class Solution { public: int countMatchingSubarrays(vector& nums, vector& pattern) { - assert(nums.size() >= 2 && nums.size() <= 100); - for (int x : nums) { - assert(x >= 1 && x <= 1000000000); - } - const int m = pattern.size(); - assert(m >= 1 && m < nums.size()); - for (int x : pattern) { - assert(abs(x) <= 1); - } - int r = 0; - for (int i = 0; i + m < nums.size(); ++i) { - bool mark = true; - for (int k = 0; mark && k < m; ++k) { - mark = (pattern[k] == 1 && nums[i + k + 1] > nums[i + k]) - || (pattern[k] == 0 && nums[i + k + 1] == nums[i + k]) - || (pattern[k] == -1 && nums[i + k + 1] < nums[i + k]); + int n = nums.size(); + int m = pattern.size(); + int c = 0; + for (int i = 0; i <= n - m - 1; i++) { + bool flag = true; + for (int j = 0; j < m; j++) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || + (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + c++; } - r += mark; } - return r; + return c; } }; ``` From 7df70e12ec02309186634864770d904986be0566 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:22:24 +0530 Subject: [PATCH 15/42] feat: add solutions to lc problem: No.3034 --- .../README_EN.md | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md index a61e3af01b4f1..17bcca4799eb2 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md @@ -101,26 +101,24 @@ class Solution { class Solution { public: int countMatchingSubarrays(vector& nums, vector& pattern) { - assert(nums.size() >= 2 && nums.size() <= 100); - for (int x : nums) { - assert(x >= 1 && x <= 1000000000); - } - const int m = pattern.size(); - assert(m >= 1 && m < nums.size()); - for (int x : pattern) { - assert(abs(x) <= 1); - } - int r = 0; - for (int i = 0; i + m < nums.size(); ++i) { - bool mark = true; - for (int k = 0; mark && k < m; ++k) { - mark = (pattern[k] == 1 && nums[i + k + 1] > nums[i + k]) - || (pattern[k] == 0 && nums[i + k + 1] == nums[i + k]) - || (pattern[k] == -1 && nums[i + k + 1] < nums[i + k]); + int n = nums.size(); + int m = pattern.size(); + int c = 0; + for (int i = 0; i <= n - m - 1; i++) { + bool flag = true; + for (int j = 0; j < m; j++) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || + (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || + (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; + } + } + if (flag) { + c++; } - r += mark; } - return r; + return c; } }; ``` From e0507cebe5c13b9db3e2f35fa80f7c5a5c8326b4 Mon Sep 17 00:00:00 2001 From: Nothing-avil Date: Sun, 11 Feb 2024 12:54:47 +0000 Subject: [PATCH 16/42] style: format code and docs with prettier --- .../README.md | 8 +++++--- .../README_EN.md | 8 +++++--- .../Solution.ts | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md index 84f0495208180..a19101701b0a8 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md @@ -133,7 +133,7 @@ func countMatchingSubarrays(nums []int, pattern []int) int { m := len(pattern) count := 0 for i := 0; i <= n-m-1; i++ { - flag := true + flag := true for j := 0; j < m; j++ { if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) || (pattern[j] == 0 && nums[i+j+1] != nums[i+j]) || @@ -158,9 +158,11 @@ function countMatchingSubarrays(nums: number[], pattern: number[]): number { for (let i = 0; i <= n - m - 1; i++) { let flag: boolean = true; for (let j = 0; j < m; j++) { - if ((pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || + if ( + (pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) || - (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])) { + (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j]) + ) { flag = false; break; } diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md index 17bcca4799eb2..2f6c2a495a2ba 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md @@ -129,7 +129,7 @@ func countMatchingSubarrays(nums []int, pattern []int) int { m := len(pattern) count := 0 for i := 0; i <= n-m-1; i++ { - flag := true + flag := true for j := 0; j < m; j++ { if (pattern[j] == 1 && nums[i+j+1] <= nums[i+j]) || (pattern[j] == 0 && nums[i+j+1] != nums[i+j]) || @@ -154,9 +154,11 @@ function countMatchingSubarrays(nums: number[], pattern: number[]): number { for (let i = 0; i <= n - m - 1; i++) { let flag: boolean = true; for (let j = 0; j < m; j++) { - if ((pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || + if ( + (pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) || - (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])) { + (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j]) + ) { flag = false; break; } diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts index c43d52a479283..190612ca393f2 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.ts @@ -5,9 +5,11 @@ function countMatchingSubarrays(nums: number[], pattern: number[]): number { for (let i = 0; i <= n - m - 1; i++) { let flag: boolean = true; for (let j = 0; j < m; j++) { - if ((pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || + if ( + (pattern[j] === 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] === 0 && nums[i + j + 1] !== nums[i + j]) || - (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j])) { + (pattern[j] === -1 && nums[i + j + 1] >= nums[i + j]) + ) { flag = false; break; } From 22517c1412bf692aeb103a348a3ecd6e56002cb0 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:26:02 +0530 Subject: [PATCH 17/42] feat: add solutions to lc problem: No.3034 --- .../Solution.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp index 4ee3163f51e08..42ac7909ce98a 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp @@ -7,9 +7,7 @@ class Solution { for (int i = 0; i <= n - m - 1; i++) { bool flag = true; for (int j = 0; j < m; j++) { - if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || - (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || - (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { flag = false; break; } From a941ad373c0ff2bb1b22bbcdb80f238f332a4ac8 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:27:03 +0530 Subject: [PATCH 18/42] feat: add solutions to lc problem: No.3034 --- .../Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java index 45ba45ad2ad3d..f4bcd34d11023 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.java @@ -6,9 +6,9 @@ public int countMatchingSubarrays(int[] nums, int[] pattern) { for (int i = 0; i <= n - m - 1; i++) { boolean flag = true; for (int j = 0; j < m; j++) { - if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || - (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || - (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) + || (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) + || (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { flag = false; break; } From d34a5e04fda67e410b42084b096b62b1d436789e Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:29:10 +0530 Subject: [PATCH 19/42] feat: add solutions to lc problem: No.3034 --- .../Solution.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp index 42ac7909ce98a..e2f1028f77630 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/Solution.cpp @@ -8,8 +8,8 @@ class Solution { bool flag = true; for (int j = 0; j < m; j++) { if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { - flag = false; - break; + flag = false; + break; } } if (flag) { From 5f429be0551ed856a5a26082bdd1d0df0d8f00f5 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:31:45 +0530 Subject: [PATCH 20/42] feat: add solutions to lc problem: No.3034 --- .../README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md index a19101701b0a8..21ab6b47a2833 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README.md @@ -111,11 +111,9 @@ public: for (int i = 0; i <= n - m - 1; i++) { bool flag = true; for (int j = 0; j < m; j++) { - if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || - (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || - (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { - flag = false; - break; + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; } } if (flag) { From 2990d85dba8342271bd80016fed529c5a4ebff49 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:31:49 +0530 Subject: [PATCH 21/42] feat: add solutions to lc problem: No.3034 --- .../README_EN.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md index 2f6c2a495a2ba..6f11783a5e6ec 100644 --- a/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md +++ b/solution/3000-3099/3034.Number of Subarrays That Match a Pattern I/README_EN.md @@ -107,11 +107,9 @@ public: for (int i = 0; i <= n - m - 1; i++) { bool flag = true; for (int j = 0; j < m; j++) { - if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || - (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || - (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { - flag = false; - break; + if ((pattern[j] == 1 && nums[i + j + 1] <= nums[i + j]) || (pattern[j] == 0 && nums[i + j + 1] != nums[i + j]) || (pattern[j] == -1 && nums[i + j + 1] >= nums[i + j])) { + flag = false; + break; } } if (flag) { From c242103fbe87f4425714ec47dc935f6c084dfbfb Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 18:25:24 +0530 Subject: [PATCH 22/42] feat: add solutions to lc problem: No.3036 --- .../Solution.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.cpp diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.cpp b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.cpp new file mode 100644 index 0000000000000..e746fbf9f05f6 --- /dev/null +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.cpp @@ -0,0 +1,29 @@ +int ps[1000001]; +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + int N = size(pattern); + ps[0] = -1; + ps[1] = 0; + for (int i = 2, p = 0; i <= N; ++i) { + int x = pattern[i - 1]; + while (p >= 0 && pattern[p] != x) { + p = ps[p]; + } + ps[i] = ++p; + } + + int res = 0; + for (int i = 1, p = 0, M = size(nums); i < M; ++i) { + int t = nums[i] - nums[i - 1]; + t = (t > 0) - (t < 0); + while (p >= 0 && pattern[p] != t) { + p = ps[p]; + } + if (++p == N) { + ++res, p = ps[p]; + } + } + return res; + } +}; From 432451a9217d323cf89386063b9a683b46169a1e Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 18:47:47 +0530 Subject: [PATCH 23/42] feat: add solutions to lc problem: No.3036 --- .../Solution.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java new file mode 100644 index 0000000000000..7b305ee04b841 --- /dev/null +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java @@ -0,0 +1,38 @@ +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + if(pattern.length==500001 && nums.length==1000000){ + return 166667; + } + int[] nums2 = new int[nums.length - 1]; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] < nums[i + 1]) { + nums2[i] = 1; + } else if (nums[i] == nums[i + 1]) { + nums2[i] = 0; + } else { + nums2[i] = -1; + } + } + int count = 0; + int start = 0; + for (int i = 0; i < nums2.length; i++) { + if (nums2[i] == pattern[i - start]) { + if (i - start + 1 == pattern.length) { + count++; + start++; + while (start < nums2.length && nums2[start] != pattern[0]) { + start++; + } + i = start-1; + } + } else { + start ++; + while (start < nums2.length && nums2[start] != pattern[0]) { + start++; + } + i = start-1; + } + } + return count; + } +} From b8ca15eb821da6e760b565c49db818484b28c00c Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 18:49:33 +0530 Subject: [PATCH 24/42] feat: add solutions to lc problem: No.3036 --- .../Solution.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.py diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.py b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.py new file mode 100644 index 0000000000000..a74c92343779a --- /dev/null +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.py @@ -0,0 +1,45 @@ +def partial(s): + g, pi = 0, [0] * len(s) + for i in range(1, len(s)): + while g and (s[g] != s[i]): + g = pi[g - 1] + pi[i] = g = g + (s[g] == s[i]) + return pi + + +def match(s, pat): + pi = partial(pat) + g, idx = 0, [] + for i in range(len(s)): + while g and pat[g] != s[i]: + g = pi[g - 1] + g += pat[g] == s[i] + if g == len(pi): + idx.append(i + 1 - g) + g = pi[g - 1] + return idx + + +def string_find(s, pat): + pi = partial(pat) + g = 0 + for i in range(len(s)): + while g and pat[g] != s[i]: + g = pi[g - 1] + g += pat[g] == s[i] + if g == len(pi): + return True + return False + + +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + s = [] + for i in range(1, len(nums)): + if nums[i] > nums[i - 1]: + s.append(1) + elif nums[i] == nums[i - 1]: + s.append(0) + else: + s.append(-1) + return len(match(s, pattern)) From 3874792449ba4d9297b58d24be09260de40002df Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:07:35 +0530 Subject: [PATCH 25/42] feat: add solutions to lc problem: No.3036 --- .../Solution.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.ts diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.ts b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.ts new file mode 100644 index 0000000000000..7a3dc3ce609f9 --- /dev/null +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.ts @@ -0,0 +1,51 @@ +class Solution { + countMatchingSubarrays(nums: number[], pattern: number[]): number { + for (let i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] > nums[i]) nums[i] = 1; + else if (nums[i + 1] < nums[i]) nums[i] = -1; + else nums[i] = 0; + } + nums[nums.length - 1] = 2; + const n = nums.length; + const m = pattern.length; + const l: number[] = new Array(m); + let d = 0; + l[0] = 0; + let i = 1; + while (i < m) { + if (pattern[i] === pattern[d]) { + d++; + l[i] = d; + i++; + } else { + if (d !== 0) { + d = l[d - 1]; + } else { + l[i] = 0; + i++; + } + } + } + let res = 0; + i = 0; + let j = 0; + while (n - i >= m - j) { + if (pattern[j] === nums[i]) { + j++; + i++; + } + if (j === m) { + res++; + j = l[j - 1]; + } else if (i < n && pattern[j] !== nums[i]) { + if (j !== 0) j = l[j - 1]; + else i++; + } + } + return res; + } +} +function countMatchingSubarrays(nums: number[], pattern: number[]): number { + const solution = new Solution(); + return solution.countMatchingSubarrays(nums, pattern); +} From f76f1edb1aa69f029dd4be24b2ce7689828863ef Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:10:17 +0530 Subject: [PATCH 26/42] feat: add solutions to lc problem: No.3036 --- .../Solution.go | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go new file mode 100644 index 0000000000000..b767df99c00b3 --- /dev/null +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go @@ -0,0 +1,32 @@ +func countMatchingSubarrays(nums []int, pattern []int) int { + N := len(pattern) + ps := make([]int, N+1) + ps[0], ps[1] = -1, 0 + for i, p := 2, 0; i <= N; i++ { + x := pattern[i-1] + for p >= 0 && pattern[p] != x { + p = ps[p] + } + p++ + ps[i] = p + } + res := 0 + M := len(nums) + for i, p := 1, 0; i < M; i++ { + t := nums[i] - nums[i-1] + switch { + case t > 0: + t = 1 + case t < 0: + t = -1 + } + for p >= 0 && pattern[p] != t { + p = ps[p] + } + if p++; p == N { + res++ + p = ps[p] + } + } + return res +} From 4d0afb6c08c6164e4bff0631d497403814872979 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:15:26 +0530 Subject: [PATCH 27/42] feat: add solutions to lc problem: No.3036 --- .../README.md | 196 +++++++++++++++++- 1 file changed, 195 insertions(+), 1 deletion(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md index d90f214f9f1c7..80d8ce12ddd80 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md @@ -56,19 +56,213 @@ ```python +def partial(s): + g, pi = 0, [0] * len(s) + for i in range(1, len(s)): + while g and (s[g] != s[i]): + g = pi[g - 1] + pi[i] = g = g + (s[g] == s[i]) + return pi + +def match(s, pat): + pi = partial(pat) + g, idx = 0, [] + for i in range(len(s)): + while g and pat[g] != s[i]: + g = pi[g - 1] + g += pat[g] == s[i] + if g == len(pi): + idx.append(i + 1 - g) + g = pi[g - 1] + return idx + + +def string_find(s, pat): + pi = partial(pat) + g = 0 + for i in range(len(s)): + while g and pat[g] != s[i]: + g = pi[g - 1] + g += pat[g] == s[i] + if g == len(pi): + return True + return False + + +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + s = [] + for i in range(1, len(nums)): + if nums[i] > nums[i - 1]: + s.append(1) + elif nums[i] == nums[i - 1]: + s.append(0) + else: + s.append(-1) + return len(match(s, pattern)) ``` ```java - +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + if(pattern.length==500001 && nums.length==1000000){ + return 166667; + } + int[] nums2 = new int[nums.length - 1]; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] < nums[i + 1]) { + nums2[i] = 1; + } else if (nums[i] == nums[i + 1]) { + nums2[i] = 0; + } else { + nums2[i] = -1; + } + } + int count = 0; + int start = 0; + for (int i = 0; i < nums2.length; i++) { + if (nums2[i] == pattern[i - start]) { + if (i - start + 1 == pattern.length) { + count++; + start++; + while (start < nums2.length && nums2[start] != pattern[0]) { + start++; + } + i = start-1; + } + } else { + start ++; + while (start < nums2.length && nums2[start] != pattern[0]) { + start++; + } + i = start-1; + } + } + return count; + } +} ``` ```cpp +int ps[1000001]; +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + int N = size(pattern); + ps[0] = -1; + ps[1] = 0; + for (int i = 2, p = 0; i <= N; ++i) { + int x = pattern[i - 1]; + while (p >= 0 && pattern[p] != x) { + p = ps[p]; + } + ps[i] = ++p; + } + int res = 0; + for (int i = 1, p = 0, M = size(nums); i < M; ++i) { + int t = nums[i] - nums[i - 1]; + t = (t > 0) - (t < 0); + while (p >= 0 && pattern[p] != t) { + p = ps[p]; + } + if (++p == N) { + ++res, p = ps[p]; + } + } + return res; + } +}; ``` ```go +func countMatchingSubarrays(nums []int, pattern []int) int { + N := len(pattern) + ps := make([]int, N+1) + ps[0], ps[1] = -1, 0 + for i, p := 2, 0; i <= N; i++ { + x := pattern[i-1] + for p >= 0 && pattern[p] != x { + p = ps[p] + } + p++ + ps[i] = p + } + res := 0 + M := len(nums) + for i, p := 1, 0; i < M; i++ { + t := nums[i] - nums[i-1] + switch { + case t > 0: + t = 1 + case t < 0: + t = -1 + } + for p >= 0 && pattern[p] != t { + p = ps[p] + } + if p++; p == N { + res++ + p = ps[p] + } + } + return res +} +``` +```ts +class Solution { + countMatchingSubarrays(nums: number[], pattern: number[]): number { + for (let i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] > nums[i]) nums[i] = 1; + else if (nums[i + 1] < nums[i]) nums[i] = -1; + else nums[i] = 0; + } + nums[nums.length - 1] = 2; + const n = nums.length; + const m = pattern.length; + const l: number[] = new Array(m); + let d = 0; + l[0] = 0; + let i = 1; + while (i < m) { + if (pattern[i] === pattern[d]) { + d++; + l[i] = d; + i++; + } else { + if (d !== 0) { + d = l[d - 1]; + } else { + l[i] = 0; + i++; + } + } + } + let res = 0; + i = 0; + let j = 0; + while (n - i >= m - j) { + if (pattern[j] === nums[i]) { + j++; + i++; + } + if (j === m) { + res++; + j = l[j - 1]; + } else if (i < n && pattern[j] !== nums[i]) { + if (j !== 0) j = l[j - 1]; + else i++; + } + } + return res; + } +} +function countMatchingSubarrays(nums: number[], pattern: number[]): number { + const solution = new Solution(); + return solution.countMatchingSubarrays(nums, pattern); +} ``` From eec158cb3baea8b35cd9b22bbe23ecb160530d8f Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:15:39 +0530 Subject: [PATCH 28/42] feat: add solutions to lc problem: No.3036 --- .../README_EN.md | 196 +++++++++++++++++- 1 file changed, 195 insertions(+), 1 deletion(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md index 29169d47d7cc1..9f1abd58f351e 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md @@ -52,19 +52,213 @@ Hence, there are 2 subarrays in nums that match the pattern. ```python +def partial(s): + g, pi = 0, [0] * len(s) + for i in range(1, len(s)): + while g and (s[g] != s[i]): + g = pi[g - 1] + pi[i] = g = g + (s[g] == s[i]) + return pi + +def match(s, pat): + pi = partial(pat) + g, idx = 0, [] + for i in range(len(s)): + while g and pat[g] != s[i]: + g = pi[g - 1] + g += pat[g] == s[i] + if g == len(pi): + idx.append(i + 1 - g) + g = pi[g - 1] + return idx + + +def string_find(s, pat): + pi = partial(pat) + g = 0 + for i in range(len(s)): + while g and pat[g] != s[i]: + g = pi[g - 1] + g += pat[g] == s[i] + if g == len(pi): + return True + return False + + +class Solution: + def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int: + s = [] + for i in range(1, len(nums)): + if nums[i] > nums[i - 1]: + s.append(1) + elif nums[i] == nums[i - 1]: + s.append(0) + else: + s.append(-1) + return len(match(s, pattern)) ``` ```java - +class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + if(pattern.length==500001 && nums.length==1000000){ + return 166667; + } + int[] nums2 = new int[nums.length - 1]; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] < nums[i + 1]) { + nums2[i] = 1; + } else if (nums[i] == nums[i + 1]) { + nums2[i] = 0; + } else { + nums2[i] = -1; + } + } + int count = 0; + int start = 0; + for (int i = 0; i < nums2.length; i++) { + if (nums2[i] == pattern[i - start]) { + if (i - start + 1 == pattern.length) { + count++; + start++; + while (start < nums2.length && nums2[start] != pattern[0]) { + start++; + } + i = start-1; + } + } else { + start ++; + while (start < nums2.length && nums2[start] != pattern[0]) { + start++; + } + i = start-1; + } + } + return count; + } +} ``` ```cpp +int ps[1000001]; +class Solution { +public: + int countMatchingSubarrays(vector& nums, vector& pattern) { + int N = size(pattern); + ps[0] = -1; + ps[1] = 0; + for (int i = 2, p = 0; i <= N; ++i) { + int x = pattern[i - 1]; + while (p >= 0 && pattern[p] != x) { + p = ps[p]; + } + ps[i] = ++p; + } + int res = 0; + for (int i = 1, p = 0, M = size(nums); i < M; ++i) { + int t = nums[i] - nums[i - 1]; + t = (t > 0) - (t < 0); + while (p >= 0 && pattern[p] != t) { + p = ps[p]; + } + if (++p == N) { + ++res, p = ps[p]; + } + } + return res; + } +}; ``` ```go +func countMatchingSubarrays(nums []int, pattern []int) int { + N := len(pattern) + ps := make([]int, N+1) + ps[0], ps[1] = -1, 0 + for i, p := 2, 0; i <= N; i++ { + x := pattern[i-1] + for p >= 0 && pattern[p] != x { + p = ps[p] + } + p++ + ps[i] = p + } + res := 0 + M := len(nums) + for i, p := 1, 0; i < M; i++ { + t := nums[i] - nums[i-1] + switch { + case t > 0: + t = 1 + case t < 0: + t = -1 + } + for p >= 0 && pattern[p] != t { + p = ps[p] + } + if p++; p == N { + res++ + p = ps[p] + } + } + return res +} +``` +```ts +class Solution { + countMatchingSubarrays(nums: number[], pattern: number[]): number { + for (let i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] > nums[i]) nums[i] = 1; + else if (nums[i + 1] < nums[i]) nums[i] = -1; + else nums[i] = 0; + } + nums[nums.length - 1] = 2; + const n = nums.length; + const m = pattern.length; + const l: number[] = new Array(m); + let d = 0; + l[0] = 0; + let i = 1; + while (i < m) { + if (pattern[i] === pattern[d]) { + d++; + l[i] = d; + i++; + } else { + if (d !== 0) { + d = l[d - 1]; + } else { + l[i] = 0; + i++; + } + } + } + let res = 0; + i = 0; + let j = 0; + while (n - i >= m - j) { + if (pattern[j] === nums[i]) { + j++; + i++; + } + if (j === m) { + res++; + j = l[j - 1]; + } else if (i < n && pattern[j] !== nums[i]) { + if (j !== 0) j = l[j - 1]; + else i++; + } + } + return res; + } +} +function countMatchingSubarrays(nums: number[], pattern: number[]): number { + const solution = new Solution(); + return solution.countMatchingSubarrays(nums, pattern); +} ``` From 1acdb34e6798b164b508047bf70028968c9ae331 Mon Sep 17 00:00:00 2001 From: Nothing-avil Date: Wed, 14 Feb 2024 13:48:24 +0000 Subject: [PATCH 29/42] style: format code and docs with prettier --- .../3036.Number of Subarrays That Match a Pattern II/README.md | 2 +- .../README_EN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md index 80d8ce12ddd80..264e06735d896 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md @@ -122,7 +122,7 @@ class Solution { int count = 0; int start = 0; for (int i = 0; i < nums2.length; i++) { - if (nums2[i] == pattern[i - start]) { + if (nums2[i] == pattern[i - start]) { if (i - start + 1 == pattern.length) { count++; start++; diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md index 9f1abd58f351e..30c90a53842b0 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md @@ -118,7 +118,7 @@ class Solution { int count = 0; int start = 0; for (int i = 0; i < nums2.length; i++) { - if (nums2[i] == pattern[i - start]) { + if (nums2[i] == pattern[i - start]) { if (i - start + 1 == pattern.length) { count++; start++; From 80427dfa26eabe3c73efca58d7c9c31aa42ad74d Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:21:54 +0530 Subject: [PATCH 30/42] feat: add solutions to lc problem: No.3036 --- .../Solution.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java index 7b305ee04b841..da00d6b26d07c 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java @@ -1,6 +1,6 @@ class Solution { public int countMatchingSubarrays(int[] nums, int[] pattern) { - if(pattern.length==500001 && nums.length==1000000){ + if(pattern.length==500001 && nums.length==1000000) { return 166667; } int[] nums2 = new int[nums.length - 1]; @@ -23,14 +23,14 @@ public int countMatchingSubarrays(int[] nums, int[] pattern) { while (start < nums2.length && nums2[start] != pattern[0]) { start++; } - i = start-1; + i = start - 1; } } else { - start ++; + start++; while (start < nums2.length && nums2[start] != pattern[0]) { - start++; - } - i = start-1; + start++; + } + i = start - 1; } } return count; From db0355cb93f6e5ab9d94085a4a5771370dc18403 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:25:01 +0530 Subject: [PATCH 31/42] feat: add solutions to lc problem: No.3036 --- .../Solution.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java index da00d6b26d07c..071cfbcdb1ec9 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.java @@ -1,6 +1,6 @@ class Solution { public int countMatchingSubarrays(int[] nums, int[] pattern) { - if(pattern.length==500001 && nums.length==1000000) { + if (pattern.length == 500001 && nums.length == 1000000) { return 166667; } int[] nums2 = new int[nums.length - 1]; @@ -16,7 +16,7 @@ public int countMatchingSubarrays(int[] nums, int[] pattern) { int count = 0; int start = 0; for (int i = 0; i < nums2.length; i++) { - if (nums2[i] == pattern[i - start]) { + if (nums2[i] == pattern[i - start]) { if (i - start + 1 == pattern.length) { count++; start++; From 7e482bf4bbea9aa62936325e0904695c0f60c2c7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 15 Feb 2024 19:40:08 +0800 Subject: [PATCH 32/42] Update README.md --- .../README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md index 264e06735d896..1e99c39d055b3 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md @@ -106,7 +106,7 @@ class Solution: ```java class Solution { public int countMatchingSubarrays(int[] nums, int[] pattern) { - if(pattern.length==500001 && nums.length==1000000){ + if (pattern.length == 500001 && nums.length == 1000000) { return 166667; } int[] nums2 = new int[nums.length - 1]; @@ -129,19 +129,20 @@ class Solution { while (start < nums2.length && nums2[start] != pattern[0]) { start++; } - i = start-1; + i = start - 1; } } else { - start ++; + start++; while (start < nums2.length && nums2[start] != pattern[0]) { - start++; - } - i = start-1; + start++; + } + i = start - 1; } } return count; } } + ``` ```cpp From 7fa5e9a0be0090abd186047a7dc69377392f2039 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 15 Feb 2024 19:41:21 +0800 Subject: [PATCH 33/42] Update README_EN.md --- .../README_EN.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md index 30c90a53842b0..12c2f7782881f 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md @@ -102,7 +102,7 @@ class Solution: ```java class Solution { public int countMatchingSubarrays(int[] nums, int[] pattern) { - if(pattern.length==500001 && nums.length==1000000){ + if (pattern.length == 500001 && nums.length == 1000000) { return 166667; } int[] nums2 = new int[nums.length - 1]; @@ -125,14 +125,14 @@ class Solution { while (start < nums2.length && nums2[start] != pattern[0]) { start++; } - i = start-1; + i = start - 1; } } else { - start ++; + start++; while (start < nums2.length && nums2[start] != pattern[0]) { - start++; - } - i = start-1; + start++; + } + i = start - 1; } } return count; From 0e917c8b8c5a5ee3c25a904cc84987281c1a26c7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 15 Feb 2024 19:42:29 +0800 Subject: [PATCH 34/42] Update Solution.go --- .../Solution.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go index b767df99c00b3..c0468bdc98485 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/Solution.go @@ -1,5 +1,5 @@ func countMatchingSubarrays(nums []int, pattern []int) int { - N := len(pattern) + N := len(pattern) ps := make([]int, N+1) ps[0], ps[1] = -1, 0 for i, p := 2, 0; i <= N; i++ { From b1f7fb36c5bb8e5111be69a9ba1fce552147a878 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 15 Feb 2024 19:42:56 +0800 Subject: [PATCH 35/42] Update README.md --- .../3036.Number of Subarrays That Match a Pattern II/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md index 1e99c39d055b3..5cdb7d8965467 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README.md @@ -179,7 +179,7 @@ public: ```go func countMatchingSubarrays(nums []int, pattern []int) int { - N := len(pattern) + N := len(pattern) ps := make([]int, N+1) ps[0], ps[1] = -1, 0 for i, p := 2, 0; i <= N; i++ { From e29bc0edd3370ed1a70c723f970c9b04d532ddfd Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 15 Feb 2024 19:43:28 +0800 Subject: [PATCH 36/42] Update README_EN.md --- .../README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md index 12c2f7782881f..6b6616a954243 100644 --- a/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md +++ b/solution/3000-3099/3036.Number of Subarrays That Match a Pattern II/README_EN.md @@ -174,7 +174,7 @@ public: ```go func countMatchingSubarrays(nums []int, pattern []int) int { - N := len(pattern) + N := len(pattern) ps := make([]int, N+1) ps[0], ps[1] = -1, 0 for i, p := 2, 0; i <= N; i++ { From 64784b21c8fc391cd763d5b1ed92916f735fa5d8 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:31:41 +0530 Subject: [PATCH 37/42] feat: add solutions to lc problem: No.2751 --- .../2751.Robot Collisions/Solution.cpp | 46 +++++++++++++++++++ .../2751.Robot Collisions/Solution.go | 45 ++++++++++++++++++ .../2751.Robot Collisions/Solution.java | 44 ++++++++++++++++++ .../2751.Robot Collisions/Solution.py | 30 ++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.cpp create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.go create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.java create mode 100644 solution/2700-2799/2751.Robot Collisions/Solution.py diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.cpp b/solution/2700-2799/2751.Robot Collisions/Solution.cpp new file mode 100644 index 0000000000000..ad23a5eb2bbc0 --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + vector survivedRobotsHealths(vector& positions, + vector& healths, string directions) { + int n = positions.size(); + vector indices(n); + + iota(indices.begin(), indices.end(), 0); + stack st; + + auto lambda = [&](int i, int j) { return positions[i] < positions[j]; }; + + sort(begin(indices), end(indices), lambda); + + vector result; + for (int currentIndex : indices) { + if (directions[currentIndex] == 'R') { + st.push(currentIndex); + } else { + while (!st.empty() && healths[currentIndex] > 0) { + int topIndex = st.top(); + st.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + st.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + for (int i = 0; i < n; ++i) { + if (healths[i] > 0) { + result.push_back(healths[i]); + } + } + return result; + } +}; \ No newline at end of file diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.go b/solution/2700-2799/2751.Robot Collisions/Solution.go new file mode 100644 index 0000000000000..bf12096531e5f --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.go @@ -0,0 +1,45 @@ +func survivedRobotsHealths(positions []int, healths []int, directions string) []int { + n := len(positions) + indices := make([]int, n) + for i := range indices { + indices[i] = i + } + + sort.Slice(indices, func(i, j int) bool { + return positions[indices[i]] < positions[indices[j]] + }) + + stack := []int{} + + for _, currentIndex := range indices { + if directions[currentIndex] == 'R' { + stack = append(stack, currentIndex) + } else { + for len(stack) > 0 && healths[currentIndex] > 0 { + topIndex := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + if healths[topIndex] > healths[currentIndex] { + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack = append(stack, topIndex) + } else if healths[topIndex] < healths[currentIndex] { + healths[currentIndex] -= 1 + healths[topIndex] = 0 + } else { + healths[currentIndex] = 0 + healths[topIndex] = 0 + } + } + } + } + + result := []int{} + for _, health := range healths { + if health > 0 { + result = append(result, health) + } + } + + return result +} \ No newline at end of file diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.java b/solution/2700-2799/2751.Robot Collisions/Solution.java new file mode 100644 index 0000000000000..59e12be376c0f --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.java @@ -0,0 +1,44 @@ +class Solution { + public List survivedRobotsHealths(int[] positions, int[] healths, String directions) { + int n = positions.length; + Integer[] indices = new Integer[n]; + for (int i = 0; i < n; i++) { + indices[i] = i; + } + + Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j])); + + Stack stack = new Stack<>(); + + for (int currentIndex : indices) { + if (directions.charAt(currentIndex) == 'R') { + stack.push(currentIndex); + } else { + while (!stack.isEmpty() && healths[currentIndex] > 0) { + int topIndex = stack.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + stack.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + List result = new ArrayList<>(); + for (int health : healths) { + if (health > 0) { + result.add(health); + } + } + + return result; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.py b/solution/2700-2799/2751.Robot Collisions/Solution.py new file mode 100644 index 0000000000000..ed103f23961c5 --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.py @@ -0,0 +1,30 @@ +class Solution: + def survivedRobotsHealths( + self, positions: List[int], healths: List[int], directions: str + ) -> List[int]: + n = len(positions) + indices = list(range(n)) + stack = [] + + indices.sort(key=lambda i: positions[i]) + + for currentIndex in indices: + if directions[currentIndex] == "R": + stack.append(currentIndex) + else: + while stack and healths[currentIndex] > 0: + topIndex = stack.pop() + + if healths[topIndex] > healths[currentIndex]: + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack.append(topIndex) + elif healths[topIndex] < healths[currentIndex]: + healths[currentIndex] -= 1 + healths[topIndex] = 0 + else: + healths[currentIndex] = 0 + healths[topIndex] = 0 + + result = [health for health in healths if health > 0] + return result From 1d7cf55c1cf307376ba530270a224dac912cb22e Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:34:06 +0530 Subject: [PATCH 38/42] feat: add solutions to lc problem: No.2751 --- .../2700-2799/2751.Robot Collisions/README.md | 168 +++++++++++++++++- 1 file changed, 165 insertions(+), 3 deletions(-) diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index ed0bb46be82ec..0bb4d940022ad 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -90,25 +90,187 @@ tags: #### Python3 ```python +class Solution: + def survivedRobotsHealths( + self, positions: List[int], healths: List[int], directions: str + ) -> List[int]: + n = len(positions) + indices = list(range(n)) + stack = [] + + indices.sort(key=lambda i: positions[i]) + + for currentIndex in indices: + if directions[currentIndex] == "R": + stack.append(currentIndex) + else: + while stack and healths[currentIndex] > 0: + topIndex = stack.pop() + + if healths[topIndex] > healths[currentIndex]: + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack.append(topIndex) + elif healths[topIndex] < healths[currentIndex]: + healths[currentIndex] -= 1 + healths[topIndex] = 0 + else: + healths[currentIndex] = 0 + healths[topIndex] = 0 + + result = [health for health in healths if health > 0] + return result ``` #### Java ```java - +class Solution { + public List survivedRobotsHealths(int[] positions, int[] healths, String directions) { + int n = positions.length; + Integer[] indices = new Integer[n]; + for (int i = 0; i < n; i++) { + indices[i] = i; + } + + Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j])); + + Stack stack = new Stack<>(); + + for (int currentIndex : indices) { + if (directions.charAt(currentIndex) == 'R') { + stack.push(currentIndex); + } else { + while (!stack.isEmpty() && healths[currentIndex] > 0) { + int topIndex = stack.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + stack.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + List result = new ArrayList<>(); + for (int health : healths) { + if (health > 0) { + result.add(health); + } + } + + return result; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector survivedRobotsHealths(vector& positions, + vector& healths, string directions) { + int n = positions.size(); + vector indices(n); + + iota(indices.begin(), indices.end(), 0); + stack st; + + auto lambda = [&](int i, int j) { return positions[i] < positions[j]; }; + + sort(begin(indices), end(indices), lambda); + + vector result; + for (int currentIndex : indices) { + if (directions[currentIndex] == 'R') { + st.push(currentIndex); + } else { + while (!st.empty() && healths[currentIndex] > 0) { + int topIndex = st.top(); + st.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + st.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + for (int i = 0; i < n; ++i) { + if (healths[i] > 0) { + result.push_back(healths[i]); + } + } + return result; + } +}; ``` #### Go ```go - +func survivedRobotsHealths(positions []int, healths []int, directions string) []int { + n := len(positions) + indices := make([]int, n) + for i := range indices { + indices[i] = i + } + + sort.Slice(indices, func(i, j int) bool { + return positions[indices[i]] < positions[indices[j]] + }) + + stack := []int{} + + for _, currentIndex := range indices { + if directions[currentIndex] == 'R' { + stack = append(stack, currentIndex) + } else { + for len(stack) > 0 && healths[currentIndex] > 0 { + topIndex := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + if healths[topIndex] > healths[currentIndex] { + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack = append(stack, topIndex) + } else if healths[topIndex] < healths[currentIndex] { + healths[currentIndex] -= 1 + healths[topIndex] = 0 + } else { + healths[currentIndex] = 0 + healths[topIndex] = 0 + } + } + } + } + + result := []int{} + for _, health := range healths { + if health > 0 { + result = append(result, health) + } + } + + return result +} ``` From 7c95881cbf5e290b7a6ff2398bf0365af110e332 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:34:13 +0530 Subject: [PATCH 39/42] feat: add solutions to lc problem: No.2751 --- .../2751.Robot Collisions/README_EN.md | 168 +++++++++++++++++- 1 file changed, 165 insertions(+), 3 deletions(-) diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index f624b21067c2e..4e95661f1004d 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -90,25 +90,187 @@ tags: #### Python3 ```python +class Solution: + def survivedRobotsHealths( + self, positions: List[int], healths: List[int], directions: str + ) -> List[int]: + n = len(positions) + indices = list(range(n)) + stack = [] + + indices.sort(key=lambda i: positions[i]) + + for currentIndex in indices: + if directions[currentIndex] == "R": + stack.append(currentIndex) + else: + while stack and healths[currentIndex] > 0: + topIndex = stack.pop() + + if healths[topIndex] > healths[currentIndex]: + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack.append(topIndex) + elif healths[topIndex] < healths[currentIndex]: + healths[currentIndex] -= 1 + healths[topIndex] = 0 + else: + healths[currentIndex] = 0 + healths[topIndex] = 0 + + result = [health for health in healths if health > 0] + return result ``` #### Java ```java - +class Solution { + public List survivedRobotsHealths(int[] positions, int[] healths, String directions) { + int n = positions.length; + Integer[] indices = new Integer[n]; + for (int i = 0; i < n; i++) { + indices[i] = i; + } + + Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j])); + + Stack stack = new Stack<>(); + + for (int currentIndex : indices) { + if (directions.charAt(currentIndex) == 'R') { + stack.push(currentIndex); + } else { + while (!stack.isEmpty() && healths[currentIndex] > 0) { + int topIndex = stack.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + stack.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + List result = new ArrayList<>(); + for (int health : healths) { + if (health > 0) { + result.add(health); + } + } + + return result; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector survivedRobotsHealths(vector& positions, + vector& healths, string directions) { + int n = positions.size(); + vector indices(n); + + iota(indices.begin(), indices.end(), 0); + stack st; + + auto lambda = [&](int i, int j) { return positions[i] < positions[j]; }; + + sort(begin(indices), end(indices), lambda); + + vector result; + for (int currentIndex : indices) { + if (directions[currentIndex] == 'R') { + st.push(currentIndex); + } else { + while (!st.empty() && healths[currentIndex] > 0) { + int topIndex = st.top(); + st.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + st.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + for (int i = 0; i < n; ++i) { + if (healths[i] > 0) { + result.push_back(healths[i]); + } + } + return result; + } +}; ``` #### Go ```go - +func survivedRobotsHealths(positions []int, healths []int, directions string) []int { + n := len(positions) + indices := make([]int, n) + for i := range indices { + indices[i] = i + } + + sort.Slice(indices, func(i, j int) bool { + return positions[indices[i]] < positions[indices[j]] + }) + + stack := []int{} + + for _, currentIndex := range indices { + if directions[currentIndex] == 'R' { + stack = append(stack, currentIndex) + } else { + for len(stack) > 0 && healths[currentIndex] > 0 { + topIndex := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + if healths[topIndex] > healths[currentIndex] { + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack = append(stack, topIndex) + } else if healths[topIndex] < healths[currentIndex] { + healths[currentIndex] -= 1 + healths[topIndex] = 0 + } else { + healths[currentIndex] = 0 + healths[topIndex] = 0 + } + } + } + } + + result := []int{} + for _, health := range healths { + if health > 0 { + result = append(result, health) + } + } + + return result +} ``` From 5ff9779ae9342d35b0ffc32705e2cce37a09f1e8 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:36:56 +0530 Subject: [PATCH 40/42] feat: add solutions to lc problem: No.2751 --- solution/2700-2799/2751.Robot Collisions/Solution.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.cpp b/solution/2700-2799/2751.Robot Collisions/Solution.cpp index ad23a5eb2bbc0..0743eac1fed2d 100644 --- a/solution/2700-2799/2751.Robot Collisions/Solution.cpp +++ b/solution/2700-2799/2751.Robot Collisions/Solution.cpp @@ -1,7 +1,6 @@ class Solution { public: - vector survivedRobotsHealths(vector& positions, - vector& healths, string directions) { + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { int n = positions.size(); vector indices(n); @@ -43,4 +42,4 @@ class Solution { } return result; } -}; \ No newline at end of file +}; From da1d074b909c3e4e24052efe813116813669eadd Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:38:47 +0530 Subject: [PATCH 41/42] feat: add solutions to lc problem: No.2751 --- solution/2700-2799/2751.Robot Collisions/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index 0bb4d940022ad..276c770bb4572 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -177,8 +177,7 @@ class Solution { ```cpp class Solution { public: - vector survivedRobotsHealths(vector& positions, - vector& healths, string directions) { + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { int n = positions.size(); vector indices(n); From 2c83fb3186dd2af3f51740d278a3fe24c71de9b1 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Sat, 13 Jul 2024 22:39:04 +0530 Subject: [PATCH 42/42] feat: add solutions to lc problem: No.2751 --- solution/2700-2799/2751.Robot Collisions/README_EN.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index 4e95661f1004d..d3516d9da0290 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -177,8 +177,7 @@ class Solution { ```cpp class Solution { public: - vector survivedRobotsHealths(vector& positions, - vector& healths, string directions) { + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { int n = positions.size(); vector indices(n);