Skip to content

Commit 23f7050

Browse files
authoredApr 28, 2024
feat: add solutions to lc problem: No.3133 (doocs#2678)
No.3133.Minimum Array End
1 parent baa1174 commit 23f7050

File tree

12 files changed

+227
-28
lines changed

12 files changed

+227
-28
lines changed
 

‎.github/workflows/black-lint.yml

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
name: black-linter
22

33
on:
4-
push:
5-
paths:
6-
- package.json
7-
- requirements.txt
4+
push:
5+
paths:
86
- solution/**
97
- lcs/**
108
- lcp/**
119
- lcof2/**
1210
- lcof/**
1311
- lcci/**
1412
- basic/**
15-
pull_request:
16-
paths:
17-
- package.json
18-
- requirements.txt
13+
pull_request:
14+
paths:
1915
- solution/**
2016
- lcs/**
2117
- lcp/**

‎.github/workflows/clang-format-lint.yml

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
name: clang-format-linter
22

33
on:
4-
push:
5-
paths:
6-
- package.json
7-
- requirements.txt
4+
push:
5+
paths:
86
- solution/**
97
- lcs/**
108
- lcp/**
119
- lcof2/**
1210
- lcof/**
1311
- lcci/**
1412
- basic/**
15-
pull_request:
16-
paths:
17-
- package.json
18-
- requirements.txt
13+
pull_request:
14+
paths:
1915
- solution/**
2016
- lcs/**
2117
- lcp/**

‎.github/workflows/compress.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ on:
1919
schedule:
2020
- cron: '00 23 * * 0'
2121

22-
concurrency:
22+
concurrency:
2323
group: ${{github.workflow}} - ${{github.ref}}
2424
cancel-in-progress: true
2525

‎.github/workflows/deploy.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- main
77
- docs
8-
paths:
8+
paths:
99
- package.json
1010
- requirements.txt
1111
- solution/**
@@ -23,7 +23,7 @@ env:
2323
permissions:
2424
contents: write
2525

26-
concurrency:
26+
concurrency:
2727
group: ${{github.workflow}} - ${{github.ref}}
2828
cancel-in-progress: true
2929

‎.github/workflows/pr-add-label.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request_target:
55
types: [opened, edited, reopened, synchronize]
66

7-
concurrency:
7+
concurrency:
88
group: ${{github.workflow}} - ${{github.event_name}}
99
cancel-in-progress: true
1010

‎solution/3100-3199/3133.Minimum Array End/README.md

+76-4
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,96 @@
4848

4949
## 解法
5050

51-
### 方法一
51+
### 方法一:贪心 + 位运算
52+
53+
根据题目描述,要使得数组的最后一个元素尽可能小,且数组中的元素按位与的结果为 $x$,那么数组的第一个元素必须为 $x$。
54+
55+
假设 $x$ 的二进制表示为 $\underline{1}00\underline{1}00$,那么数组序列为 $\underline{1}00\underline{1}00$, $\underline{1}00\underline{1}01$, $\underline{1}00\underline{1}10$, $\underline{1}00\underline{1}11$...
56+
57+
如果我们忽略掉下划线部分,那么数组序列为 $0000$, $0001$, $0010$, $0011$...,第一项为 $0$,那么第 $n$ 项为 $n-1$。
58+
59+
因此,答案就是在 $x$ 的基础上,将 $n-1$ 的二进制的每一位依次填入 $x$ 的二进制中的 $0$ 位。
60+
61+
时间复杂度 $O(\log x)$,空间复杂度 $O(1)$。
5262

5363
<!-- tabs:start -->
5464

5565
```python
56-
66+
class Solution:
67+
def minEnd(self, n: int, x: int) -> int:
68+
n -= 1
69+
ans = x
70+
for i in range(31):
71+
if x >> i & 1 ^ 1:
72+
ans |= (n & 1) << i
73+
n >>= 1
74+
ans |= n << 31
75+
return ans
5776
```
5877

5978
```java
60-
79+
class Solution {
80+
public long minEnd(int n, int x) {
81+
--n;
82+
long ans = x;
83+
for (int i = 0; i < 31; ++i) {
84+
if ((x >> i & 1) == 0) {
85+
ans |= (n & 1) << i;
86+
n >>= 1;
87+
}
88+
}
89+
ans |= (long) n << 31;
90+
return ans;
91+
}
92+
}
6193
```
6294

6395
```cpp
64-
96+
class Solution {
97+
public:
98+
long long minEnd(int n, int x) {
99+
--n;
100+
long long ans = x;
101+
for (int i = 0; i < 31; ++i) {
102+
if (x >> i & 1 ^ 1) {
103+
ans |= (n & 1) << i;
104+
n >>= 1;
105+
}
106+
}
107+
ans |= (1LL * n) << 31;
108+
return ans;
109+
}
110+
};
65111
```
66112
67113
```go
114+
func minEnd(n int, x int) (ans int64) {
115+
n--
116+
ans = int64(x)
117+
for i := 0; i < 31; i++ {
118+
if x>>i&1 == 0 {
119+
ans |= int64((n & 1) << i)
120+
n >>= 1
121+
}
122+
}
123+
ans |= int64(n) << 31
124+
return
125+
}
126+
```
68127

128+
```ts
129+
function minEnd(n: number, x: number): number {
130+
--n;
131+
let ans: bigint = BigInt(x);
132+
for (let i = 0; i < 31; ++i) {
133+
if (((x >> i) & 1) ^ 1) {
134+
ans |= BigInt(n & 1) << BigInt(i);
135+
n >>= 1;
136+
}
137+
}
138+
ans |= BigInt(n) << BigInt(31);
139+
return Number(ans);
140+
}
69141
```
70142

71143
<!-- tabs:end -->

‎solution/3100-3199/3133.Minimum Array End/README_EN.md

+76-4
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,96 @@
4444

4545
## Solutions
4646

47-
### Solution 1
47+
### Solution 1: Greedy + Bit Manipulation
48+
49+
According to the problem description, to make the last element of the array as small as possible and the bitwise AND result of the elements in the array is $x$, the first element of the array must be $x$.
50+
51+
Assume the binary representation of $x$ is $\underline{1}00\underline{1}00$, then the array sequence is $\underline{1}00\underline{1}00$, $\underline{1}00\underline{1}01$, $\underline{1}00\underline{1}10$, $\underline{1}00\underline{1}11$...
52+
53+
If we ignore the underlined part, then the array sequence is $0000$, $0001$, $0010$, $0011$..., the first item is $0$, then the $n$-th item is $n-1$.
54+
55+
Therefore, the answer is to fill each bit of the binary of $n-1$ into the $0$ bit of the binary of $x$ based on $x$.
56+
57+
The time complexity is $O(\log x)$, and the space complexity is $O(1)$.
4858

4959
<!-- tabs:start -->
5060

5161
```python
52-
62+
class Solution:
63+
def minEnd(self, n: int, x: int) -> int:
64+
n -= 1
65+
ans = x
66+
for i in range(31):
67+
if x >> i & 1 ^ 1:
68+
ans |= (n & 1) << i
69+
n >>= 1
70+
ans |= n << 31
71+
return ans
5372
```
5473

5574
```java
56-
75+
class Solution {
76+
public long minEnd(int n, int x) {
77+
--n;
78+
long ans = x;
79+
for (int i = 0; i < 31; ++i) {
80+
if ((x >> i & 1) == 0) {
81+
ans |= (n & 1) << i;
82+
n >>= 1;
83+
}
84+
}
85+
ans |= (long) n << 31;
86+
return ans;
87+
}
88+
}
5789
```
5890

5991
```cpp
60-
92+
class Solution {
93+
public:
94+
long long minEnd(int n, int x) {
95+
--n;
96+
long long ans = x;
97+
for (int i = 0; i < 31; ++i) {
98+
if (x >> i & 1 ^ 1) {
99+
ans |= (n & 1) << i;
100+
n >>= 1;
101+
}
102+
}
103+
ans |= (1LL * n) << 31;
104+
return ans;
105+
}
106+
};
61107
```
62108
63109
```go
110+
func minEnd(n int, x int) (ans int64) {
111+
n--
112+
ans = int64(x)
113+
for i := 0; i < 31; i++ {
114+
if x>>i&1 == 0 {
115+
ans |= int64((n & 1) << i)
116+
n >>= 1
117+
}
118+
}
119+
ans |= int64(n) << 31
120+
return
121+
}
122+
```
64123

124+
```ts
125+
function minEnd(n: number, x: number): number {
126+
--n;
127+
let ans: bigint = BigInt(x);
128+
for (let i = 0; i < 31; ++i) {
129+
if (((x >> i) & 1) ^ 1) {
130+
ans |= BigInt(n & 1) << BigInt(i);
131+
n >>= 1;
132+
}
133+
}
134+
ans |= BigInt(n) << BigInt(31);
135+
return Number(ans);
136+
}
65137
```
66138

67139
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
long long minEnd(int n, int x) {
4+
--n;
5+
long long ans = x;
6+
for (int i = 0; i < 31; ++i) {
7+
if (x >> i & 1 ^ 1) {
8+
ans |= (n & 1) << i;
9+
n >>= 1;
10+
}
11+
}
12+
ans |= (1LL * n) << 31;
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func minEnd(n int, x int) (ans int64) {
2+
n--
3+
ans = int64(x)
4+
for i := 0; i < 31; i++ {
5+
if x>>i&1 == 0 {
6+
ans |= int64((n & 1) << i)
7+
n >>= 1
8+
}
9+
}
10+
ans |= int64(n) << 31
11+
return
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public long minEnd(int n, int x) {
3+
--n;
4+
long ans = x;
5+
for (int i = 0; i < 31; ++i) {
6+
if ((x >> i & 1) == 0) {
7+
ans |= (n & 1) << i;
8+
n >>= 1;
9+
}
10+
}
11+
ans |= (long) n << 31;
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minEnd(self, n: int, x: int) -> int:
3+
n -= 1
4+
ans = x
5+
for i in range(31):
6+
if x >> i & 1 ^ 1:
7+
ans |= (n & 1) << i
8+
n >>= 1
9+
ans |= n << 31
10+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minEnd(n: number, x: number): number {
2+
--n;
3+
let ans: bigint = BigInt(x);
4+
for (let i = 0; i < 31; ++i) {
5+
if (((x >> i) & 1) ^ 1) {
6+
ans |= BigInt(n & 1) << BigInt(i);
7+
n >>= 1;
8+
}
9+
}
10+
ans |= BigInt(n) << BigInt(31);
11+
return Number(ans);
12+
}

0 commit comments

Comments
 (0)