diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/README.md b/solution/3700-3799/3718.Smallest Missing Multiple of K/README.md index 1af5cb45c646d..9535df65fab9a 100644 --- a/solution/3700-3799/3718.Smallest Missing Multiple of K/README.md +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/README.md @@ -60,32 +60,93 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3718.Sm -### 方法一 +### 方法一:哈希表 + 枚举 + +我们先用一个哈希表 $\textit{s}$ 存储数组 $\textit{nums}$ 中出现的数字。然后从 $k$ 的第一个正倍数 $k \times 1$ 开始,依次枚举每个正倍数,直到找到第一个不在哈希表 $\textit{s}$ 中出现的倍数,即为答案。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def missingMultiple(self, nums: List[int], k: int) -> int: + s = set(nums) + for i in count(1): + x = k * i + if x not in s: + return x ``` #### Java ```java - +class Solution { + public int missingMultiple(int[] nums, int k) { + boolean[] s = new boolean[101]; + for (int x : nums) { + s[x] = true; + } + for (int i = 1;; ++i) { + int x = k * i; + if (x >= s.length || !s[x]) { + return x; + } + } + } +} ``` #### C++ ```cpp - +class Solution { +public: + int missingMultiple(vector& nums, int k) { + unordered_set s; + for (int x : nums) { + s.insert(x); + } + for (int i = 1;; ++i) { + int x = k * i; + if (!s.contains(x)) { + return x; + } + } + } +}; ``` #### Go ```go +func missingMultiple(nums []int, k int) int { + s := map[int]bool{} + for _, x := range nums { + s[x] = true + } + for i := 1; ; i++ { + if x := k * i; !s[x] { + return x + } + } +} +``` +#### TypeScript + +```ts +function missingMultiple(nums: number[], k: number): number { + const s = new Set(nums); + for (let i = 1; ; ++i) { + const x = k * i; + if (!s.has(x)) { + return x; + } + } +} ``` diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/README_EN.md b/solution/3700-3799/3718.Smallest Missing Multiple of K/README_EN.md index 89fd68a9b8009..97eb083ab9b0e 100644 --- a/solution/3700-3799/3718.Smallest Missing Multiple of K/README_EN.md +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/README_EN.md @@ -58,32 +58,93 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3718.Sm -### Solution 1 +### Solution 1: Hash Table + Enumeration + +We first use a hash table $\textit{s}$ to store the numbers that appear in the array $\textit{nums}$. Then, starting from the first positive multiple of $k$, which is $k \times 1$, we enumerate each positive multiple in sequence until we find the first multiple that does not appear in the hash table $\textit{s}$, which is the answer. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. #### Python3 ```python - +class Solution: + def missingMultiple(self, nums: List[int], k: int) -> int: + s = set(nums) + for i in count(1): + x = k * i + if x not in s: + return x ``` #### Java ```java - +class Solution { + public int missingMultiple(int[] nums, int k) { + boolean[] s = new boolean[101]; + for (int x : nums) { + s[x] = true; + } + for (int i = 1;; ++i) { + int x = k * i; + if (x >= s.length || !s[x]) { + return x; + } + } + } +} ``` #### C++ ```cpp - +class Solution { +public: + int missingMultiple(vector& nums, int k) { + unordered_set s; + for (int x : nums) { + s.insert(x); + } + for (int i = 1;; ++i) { + int x = k * i; + if (!s.contains(x)) { + return x; + } + } + } +}; ``` #### Go ```go +func missingMultiple(nums []int, k int) int { + s := map[int]bool{} + for _, x := range nums { + s[x] = true + } + for i := 1; ; i++ { + if x := k * i; !s[x] { + return x + } + } +} +``` +#### TypeScript + +```ts +function missingMultiple(nums: number[], k: number): number { + const s = new Set(nums); + for (let i = 1; ; ++i) { + const x = k * i; + if (!s.has(x)) { + return x; + } + } +} ``` diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.cpp b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.cpp new file mode 100644 index 0000000000000..374ffaa71975e --- /dev/null +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int missingMultiple(vector& nums, int k) { + unordered_set s; + for (int x : nums) { + s.insert(x); + } + for (int i = 1;; ++i) { + int x = k * i; + if (!s.contains(x)) { + return x; + } + } + } +}; diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.go b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.go new file mode 100644 index 0000000000000..f317107040a21 --- /dev/null +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.go @@ -0,0 +1,11 @@ +func missingMultiple(nums []int, k int) int { + s := map[int]bool{} + for _, x := range nums { + s[x] = true + } + for i := 1; ; i++ { + if x := k * i; !s[x] { + return x + } + } +} diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.java b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.java new file mode 100644 index 0000000000000..548dfd232fa39 --- /dev/null +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int missingMultiple(int[] nums, int k) { + boolean[] s = new boolean[101]; + for (int x : nums) { + s[x] = true; + } + for (int i = 1;; ++i) { + int x = k * i; + if (x >= s.length || !s[x]) { + return x; + } + } + } +} diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.py b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.py new file mode 100644 index 0000000000000..9430afcee8d4d --- /dev/null +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def missingMultiple(self, nums: List[int], k: int) -> int: + s = set(nums) + for i in count(1): + x = k * i + if x not in s: + return x diff --git a/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.ts b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.ts new file mode 100644 index 0000000000000..1040f9cbb6e69 --- /dev/null +++ b/solution/3700-3799/3718.Smallest Missing Multiple of K/Solution.ts @@ -0,0 +1,9 @@ +function missingMultiple(nums: number[], k: number): number { + const s = new Set(nums); + for (let i = 1; ; ++i) { + const x = k * i; + if (!s.has(x)) { + return x; + } + } +} diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/README.md b/solution/3700-3799/3719.Longest Balanced Subarray I/README.md index bcf31028431c7..dcee0fa22643e 100644 --- a/solution/3700-3799/3719.Longest Balanced Subarray I/README.md +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/README.md @@ -85,32 +85,126 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3719.Lo -### 方法一 +### 方法一:哈希表 + 枚举 + +我们可以枚举子数组的左端点 $i$,然后从左端点开始向右枚举右端点 $j$,在枚举的过程中使用一个哈希表 $\textit{vis}$ 来记录子数组中出现过的数字,同时使用一个长度为 $2$ 的数组 $\textit{cnt}$ 来分别记录子数组中不同偶数和不同奇数的数量。当 $\textit{cnt}[0] = \textit{cnt}[1]$ 时,更新答案 $\textit{ans} = \max(\textit{ans}, j - i + 1)$。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def longestBalanced(self, nums: List[int]) -> int: + n = len(nums) + ans = 0 + for i in range(n): + cnt = [0, 0] + vis = set() + for j in range(i, n): + if nums[j] not in vis: + cnt[nums[j] & 1] += 1 + vis.add(nums[j]) + if cnt[0] == cnt[1]: + ans = max(ans, j - i + 1) + return ans ``` #### Java ```java - +class Solution { + public int longestBalanced(int[] nums) { + int n = nums.length; + int ans = 0; + for (int i = 0; i < n; ++i) { + Set vis = new HashSet<>(); + int[] cnt = new int[2]; + for (int j = i; j < n; ++j) { + if (vis.add(nums[j])) { + ++cnt[nums[j] & 1]; + } + if (cnt[0] == cnt[1]) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int longestBalanced(vector& nums) { + int n = nums.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + unordered_set vis; + int cnt[2]{}; + for (int j = i; j < n; ++j) { + if (!vis.contains(nums[j])) { + vis.insert(nums[j]); + ++cnt[nums[j] & 1]; + } + if (cnt[0] == cnt[1]) { + ans = max(ans, j - i + 1); + } + } + } + return ans; + } +}; ``` #### Go ```go +func longestBalanced(nums []int) (ans int) { + n := len(nums) + for i := 0; i < n; i++ { + vis := map[int]bool{} + cnt := [2]int{} + for j := i; j < n; j++ { + if !vis[nums[j]] { + vis[nums[j]] = true + cnt[nums[j]&1]++ + } + if cnt[0] == cnt[1] { + ans = max(ans, j-i+1) + } + } + } + return +} +``` +#### TypeScript + +```ts +function longestBalanced(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const vis = new Set(); + const cnt: number[] = Array(2).fill(0); + for (let j = i; j < n; ++j) { + if (!vis.has(nums[j])) { + vis.add(nums[j]); + ++cnt[nums[j] & 1]; + } + if (cnt[0] === cnt[1]) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +} ``` diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/README_EN.md b/solution/3700-3799/3719.Longest Balanced Subarray I/README_EN.md index 759c12e60f2a5..e73d711e537be 100644 --- a/solution/3700-3799/3719.Longest Balanced Subarray I/README_EN.md +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/README_EN.md @@ -80,32 +80,126 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3719.Lo -### Solution 1 +### Solution 1: Hash Table + Enumeration + +We can enumerate the left endpoint $i$ of the subarray, and then enumerate the right endpoint $j$ from the left endpoint. During the enumeration process, we use a hash table $\textit{vis}$ to record the numbers that have appeared in the subarray, and use an array $\textit{cnt}$ of length $2$ to record the count of distinct even numbers and distinct odd numbers in the subarray respectively. When $\textit{cnt}[0] = \textit{cnt}[1]$, we update the answer $\textit{ans} = \max(\textit{ans}, j - i + 1)$. + +The time complexity is $O(n^2)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. #### Python3 ```python - +class Solution: + def longestBalanced(self, nums: List[int]) -> int: + n = len(nums) + ans = 0 + for i in range(n): + cnt = [0, 0] + vis = set() + for j in range(i, n): + if nums[j] not in vis: + cnt[nums[j] & 1] += 1 + vis.add(nums[j]) + if cnt[0] == cnt[1]: + ans = max(ans, j - i + 1) + return ans ``` #### Java ```java - +class Solution { + public int longestBalanced(int[] nums) { + int n = nums.length; + int ans = 0; + for (int i = 0; i < n; ++i) { + Set vis = new HashSet<>(); + int[] cnt = new int[2]; + for (int j = i; j < n; ++j) { + if (vis.add(nums[j])) { + ++cnt[nums[j] & 1]; + } + if (cnt[0] == cnt[1]) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int longestBalanced(vector& nums) { + int n = nums.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + unordered_set vis; + int cnt[2]{}; + for (int j = i; j < n; ++j) { + if (!vis.contains(nums[j])) { + vis.insert(nums[j]); + ++cnt[nums[j] & 1]; + } + if (cnt[0] == cnt[1]) { + ans = max(ans, j - i + 1); + } + } + } + return ans; + } +}; ``` #### Go ```go +func longestBalanced(nums []int) (ans int) { + n := len(nums) + for i := 0; i < n; i++ { + vis := map[int]bool{} + cnt := [2]int{} + for j := i; j < n; j++ { + if !vis[nums[j]] { + vis[nums[j]] = true + cnt[nums[j]&1]++ + } + if cnt[0] == cnt[1] { + ans = max(ans, j-i+1) + } + } + } + return +} +``` +#### TypeScript + +```ts +function longestBalanced(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const vis = new Set(); + const cnt: number[] = Array(2).fill(0); + for (let j = i; j < n; ++j) { + if (!vis.has(nums[j])) { + vis.add(nums[j]); + ++cnt[nums[j] & 1]; + } + if (cnt[0] === cnt[1]) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +} ``` diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.cpp b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.cpp new file mode 100644 index 0000000000000..ce360d42849d3 --- /dev/null +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestBalanced(vector& nums) { + int n = nums.size(); + int ans = 0; + for (int i = 0; i < n; ++i) { + unordered_set vis; + int cnt[2]{}; + for (int j = i; j < n; ++j) { + if (!vis.contains(nums[j])) { + vis.insert(nums[j]); + ++cnt[nums[j] & 1]; + } + if (cnt[0] == cnt[1]) { + ans = max(ans, j - i + 1); + } + } + } + return ans; + } +}; diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.go b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.go new file mode 100644 index 0000000000000..f5148c00fc916 --- /dev/null +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.go @@ -0,0 +1,17 @@ +func longestBalanced(nums []int) (ans int) { + n := len(nums) + for i := 0; i < n; i++ { + vis := map[int]bool{} + cnt := [2]int{} + for j := i; j < n; j++ { + if !vis[nums[j]] { + vis[nums[j]] = true + cnt[nums[j]&1]++ + } + if cnt[0] == cnt[1] { + ans = max(ans, j-i+1) + } + } + } + return +} diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.java b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.java new file mode 100644 index 0000000000000..5b529bf0d3155 --- /dev/null +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int longestBalanced(int[] nums) { + int n = nums.length; + int ans = 0; + for (int i = 0; i < n; ++i) { + Set vis = new HashSet<>(); + int[] cnt = new int[2]; + for (int j = i; j < n; ++j) { + if (vis.add(nums[j])) { + ++cnt[nums[j] & 1]; + } + if (cnt[0] == cnt[1]) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; + } +} diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.py b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.py new file mode 100644 index 0000000000000..147e23fb0cf0b --- /dev/null +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def longestBalanced(self, nums: List[int]) -> int: + n = len(nums) + ans = 0 + for i in range(n): + cnt = [0, 0] + vis = set() + for j in range(i, n): + if nums[j] not in vis: + cnt[nums[j] & 1] += 1 + vis.add(nums[j]) + if cnt[0] == cnt[1]: + ans = max(ans, j - i + 1) + return ans diff --git a/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.ts b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.ts new file mode 100644 index 0000000000000..fc2001fd002e3 --- /dev/null +++ b/solution/3700-3799/3719.Longest Balanced Subarray I/Solution.ts @@ -0,0 +1,18 @@ +function longestBalanced(nums: number[]): number { + const n = nums.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + const vis = new Set(); + const cnt: number[] = Array(2).fill(0); + for (let j = i; j < n; ++j) { + if (!vis.has(nums[j])) { + vis.add(nums[j]); + ++cnt[nums[j] & 1]; + } + if (cnt[0] === cnt[1]) { + ans = Math.max(ans, j - i + 1); + } + } + } + return ans; +}