Skip to content

Commit e7209ac

Browse files
committed
feat: add solutions to lc problems: No.0820,1338
* No.0820.Short Encoding of Words * No.1338.Reduce Array Size to The Half
1 parent 728ba3b commit e7209ac

File tree

8 files changed

+369
-3
lines changed

8 files changed

+369
-3
lines changed

solution/0800-0899/0820.Short Encoding of Words/README.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ words[2] = "bell" ,s 开始于 indices[2] = 5 到下一个 '#' 结束的子字
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
题目大意:充分利用重叠的后缀,使有效编码尽可能短
54+
题目大意:充分利用重叠的后缀,使有效编码尽可能短
5555

5656
<!-- tabs:start -->
5757

@@ -88,6 +88,16 @@ class Solution:
8888
return ans
8989
```
9090

91+
```python
92+
class Solution:
93+
def minimumLengthEncoding(self, words: List[str]) -> int:
94+
s = set(words)
95+
for word in words:
96+
for i in range(1, len(word)):
97+
s.discard(word[i:])
98+
return sum(len(word) + 1 for word in s)
99+
```
100+
91101
### **Java**
92102

93103
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -130,6 +140,24 @@ class Solution {
130140
}
131141
```
132142

143+
```java
144+
class Solution {
145+
public int minimumLengthEncoding(String[] words) {
146+
Set<String> s = new HashSet<>(Arrays.asList(words));
147+
for (String word : words) {
148+
for (int i = 1; i < word.length(); ++i) {
149+
s.remove(word.substring(i));
150+
}
151+
}
152+
int ans = 0;
153+
for (String word : s) {
154+
ans += word.length() + 1;
155+
}
156+
return ans;
157+
}
158+
}
159+
```
160+
133161
### **Go**
134162

135163
```go
@@ -166,6 +194,25 @@ func dfs(cur *trie, l int) int {
166194
}
167195
```
168196

197+
```go
198+
func minimumLengthEncoding(words []string) int {
199+
s := make(map[string]bool)
200+
for _, word := range words {
201+
s[word] = true
202+
}
203+
for _, word := range words {
204+
for i, n := 1, len(word); i < n; i++ {
205+
delete(s, word[i:n])
206+
}
207+
}
208+
ans := 0
209+
for word := range s {
210+
ans += len(word) + 1
211+
}
212+
return ans
213+
}
214+
```
215+
169216
### **C++**
170217

171218
```cpp
@@ -207,6 +254,22 @@ private:
207254
};
208255
```
209256
257+
```cpp
258+
class Solution {
259+
public:
260+
int minimumLengthEncoding(vector<string>& words) {
261+
unordered_set<string> s(words.begin(), words.end());
262+
for (auto& word : words)
263+
for (int i = 1; i < word.size(); ++i)
264+
s.erase(word.substr(i));
265+
int ans = 0;
266+
for (auto& word : s)
267+
ans += word.size() + 1;
268+
return ans;
269+
}
270+
};
271+
```
272+
210273
### **...**
211274

212275
```

solution/0800-0899/0820.Short Encoding of Words/README_EN.md

+63
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ class Solution:
7878
return ans
7979
```
8080

81+
```python
82+
class Solution:
83+
def minimumLengthEncoding(self, words: List[str]) -> int:
84+
s = set(words)
85+
for word in words:
86+
for i in range(1, len(word)):
87+
s.discard(word[i:])
88+
return sum(len(word) + 1 for word in s)
89+
```
90+
8191
### **Java**
8292

8393
```java
@@ -118,6 +128,24 @@ class Solution {
118128
}
119129
```
120130

131+
```java
132+
class Solution {
133+
public int minimumLengthEncoding(String[] words) {
134+
Set<String> s = new HashSet<>(Arrays.asList(words));
135+
for (String word : words) {
136+
for (int i = 1; i < word.length(); ++i) {
137+
s.remove(word.substring(i));
138+
}
139+
}
140+
int ans = 0;
141+
for (String word : s) {
142+
ans += word.length() + 1;
143+
}
144+
return ans;
145+
}
146+
}
147+
```
148+
121149
### **Go**
122150

123151
```go
@@ -195,6 +223,41 @@ private:
195223
};
196224
```
197225
226+
```go
227+
func minimumLengthEncoding(words []string) int {
228+
s := make(map[string]bool)
229+
for _, word := range words {
230+
s[word] = true
231+
}
232+
for _, word := range words {
233+
for i, n := 1, len(word); i < n; i++ {
234+
delete(s, word[i:n])
235+
}
236+
}
237+
ans := 0
238+
for word := range s {
239+
ans += len(word) + 1
240+
}
241+
return ans
242+
}
243+
```
244+
245+
```cpp
246+
class Solution {
247+
public:
248+
int minimumLengthEncoding(vector<string>& words) {
249+
unordered_set<string> s(words.begin(), words.end());
250+
for (auto& word : words)
251+
for (int i = 1; i < word.size(); ++i)
252+
s.erase(word.substr(i));
253+
int ans = 0;
254+
for (auto& word : s)
255+
ans += word.size() + 1;
256+
return ans;
257+
}
258+
};
259+
```
260+
198261
### **...**
199262
200263
```

solution/1300-1399/1338.Reduce Array Size to The Half/README.md

+85-1
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,106 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
哈希表计数,按出现的频率倒序。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

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

6971
```python
70-
72+
class Solution:
73+
def minSetSize(self, arr: List[int]) -> int:
74+
couter = Counter(arr)
75+
ans = n = 0
76+
for _, cnt in couter.most_common():
77+
n += cnt
78+
ans += 1
79+
if n * 2 >= len(arr):
80+
break
81+
return ans
7182
```
7283

7384
### **Java**
7485

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

7788
```java
89+
class Solution {
90+
public int minSetSize(int[] arr) {
91+
Map<Integer, Integer> counter = new HashMap<>();
92+
for (int v : arr) {
93+
counter.put(v, counter.getOrDefault(v, 0) + 1);
94+
}
95+
List<Integer> t = new ArrayList<>();
96+
for (int cnt : counter.values()) {
97+
t.add(cnt);
98+
}
99+
Collections.sort(t, Collections.reverseOrder());
100+
int ans = 0;
101+
int n = 0;
102+
for (int cnt : t) {
103+
n += cnt;
104+
++ans;
105+
if (n * 2 >= arr.length) {
106+
break;
107+
}
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int minSetSize(vector<int>& arr) {
120+
unordered_map<int, int> counter;
121+
for (int v : arr) ++counter[v];
122+
vector<int> t;
123+
for (auto& [k, v] : counter) t.push_back(v);
124+
sort(t.begin(), t.end(), greater<int>());
125+
int ans = 0;
126+
int n = 0;
127+
for (int cnt : t)
128+
{
129+
n += cnt;
130+
++ans;
131+
if (n * 2 >= arr.size()) break;
132+
}
133+
return ans;
134+
}
135+
};
136+
```
78137
138+
### **Go**
139+
140+
```go
141+
func minSetSize(arr []int) int {
142+
counter := make(map[int]int)
143+
for _, v := range arr {
144+
counter[v]++
145+
}
146+
var t []int
147+
for _, v := range counter {
148+
t = append(t, v)
149+
}
150+
sort.Slice(t, func(i, j int) bool {
151+
return t[i] > t[j]
152+
})
153+
ans, n := 0, 0
154+
for _, cnt := range t {
155+
n += cnt
156+
ans++
157+
if n*2 >= len(arr) {
158+
break
159+
}
160+
}
161+
return ans
162+
}
79163
```
80164

81165
### **...**

solution/1300-1399/1338.Reduce Array Size to The Half/README_EN.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,95 @@ Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5]
8585
### **Python3**
8686

8787
```python
88-
88+
class Solution:
89+
def minSetSize(self, arr: List[int]) -> int:
90+
couter = Counter(arr)
91+
ans = n = 0
92+
for _, cnt in couter.most_common():
93+
n += cnt
94+
ans += 1
95+
if n * 2 >= len(arr):
96+
break
97+
return ans
8998
```
9099

91100
### **Java**
92101

93102
```java
103+
class Solution {
104+
public int minSetSize(int[] arr) {
105+
Map<Integer, Integer> counter = new HashMap<>();
106+
for (int v : arr) {
107+
counter.put(v, counter.getOrDefault(v, 0) + 1);
108+
}
109+
List<Integer> t = new ArrayList<>();
110+
for (int cnt : counter.values()) {
111+
t.add(cnt);
112+
}
113+
Collections.sort(t, Collections.reverseOrder());
114+
int ans = 0;
115+
int n = 0;
116+
for (int cnt : t) {
117+
n += cnt;
118+
++ans;
119+
if (n * 2 >= arr.length) {
120+
break;
121+
}
122+
}
123+
return ans;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int minSetSize(vector<int>& arr) {
134+
unordered_map<int, int> counter;
135+
for (int v : arr) ++counter[v];
136+
vector<int> t;
137+
for (auto& [k, v] : counter) t.push_back(v);
138+
sort(t.begin(), t.end(), greater<int>());
139+
int ans = 0;
140+
int n = 0;
141+
for (int cnt : t)
142+
{
143+
n += cnt;
144+
++ans;
145+
if (n * 2 >= arr.size()) break;
146+
}
147+
return ans;
148+
}
149+
};
150+
```
94151
152+
### **Go**
153+
154+
```go
155+
func minSetSize(arr []int) int {
156+
counter := make(map[int]int)
157+
for _, v := range arr {
158+
counter[v]++
159+
}
160+
var t []int
161+
for _, v := range counter {
162+
t = append(t, v)
163+
}
164+
sort.Slice(t, func(i, j int) bool {
165+
return t[i] > t[j]
166+
})
167+
ans, n := 0, 0
168+
for _, cnt := range t {
169+
n += cnt
170+
ans++
171+
if n*2 >= len(arr) {
172+
break
173+
}
174+
}
175+
return ans
176+
}
95177
```
96178

97179
### **...**

0 commit comments

Comments
 (0)