Skip to content

feat: add solutions to lc problems: No.2154,2155 #3281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表

我们用一个哈希表 $\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}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -110,9 +116,10 @@ class Solution {
class Solution {
public:
int findFinalValue(vector<int>& nums, int original) {
unordered_set<int> s;
for (int num : nums) s.insert(num);
while (s.count(original)) original <<= 1;
unordered_set<int> s(nums.begin(), nums.end());
while (s.contains(original)) {
original <<= 1;
}
return original;
}
};
Expand All @@ -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
Expand All @@ -137,9 +144,9 @@ func findFinalValue(nums []int, original int) int {

```ts
function findFinalValue(nums: number[], original: number): number {
let set: Set<number> = new Set(nums);
while (set.has(original)) {
original *= 2;
const s: Set<number> = new Set([...nums]);
while (s.has(original)) {
original <<= 1;
}
return original;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ tags:

<!-- solution:start -->

### 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}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -108,9 +114,10 @@ class Solution {
class Solution {
public:
int findFinalValue(vector<int>& nums, int original) {
unordered_set<int> s;
for (int num : nums) s.insert(num);
while (s.count(original)) original <<= 1;
unordered_set<int> s(nums.begin(), nums.end());
while (s.contains(original)) {
original <<= 1;
}
return original;
}
};
Expand All @@ -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
Expand All @@ -135,9 +142,9 @@ func findFinalValue(nums []int, original int) int {

```ts
function findFinalValue(nums: number[], original: number): number {
let set: Set<number> = new Set(nums);
while (set.has(original)) {
original *= 2;
const s: Set<number> = new Set([...nums]);
while (s.has(original)) {
original <<= 1;
}
return original;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Solution {
public:
int findFinalValue(vector<int>& nums, int original) {
unordered_set<int> s;
for (int num : nums) s.insert(num);
while (s.count(original)) original <<= 1;
unordered_set<int> s(nums.begin(), nums.end());
while (s.contains(original)) {
original <<= 1;
}
return original;
}
};
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function findFinalValue(nums: number[], original: number): number {
let set: Set<number> = new Set(nums);
while (set.has(original)) {
original *= 2;
const s: Set<number> = new Set([...nums]);
while (s.has(original)) {
original <<= 1;
}
return original;
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:前缀和

我们从 $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)$。

<!-- tabs:start -->

Expand All @@ -93,58 +101,45 @@ 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
```

#### Java

```java
class Solution {

public List<Integer> maxScoreIndices(int[] nums) {
int left = 0, right = sum(nums);
int mx = right;
int l0 = 0, r1 = Arrays.stream(nums).sum();
int mx = r1;
List<Integer> 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;
}
}
```

Expand All @@ -154,22 +149,19 @@ class Solution {
class Solution {
public:
vector<int> maxScoreIndices(vector<int>& nums) {
int left = 0, right = accumulate(nums.begin(), nums.end(), 0);
int mx = right;
vector<int> 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<int> 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;
Expand All @@ -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 {
Expand All @@ -210,29 +199,54 @@ 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<number> = [total];
for (const num of nums) {
if (num == 0) {
left++;
} else {
right--;
}
record.push(left + right);
}
const max = Math.max(...record);
let ans: Array<number> = [];
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);
}
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn max_score_indices(nums: Vec<i32>) -> Vec<i32> {
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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading