Skip to content

Commit 0d5c0fc

Browse files
committed
test: 672 solution
py, c++, go, java
1 parent b9f4acd commit 0d5c0fc

File tree

10 files changed

+252
-3
lines changed

10 files changed

+252
-3
lines changed

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "3440",
2+
"daily": "672",
33
"plans": ["3602", "problems", "3603", "problems", "3604", "problems", "3605", "problems", "3606", "problems", "3607", "problems", "3608", "problems", "3609", "problems"]
44
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_3440"
4+
problem "leetCode/problems/problems_672"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "3440", "problems", problem.Solve)
9+
TestEach(t, "672", "problems", problem.Solve)
1010
}

problems/problems_672/Solution.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
int flipLights(int n, int presses) {
10+
if (presses == 0)
11+
return 1;
12+
if (n == 1)
13+
return 2;
14+
if (n == 2)
15+
return presses == 1 ? 3 : 4;
16+
return presses == 1 ? 4 : presses == 2 ? 7 : 8;
17+
}
18+
};
19+
20+
json leetcode::qubh::Solve(string input_json_values) {
21+
vector<string> inputArray;
22+
size_t pos = input_json_values.find('\n');
23+
while (pos != string::npos) {
24+
inputArray.push_back(input_json_values.substr(0, pos));
25+
input_json_values = input_json_values.substr(pos + 1);
26+
pos = input_json_values.find('\n');
27+
}
28+
inputArray.push_back(input_json_values);
29+
30+
Solution solution;
31+
int n = json::parse(inputArray.at(0));
32+
int presses = json::parse(inputArray.at(1));
33+
return solution.flipLights(n, presses);
34+
}

problems/problems_672/Solution.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package problems.problems_672;
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 flipLights(int n, int presses) {
10+
if (presses == 0) return 1;
11+
if (n == 1) return 2;
12+
if (n == 2) return presses == 1 ? 3 : 4;
13+
return presses == 1 ? 4 : presses == 2 ? 7 : 8;
14+
}
15+
16+
@Override
17+
public Object solve(String[] inputJsonValues) {
18+
int n = Integer.parseInt(inputJsonValues[0]);
19+
int presses = Integer.parseInt(inputJsonValues[1]);
20+
return JSON.toJSON(flipLights(n, presses));
21+
}
22+
}

problems/problems_672/problem.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 672. Bulb Switcher II
2+
3+
<p>There is a room with <code>n</code> bulbs labeled from <code>1</code> to <code>n</code> that all are turned on initially, and <strong>four buttons</strong> on the wall. Each of the four buttons has a different functionality where:</p>
4+
5+
<ul>
6+
<li><strong>Button 1:</strong> Flips the status of all the bulbs.</li>
7+
<li><strong>Button 2:</strong> Flips the status of all the bulbs with even labels (i.e., <code>2, 4, ...</code>).</li>
8+
<li><strong>Button 3:</strong> Flips the status of all the bulbs with odd labels (i.e., <code>1, 3, ...</code>).</li>
9+
<li><strong>Button 4:</strong> Flips the status of all the bulbs with a label <code>j = 3k + 1</code> where <code>k = 0, 1, 2, ...</code> (i.e., <code>1, 4, 7, 10, ...</code>).</li>
10+
</ul>
11+
12+
<p>You must make <strong>exactly</strong> <code>presses</code> button presses in total. For each press, you may pick <strong>any</strong> of the four buttons to press.</p>
13+
14+
<p>Given the two integers <code>n</code> and <code>presses</code>, return <em>the number of <strong>different possible statuses</strong> after performing all </em><code>presses</code><em> button presses</em>.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> n = 1, presses = 1
21+
<strong>Output:</strong> 2
22+
<strong>Explanation:</strong> Status can be:
23+
- [off] by pressing button 1
24+
- [on] by pressing button 2
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> n = 2, presses = 1
31+
<strong>Output:</strong> 3
32+
<strong>Explanation:</strong> Status can be:
33+
- [off, off] by pressing button 1
34+
- [on, off] by pressing button 2
35+
- [off, on] by pressing button 3
36+
</pre>
37+
38+
<p><strong class="example">Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> n = 3, presses = 1
42+
<strong>Output:</strong> 4
43+
<strong>Explanation:</strong> Status can be:
44+
- [off, off, off] by pressing button 1
45+
- [off, on, off] by pressing button 2
46+
- [on, off, on] by pressing button 3
47+
- [off, on, on] by pressing button 4
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= n &lt;= 1000</code></li>
55+
<li><code>0 &lt;= presses &lt;= 1000</code></li>
56+
</ul>

problems/problems_672/problem_zh.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 672. 灯泡开关 Ⅱ
2+
3+
<p>房间中有 <code>n</code>&nbsp;只已经打开的灯泡,编号从 <code>1</code> 到 <code>n</code> 。墙上挂着 <strong>4 个开关</strong> 。</p>
4+
5+
<p>这 4 个开关各自都具有不同的功能,其中:</p>
6+
7+
<ul>
8+
<li><strong>开关 1 :</strong>反转当前所有灯的状态(即开变为关,关变为开)</li>
9+
<li><strong>开关 2 :</strong>反转编号为偶数的灯的状态(即 <code>0, 2, 4, ...</code>)</li>
10+
<li><strong>开关 3 :</strong>反转编号为奇数的灯的状态(即 <code>1, 3, ...</code>)</li>
11+
<li><strong>开关 4 :</strong>反转编号为 <code>j = 3k + 1</code> 的灯的状态,其中 <code>k = 0, 1, 2, ...</code>(即 <code>1, 4, 7, 10, ...</code>)</li>
12+
</ul>
13+
14+
<p>你必须 <strong>恰好</strong> 按压开关 <code>presses</code> 次。每次按压,你都需要从 4 个开关中选出一个来执行按压操作。</p>
15+
16+
<p>给你两个整数 <code>n</code> 和 <code>presses</code> ,执行完所有按压之后,返回 <strong>不同可能状态</strong> 的数量。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>n = 1, presses = 1
24+
<strong>输出:</strong>2
25+
<strong>解释:</strong>状态可以是:
26+
- 按压开关 1 ,[关]
27+
- 按压开关 2 ,[开]
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>n = 2, presses = 1
34+
<strong>输出:</strong>3
35+
<strong>解释:</strong>状态可以是:
36+
- 按压开关 1 ,[关, 关]
37+
- 按压开关 2 ,[开, 关]
38+
- 按压开关 3 ,[关, 开]
39+
</pre>
40+
41+
<p><strong>示例 3:</strong></p>
42+
43+
<pre>
44+
<strong>输入:</strong>n = 3, presses = 1
45+
<strong>输出:</strong>4
46+
<strong>解释:</strong>状态可以是:
47+
- 按压开关 1 ,[关, 关, 关]
48+
- 按压开关 2 ,[关, 开, 关]
49+
- 按压开关 3 ,[开, 关, 开]
50+
- 按压开关 4 ,[关, 开, 开]
51+
</pre>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= n &lt;= 1000</code></li>
59+
<li><code>0 &lt;= presses &lt;= 1000</code></li>
60+
</ul>

problems/problems_672/solution.go

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

problems/problems_672/solution.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import solution
2+
from typing import *
3+
4+
5+
class Solution(solution.Solution):
6+
def solve(self, test_input=None):
7+
return self.flipLights(*test_input)
8+
9+
def flipLights(self, n: int, presses: int) -> int:
10+
if presses == 0:
11+
return 1
12+
if n == 1:
13+
return 2
14+
if n == 2:
15+
return 3 if presses == 1 else 4
16+
return 4 if presses == 1 else (7 if presses == 2 else 8)

problems/problems_672/testcase

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

problems/problems_672/testcase.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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=[1, 1], Output=2))
11+
self.testcases.append(case(Input=[2, 1], Output=3))
12+
self.testcases.append(case(Input=[3, 1], Output=4))
13+
14+
def get_testcases(self):
15+
return self.testcases

0 commit comments

Comments
 (0)