-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78e93bd
commit 8b43459
Showing
41 changed files
with
2,106 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 |
---|---|---|
@@ -1,5 +1,56 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 68 ms submission | ||
class Solution { | ||
public: | ||
int countCharacters(vector<string>& W, string C) { | ||
int r = 0; | ||
array<int, 26> cnt{}; | ||
for (char c : C) | ||
cnt[c - 'a']++; | ||
for (auto &s : W) { | ||
if (s.size() > C.size()) | ||
continue; | ||
auto t = cnt; | ||
bool f = true; | ||
for (char c : s) | ||
if (--t[c - 'a'] < 0) { | ||
f = false; | ||
break; | ||
} | ||
if (f) | ||
r += s.size(); | ||
} | ||
return r; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 76 ms submission | ||
class Solution { | ||
public: | ||
vector < int > cnt; | ||
int countCharacters(vector<string>& W, string C) { | ||
cnt.resize(27); | ||
for(int i = 0;i < C.size();++i) { | ||
++cnt[C[i] - 'a']; | ||
} | ||
|
||
int ans = 0; | ||
for(int i = 0;i < W.size();++i) { | ||
vector < int > ccnt = cnt; | ||
bool ok = true; | ||
for(int j = 0;j < W[i].size();++j) { | ||
ccnt[W[i][j] - 'a']--; | ||
if(ccnt[W[i][j] - 'a'] < 0) { | ||
ok = false; | ||
break; | ||
} | ||
} | ||
|
||
if(ok) { | ||
ans += W[i].size(); | ||
} | ||
} | ||
return ans; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
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,5 +1,70 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
Runtime: 216 ms | ||
Memory Usage: 70.4 MB | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
vector<int> sums; | ||
void dfs(TreeNode* r, int lvl) { | ||
if (r != nullptr) { | ||
if (sums.size() < lvl) sums.resize(lvl); | ||
sums[lvl - 1] += r->val; | ||
dfs(r->left, lvl + 1); | ||
dfs(r->right, lvl + 1); | ||
} | ||
} | ||
int maxLevelSum(TreeNode* r, int res = 0) { | ||
dfs(r, 1); | ||
for (auto i = 1; i < sums.size(); ++i) { | ||
if (sums[i] > sums[res]) res = i; | ||
} | ||
return res + 1; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 244 ms submission | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
|
||
vector<int> sum; | ||
void dfs(TreeNode* root, int level = 0){ | ||
if(root == NULL){ | ||
return; | ||
} | ||
if(level >= sum.size() ){ | ||
sum.push_back(0); | ||
} | ||
sum[level] += root->val; | ||
dfs(root->left, level + 1); | ||
dfs(root->right, level + 1); | ||
} | ||
int maxLevelSum(TreeNode* root) { | ||
sum.clear(); | ||
dfs(root); | ||
int maximum = *max_element(sum.begin(), sum.end()); | ||
for(int i = 0; i < (int)sum.size(); i++){ | ||
if(sum[i] == maximum){ | ||
return i + 1; | ||
} | ||
} | ||
return (int)sum.size(); | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
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,5 +1,113 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 48 ms submission | ||
class Solution { | ||
public: | ||
int maxDistance(vector<vector<int>>& grid) { | ||
int maxUp[100][100], maxDown[100][100]; | ||
int n = grid.size(), m = grid[0].size(); | ||
|
||
bool has1 = false, has0 = false; | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < m; j++) { | ||
if (grid[i][j] == 1) has1 = true; | ||
else has0 = true; | ||
if (has1 && has0) break; | ||
} | ||
if (has1 && has0) break; | ||
} | ||
if (!has1 || !has0) return -1; | ||
|
||
for (int j = 0; j < m; j++) { | ||
for (int i = 0; i < n; i++) { | ||
if (grid[i][j] == 1) maxUp[i][j] = -1e9; | ||
else if (i > 0) { | ||
if (grid[i-1][j] == 1) maxUp[i][j] = 0; | ||
else maxUp[i][j] = maxUp[i-1][j] + 1; | ||
} | ||
else | ||
maxUp[i][j] = 1e9; | ||
} | ||
for (int i = n - 1; i >= 0; i--) { | ||
if (grid[i][j] == 1) maxUp[i][j] = -1e9; | ||
else if (i < n - 1) { | ||
if (grid[i+1][j] == 1) maxDown[i][j] = 0; | ||
else maxDown[i][j] = maxDown[i+1][j] + 1; | ||
} | ||
else | ||
maxDown[i][j] = 1e9; | ||
} | ||
} | ||
|
||
/* | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < m; j++) | ||
cout << i << ' ' << j << ' ' << maxUp[i][j] << ' ' << maxDown[i][j] << endl; | ||
*/ | ||
|
||
int ans = 0; | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < m; j++) { | ||
if (grid[i][j] == 1) continue; | ||
int d = min(maxUp[i][j], maxDown[i][j]); | ||
// cout << d << ' '; | ||
for (int k = 1; k <= d && j + k < m; k++) { | ||
if (grid[i][j+k] != 1) { | ||
d = min(d, maxUp[i][j + k] + k); | ||
d = min(d, maxDown[i][j + k] + k); | ||
} | ||
else { | ||
d = min(d, k - 1); | ||
break; | ||
} | ||
} | ||
for (int k = 1; k <= d && j - k >= 0; k++) { | ||
if (grid[i][j - k] != 1) { | ||
d = min(d, maxUp[i][j - k] + k); | ||
d = min(d, maxDown[i][j - k] + k); | ||
} | ||
else { | ||
d = min(d, k - 1); | ||
break; | ||
} | ||
} | ||
// cout << i << ' ' << j << ' ' << d << endl; | ||
ans = max(ans, d); | ||
} | ||
} | ||
return ans + 1; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
sample 52 ms submission | ||
class Solution { | ||
public: | ||
int maxDistance(vector<vector<int>>& grid) { | ||
queue<pair<int, int>> q; | ||
int n = grid.size(), m = grid[0].size(); | ||
for (int i = 0; i < n; i++) | ||
for (int j = 0; j < m; j++) | ||
if (grid[i][j]) | ||
q.emplace(i, j); | ||
|
||
if (n * m == q.size() || 0 == q.size()) | ||
return -1; | ||
|
||
int ds[4][2] = {0,1,0,-1,1,0,-1,0}; | ||
for (int k = 1; ; k++) { | ||
int N = q.size(); | ||
for (int i = 0; i < N; i++) { | ||
auto [u, v] = q.front(); | ||
q.pop(); | ||
for (auto [du, dv] : ds) { | ||
int nu = u + du, nv = v + dv; | ||
if (nu < 0 || nu >= n || nv < 0 || nv >= m || grid[nu][nv]) | ||
continue; | ||
grid[nu][nv] = k; | ||
q.emplace(nu, nv); | ||
} | ||
} | ||
if (q.empty()) return k - 1; | ||
} | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
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,5 +1,39 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 16 ms submission | ||
class Solution { | ||
public: | ||
string lastSubstring(string S) { | ||
double mx = 0.0, pre = 0.0; | ||
int idx = 0, n = S.length(); | ||
for(int i = n - 1; i > -1; --i) { | ||
double cur = pre / 26.0 + S[i] - 'a'; | ||
if (cur > mx) { | ||
mx = cur; | ||
idx = i; | ||
} | ||
pre = cur; | ||
} | ||
return S.substr(idx); | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
sample 20 ms submission | ||
class Solution { | ||
typedef long double ld; | ||
public: | ||
string lastSubstring(string s) { | ||
int N = s.size(); | ||
int idx = N; | ||
vector<double> val(N+1, 0.0); | ||
double maxv = 0.0; | ||
for (int i=s.size()-1; i>-1; i--) { | ||
val[i] = val[i+1] / 26.0 + s[i] - 'a' + 1; | ||
if (val[i] > maxv) { | ||
idx = i; | ||
maxv = val[i]; | ||
} | ||
} | ||
return s.substr(idx); | ||
} | ||
}; | ||
__________________________________________________________________________________________________ |
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,5 +1,82 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 4 ms submission | ||
class Solution { | ||
public int countCharacters(String[] W, String C) { | ||
int[] dict = new int[26]; | ||
Arrays.fill(dict, 0); | ||
for (int i = 0; i < C.length(); i++) { | ||
dict[C.charAt(i) - 'a']++; | ||
} | ||
int res = 0; | ||
for (String s : W) { | ||
int[] copy = new int[26]; | ||
System.arraycopy(dict, 0, copy, 0, 26); | ||
boolean b = true; | ||
for (int i = 0; i < s.length(); i++) { | ||
if (--copy[s.charAt(i) - 'a'] < 0) { | ||
b = false; | ||
break; | ||
} | ||
} | ||
if (b) res += s.length(); | ||
} | ||
return res; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
sample 5 ms submission | ||
class Solution { | ||
public int countCharacters(String[] W, String C) { | ||
int[] base = toArray(C); | ||
int res = 0; | ||
for(String s : W) { | ||
if(isAchievable(base, toArray(s))) { | ||
res += s.length(); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
private int[] toArray(String s) { | ||
int[] res = new int[26]; | ||
for(int i = 0; i < s.length(); i++) { | ||
res[s.charAt(i) - 'a']++; | ||
} | ||
return res; | ||
} | ||
|
||
private boolean isAchievable(int[] base, int[] target) { | ||
for(int i = 0; i < target.length; i++) { | ||
if(target[i] > base[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
sample 8 ms submission | ||
class Solution { | ||
public int countCharacters(String[] W, String C) { | ||
int[] count = new int[256]; | ||
int res = 0; | ||
for (char ch : C.toCharArray()) | ||
count[ch]++; | ||
|
||
for (String w : W) { | ||
int[] temp = new int[256]; | ||
for (char ch : w.toCharArray()) | ||
temp[ch]++; | ||
if (match(count, temp)) | ||
res += w.length(); | ||
} | ||
return res; | ||
} | ||
|
||
private boolean match(int[] a, int[] b) { | ||
for (int i = 0; i < 256; i++) { | ||
if (a[i] < b[i]) return false; | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.