Skip to content

Commit c215365

Browse files
committed
test: 667 solution
py, c++, go, java
1 parent 4560d6c commit c215365

File tree

9 files changed

+210
-1
lines changed

9 files changed

+210
-1
lines changed

problems/problems_667/Solution.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
using namespace std;
5+
using json = nlohmann::json;
6+
7+
class Solution {
8+
public:
9+
vector<int> constructArray(int n, int k) {
10+
vector<int> result;
11+
for (int i = 0; i < k + 1; ++i) {
12+
if (i % 2 == 0) {
13+
result.push_back(i / 2 + 1);
14+
} else {
15+
result.push_back(k - i / 2 + 1);
16+
}
17+
}
18+
for (int i = k + 2; i <= n; ++i) {
19+
result.push_back(i);
20+
}
21+
return result;
22+
}
23+
};
24+
25+
json leetcode::qubh::Solve(string input_json_values) {
26+
vector<string> inputArray;
27+
size_t pos = input_json_values.find('\n');
28+
while (pos != string::npos) {
29+
inputArray.push_back(input_json_values.substr(0, pos));
30+
input_json_values = input_json_values.substr(pos + 1);
31+
pos = input_json_values.find('\n');
32+
}
33+
inputArray.push_back(input_json_values);
34+
35+
Solution solution;
36+
int n = json::parse(inputArray.at(0));
37+
int k = json::parse(inputArray.at(1));
38+
return solution.constructArray(n, k);
39+
}

problems/problems_667/Solution.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problems.problems_667;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
6+
7+
8+
public class Solution extends BaseSolution {
9+
public int[] constructArray(int n, int k) {
10+
int[] result = new int[n];
11+
for (int i = 0; i <= k; ++i) {
12+
if (i % 2 == 0) {
13+
result[i] = i / 2 + 1;
14+
} else {
15+
result[i] = k - i / 2 + 1;
16+
}
17+
}
18+
for (int i = k + 2; i <= n; ++i) {
19+
result[i-1] = i;
20+
}
21+
return result;
22+
}
23+
24+
@Override
25+
public Object solve(String[] inputJsonValues) {
26+
int n = Integer.parseInt(inputJsonValues[0]);
27+
int k = Integer.parseInt(inputJsonValues[1]);
28+
return JSON.toJSON(constructArray(n, k));
29+
}
30+
}

problems/problems_667/problem.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 667. Beautiful Arrangement II
2+
3+
<p>Given two integers <code>n</code> and <code>k</code>, construct a list <code>answer</code> that contains <code>n</code> different positive integers ranging from <code>1</code> to <code>n</code> and obeys the following requirement:</p>
4+
5+
<ul>
6+
<li>Suppose this list is <code>answer =&nbsp;[a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>, ... , a<sub>n</sub>]</code>, then the list <code>[|a<sub>1</sub> - a<sub>2</sub>|, |a<sub>2</sub> - a<sub>3</sub>|, |a<sub>3</sub> - a<sub>4</sub>|, ... , |a<sub>n-1</sub> - a<sub>n</sub>|]</code> has exactly <code>k</code> distinct integers.</li>
7+
</ul>
8+
9+
<p>Return <em>the list</em> <code>answer</code>. If there multiple valid answers, return <strong>any of them</strong>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> n = 3, k = 1
16+
<strong>Output:</strong> [1,2,3]
17+
Explanation: The [1,2,3] has three different positive integers ranging from 1 to 3, and the [1,1] has exactly 1 distinct integer: 1
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> n = 3, k = 2
24+
<strong>Output:</strong> [1,3,2]
25+
Explanation: The [1,3,2] has three different positive integers ranging from 1 to 3, and the [2,1] has exactly 2 distinct integers: 1 and 2.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= k &lt; n &lt;= 10<sup>4</sup></code></li>
33+
</ul>

problems/problems_667/problem_zh.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 667. 优美的排列 II
2+
3+
<p>给你两个整数 <code>n</code> 和 <code>k</code> ,请你构造一个答案列表 <code>answer</code> ,该列表应当包含从 <code>1</code> 到 <code>n</code> 的 <code>n</code> 个不同正整数,并同时满足下述条件:</p>
4+
5+
<ul>
6+
<li>假设该列表是 <code>answer = [a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>, ... , a<sub>n</sub>]</code> ,那么列表 <code>[|a<sub>1</sub> - a<sub>2</sub>|, |a<sub>2</sub> - a<sub>3</sub>|, |a<sub>3</sub> - a<sub>4</sub>|, ... , |a<sub>n-1</sub> - a<sub>n</sub>|]</code> 中应该有且仅有 <code>k</code> 个不同整数。</li>
7+
</ul>
8+
9+
<p>返回列表 <code>answer</code> 。如果存在多种答案,只需返回其中 <strong>任意一种</strong> 。</p>
10+
11+
<p> </p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre>
16+
<strong>输入:</strong>n = 3, k = 1
17+
<strong>输出:</strong>[1, 2, 3]
18+
<strong>解释:</strong>[1, 2, 3] 包含 3 个范围在 1-3 的不同整数,并且 [1, 1] 中有且仅有 1 个不同整数:1
19+
</pre>
20+
21+
<p><strong>示例 2:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>n = 3, k = 2
25+
<strong>输出:</strong>[1, 3, 2]
26+
<strong>解释:</strong>[1, 3, 2] 包含 3 个范围在 1-3 的不同整数,并且 [2, 1] 中有且仅有 2 个不同整数:1 和 2
27+
</pre>
28+
29+
<p> </p>
30+
31+
<p><strong>提示:</strong></p>
32+
33+
<ul>
34+
<li><code>1 <= k < n <= 10<sup>4</sup></code></li>
35+
</ul>
36+
37+
<p> </p>

problems/problems_667/solution.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package problem667
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
)
8+
9+
func constructArray(n int, k int) (ans []int) {
10+
for i := range k + 1 {
11+
if i%2 == 0 {
12+
ans = append(ans, i/2+1)
13+
} else {
14+
ans = append(ans, k-i/2+1)
15+
}
16+
}
17+
for i := k + 2; i <= n; i++ {
18+
ans = append(ans, i)
19+
}
20+
return
21+
}
22+
23+
func Solve(inputJsonValues string) any {
24+
inputValues := strings.Split(inputJsonValues, "\n")
25+
var n int
26+
var k int
27+
28+
if err := json.Unmarshal([]byte(inputValues[0]), &n); err != nil {
29+
log.Fatal(err)
30+
}
31+
if err := json.Unmarshal([]byte(inputValues[1]), &k); err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
return constructArray(n, k)
36+
}

problems/problems_667/solution.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import solution
2+
from typing import *
3+
4+
5+
class Solution(solution.Solution):
6+
def solve(self, test_input=None):
7+
return self.constructArray(*test_input)
8+
9+
def constructArray(self, n: int, k: int) -> List[int]:
10+
ans = []
11+
for i in range(k + 1):
12+
if i % 2 == 0:
13+
ans.append(i // 2 + 1)
14+
else:
15+
ans.append(k - i // 2 + 1)
16+
for i in range(k + 2, n + 1):
17+
ans.append(i)
18+
return ans

problems/problems_667/testcase

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
["3\n1", "3\n2"]
2+
[[1, 2, 3], [1, 3, 2]]

problems/problems_667/testcase.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from collections import namedtuple
2+
import testcase
3+
4+
case = namedtuple("Testcase", ["Input", "Output"])
5+
6+
7+
class Testcase(testcase.Testcase):
8+
def __init__(self):
9+
self.testcases = []
10+
self.testcases.append(case(Input=[3, 1], Output=[1, 2, 3]))
11+
self.testcases.append(case(Input=[3, 2], Output=[1, 3, 2]))
12+
13+
def get_testcases(self):
14+
return self.testcases

problems/problems_788/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// go:build ignore
1+
//go:build ignore
22
#include "cpp/common/Solution.h"
33

44
using namespace std;

0 commit comments

Comments
 (0)