diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README.md b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README.md index 57a2f532f7825..239e822b7929b 100644 --- a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README.md +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README.md @@ -85,24 +85,116 @@ ## 解法 -### 方法一 +### 方法一:单调栈 + +我们考虑以数组 $nums$ 中的每个元素 $x$ 作为子数组的边界元素且最大值的情况。 + +每个长度为 $1$ 的子数组都满足条件,而对于长度大于 $1$ 的子数组,子数组中的所有元素都不能大于边界元素 $x$,我们可以用单调栈来实现。 + +我们维护一个从栈底到栈顶单调递减的栈,单调栈的每个元素是一个二元组 $[x, cnt]$,表示元素 $x$ 且以 $x$ 为边界元素且最大值的子数组的个数为 $cnt$。 + +我们从左到右遍历数组 $nums$,对于每个元素 $x$,我们不断地将栈顶元素弹出,直到栈为空或者栈顶元素的第一个元素大于等于 $x$。如果栈为空,或者栈顶元素的第一个元素大于 $x$,说明当前遇到第一个边界元素为 $x$ 且最大值的子数组,该子数组的长度为 $1$,所以我们将 $[x, 1]$ 入栈。如果栈顶元素的第一个元素等于 $x$,说明当前遇到的边界元素为 $x$ 且最大值的子数组,我们将栈顶元素的第二个元素加 $1$。然后,我们将栈顶元素的第二个元素加到答案中。 + +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。 ```python - +class Solution: + def numberOfSubarrays(self, nums: List[int]) -> int: + stk = [] + ans = 0 + for x in nums: + while stk and stk[-1][0] < x: + stk.pop() + if not stk or stk[-1][0] > x: + stk.append([x, 1]) + else: + stk[-1][1] += 1 + ans += stk[-1][1] + return ans ``` ```java - +class Solution { + public long numberOfSubarrays(int[] nums) { + Deque stk = new ArrayDeque<>(); + long ans = 0; + for (int x : nums) { + while (!stk.isEmpty() && stk.peek()[0] < x) { + stk.pop(); + } + if (stk.isEmpty() || stk.peek()[0] > x) { + stk.push(new int[] {x, 1}); + } else { + stk.peek()[1]++; + } + ans += stk.peek()[1]; + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + long long numberOfSubarrays(vector& nums) { + vector> stk; + long long ans = 0; + for (int x : nums) { + while (!stk.empty() && stk.back().first < x) { + stk.pop_back(); + } + if (stk.empty() || stk.back().first > x) { + stk.push_back(make_pair(x, 1)); + } else { + stk.back().second++; + } + ans += stk.back().second; + } + return ans; + } +}; ``` ```go +func numberOfSubarrays(nums []int) (ans int64) { + stk := [][2]int{} + for _, x := range nums { + for len(stk) > 0 && stk[len(stk)-1][0] < x { + stk = stk[:len(stk)-1] + } + if len(stk) == 0 || stk[len(stk)-1][0] > x { + stk = append(stk, [2]int{x, 1}) + } else { + stk[len(stk)-1][1]++ + } + ans += int64(stk[len(stk)-1][1]) + } + return +} +``` +```ts +function numberOfSubarrays(nums: number[]): number { + const stk: number[][] = []; + let ans = 0; + for (const x of nums) { + while (stk.length > 0 && stk.at(-1)![0] < x) { + stk.pop(); + } + if (stk.length === 0 || stk.at(-1)![0] > x) { + stk.push([x, 1]); + } else { + stk.at(-1)![1]++; + } + ans += stk.at(-1)![1]; + } + return ans; +} ``` diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README_EN.md b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README_EN.md index 5a370a7fc4df3..90239134dc7c0 100644 --- a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README_EN.md +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README_EN.md @@ -81,24 +81,116 @@ ## Solutions -### Solution 1 +### Solution 1: Monotonic Stack + +We consider each element $x$ in the array $nums$ as the boundary element and the maximum value of the subarray. + +Each subarray of length $1$ meets the condition, and for subarrays with length greater than $1$, all elements in the subarray cannot be greater than the boundary element $x$. We can implement this with a monotonic stack. + +We maintain a stack that decreases monotonically from the bottom to the top. Each element in the monotonic stack is a pair $[x, cnt]$, representing the element $x$ and the count $cnt$ of subarrays with $x$ as the boundary element and the maximum value. + +We traverse the array $nums$ from left to right. For each element $x$, we continuously pop the top element of the stack until the stack is empty or the first element of the top element of the stack is greater than or equal to $x$. If the stack is empty, or the first element of the top element of the stack is greater than $x$, it means that we have encountered the first subarray with $x$ as the boundary element and the maximum value, and the length of this subarray is $1$, so we push $[x, 1]$ into the stack. If the first element of the top element of the stack is equal to $x$, it means that we have encountered a subarray with $x$ as the boundary element and the maximum value, and we add $1$ to the second element of the top element of the stack. Then, we add the second element of the top element of the stack to the answer. + +After the traversal, we return the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$. ```python - +class Solution: + def numberOfSubarrays(self, nums: List[int]) -> int: + stk = [] + ans = 0 + for x in nums: + while stk and stk[-1][0] < x: + stk.pop() + if not stk or stk[-1][0] > x: + stk.append([x, 1]) + else: + stk[-1][1] += 1 + ans += stk[-1][1] + return ans ``` ```java - +class Solution { + public long numberOfSubarrays(int[] nums) { + Deque stk = new ArrayDeque<>(); + long ans = 0; + for (int x : nums) { + while (!stk.isEmpty() && stk.peek()[0] < x) { + stk.pop(); + } + if (stk.isEmpty() || stk.peek()[0] > x) { + stk.push(new int[] {x, 1}); + } else { + stk.peek()[1]++; + } + ans += stk.peek()[1]; + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + long long numberOfSubarrays(vector& nums) { + vector> stk; + long long ans = 0; + for (int x : nums) { + while (!stk.empty() && stk.back().first < x) { + stk.pop_back(); + } + if (stk.empty() || stk.back().first > x) { + stk.push_back(make_pair(x, 1)); + } else { + stk.back().second++; + } + ans += stk.back().second; + } + return ans; + } +}; ``` ```go +func numberOfSubarrays(nums []int) (ans int64) { + stk := [][2]int{} + for _, x := range nums { + for len(stk) > 0 && stk[len(stk)-1][0] < x { + stk = stk[:len(stk)-1] + } + if len(stk) == 0 || stk[len(stk)-1][0] > x { + stk = append(stk, [2]int{x, 1}) + } else { + stk[len(stk)-1][1]++ + } + ans += int64(stk[len(stk)-1][1]) + } + return +} +``` +```ts +function numberOfSubarrays(nums: number[]): number { + const stk: number[][] = []; + let ans = 0; + for (const x of nums) { + while (stk.length > 0 && stk.at(-1)![0] < x) { + stk.pop(); + } + if (stk.length === 0 || stk.at(-1)![0] > x) { + stk.push([x, 1]); + } else { + stk.at(-1)![1]++; + } + ans += stk.at(-1)![1]; + } + return ans; +} ``` diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.cpp b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.cpp new file mode 100644 index 0000000000000..f25e8656690d1 --- /dev/null +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + long long numberOfSubarrays(vector& nums) { + vector> stk; + long long ans = 0; + for (int x : nums) { + while (!stk.empty() && stk.back().first < x) { + stk.pop_back(); + } + if (stk.empty() || stk.back().first > x) { + stk.push_back(make_pair(x, 1)); + } else { + stk.back().second++; + } + ans += stk.back().second; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.go b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.go new file mode 100644 index 0000000000000..70f5ea17bb686 --- /dev/null +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.go @@ -0,0 +1,15 @@ +func numberOfSubarrays(nums []int) (ans int64) { + stk := [][2]int{} + for _, x := range nums { + for len(stk) > 0 && stk[len(stk)-1][0] < x { + stk = stk[:len(stk)-1] + } + if len(stk) == 0 || stk[len(stk)-1][0] > x { + stk = append(stk, [2]int{x, 1}) + } else { + stk[len(stk)-1][1]++ + } + ans += int64(stk[len(stk)-1][1]) + } + return +} \ No newline at end of file diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.java b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.java new file mode 100644 index 0000000000000..0b98fb7d82584 --- /dev/null +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public long numberOfSubarrays(int[] nums) { + Deque stk = new ArrayDeque<>(); + long ans = 0; + for (int x : nums) { + while (!stk.isEmpty() && stk.peek()[0] < x) { + stk.pop(); + } + if (stk.isEmpty() || stk.peek()[0] > x) { + stk.push(new int[] {x, 1}); + } else { + stk.peek()[1]++; + } + ans += stk.peek()[1]; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.py b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.py new file mode 100644 index 0000000000000..fc600b4f223f8 --- /dev/null +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def numberOfSubarrays(self, nums: List[int]) -> int: + stk = [] + ans = 0 + for x in nums: + while stk and stk[-1][0] < x: + stk.pop() + if not stk or stk[-1][0] > x: + stk.append([x, 1]) + else: + stk[-1][1] += 1 + ans += stk[-1][1] + return ans diff --git a/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.ts b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.ts new file mode 100644 index 0000000000000..45b90595431e3 --- /dev/null +++ b/solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/Solution.ts @@ -0,0 +1,16 @@ +function numberOfSubarrays(nums: number[]): number { + const stk: number[][] = []; + let ans = 0; + for (const x of nums) { + while (stk.length > 0 && stk.at(-1)![0] < x) { + stk.pop(); + } + if (stk.length === 0 || stk.at(-1)![0] > x) { + stk.push([x, 1]); + } else { + stk.at(-1)![1]++; + } + ans += stk.at(-1)![1]; + } + return ans; +} diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README.md b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README.md index 8973e0c95af2b..c77a888ead9c8 100644 --- a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README.md +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README.md @@ -51,24 +51,108 @@ ## 解法 -### 方法一 +### 方法一:枚举 + +我们可以从大到小枚举所有的时间,其中小时 $h$ 从 $11$ 到 $0$,分钟 $m$ 从 $59$ 到 $0$。对于每一个时间 $t$,我们检查是否满足 $t$ 的每一位字符都与 $s$ 的对应位置字符相等(如果 $s$ 的对应位置字符不是 "?" 的话)。如果满足,那么我们就找到了答案,返回 $t$。 + +时间复杂度 $O(h \times m)$,其中 $h = 12$, $m = 60$。空间复杂度 $O(1)$。 ```python - +class Solution: + def findLatestTime(self, s: str) -> str: + for h in range(11, -1, -1): + for m in range(59, -1, -1): + t = f"{h:02d}:{m:02d}" + if all(a == b for a, b in zip(s, t) if a != "?"): + return t ``` ```java - +class Solution { + public String findLatestTime(String s) { + for (int h = 11;; h--) { + for (int m = 59; m >= 0; m--) { + String t = String.format("%02d:%02d", h, m); + boolean ok = true; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != '?' && s.charAt(i) != t.charAt(i)) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } + } +} ``` ```cpp - +class Solution { +public: + string findLatestTime(string s) { + for (int h = 11;; h--) { + for (int m = 59; m >= 0; m--) { + char t[6]; + sprintf(t, "%02d:%02d", h, m); + bool ok = true; + for (int i = 0; i < s.length(); i++) { + if (s[i] != '?' && s[i] != t[i]) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } + } +}; ``` ```go +func findLatestTime(s string) string { + for h := 11; ; h-- { + for m := 59; m >= 0; m-- { + t := fmt.Sprintf("%02d:%02d", h, m) + ok := true + for i := 0; i < len(s); i++ { + if s[i] != '?' && s[i] != t[i] { + ok = false + break + } + } + if ok { + return t + } + } + } +} +``` +```ts +function findLatestTime(s: string): string { + for (let h = 11; ; h--) { + for (let m = 59; m >= 0; m--) { + const t: string = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`; + let ok: boolean = true; + for (let i = 0; i < s.length; i++) { + if (s[i] !== '?' && s[i] !== t[i]) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } +} ``` diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README_EN.md b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README_EN.md index 0396dc8b4bff4..7a7a4b2335961 100644 --- a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README_EN.md +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/README_EN.md @@ -47,24 +47,108 @@ ## Solutions -### Solution 1 +### Solution 1: Enumeration + +We can enumerate all times from large to small, where the hour $h$ ranges from $11$ to $0$, and the minute $m$ ranges from $59$ to $0$. For each time $t$, we check whether each digit of $t$ matches the corresponding digit in $s$ (if the corresponding digit in $s$ is not "?"). If it does, then we have found the answer and return $t$. + +The time complexity is $O(h \times m)$, where $h = 12$ and $m = 60$. The space complexity is $O(1)$. ```python - +class Solution: + def findLatestTime(self, s: str) -> str: + for h in range(11, -1, -1): + for m in range(59, -1, -1): + t = f"{h:02d}:{m:02d}" + if all(a == b for a, b in zip(s, t) if a != "?"): + return t ``` ```java - +class Solution { + public String findLatestTime(String s) { + for (int h = 11;; h--) { + for (int m = 59; m >= 0; m--) { + String t = String.format("%02d:%02d", h, m); + boolean ok = true; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != '?' && s.charAt(i) != t.charAt(i)) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } + } +} ``` ```cpp - +class Solution { +public: + string findLatestTime(string s) { + for (int h = 11;; h--) { + for (int m = 59; m >= 0; m--) { + char t[6]; + sprintf(t, "%02d:%02d", h, m); + bool ok = true; + for (int i = 0; i < s.length(); i++) { + if (s[i] != '?' && s[i] != t[i]) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } + } +}; ``` ```go +func findLatestTime(s string) string { + for h := 11; ; h-- { + for m := 59; m >= 0; m-- { + t := fmt.Sprintf("%02d:%02d", h, m) + ok := true + for i := 0; i < len(s); i++ { + if s[i] != '?' && s[i] != t[i] { + ok = false + break + } + } + if ok { + return t + } + } + } +} +``` +```ts +function findLatestTime(s: string): string { + for (let h = 11; ; h--) { + for (let m = 59; m >= 0; m--) { + const t: string = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`; + let ok: boolean = true; + for (let i = 0; i < s.length; i++) { + if (s[i] !== '?' && s[i] !== t[i]) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } +} ``` diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.cpp b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.cpp new file mode 100644 index 0000000000000..fe82c94510eb9 --- /dev/null +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + string findLatestTime(string s) { + for (int h = 11;; h--) { + for (int m = 59; m >= 0; m--) { + char t[6]; + sprintf(t, "%02d:%02d", h, m); + bool ok = true; + for (int i = 0; i < s.length(); i++) { + if (s[i] != '?' && s[i] != t[i]) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.go b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.go new file mode 100644 index 0000000000000..50fa95d62773f --- /dev/null +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.go @@ -0,0 +1,17 @@ +func findLatestTime(s string) string { + for h := 11; ; h-- { + for m := 59; m >= 0; m-- { + t := fmt.Sprintf("%02d:%02d", h, m) + ok := true + for i := 0; i < len(s); i++ { + if s[i] != '?' && s[i] != t[i] { + ok = false + break + } + } + if ok { + return t + } + } + } +} \ No newline at end of file diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.java b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.java new file mode 100644 index 0000000000000..68f206c9536dd --- /dev/null +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public String findLatestTime(String s) { + for (int h = 11;; h--) { + for (int m = 59; m >= 0; m--) { + String t = String.format("%02d:%02d", h, m); + boolean ok = true; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != '?' && s.charAt(i) != t.charAt(i)) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } + } +} \ No newline at end of file diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.py b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.py new file mode 100644 index 0000000000000..dc3b0c093ad77 --- /dev/null +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def findLatestTime(self, s: str) -> str: + for h in range(11, -1, -1): + for m in range(59, -1, -1): + t = f"{h:02d}:{m:02d}" + if all(a == b for a, b in zip(s, t) if a != "?"): + return t diff --git a/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.ts b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.ts new file mode 100644 index 0000000000000..6eed366fcc986 --- /dev/null +++ b/solution/3100-3199/3114.Latest Time You Can Obtain After Replacing Characters/Solution.ts @@ -0,0 +1,17 @@ +function findLatestTime(s: string): string { + for (let h = 11; ; h--) { + for (let m = 59; m >= 0; m--) { + const t: string = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`; + let ok: boolean = true; + for (let i = 0; i < s.length; i++) { + if (s[i] !== '?' && s[i] !== t[i]) { + ok = false; + break; + } + } + if (ok) { + return t; + } + } + } +} diff --git a/solution/3100-3199/3115.Maximum Prime Difference/README.md b/solution/3100-3199/3115.Maximum Prime Difference/README.md index a063a96ab052c..746141c7d600c 100644 --- a/solution/3100-3199/3115.Maximum Prime Difference/README.md +++ b/solution/3100-3199/3115.Maximum Prime Difference/README.md @@ -46,24 +46,137 @@ ## 解法 -### 方法一 +### 方法一:遍历 + +根据题目描述,我们需要找出第一个素数所在的下标 $i$,然后找出最后一个素数所在的下标 $j$,将 $j - i$ 作为答案返回即可。 + +因此,我们可以从左到右遍历数组,找到第一个素数所在的下标 $i$,然后从右到左遍历数组,找到最后一个素数所在的下标 $j$,答案即为 $j - i$。 + +时间复杂度 $O(n \times \sqrt{M})$,其中 $n$ 和 $M$ 分别是数组 $nums$ 的长度和数组中的最大值。空间复杂度 $O(1)$。 ```python - +class Solution: + def maximumPrimeDifference(self, nums: List[int]) -> int: + def is_prime(x: int) -> bool: + if x < 2: + return False + return all(x % i for i in range(2, int(sqrt(x)) + 1)) + + for i, x in enumerate(nums): + if is_prime(x): + for j in range(len(nums) - 1, i - 1, -1): + if is_prime(nums[j]): + return j - i ``` ```java - +class Solution { + public int maximumPrimeDifference(int[] nums) { + for (int i = 0;; ++i) { + if (isPrime(nums[i])) { + for (int j = nums.length - 1;; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } + } + + private boolean isPrime(int x) { + if (x < 2) { + return false; + } + for (int v = 2; v * v <= x; ++v) { + if (x % v == 0) { + return false; + } + } + return true; + } +} ``` ```cpp - +class Solution { +public: + int maximumPrimeDifference(vector& nums) { + for (int i = 0;; ++i) { + if (isPrime(nums[i])) { + for (int j = nums.size() - 1;; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } + } + + bool isPrime(int n) { + if (n < 2) { + return false; + } + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +}; ``` ```go +func maximumPrimeDifference(nums []int) int { + for i := 0; ; i++ { + if isPrime(nums[i]) { + for j := len(nums) - 1; ; j-- { + if isPrime(nums[j]) { + return j - i + } + } + } + } +} + +func isPrime(n int) bool { + if n < 2 { + return false + } + for i := 2; i <= n/i; i++ { + if n%i == 0 { + return false + } + } + return true +} +``` +```ts +function maximumPrimeDifference(nums: number[]): number { + const isPrime = (x: number): boolean => { + if (x < 2) { + return false; + } + for (let i = 2; i <= x / i; i++) { + if (x % i === 0) { + return false; + } + } + return true; + }; + for (let i = 0; ; ++i) { + if (isPrime(nums[i])) { + for (let j = nums.length - 1; ; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } +} ``` diff --git a/solution/3100-3199/3115.Maximum Prime Difference/README_EN.md b/solution/3100-3199/3115.Maximum Prime Difference/README_EN.md index ea98cee50aa02..cbd7e11ca07ba 100644 --- a/solution/3100-3199/3115.Maximum Prime Difference/README_EN.md +++ b/solution/3100-3199/3115.Maximum Prime Difference/README_EN.md @@ -42,24 +42,137 @@ ## Solutions -### Solution 1 +### Solution 1: Traversal + +According to the problem description, we need to find the index $i$ of the first prime number, then find the index $j$ of the last prime number, and return $j - i$ as the answer. + +Therefore, we can traverse the array from left to right to find the index $i$ of the first prime number, then traverse the array from right to left to find the index $j$ of the last prime number. The answer is $j - i$. + +The time complexity is $O(n \times \sqrt{M})$, where $n$ and $M$ are the length of the array $nums$ and the maximum value in the array, respectively. The space complexity is $O(1)$. ```python - +class Solution: + def maximumPrimeDifference(self, nums: List[int]) -> int: + def is_prime(x: int) -> bool: + if x < 2: + return False + return all(x % i for i in range(2, int(sqrt(x)) + 1)) + + for i, x in enumerate(nums): + if is_prime(x): + for j in range(len(nums) - 1, i - 1, -1): + if is_prime(nums[j]): + return j - i ``` ```java - +class Solution { + public int maximumPrimeDifference(int[] nums) { + for (int i = 0;; ++i) { + if (isPrime(nums[i])) { + for (int j = nums.length - 1;; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } + } + + private boolean isPrime(int x) { + if (x < 2) { + return false; + } + for (int v = 2; v * v <= x; ++v) { + if (x % v == 0) { + return false; + } + } + return true; + } +} ``` ```cpp - +class Solution { +public: + int maximumPrimeDifference(vector& nums) { + for (int i = 0;; ++i) { + if (isPrime(nums[i])) { + for (int j = nums.size() - 1;; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } + } + + bool isPrime(int n) { + if (n < 2) { + return false; + } + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +}; ``` ```go +func maximumPrimeDifference(nums []int) int { + for i := 0; ; i++ { + if isPrime(nums[i]) { + for j := len(nums) - 1; ; j-- { + if isPrime(nums[j]) { + return j - i + } + } + } + } +} + +func isPrime(n int) bool { + if n < 2 { + return false + } + for i := 2; i <= n/i; i++ { + if n%i == 0 { + return false + } + } + return true +} +``` +```ts +function maximumPrimeDifference(nums: number[]): number { + const isPrime = (x: number): boolean => { + if (x < 2) { + return false; + } + for (let i = 2; i <= x / i; i++) { + if (x % i === 0) { + return false; + } + } + return true; + }; + for (let i = 0; ; ++i) { + if (isPrime(nums[i])) { + for (let j = nums.length - 1; ; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } +} ``` diff --git a/solution/3100-3199/3115.Maximum Prime Difference/Solution.cpp b/solution/3100-3199/3115.Maximum Prime Difference/Solution.cpp new file mode 100644 index 0000000000000..6d86442bfc8ae --- /dev/null +++ b/solution/3100-3199/3115.Maximum Prime Difference/Solution.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + int maximumPrimeDifference(vector& nums) { + for (int i = 0;; ++i) { + if (isPrime(nums[i])) { + for (int j = nums.size() - 1;; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } + } + + bool isPrime(int n) { + if (n < 2) { + return false; + } + for (int i = 2; i <= n / i; ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3115.Maximum Prime Difference/Solution.go b/solution/3100-3199/3115.Maximum Prime Difference/Solution.go new file mode 100644 index 0000000000000..1493bb8ea117d --- /dev/null +++ b/solution/3100-3199/3115.Maximum Prime Difference/Solution.go @@ -0,0 +1,23 @@ +func maximumPrimeDifference(nums []int) int { + for i := 0; ; i++ { + if isPrime(nums[i]) { + for j := len(nums) - 1; ; j-- { + if isPrime(nums[j]) { + return j - i + } + } + } + } +} + +func isPrime(n int) bool { + if n < 2 { + return false + } + for i := 2; i <= n/i; i++ { + if n%i == 0 { + return false + } + } + return true +} \ No newline at end of file diff --git a/solution/3100-3199/3115.Maximum Prime Difference/Solution.java b/solution/3100-3199/3115.Maximum Prime Difference/Solution.java new file mode 100644 index 0000000000000..970bc89ae3342 --- /dev/null +++ b/solution/3100-3199/3115.Maximum Prime Difference/Solution.java @@ -0,0 +1,25 @@ +class Solution { + public int maximumPrimeDifference(int[] nums) { + for (int i = 0;; ++i) { + if (isPrime(nums[i])) { + for (int j = nums.length - 1;; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } + } + + private boolean isPrime(int x) { + if (x < 2) { + return false; + } + for (int v = 2; v * v <= x; ++v) { + if (x % v == 0) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3115.Maximum Prime Difference/Solution.py b/solution/3100-3199/3115.Maximum Prime Difference/Solution.py new file mode 100644 index 0000000000000..c7c99bf943b1d --- /dev/null +++ b/solution/3100-3199/3115.Maximum Prime Difference/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def maximumPrimeDifference(self, nums: List[int]) -> int: + def is_prime(x: int) -> bool: + if x < 2: + return False + return all(x % i for i in range(2, int(sqrt(x)) + 1)) + + for i, x in enumerate(nums): + if is_prime(x): + for j in range(len(nums) - 1, i - 1, -1): + if is_prime(nums[j]): + return j - i diff --git a/solution/3100-3199/3115.Maximum Prime Difference/Solution.ts b/solution/3100-3199/3115.Maximum Prime Difference/Solution.ts new file mode 100644 index 0000000000000..c1b15a6f27efa --- /dev/null +++ b/solution/3100-3199/3115.Maximum Prime Difference/Solution.ts @@ -0,0 +1,22 @@ +function maximumPrimeDifference(nums: number[]): number { + const isPrime = (x: number): boolean => { + if (x < 2) { + return false; + } + for (let i = 2; i <= x / i; i++) { + if (x % i === 0) { + return false; + } + } + return true; + }; + for (let i = 0; ; ++i) { + if (isPrime(nums[i])) { + for (let j = nums.length - 1; ; --j) { + if (isPrime(nums[j])) { + return j - i; + } + } + } + } +}