Skip to content

Commit 8b477c6

Browse files
committed
feat: update solutions to lc problems: No.1144,1160
* No.1144.Decrease Elements To Make Array Zigzag * No.1160.Find Words That Can Be Formed by Characters
1 parent bdcfab5 commit 8b477c6

File tree

12 files changed

+267
-204
lines changed

12 files changed

+267
-204
lines changed

solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ class TimeMap {
8383
public TimeMap() {
8484

8585
}
86-
86+
8787
public void set(String key, String value, int timestamp) {
8888
ktv.computeIfAbsent(key, k -> new TreeMap<>()).put(timestamp, value);
8989
}
90-
90+
9191
public String get(String key, int timestamp) {
9292
if (!ktv.containsKey(key)) {
9393
return "";
@@ -114,11 +114,11 @@ public:
114114
TimeMap() {
115115

116116
}
117-
117+
118118
void set(string key, string value, int timestamp) {
119119
ktv[key].emplace_back(timestamp, value);
120120
}
121-
121+
122122
string get(string key, int timestamp) {
123123
auto& pairs = ktv[key];
124124
pair<int, string> p = {timestamp, string({127})};

solution/1100-1199/1137.N-th Tribonacci Number/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ T_4 = 1 + 1 + 2 = 4
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:动态规划**
46+
47+
由于题目中给出的递推式,可以使用动态规划求解。
48+
49+
我们定义三个变量 $a$, $b$, $c$,分别表示 $T_{n-3}$, $T_{n-2}$, $T_{n-1}$,初始值分别为 $0$, $1$, $1$。
50+
51+
然后从 $n$ 减小到 $0$,每次更新 $a$, $b$, $c$ 的值,直到 $n$ 为 $0$ 时,答案即为 $a$。
52+
53+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为给定的整数。
54+
4555
<!-- tabs:start -->
4656

4757
### **Python3**

solution/1100-1199/1144.Decrease Elements To Make Array Zigzag/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public:
124124
125125
```go
126126
func movesToMakeZigzag(nums []int) int {
127-
ans := make([]int, 2)
127+
ans := [2]int{}
128128
n := len(nums)
129129
for i := 0; i < 2; i++ {
130130
for j := i; j < n; j += 2 {

solution/1100-1199/1144.Decrease Elements To Make Array Zigzag/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public:
110110
111111
```go
112112
func movesToMakeZigzag(nums []int) int {
113-
ans := make([]int, 2)
113+
ans := [2]int{}
114114
n := len(nums)
115115
for i := 0; i < 2; i++ {
116116
for j := i; j < n; j += 2 {

solution/1100-1199/1144.Decrease Elements To Make Array Zigzag/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
func movesToMakeZigzag(nums []int) int {
2-
ans := make([]int, 2)
2+
ans := [2]int{}
33
n := len(nums)
44
for i := 0; i < 2; i++ {
55
for j := i; j < n; j += 2 {

solution/1100-1199/1160.Find Words That Can Be Formed by Characters/README.md

Lines changed: 91 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:计数**
50+
51+
我们可以用一个长度为 $26$ 的数组 $cnt$ 统计字符串 $chars$ 中每个字母出现的次数。
52+
53+
然后遍历字符串数组 $words$,对于每个字符串 $w$,我们用一个长度为 $26$ 的数组 $wc$ 统计字符串 $w$ 中每个字母出现的次数,如果对于每个字母 $c$,$wc[c] \leq cnt[c]$,那么我们就可以用 $chars$ 中的字母拼写出字符串 $w$,否则我们无法拼写出字符串 $w$。如果可以拼写出字符串 $w$,那么我们就将字符串 $w$ 的长度加到答案中。
54+
55+
遍历结束后,即可得到答案。
56+
57+
时间复杂度 $(L)$,空间复杂度 $O(C)$。其中 $L$ 为题目中所有字符串的长度之和;而 $C$ 为字符集的大小,本题中 $C = 26$。
58+
4959
<!-- tabs:start -->
5060

5161
### **Python3**
@@ -55,12 +65,12 @@
5565
```python
5666
class Solution:
5767
def countCharacters(self, words: List[str], chars: str) -> int:
58-
counter = Counter(chars)
68+
cnt = Counter(chars)
5969
ans = 0
60-
for word in words:
61-
cnt = Counter(word)
62-
if all([counter[c] >= v for c, v in cnt.items()]):
63-
ans += len(word)
70+
for w in words:
71+
wc = Counter(w)
72+
if all(cnt[c] >= v for c, v in wc.items()):
73+
ans += len(w)
6474
return ans
6575
```
6676

@@ -71,32 +81,26 @@ class Solution:
7181
```java
7282
class Solution {
7383
public int countCharacters(String[] words, String chars) {
74-
int[] counter = count(chars);
84+
int[] cnt = new int[26];
85+
for (int i = 0; i < chars.length(); ++i) {
86+
++cnt[chars.charAt(i) - 'a'];
87+
}
7588
int ans = 0;
76-
for (String word : words) {
77-
int[] cnt = count(word);
78-
if (check(counter, cnt)) {
79-
ans += word.length();
89+
for (String w : words) {
90+
int[] wc = new int[26];
91+
boolean ok = true;
92+
for (int i = 0; i < w.length(); ++i) {
93+
int j = w.charAt(i) - 'a';
94+
if (++wc[j] > cnt[j]) {
95+
ok = false;
96+
break;
97+
}
8098
}
81-
}
82-
return ans;
83-
}
84-
85-
private int[] count(String s) {
86-
int[] counter = new int[26];
87-
for (char c : s.toCharArray()) {
88-
++counter[c - 'a'];
89-
}
90-
return counter;
91-
}
92-
93-
private boolean check(int[] cnt1, int[] cnt2) {
94-
for (int i = 0; i < 26; ++i) {
95-
if (cnt1[i] < cnt2[i]) {
96-
return false;
99+
if (ok) {
100+
ans += w.length();
97101
}
98102
}
99-
return true;
103+
return ans;
100104
}
101105
}
102106
```
@@ -107,59 +111,81 @@ class Solution {
107111
class Solution {
108112
public:
109113
int countCharacters(vector<string>& words, string chars) {
110-
vector<int> counter = count(chars);
114+
int cnt[26]{};
115+
for (char& c : chars) {
116+
++cnt[c - 'a'];
117+
}
111118
int ans = 0;
112-
for (auto& word : words) {
113-
vector<int> cnt = count(word);
114-
if (check(counter, cnt)) ans += word.size();
119+
for (auto& w : words) {
120+
int wc[26]{};
121+
bool ok = true;
122+
for (auto& c : w) {
123+
int i = c - 'a';
124+
if (++wc[i] > cnt[i]) {
125+
ok = false;
126+
break;
127+
}
128+
}
129+
if (ok) {
130+
ans += w.size();
131+
}
115132
}
116133
return ans;
117134
}
118-
119-
vector<int> count(string s) {
120-
vector<int> counter(26);
121-
for (char c : s) ++counter[c - 'a'];
122-
return counter;
123-
}
124-
125-
bool check(vector<int>& cnt1, vector<int>& cnt2) {
126-
for (int i = 0; i < 26; ++i)
127-
if (cnt1[i] < cnt2[i]) return false;
128-
return true;
129-
}
130135
};
131136
```
132137
133138
### **Go**
134139
135140
```go
136-
func countCharacters(words []string, chars string) int {
137-
counter := count(chars)
138-
ans := 0
139-
for _, word := range words {
140-
cnt := count(word)
141-
if check(counter, cnt) {
142-
ans += len(word)
141+
func countCharacters(words []string, chars string) (ans int) {
142+
cnt := [26]int{}
143+
for _, c := range chars {
144+
cnt[c-'a']++
145+
}
146+
for _, w := range words {
147+
wc := [26]int{}
148+
ok := true
149+
for _, c := range w {
150+
c -= 'a'
151+
wc[c]++
152+
if wc[c] > cnt[c] {
153+
ok = false
154+
break
155+
}
156+
}
157+
if ok {
158+
ans += len(w)
143159
}
144160
}
145-
return ans
161+
return
146162
}
163+
```
147164

148-
func count(s string) []int {
149-
counter := make([]int, 26)
150-
for _, c := range s {
151-
counter[c-'a']++
152-
}
153-
return counter
154-
}
165+
### **TypeScript**
155166

156-
func check(cnt1, cnt2 []int) bool {
157-
for i := 0; i < 26; i++ {
158-
if cnt1[i] < cnt2[i] {
159-
return false
160-
}
161-
}
162-
return true
167+
```ts
168+
function countCharacters(words: string[], chars: string): number {
169+
const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
170+
const cnt = new Array(26).fill(0);
171+
for (const c of chars) {
172+
cnt[idx(c)]++;
173+
}
174+
let ans = 0;
175+
for (const w of words) {
176+
const wc = new Array(26).fill(0);
177+
let ok = true;
178+
for (const c of w) {
179+
if (++wc[idx(c)] > cnt[idx(c)]) {
180+
ok = false;
181+
break;
182+
}
183+
}
184+
if (ok) {
185+
ans += w.length;
186+
}
187+
}
188+
return ans;
163189
}
164190
```
165191

0 commit comments

Comments
 (0)