Skip to content

Commit 7f85814

Browse files
authored
Merge pull request #1026 from 0xff-dev/3133
Add solution and test-cases for problem 3133
2 parents 77e3030 + 6837093 commit 7f85814

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

leetcode/3101-3200/3133.Minimum-Array-End/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [3133.Minimum Array End][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two integers `n` and `x`. You have to construct an array of **positive** integers `nums` of size `n` where for every `0 <= i < n - 1`, `nums[i + 1]` is **greater than** `nums[i]`, and the result of the bitwise `AND` operation between all elements of `nums` is `x`.
5+
6+
Return the **minimum** possible value of `nums[n - 1]`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
11+
Input: n = 3, x = 4
1412
15-
## 题意
16-
> ...
13+
Output: 6
1714
18-
## 题解
15+
Explanation:
1916
20-
### 思路1
21-
> ...
22-
Minimum Array End
23-
```go
17+
nums can be [4,5,6] and its last element is 6.
2418
```
2519

20+
**Example 2:**
21+
22+
```
23+
Input: n = 2, x = 7
24+
25+
Output: 15
26+
27+
Explanation:
28+
29+
nums can be [7,15] and its last element is 15.
30+
```
2631

2732
## 结语
2833

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, x int) int64 {
4+
ans := int64(x)
5+
if n == 1 {
6+
return ans
7+
}
8+
nn := int64(n) - 1
9+
zero := 0
10+
c := int64(0)
11+
for {
12+
zero++
13+
c = 1 << (zero - 1)
14+
if nn <= c {
15+
break
16+
}
17+
nn -= c
18+
}
19+
c |= (nn - 1)
20+
cur := int64(1)
21+
for ; c > 0; c >>= 1 {
22+
for ans&cur != 0 {
23+
cur <<= 1
24+
}
25+
now := c & 1
26+
if now == 1 {
27+
ans |= cur
28+
continue
29+
}
30+
cur <<= 1
31+
}
32+
return ans
533
}

leetcode/3101-3200/3133.Minimum-Array-End/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n, x int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 3, 4, 6},
17+
{"TestCase2", 2, 7, 15},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.n, c.x)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.n, c.x)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)