8000 :sparkles: Added solution to 1409 · ninefive/awesome-golang-leetcode@47d7939 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47d7939

Browse files
author
Sathish Babu
committed
✨ Added solution to 1409
1 parent 40ca759 commit 47d7939

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# [1362.Closest Divisors][title]
2+
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-leetcode)
5+
6+
## Description
7+
8+
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules:
9+
10+
In the beginning, you have the permutation P=[1,2,3,...,m].
11+
For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].
12+
13+
Return an array containing the result for the given queries.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: queries = [3,1,2,1], m = 5
19+
Output: [2,1,2,1]
20+
Explanation: The queries are processed as follow:
21+
For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5].
22+
For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5].
23+
For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5].
24+
For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5].
25+
Therefore, the array containing the result is [2,1,2,1].
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: queries = [4,1,2,2], m = 4
32+
Output: [3,1,2,0]
33+
```
34+
35+
**Example 3:**
36+
37+
```
38+
Input: queries = [7,5,5,8,3], m = 8
39+
Output: [6,5,0,7,5]
40+
```
41+
42+
## 题意
43+
> ...
44+
45+
## 题解
46+
47+
### 思路1
48+
> ...
49+
Closest Divisors
50+
```go
51+
```
52+
53+
54+
## 结语
55+
56+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
57+
58+
[title]: https://leetcode.com/problems/closest-divisors/
59+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Solution
2+
3+
func Solution(queries []int, m int) []int {
4+
res, p := make([]int, len(queries)), make([]int, m)
5+
for i := 0; i < m; i++ {
6+
p[i] = i + 1
7+
}
8+
for idx, q := range queries {
9+
pos := -1
10+
for i, v := range p {
11+
if v == q {
12+
pos = i
13+
break
14+
}
15+
}
16+
res[idx] = pos
17+
for i := pos; i > 0; i-- {
18+
p[i] = p[i-1]
19+
}
20+
p[0] = q
21+
}
22+
return res
23+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
queries []int
14+
m int
15+
expect []int
16+
}{
17+
{"TestCase", []int{3, 1, 2, 1}, 5, []int{2, 1, 2, 1}},
18+
{"TestCase", []int{4, 1, 2, 2}, 4, []int{3, 1, 2, 0}},
19+
{"TestCase", []int{7, 5, 5, 8, 3}, 8, []int{6, 5, 0, 7, 5}},
20+
}
21+
22+
// 开始测试
23+
for i, c := range cases {
24+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
25+
got := Solution(c.queries, c.m)
26+
if !reflect.DeepEqual(got, c.expect) {
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.queries, c.m)
29+
}
30+
})
31+
}
32+
}
33+
34+
// 压力测试
35+
func BenchmarkSolution(b *testing.B) {
36+
37+
}
38+
39+
// 使用案列
40+
func ExampleSolution() {
41+
42+
}

0 commit comments

Comments
 (0)
0