Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23f7050

Browse files
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‎

Lines changed: 4 additions & 8 deletions
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‎

Lines changed: 4 additions & 8 deletions
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 2 additions & 2 deletions
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 76 additions & 4 deletions
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‎

Lines changed: 76 additions & 4 deletions
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 -->
Lines changed: 15 additions & 0 deletions
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+
};
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 14 additions & 0 deletions
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /