Skip to content

Commit 4af8a89

Browse files
committed
modified: .vscode/settings.json
new file: bytedance/小C的类二进制拼图.py new file: bytedance/打点计时器的区间合并.py new file: bytedance/最大UCC子串计算.py modified: bytedance/最大连续子数组和问题.py modified: test.cpp modified: test.py modified: test2.cpp
1 parent 57a1ec4 commit 4af8a89

File tree

8 files changed

+276
-55
lines changed

8 files changed

+276
-55
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,6 @@
9797
"typeinfo": "cpp",
9898
"valarray": "cpp",
9999
"variant": "cpp"
100-
}
100+
},
101+
"python.analysis.typeCheckingMode": "off"
101102
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def solution(n: str) -> int:
2+
def check(s: str) -> bool:
3+
"""
4+
# 判断s中是否含有除了0和1之外的字符
5+
for i in s:
6+
if i != '0' and i != '1':
7+
return False
8+
return True
9+
"""
10+
return all(c in "01" for c in s)
11+
12+
ans = 1
13+
while not check(n):
14+
tosub = "".join("1" if c > "1" else c for c in n)
15+
n = str(int(n) - int(tosub))
16+
ans += 1
17+
return ans
18+
19+
20+
if __name__ == "__main__":
21+
print(solution("10101") == 1)
22+
print(solution("212") == 2)
23+
print(solution("1000000") == 1)
24+
print(solution("123456789") == 9)
25+
print(solution("9876543210") == 9)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def solution(inputArray):
2+
inputArray.sort() # list的比较大小方法就是从第一个元素开始比较,如果第一个元素相等,就比较第二个元素,以此类推
3+
ans = 0
4+
st, ed = inputArray[0]
5+
for begin, end in inputArray[1:]:
6+
if begin > ed:
7+
ans += ed - st + 1
8+
st, ed = begin, end
9+
else:
10+
ed = max(ed, end)
11+
ans += ed - st + 1
12+
return ans
13+
14+
15+
if __name__ == "__main__":
16+
# You can add more test cases here
17+
testArray1 = [[1, 4], [7, 10], [3, 5]]
18+
testArray2 = [[1, 2], [6, 10], [11, 15]]
19+
20+
print(solution(testArray1) == 9)
21+
print(solution(testArray2) == 12)

bytedance/最大UCC子串计算.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
题解:https://bytedance.larkoffice.com/docx/WGGidwQk3o5h3XxjWGEcHwDGnZb
3+
4+
经验:题目中给了多个字符串的操作,我们需要先考虑化简这些操作。考虑哪些操作可以被其他操作替代,哪些操作对结果没有影响。这道题的结论是插入是最优的操作,删除是完全不操作的,替换操作完全可以由插入操作代替。
5+
6+
已知只执行插入操作,那如何操作可以得到最多的UCC子串?
7+
前提:字符不能重复利用
8+
1. 优先记录已经有的UCC子串的个数
9+
2. 优先记录UC和CC的个数,因为它们只需要插入一次就可以得到UCC子串
10+
3. 剩下的字符,需要插入两次才能得到UCC子串
11+
4. 如果对现有的字符进行插入操作后次数还有剩余,那么剩余的次数可以用来直接插入UCC
12+
"""
13+
14+
15+
def solution(m: int, s: str) -> int:
16+
res, n = 0, 0
17+
i = 0
18+
length = len(s)
19+
while i < length:
20+
if (
21+
s[i : i + 3] == "UCC"
22+
): # 这里的判断条件可以更简洁,不用每次都判断i是否越界 因为如果i+3越界的话,s[i:i+3]得到的是s[i:],不会报错,也不会等于'UCC'
23+
res += 1
24+
i += 3
25+
elif s[i : i + 2] == "UC" or s[i : i + 2] == "CC":
26+
n += 1
27+
i += 2
28+
else:
29+
i += 1
30+
31+
# 优化的写法,更简洁,但是不容易理解
32+
ans = res + min(n, m)
33+
m = max(0, m - n)
34+
remaining = length - res * 3 - n * 2
35+
ans += min(remaining, m // 2)
36+
m = max(0, m - remaining * 2) # m -= min(m, remaining * 2) 也可以
37+
ans += m // 3
38+
"""
39+
ans = res
40+
if n >= m:
41+
ans += m
42+
else:
43+
ans += n
44+
m -= n
45+
twice = len(s) - res * 3 - n * 2
46+
if m >= twice * 2:
47+
ans += twice
48+
m -= twice * 2
49+
ans += m // 3
50+
else:
51+
ans += m // 2
52+
"""
53+
return ans
54+
55+
56+
if __name__ == "__main__":
57+
print(solution(m=3, s="UCUUCCCCC") == 3)
58+
print(solution(m=6, s="U") == 2)
59+
print(solution(m=2, s="UCCUUU") == 2)
60+
61+
62+
# UC U UCC CC C

bytedance/最大连续子数组和问题.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
朴素的思路:枚举替换的位置,然后计算替换后的最大子数组和
44
对于每个位置i,计算包含a[i]的最大子数组和的方法是先计算前面的元素的贡献和后面的元素的贡献,然后再加上max(a[i], x)。而计算贡献可以用前缀和来优化。
55
6-
这道题与最大子数组和的区别在于,最大子数组和是不允许替换元素的,而这道题是允许替换元素的。对于前者,我们可以枚举每个位置i,然后计算以a[i]结尾的最大子数组和,最后取最大值。但是在这道题中,如果采用同样方法,我们就无法考虑替换后的a[i]对后面的数的贡献。因此我们枚举的是包含a[i]的最大子数组和,而不是以a[i]结尾的最大子数组和。
6+
这道题与最大连续子数组和模板题的区别在于,模板题是不允许替换元素的,而这道题是允许替换元素的。对于前者,我们可以枚举每个位置i,然后计算以a[i]结尾的最大连续子数组和,最后取最大值。但是在这道题中,如果采用同样方法,我们就无法考虑替换后的a[i]对后面的数的贡献。因此我们枚举的是包含a[i]的最大子数组和,而不是以a[i]结尾的最大子数组和。
77
'''
88
def solution(n: int, x: int, a: list) -> int:
99
# 将a数组中的最小值替换为x

test.cpp

Lines changed: 104 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,118 @@
1-
#include <bits/stdc++.h>
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
24
using namespace std;
3-
inline int read()
5+
6+
const int N = 110;
7+
int a[N], b[N];
8+
int n;
9+
10+
bool compare(const vector<int> &arr1, const vector<int> &arr2)
11+
{
12+
for (int i = 0; i < arr1.size(); i++)
13+
if (arr1[i] != arr2[i])
14+
return false;
15+
return true;
16+
}
17+
18+
void insertionStep(vector<int> &arr, int step) // 这是第step+1步
419
{
5-
int x = 0, f = 1;
6-
char ch = getchar();
7-
while (!isdigit(ch))
20+
for (int i = 0; i < step; i++)
821
{
9-
f = ch != '-';
10-
ch = getchar();
22+
if (arr[i] <= arr[step])
23+
continue;
24+
else
25+
{
26+
int temp = arr[step];
27+
for (int j = step - 1; j >= i; j--)
28+
arr[j + 1] = arr[j];
29+
arr[i] = temp;
30+
break;
31+
}
1132
}
12-
while (isdigit(ch))
33+
}
34+
35+
void HeapAdjust(vector<int> &arr, int start, int fina)
36+
{
37+
int temp = arr[start];
38+
for (int j = 2 * start + 1; j <= fina; j = j * 2 + 1)
1339
{
14-
x = (x << 1) + (x << 3) + (ch ^ 48);
15-
ch = getchar();
40+
if (j < fina && arr[j] < arr[j + 1])
41+
j++;
42+
if (temp >= arr[j])
43+
break;
44+
arr[start] = arr[j];
45+
start = j;
1646
}
17-
return f ? x : -x;
47+
arr[start] = temp;
1848
}
19-
int n;
20-
bitset<110> a[110];
49+
50+
void Heap(vector<int> &arr, int step) // 这是第step+1步
51+
{
52+
swap(arr[0], arr[arr.size() - step]);
53+
HeapAdjust(arr, 0, arr.size() - step - 1);
54+
}
55+
56+
57+
2158
int main()
2259
{
23-
n = read();
60+
61+
cin >> n;
62+
vector<int> initial(n), middle(n);
63+
vector<int> temp(n);
64+
65+
for (int i = 0; i < n; i++)
66+
{
67+
cin >> initial[i];
68+
temp[i] = initial[i];
69+
}
70+
for (int i = 0; i < n; i++)
71+
cin >> middle[i];
72+
73+
// for (int i = 0; i < n; i++)
74+
// {
75+
// insertionStep(initial, i);
76+
// if (compare(initial, middle))
77+
// {
78+
// cout << "Insertion Sort" << endl;
79+
// if (i < n - 1)
80+
// {
81+
// while (true)
82+
// {
83+
// insertionStep(initial, i + 1); // modified
84+
// if (compare(initial, middle))
85+
// i++;
86+
// else
87+
// break;
88+
// }
89+
90+
// }
91+
// cout << initial[0];
92+
// for (int i = 1; i < n; i++)
93+
// cout << " " << initial[i];
94+
// return 0;
95+
// }
96+
// }
97+
98+
for (int i = 0; i < n; i++)
99+
initial[i] = temp[i];
100+
101+
102+
for (int i = (initial.size() - 1) / 2; i >= 0; i--)
103+
HeapAdjust(initial, i, initial.size()-1);//建初始大堆
104+
24105
for (int i = 1; i <= n; i++)
25-
for (int j = 1; j <= n; j++)
106+
{
107+
Heap(initial, i);
108+
if (compare(initial, middle))
26109
{
27-
int x;
28-
cin >> x;
29-
a[i][j] = x;
110+
cout << "Heap Sort" << endl;
111+
Heap(initial, i + 1);
112+
cout << initial[0];
113+
for (int i = 1; i < n; i++)
114+
cout << " " << initial[i];
115+
return 0;
30116
}
31-
for (int i = 1; i <= n; i++)
32-
cout << a[i] << "\n";
33-
for (int j = 1; j <= n; j++)//注意j循环在i循环外
34-
for (int i = 1; i <= n; i++)
35-
if (a[i][j])
36-
a[i] |= a[j];//bitset也挺好写的
37-
for (int i = 1; i <= n; i++)
38-
{
39-
for (int j = 1; j <= n; j++)
40-
putchar(a[i][j] + '0'), putchar(' ');
41-
putchar('\n');
42117
}
43-
return 0;
44118
}

test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("你好")

test2.cpp

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,73 @@
11
#include <bits/stdc++.h>
22
using namespace std;
3-
int n;
4-
bitset<110> a[110];
5-
int main()
3+
4+
using ll = long long;
5+
using pii = pair<int, int>;
6+
const int N = 110;
7+
int a[N], b[N], backup[N], n;
8+
9+
bool isInsert()
610
{
7-
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
8-
cin >> n;
9-
for (int i = 1; i <= n; i++)
11+
bool flag = false;
12+
for (int i = 2; i <= n; i ++ )
1013
{
11-
for (int j = 1; j <= n; j++)
12-
{
13-
bool x;
14-
cin >> x;
15-
cout << x << "\n";
16-
a[i][j] = x;
17-
}
14+
a[0] = a[i];
15+
int pos = i - 1;
16+
while (a[pos] > a[0]) a[pos + 1] = a[pos], pos -- ;
17+
a[pos + 1] = a[0];
18+
if (flag) return true;
19+
if (equal(b + 1, b + n + 1, a + 1)) flag = true;
1820
}
19-
for (int j = 1; j <= n; j++)
21+
return false;
22+
}
23+
24+
void down(int u, int sz)
25+
{
26+
int t = u;
27+
if (u * 2 <= sz && a[u * 2] > a[t]) t = 2 * u;
28+
if (u * 2 + 1 <= sz && a[u * 2 + 1] > a[t]) t = 2 * u + 1;
29+
if (u != t)
2030
{
21-
for (int i = 1; i <= n; i++)
22-
{
23-
if (a[i][j])
24-
a[i] |= a[j]; // 好好体会位运算
25-
}
31+
swap(a[u], a[t]);
32+
down(t, sz);
2633
}
27-
for (int i = 1; i <= n; i++)
34+
}
35+
36+
bool isHeap()
37+
{
38+
memcpy(a, backup, sizeof a);
39+
bool flag = false;
40+
for (int sz = n; sz >= 2; sz -- )
2841
{
29-
for (int j = 1; j <= n; j++)
42+
if (sz == n)
3043
{
31-
cout << a[i][j] + '0' << ' ';
44+
for (int j = sz / 2; j; j -- )
45+
down(j, sz);
3246
}
33-
cout << endl;
47+
else down(1, sz);
48+
if (flag) return true;
49+
if (equal(b + 1, b + n + 1, a + 1)) flag = true;
50+
swap(a[1], a[sz]);
51+
}
52+
return false;
53+
}
54+
55+
int main()
56+
{
57+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
58+
59+
cin >> n;
60+
for (int i = 1; i <= n; i ++ ) cin >> a[i];
61+
for (int i = 1; i <= n; i ++ ) cin >> b[i];
62+
memcpy(backup, a, sizeof a);
63+
// if (isInsert()) cout << "Insertion Sort\n";
64+
if (isHeap()) cout << "Heap Sort\n";
65+
66+
bool isfirst = true;
67+
for (int i = 1; i <= n; i ++ )
68+
{
69+
if (isfirst) cout << a[i], isfirst = false;
70+
else cout << ' ' << a[i];
3471
}
3572
return 0;
3673
}

0 commit comments

Comments
 (0)