-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
57a1ec4
commit 4af8a89
Showing
8 changed files
with
276 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,5 +97,6 @@ | |
"typeinfo": "cpp", | ||
"valarray": "cpp", | ||
"variant": "cpp" | ||
} | ||
}, | ||
"python.analysis.typeCheckingMode": "off" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def solution(n: str) -> int: | ||
def check(s: str) -> bool: | ||
""" | ||
# 判断s中是否含有除了0和1之外的字符 | ||
for i in s: | ||
if i != '0' and i != '1': | ||
return False | ||
return True | ||
""" | ||
return all(c in "01" for c in s) | ||
|
||
ans = 1 | ||
while not check(n): | ||
tosub = "".join("1" if c > "1" else c for c in n) | ||
n = str(int(n) - int(tosub)) | ||
ans += 1 | ||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
print(solution("10101") == 1) | ||
print(solution("212") == 2) | ||
print(solution("1000000") == 1) | ||
print(solution("123456789") == 9) | ||
print(solution("9876543210") == 9) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def solution(inputArray): | ||
inputArray.sort() # list的比较大小方法就是从第一个元素开始比较,如果第一个元素相等,就比较第二个元素,以此类推 | ||
ans = 0 | ||
st, ed = inputArray[0] | ||
for begin, end in inputArray[1:]: | ||
if begin > ed: | ||
ans += ed - st + 1 | ||
st, ed = begin, end | ||
else: | ||
ed = max(ed, end) | ||
ans += ed - st + 1 | ||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
# You can add more test cases here | ||
testArray1 = [[1, 4], [7, 10], [3, 5]] | ||
testArray2 = [[1, 2], [6, 10], [11, 15]] | ||
|
||
print(solution(testArray1) == 9) | ||
print(solution(testArray2) == 12) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
题解:https://bytedance.larkoffice.com/docx/WGGidwQk3o5h3XxjWGEcHwDGnZb | ||
经验:题目中给了多个字符串的操作,我们需要先考虑化简这些操作。考虑哪些操作可以被其他操作替代,哪些操作对结果没有影响。这道题的结论是插入是最优的操作,删除是完全不操作的,替换操作完全可以由插入操作代替。 | ||
已知只执行插入操作,那如何操作可以得到最多的UCC子串? | ||
前提:字符不能重复利用 | ||
1. 优先记录已经有的UCC子串的个数 | ||
2. 优先记录UC和CC的个数,因为它们只需要插入一次就可以得到UCC子串 | ||
3. 剩下的字符,需要插入两次才能得到UCC子串 | ||
4. 如果对现有的字符进行插入操作后次数还有剩余,那么剩余的次数可以用来直接插入UCC | ||
""" | ||
|
||
|
||
def solution(m: int, s: str) -> int: | ||
res, n = 0, 0 | ||
i = 0 | ||
length = len(s) | ||
while i < length: | ||
if ( | ||
s[i : i + 3] == "UCC" | ||
): # 这里的判断条件可以更简洁,不用每次都判断i是否越界 因为如果i+3越界的话,s[i:i+3]得到的是s[i:],不会报错,也不会等于'UCC' | ||
res += 1 | ||
i += 3 | ||
elif s[i : i + 2] == "UC" or s[i : i + 2] == "CC": | ||
n += 1 | ||
i += 2 | ||
else: | ||
i += 1 | ||
|
||
# 优化的写法,更简洁,但是不容易理解 | ||
ans = res + min(n, m) | ||
m = max(0, m - n) | ||
remaining = length - res * 3 - n * 2 | ||
ans += min(remaining, m // 2) | ||
m = max(0, m - remaining * 2) # m -= min(m, remaining * 2) 也可以 | ||
ans += m // 3 | ||
""" | ||
ans = res | ||
if n >= m: | ||
ans += m | ||
else: | ||
ans += n | ||
m -= n | ||
twice = len(s) - res * 3 - n * 2 | ||
if m >= twice * 2: | ||
ans += twice | ||
m -= twice * 2 | ||
ans += m // 3 | ||
else: | ||
ans += m // 2 | ||
""" | ||
return ans | ||
|
||
|
||
if __name__ == "__main__": | ||
print(solution(m=3, s="UCUUCCCCC") == 3) | ||
print(solution(m=6, s="U") == 2) | ||
print(solution(m=2, s="UCCUUU") == 2) | ||
|
||
|
||
# UC U UCC CC C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,118 @@ | ||
#include <bits/stdc++.h> | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
inline int read() | ||
|
||
const int N = 110; | ||
int a[N], b[N]; | ||
int n; | ||
|
||
bool compare(const vector<int> &arr1, const vector<int> &arr2) | ||
{ | ||
for (int i = 0; i < arr1.size(); i++) | ||
if (arr1[i] != arr2[i]) | ||
return false; | ||
return true; | ||
} | ||
|
||
void insertionStep(vector<int> &arr, int step) // 这是第step+1步 | ||
{ | ||
int x = 0, f = 1; | ||
char ch = getchar(); | ||
while (!isdigit(ch)) | ||
for (int i = 0; i < step; i++) | ||
{ | ||
f = ch != '-'; | ||
ch = getchar(); | ||
if (arr[i] <= arr[step]) | ||
continue; | ||
else | ||
{ | ||
int temp = arr[step]; | ||
for (int j = step - 1; j >= i; j--) | ||
arr[j + 1] = arr[j]; | ||
arr[i] = temp; | ||
break; | ||
} | ||
} | ||
while (isdigit(ch)) | ||
} | ||
|
||
void HeapAdjust(vector<int> &arr, int start, int fina) | ||
{ | ||
int temp = arr[start]; | ||
for (int j = 2 * start + 1; j <= fina; j = j * 2 + 1) | ||
{ | ||
x = (x << 1) + (x << 3) + (ch ^ 48); | ||
ch = getchar(); | ||
if (j < fina && arr[j] < arr[j + 1]) | ||
j++; | ||
if (temp >= arr[j]) | ||
break; | ||
arr[start] = arr[j]; | ||
start = j; | ||
} | ||
return f ? x : -x; | ||
arr[start] = temp; | ||
} | ||
int n; | ||
bitset<110> a[110]; | ||
|
||
void Heap(vector<int> &arr, int step) // 这是第step+1步 | ||
{ | ||
swap(arr[0], arr[arr.size() - step]); | ||
HeapAdjust(arr, 0, arr.size() - step - 1); | ||
} | ||
|
||
|
||
|
||
int main() | ||
{ | ||
n = read(); | ||
|
||
cin >> n; | ||
vector<int> initial(n), middle(n); | ||
vector<int> temp(n); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> initial[i]; | ||
temp[i] = initial[i]; | ||
} | ||
for (int i = 0; i < n; i++) | ||
cin >> middle[i]; | ||
|
||
// for (int i = 0; i < n; i++) | ||
// { | ||
// insertionStep(initial, i); | ||
// if (compare(initial, middle)) | ||
// { | ||
// cout << "Insertion Sort" << endl; | ||
// if (i < n - 1) | ||
// { | ||
// while (true) | ||
// { | ||
// insertionStep(initial, i + 1); // modified | ||
// if (compare(initial, middle)) | ||
// i++; | ||
// else | ||
// break; | ||
// } | ||
|
||
// } | ||
// cout << initial[0]; | ||
// for (int i = 1; i < n; i++) | ||
// cout << " " << initial[i]; | ||
// return 0; | ||
// } | ||
// } | ||
|
||
for (int i = 0; i < n; i++) | ||
initial[i] = temp[i]; | ||
|
||
|
||
for (int i = (initial.size() - 1) / 2; i >= 0; i--) | ||
HeapAdjust(initial, i, initial.size()-1);//建初始大堆 | ||
|
||
for (int i = 1; i <= n; i++) | ||
for (int j = 1; j <= n; j++) | ||
{ | ||
Heap(initial, i); | ||
if (compare(initial, middle)) | ||
{ | ||
int x; | ||
cin >> x; | ||
a[i][j] = x; | ||
cout << "Heap Sort" << endl; | ||
Heap(initial, i + 1); | ||
cout << initial[0]; | ||
for (int i = 1; i < n; i++) | ||
cout << " " << initial[i]; | ||
return 0; | ||
} | ||
for (int i = 1; i <= n; i++) | ||
cout << a[i] << "\n"; | ||
for (int j = 1; j <= n; j++)//注意j循环在i循环外 | ||
for (int i = 1; i <= n; i++) | ||
if (a[i][j]) | ||
a[i] |= a[j];//bitset也挺好写的 | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
for (int j = 1; j <= n; j++) | ||
putchar(a[i][j] + '0'), putchar(' '); | ||
putchar('\n'); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("你好") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,73 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
int n; | ||
bitset<110> a[110]; | ||
int main() | ||
|
||
using ll = long long; | ||
using pii = pair<int, int>; | ||
const int N = 110; | ||
int a[N], b[N], backup[N], n; | ||
|
||
bool isInsert() | ||
{ | ||
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); | ||
cin >> n; | ||
for (int i = 1; i <= n; i++) | ||
bool flag = false; | ||
for (int i = 2; i <= n; i ++ ) | ||
{ | ||
for (int j = 1; j <= n; j++) | ||
{ | ||
bool x; | ||
cin >> x; | ||
cout << x << "\n"; | ||
a[i][j] = x; | ||
} | ||
a[0] = a[i]; | ||
int pos = i - 1; | ||
while (a[pos] > a[0]) a[pos + 1] = a[pos], pos -- ; | ||
a[pos + 1] = a[0]; | ||
if (flag) return true; | ||
if (equal(b + 1, b + n + 1, a + 1)) flag = true; | ||
} | ||
for (int j = 1; j <= n; j++) | ||
return false; | ||
} | ||
|
||
void down(int u, int sz) | ||
{ | ||
int t = u; | ||
if (u * 2 <= sz && a[u * 2] > a[t]) t = 2 * u; | ||
if (u * 2 + 1 <= sz && a[u * 2 + 1] > a[t]) t = 2 * u + 1; | ||
if (u != t) | ||
{ | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
if (a[i][j]) | ||
a[i] |= a[j]; // 好好体会位运算 | ||
} | ||
swap(a[u], a[t]); | ||
down(t, sz); | ||
} | ||
for (int i = 1; i <= n; i++) | ||
} | ||
|
||
bool isHeap() | ||
{ | ||
memcpy(a, backup, sizeof a); | ||
bool flag = false; | ||
for (int sz = n; sz >= 2; sz -- ) | ||
{ | ||
for (int j = 1; j <= n; j++) | ||
if (sz == n) | ||
{ | ||
cout << a[i][j] + '0' << ' '; | ||
for (int j = sz / 2; j; j -- ) | ||
down(j, sz); | ||
} | ||
cout << endl; | ||
else down(1, sz); | ||
if (flag) return true; | ||
if (equal(b + 1, b + n + 1, a + 1)) flag = true; | ||
swap(a[1], a[sz]); | ||
} | ||
return false; | ||
} | ||
|
||
int main() | ||
{ | ||
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); | ||
|
||
cin >> n; | ||
for (int i = 1; i <= n; i ++ ) cin >> a[i]; | ||
for (int i = 1; i <= n; i ++ ) cin >> b[i]; | ||
memcpy(backup, a, sizeof a); | ||
// if (isInsert()) cout << "Insertion Sort\n"; | ||
if (isHeap()) cout << "Heap Sort\n"; | ||
|
||
bool isfirst = true; | ||
for (int i = 1; i <= n; i ++ ) | ||
{ | ||
if (isfirst) cout << a[i], isfirst = false; | ||
else cout << ' ' << a[i]; | ||
} | ||
return 0; | ||
} |