diff --git a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README.md b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README.md index bca847888e4c5..f4af2f9077b3a 100644 --- a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README.md +++ b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README.md @@ -71,7 +71,13 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们用一个哈希表 $\textit{s}$ 记录数组 $\textit{nums}$ 中的所有数字。 + +接下来,我们从 $\textit{original}$ 开始,如果 $\textit{original}$ 在 $\textit{s}$ 中,我们将 $\textit{original}$ 乘以 $2$,直到 $\textit{original}$ 不在 $\textit{s}$ 中,返回 $\textit{original}$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -110,9 +116,10 @@ class Solution { class Solution { public: int findFinalValue(vector& nums, int original) { - unordered_set s; - for (int num : nums) s.insert(num); - while (s.count(original)) original <<= 1; + unordered_set s(nums.begin(), nums.end()); + while (s.contains(original)) { + original <<= 1; + } return original; } }; @@ -122,9 +129,9 @@ public: ```go func findFinalValue(nums []int, original int) int { - s := make(map[int]bool) - for _, num := range nums { - s[num] = true + s := map[int]bool{} + for _, x := range nums { + s[x] = true } for s[original] { original <<= 1 @@ -137,9 +144,9 @@ func findFinalValue(nums []int, original int) int { ```ts function findFinalValue(nums: number[], original: number): number { - let set: Set = new Set(nums); - while (set.has(original)) { - original *= 2; + const s: Set = new Set([...nums]); + while (s.has(original)) { + original <<= 1; } return original; } diff --git a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README_EN.md b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README_EN.md index 98b26eff9d52a..6d4f23cb49bf0 100644 --- a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README_EN.md +++ b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/README_EN.md @@ -69,7 +69,13 @@ tags: -### Solution 1 +### Solution 1: Hash Table + +We use a hash table $\textit{s}$ to record all the numbers in the array $\textit{nums}$. + +Next, starting from $\textit{original}$, if $\textit{original}$ is in $\textit{s}$, we multiply $\textit{original}$ by $2$ until $\textit{original}$ is not in $\textit{s}$ anymore, then return $\textit{original}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -108,9 +114,10 @@ class Solution { class Solution { public: int findFinalValue(vector& nums, int original) { - unordered_set s; - for (int num : nums) s.insert(num); - while (s.count(original)) original <<= 1; + unordered_set s(nums.begin(), nums.end()); + while (s.contains(original)) { + original <<= 1; + } return original; } }; @@ -120,9 +127,9 @@ public: ```go func findFinalValue(nums []int, original int) int { - s := make(map[int]bool) - for _, num := range nums { - s[num] = true + s := map[int]bool{} + for _, x := range nums { + s[x] = true } for s[original] { original <<= 1 @@ -135,9 +142,9 @@ func findFinalValue(nums []int, original int) int { ```ts function findFinalValue(nums: number[], original: number): number { - let set: Set = new Set(nums); - while (set.has(original)) { - original *= 2; + const s: Set = new Set([...nums]); + while (s.has(original)) { + original <<= 1; } return original; } diff --git a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.cpp b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.cpp index 230789e06eb05..49f771e205d59 100644 --- a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.cpp +++ b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.cpp @@ -1,9 +1,10 @@ class Solution { public: int findFinalValue(vector& nums, int original) { - unordered_set s; - for (int num : nums) s.insert(num); - while (s.count(original)) original <<= 1; + unordered_set s(nums.begin(), nums.end()); + while (s.contains(original)) { + original <<= 1; + } return original; } }; \ No newline at end of file diff --git a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.go b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.go index 9d20d42cd7b74..0bf0675015e1c 100644 --- a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.go +++ b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.go @@ -1,7 +1,7 @@ func findFinalValue(nums []int, original int) int { - s := make(map[int]bool) - for _, num := range nums { - s[num] = true + s := map[int]bool{} + for _, x := range nums { + s[x] = true } for s[original] { original <<= 1 diff --git a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.ts b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.ts index d44e4f74b4847..137178dd9ed28 100644 --- a/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.ts +++ b/solution/2100-2199/2154.Keep Multiplying Found Values by Two/Solution.ts @@ -1,7 +1,7 @@ function findFinalValue(nums: number[], original: number): number { - let set: Set = new Set(nums); - while (set.has(original)) { - original *= 2; + const s: Set = new Set([...nums]); + while (s.has(original)) { + original <<= 1; } return original; } diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README.md b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README.md index afa9f90ecb617..c9b3b2dcf9109 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README.md +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README.md @@ -84,7 +84,15 @@ tags: -### 方法一 +### 方法一:前缀和 + +我们从 $i = 0$ 开始,用两个变量 $\textit{l0}$ 和 $\textit{r1}$ 分别记录 $i$ 左侧和右侧的 $1$ 的个数,初始时 $\textit{l0} = 0$,而 $\textit{r1} = \sum \textit{nums}$。 + +我们遍历数组 $\textit{nums}$,对于每个 $i$,更新 $\textit{l0}$ 和 $\textit{r1}$,计算当前分组得分 $t = \textit{l0} + \textit{r1}$,如果 $t$ 等于当前最大分组得分 $\textit{mx}$,则将 $i$ 加入答案数组,如果 $t$ 大于 $\textit{mx}$,则更新 $\textit{mx}$ 为 $t$,并将答案数组清空,然后将 $i$ 加入答案数组。 + +遍历结束后,返回答案数组。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -93,20 +101,18 @@ tags: ```python class Solution: def maxScoreIndices(self, nums: List[int]) -> List[int]: - left, right = 0, sum(nums) - mx = right + l0, r1 = 0, sum(nums) + mx = r1 ans = [0] - for i, num in enumerate(nums): - if num == 0: - left += 1 - else: - right -= 1 - t = left + right + for i, x in enumerate(nums, 1): + l0 += x ^ 1 + r1 -= x + t = l0 + r1 if mx == t: - ans.append(i + 1) + ans.append(i) elif mx < t: mx = t - ans = [i + 1] + ans = [i] return ans ``` @@ -114,37 +120,26 @@ class Solution: ```java class Solution { - public List maxScoreIndices(int[] nums) { - int left = 0, right = sum(nums); - int mx = right; + int l0 = 0, r1 = Arrays.stream(nums).sum(); + int mx = r1; List ans = new ArrayList<>(); ans.add(0); - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++left; - } else { - --right; - } - int t = left + right; + for (int i = 1; i <= nums.length; ++i) { + int x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + int t = l0 + r1; if (mx == t) { - ans.add(i + 1); + ans.add(i); } else if (mx < t) { mx = t; ans.clear(); - ans.add(i + 1); + ans.add(i); } } return ans; } - - private int sum(int[] nums) { - int s = 0; - for (int num : nums) { - s += num; - } - return s; - } } ``` @@ -154,22 +149,19 @@ class Solution { class Solution { public: vector maxScoreIndices(vector& nums) { - int left = 0, right = accumulate(nums.begin(), nums.end(), 0); - int mx = right; - vector ans; - ans.push_back(0); - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) - ++left; - else - --right; - int t = left + right; - if (mx == t) - ans.push_back(i + 1); - else if (mx < t) { + int l0 = 0, r1 = accumulate(nums.begin(), nums.end(), 0); + int mx = r1; + vector ans = {0}; + for (int i = 1; i <= nums.size(); ++i) { + int x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + int t = l0 + r1; + if (mx == t) { + ans.push_back(i); + } else if (mx < t) { mx = t; - ans.clear(); - ans.push_back(i + 1); + ans = {i}; } } return ans; @@ -181,19 +173,16 @@ public: ```go func maxScoreIndices(nums []int) []int { - left, right := 0, 0 - for _, num := range nums { - right += num + l0, r1 := 0, 0 + for _, x := range nums { + r1 += x } - mx := right + mx := r1 ans := []int{0} - for i, num := range nums { - if num == 0 { - left++ - } else { - right-- - } - t := left + right + for i, x := range nums { + l0 += x ^ 1 + r1 -= x + t := l0 + r1 if mx == t { ans = append(ans, i+1) } else if mx < t { @@ -210,22 +199,19 @@ func maxScoreIndices(nums []int) []int { ```ts function maxScoreIndices(nums: number[]): number[] { const n = nums.length; - const total = nums.reduce((a, c) => a + c, 0); - let left = 0, - right = total; - let record: Array = [total]; - for (const num of nums) { - if (num == 0) { - left++; - } else { - right--; - } - record.push(left + right); - } - const max = Math.max(...record); - let ans: Array = []; - for (let i = 0; i <= n; i++) { - if (record[i] == max) { + let [l0, r1] = [0, nums.reduce((a, b) => a + b, 0)]; + let mx = r1; + const ans: number[] = [0]; + for (let i = 1; i <= n; ++i) { + const x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + const t = l0 + r1; + if (mx === t) { + ans.push(i); + } else if (mx < t) { + mx = t; + ans.length = 0; ans.push(i); } } @@ -233,6 +219,34 @@ function maxScoreIndices(nums: number[]): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_score_indices(nums: Vec) -> Vec { + let mut l0 = 0; + let mut r1: i32 = nums.iter().sum(); + let mut mx = r1; + let mut ans = vec![0]; + + for i in 1..=nums.len() { + let x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + let t = l0 + r1; + if mx == t { + ans.push(i as i32); + } else if mx < t { + mx = t; + ans = vec![i as i32]; + } + } + + ans + } +} +``` + diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README_EN.md b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README_EN.md index 32825443cbf7b..2ef4a231ff1cd 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README_EN.md +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/README_EN.md @@ -85,7 +85,15 @@ Only index 0 has the highest possible division score 2. -### Solution 1 +### Solution 1: Prefix Sum + +We start from $i = 0$, using two variables $\textit{l0}$ and $\textit{r1}$ to respectively record the number of $1$s to the left and right of $i$. Initially, $\textit{l0} = 0$, while $\textit{r1} = \sum \textit{nums}$. + +We iterate through the array $\textit{nums}$. For each $i$, we update $\textit{l0}$ and $\textit{r1}$, calculate the current grouping score $t = \textit{l0} + \textit{r1}$. If $t$ equals the current maximum grouping score $\textit{mx}$, then we add $i$ to the answer array. If $t$ is greater than $\textit{mx}$, we update $\textit{mx}$ to $t$, clear the answer array, and then add $i$ to the answer array. + +After the iteration ends, we return the answer array. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -94,20 +102,18 @@ Only index 0 has the highest possible division score 2. ```python class Solution: def maxScoreIndices(self, nums: List[int]) -> List[int]: - left, right = 0, sum(nums) - mx = right + l0, r1 = 0, sum(nums) + mx = r1 ans = [0] - for i, num in enumerate(nums): - if num == 0: - left += 1 - else: - right -= 1 - t = left + right + for i, x in enumerate(nums, 1): + l0 += x ^ 1 + r1 -= x + t = l0 + r1 if mx == t: - ans.append(i + 1) + ans.append(i) elif mx < t: mx = t - ans = [i + 1] + ans = [i] return ans ``` @@ -115,37 +121,26 @@ class Solution: ```java class Solution { - public List maxScoreIndices(int[] nums) { - int left = 0, right = sum(nums); - int mx = right; + int l0 = 0, r1 = Arrays.stream(nums).sum(); + int mx = r1; List ans = new ArrayList<>(); ans.add(0); - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++left; - } else { - --right; - } - int t = left + right; + for (int i = 1; i <= nums.length; ++i) { + int x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + int t = l0 + r1; if (mx == t) { - ans.add(i + 1); + ans.add(i); } else if (mx < t) { mx = t; ans.clear(); - ans.add(i + 1); + ans.add(i); } } return ans; } - - private int sum(int[] nums) { - int s = 0; - for (int num : nums) { - s += num; - } - return s; - } } ``` @@ -155,22 +150,19 @@ class Solution { class Solution { public: vector maxScoreIndices(vector& nums) { - int left = 0, right = accumulate(nums.begin(), nums.end(), 0); - int mx = right; - vector ans; - ans.push_back(0); - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) - ++left; - else - --right; - int t = left + right; - if (mx == t) - ans.push_back(i + 1); - else if (mx < t) { + int l0 = 0, r1 = accumulate(nums.begin(), nums.end(), 0); + int mx = r1; + vector ans = {0}; + for (int i = 1; i <= nums.size(); ++i) { + int x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + int t = l0 + r1; + if (mx == t) { + ans.push_back(i); + } else if (mx < t) { mx = t; - ans.clear(); - ans.push_back(i + 1); + ans = {i}; } } return ans; @@ -182,19 +174,16 @@ public: ```go func maxScoreIndices(nums []int) []int { - left, right := 0, 0 - for _, num := range nums { - right += num + l0, r1 := 0, 0 + for _, x := range nums { + r1 += x } - mx := right + mx := r1 ans := []int{0} - for i, num := range nums { - if num == 0 { - left++ - } else { - right-- - } - t := left + right + for i, x := range nums { + l0 += x ^ 1 + r1 -= x + t := l0 + r1 if mx == t { ans = append(ans, i+1) } else if mx < t { @@ -211,22 +200,19 @@ func maxScoreIndices(nums []int) []int { ```ts function maxScoreIndices(nums: number[]): number[] { const n = nums.length; - const total = nums.reduce((a, c) => a + c, 0); - let left = 0, - right = total; - let record: Array = [total]; - for (const num of nums) { - if (num == 0) { - left++; - } else { - right--; - } - record.push(left + right); - } - const max = Math.max(...record); - let ans: Array = []; - for (let i = 0; i <= n; i++) { - if (record[i] == max) { + let [l0, r1] = [0, nums.reduce((a, b) => a + b, 0)]; + let mx = r1; + const ans: number[] = [0]; + for (let i = 1; i <= n; ++i) { + const x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + const t = l0 + r1; + if (mx === t) { + ans.push(i); + } else if (mx < t) { + mx = t; + ans.length = 0; ans.push(i); } } @@ -234,6 +220,34 @@ function maxScoreIndices(nums: number[]): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_score_indices(nums: Vec) -> Vec { + let mut l0 = 0; + let mut r1: i32 = nums.iter().sum(); + let mut mx = r1; + let mut ans = vec![0]; + + for i in 1..=nums.len() { + let x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + let t = l0 + r1; + if mx == t { + ans.push(i as i32); + } else if mx < t { + mx = t; + ans = vec![i as i32]; + } + } + + ans + } +} +``` + diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp index e2c840cb86711..f03017a72e497 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp @@ -1,22 +1,19 @@ class Solution { public: vector maxScoreIndices(vector& nums) { - int left = 0, right = accumulate(nums.begin(), nums.end(), 0); - int mx = right; - vector ans; - ans.push_back(0); - for (int i = 0; i < nums.size(); ++i) { - if (nums[i] == 0) - ++left; - else - --right; - int t = left + right; - if (mx == t) - ans.push_back(i + 1); - else if (mx < t) { + int l0 = 0, r1 = accumulate(nums.begin(), nums.end(), 0); + int mx = r1; + vector ans = {0}; + for (int i = 1; i <= nums.size(); ++i) { + int x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + int t = l0 + r1; + if (mx == t) { + ans.push_back(i); + } else if (mx < t) { mx = t; - ans.clear(); - ans.push_back(i + 1); + ans = {i}; } } return ans; diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.go b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.go index 77368d38418c4..a241280358aaf 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.go +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.go @@ -1,17 +1,14 @@ func maxScoreIndices(nums []int) []int { - left, right := 0, 0 - for _, num := range nums { - right += num + l0, r1 := 0, 0 + for _, x := range nums { + r1 += x } - mx := right + mx := r1 ans := []int{0} - for i, num := range nums { - if num == 0 { - left++ - } else { - right-- - } - t := left + right + for i, x := range nums { + l0 += x ^ 1 + r1 -= x + t := l0 + r1 if mx == t { ans = append(ans, i+1) } else if mx < t { diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.java b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.java index abb45aa11d3ae..1a76ab445d329 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.java +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.java @@ -1,33 +1,22 @@ class Solution { - public List maxScoreIndices(int[] nums) { - int left = 0, right = sum(nums); - int mx = right; + int l0 = 0, r1 = Arrays.stream(nums).sum(); + int mx = r1; List ans = new ArrayList<>(); ans.add(0); - for (int i = 0; i < nums.length; ++i) { - if (nums[i] == 0) { - ++left; - } else { - --right; - } - int t = left + right; + for (int i = 1; i <= nums.length; ++i) { + int x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + int t = l0 + r1; if (mx == t) { - ans.add(i + 1); + ans.add(i); } else if (mx < t) { mx = t; ans.clear(); - ans.add(i + 1); + ans.add(i); } } return ans; } - - private int sum(int[] nums) { - int s = 0; - for (int num : nums) { - s += num; - } - return s; - } } \ No newline at end of file diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.py b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.py index 08306c5f4b4db..7eee78e794f47 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.py +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.py @@ -1,17 +1,15 @@ class Solution: def maxScoreIndices(self, nums: List[int]) -> List[int]: - left, right = 0, sum(nums) - mx = right + l0, r1 = 0, sum(nums) + mx = r1 ans = [0] - for i, num in enumerate(nums): - if num == 0: - left += 1 - else: - right -= 1 - t = left + right + for i, x in enumerate(nums, 1): + l0 += x ^ 1 + r1 -= x + t = l0 + r1 if mx == t: - ans.append(i + 1) + ans.append(i) elif mx < t: mx = t - ans = [i + 1] + ans = [i] return ans diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.rs b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.rs new file mode 100644 index 0000000000000..7c1d77ac27184 --- /dev/null +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn max_score_indices(nums: Vec) -> Vec { + let mut l0 = 0; + let mut r1: i32 = nums.iter().sum(); + let mut mx = r1; + let mut ans = vec![0]; + + for i in 1..=nums.len() { + let x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + let t = l0 + r1; + if mx == t { + ans.push(i as i32); + } else if mx < t { + mx = t; + ans = vec![i as i32]; + } + } + + ans + } +} diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.ts b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.ts index 9dfb2fa8d2935..b65d600083e1d 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.ts +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.ts @@ -1,21 +1,18 @@ function maxScoreIndices(nums: number[]): number[] { const n = nums.length; - const total = nums.reduce((a, c) => a + c, 0); - let left = 0, - right = total; - let record: Array = [total]; - for (const num of nums) { - if (num == 0) { - left++; - } else { - right--; - } - record.push(left + right); - } - const max = Math.max(...record); - let ans: Array = []; - for (let i = 0; i <= n; i++) { - if (record[i] == max) { + let [l0, r1] = [0, nums.reduce((a, b) => a + b, 0)]; + let mx = r1; + const ans: number[] = [0]; + for (let i = 1; i <= n; ++i) { + const x = nums[i - 1]; + l0 += x ^ 1; + r1 -= x; + const t = l0 + r1; + if (mx === t) { + ans.push(i); + } else if (mx < t) { + mx = t; + ans.length = 0; ans.push(i); } }