Skip to content

Commit 4147252

Browse files
committed
feat: add solutions to lc problem: No.1217
No.1217.Minimum Cost to Move Chips to The Same Position
1 parent 5e1ff0b commit 4147252

File tree

9 files changed

+133
-17
lines changed

9 files changed

+133
-17
lines changed

solution/0500-0599/0591.Tag Validator/README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ class Solution:
108108
def isValid(self, code: str) -> bool:
109109
def check(tag):
110110
n = len(tag)
111-
if n < 1 or n > 9:
112-
return False
113-
return all(c.isupper() for c in tag)
111+
return 1 <= n <= 9 and all(c.isupper() for c in tag)
114112

115113
stk = []
116114
i, n = 0, len(code)
@@ -128,9 +126,8 @@ class Solution:
128126
if i < 0:
129127
return False
130128
t = code[j: i]
131-
if not check(t) or not stk or stk[-1] != t:
129+
if not check(t) or not stk or stk.pop() != t:
132130
return False
133-
stk.pop()
134131
elif code[i] == '<':
135132
j = i + 1
136133
i = code.find('>', j)

solution/0500-0599/0591.Tag Validator/README_EN.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ class Solution:
7676
def isValid(self, code: str) -> bool:
7777
def check(tag):
7878
n = len(tag)
79-
if n < 1 or n > 9:
80-
return False
81-
return all(c.isupper() for c in tag)
79+
return 1 <= n <= 9 and all(c.isupper() for c in tag)
8280

8381
stk = []
8482
i, n = 0, len(code)
@@ -96,9 +94,8 @@ class Solution:
9694
if i < 0:
9795
return False
9896
t = code[j: i]
99-
if not check(t) or not stk or stk[-1] != t:
97+
if not check(t) or not stk or stk.pop() != t:
10098
return False
101-
stk.pop()
10299
elif code[i] == '<':
103100
j = i + 1
104101
i = code.find('>', j)

solution/0500-0599/0591.Tag Validator/Solution.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ class Solution:
22
def isValid(self, code: str) -> bool:
33
def check(tag):
44
n = len(tag)
5-
if n < 1 or n > 9:
6-
return False
7-
return all(c.isupper() for c in tag)
5+
return 1 <= n <= 9 and all(c.isupper() for c in tag)
86

97
stk = []
108
i, n = 0, len(code)
@@ -22,9 +20,8 @@ def check(tag):
2220
if i < 0:
2321
return False
2422
t = code[j: i]
25-
if not check(t) or not stk or stk[-1] != t:
23+
if not check(t) or not stk or stk.pop() != t:
2624
return False
27-
stk.pop()
2825
elif code[i] == '<':
2926
j = i + 1
3027
i = code.find('>', j)

solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,69 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:脑筋急转弯**
67+
68+
将所有偶数下标的芯片移动到 0 号位置,所有奇数下标的芯片移动到 1 号位置,所有的代价为 0,接下来只需要在 0/1 号位置中选择其中一个较小数量的芯片,移动到另一个位置。所需的最小代价就是那个较小的数量。
69+
6670
<!-- tabs:start -->
6771

6872
### **Python3**
6973

7074
<!-- 这里可写当前语言的特殊实现逻辑 -->
7175

7276
```python
73-
77+
class Solution:
78+
def minCostToMoveChips(self, position: List[int]) -> int:
79+
a = sum(p % 2 for p in position)
80+
b = len(position) - a
81+
return min(a, b)
7482
```
7583

7684
### **Java**
7785

7886
<!-- 这里可写当前语言的特殊实现逻辑 -->
7987

8088
```java
89+
class Solution {
90+
public int minCostToMoveChips(int[] position) {
91+
int a = 0;
92+
for (int p : position) {
93+
a += p % 2;
94+
}
95+
int b = position.length - a;
96+
return Math.min(a, b);
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
int minCostToMoveChips(vector<int>& position) {
107+
int a = 0;
108+
for (auto& p : position) a += p & 1;
109+
int b = position.size() - a;
110+
return min(a, b);
111+
}
112+
};
113+
```
81114
115+
### **Go**
116+
117+
```go
118+
func minCostToMoveChips(position []int) int {
119+
a := 0
120+
for _, p := range position {
121+
a += p & 1
122+
}
123+
b := len(position) - a
124+
if a < b {
125+
return a
126+
}
127+
return b
128+
}
82129
```
83130

84131
### **...**

solution/1200-1299/1217.Minimum Cost to Move Chips to The Same Position/README_EN.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,56 @@ Total cost is 1.
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def minCostToMoveChips(self, position: List[int]) -> int:
61+
a = sum(p % 2 for p in position)
62+
b = len(position) - a
63+
return min(a, b)
6064
```
6165

6266
### **Java**
6367

6468
```java
69+
class Solution {
70+
public int minCostToMoveChips(int[] position) {
71+
int a = 0;
72+
for (int p : position) {
73+
a += p % 2;
74+
}
75+
int b = position.length - a;
76+
return Math.min(a, b);
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int minCostToMoveChips(vector<int>& position) {
87+
int a = 0;
88+
for (auto& p : position) a += p & 1;
89+
int b = position.size() - a;
90+
return min(a, b);
91+
}
92+
};
93+
```
6594
95+
### **Go**
96+
97+
```go
98+
func minCostToMoveChips(position []int) int {
99+
a := 0
100+
for _, p := range position {
101+
a += p & 1
102+
}
103+
b := len(position) - a
104+
if a < b {
105+
return a
106+
}
107+
return b
108+
}
66109
```
67110

68111
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int minCostToMoveChips(vector<int>& position) {
4+
int a = 0;
5+
for (auto& p : position) a += p & 1;
6+
int b = position.size() - a;
7+
return min(a, b);
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minCostToMoveChips(position []int) int {
2+
a := 0
3+
for _, p := range position {
4+
a += p & 1
5+
}
6+
b := len(position) - a
7+
if a < b {
8+
return a
9+
}
10+
return b
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int minCostToMoveChips(int[] position) {
3+
int a = 0;
4+
for (int p : position) {
5+
a += p % 2;
6+
}
7+
int b = position.length - a;
8+
return Math.min(a, b);
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def minCostToMoveChips(self, position: List[int]) -> int:
3+
a = sum(p % 2 for p in position)
4+
b = len(position) - a
5+
return min(a, b)

0 commit comments

Comments
 (0)