-
-
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.
Showing
12 changed files
with
260 additions
and
10 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,16 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public: | ||
void duplicateZeros(vector<int>& A) { | ||
int n = A.size(), j = n + count(A.begin(), A.end(), 0); | ||
for (int i = n - 1; i >= 0; --i) { | ||
if (--j < n) | ||
A[j] = A[i]; | ||
if (A[i] == 0 && --j < n) | ||
A[j] = 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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public: | ||
int largestValsFromLabels(vector<int>& values, vector<int>& labels, int num_wanted, int use_limit, int res = 0) { | ||
multimap<int, int> s; | ||
unordered_map<int, int> m; | ||
for (auto i = 0; i < values.size(); ++i) s.insert({values[i], labels[i]}); | ||
for (auto it = rbegin(s); it != rend(s) && num_wanted > 0; ++it) { | ||
if (++m[it->second] <= use_limit) { | ||
res += it->first; | ||
--num_wanted; | ||
} | ||
} | ||
return res; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
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,28 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public: | ||
int shortestPathBinaryMatrix(vector<vector<int>>& g, int steps = 0) { | ||
queue<pair<int, int>> q; | ||
q.push({ 0, 0 }); | ||
while (!q.empty()) { | ||
++steps; | ||
queue<pair<int, int>> q1; | ||
while (!q.empty()) { | ||
auto c = q.front(); | ||
q.pop(); | ||
if (c.first >= 0 && c.second >= 0 && c.first < g.size() && c.second < g.size() && !g[c.first][c.second]) { | ||
g[c.first][c.second] = 1; | ||
if (c.first == g.size() - 1 && c.second == g.size() - 1) return steps; | ||
for (auto i = -1; i < 2; ++i) | ||
for (auto j = -1; j < 2; ++j) | ||
if (i != 0 || j != 0) q1.push({ c.first + i, c.second + j }); | ||
} | ||
} | ||
swap(q, q1); | ||
} | ||
return -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,31 @@ | ||
__________________________________________________________________________________________________ | ||
class Solution { | ||
public: | ||
string shortestCommonSupersequence(string& A, string& B) { | ||
int i = 0, j = 0; | ||
string res = ""; | ||
for (char c : lcs(A, B)) { | ||
while (A[i] != c) | ||
res += A[i++]; | ||
while (B[j] != c) | ||
res += B[j++]; | ||
res += c, i++, j++; | ||
} | ||
return res + A.substr(i) + B.substr(j); | ||
} | ||
|
||
string lcs(string& A, string& B) { | ||
int n = A.size(), m = B.size(); | ||
vector<vector<string>> dp(n + 1, vector<string>(m + 1, "")); | ||
for (int i = 0; i < n; ++i) | ||
for (int j = 0; j < m; ++j) | ||
if (A[i] == B[j]) | ||
dp[i + 1][j + 1] = dp[i][j] + A[i]; | ||
else | ||
dp[i + 1][j + 1] = dp[i + 1][j].size() > dp[i][j + 1].size() ? dp[i + 1][j] : dp[i][j + 1]; | ||
return dp[n][m]; | ||
} | ||
}; | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
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,18 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public void duplicateZeros(int[] A) { | ||
int n = A.length, i = 0, j = 0; | ||
for (i = 0; i < n; ++i, ++j) { | ||
if (A[i] == 0) ++j; | ||
} | ||
for (i = i - 1; i >= 0; --i) { | ||
if (--j < n) | ||
A[j] = A[i]; | ||
if (A[i] == 0 && --j < n) | ||
A[j] = 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 |
---|---|---|
@@ -1,5 +1,27 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
public int largestValsFromLabels(int[] v, int[] l, int n, int u) { | ||
int[][] arr=new int[v.length][2]; | ||
for(int i=0;i<arr.length;i++){ | ||
arr[i][0]=v[i];arr[i][1]=l[i]; | ||
} | ||
Arrays.sort(arr,(a,b)->-a[0]+b[0]); | ||
Map<Integer,Integer> map=new HashMap<>(); | ||
for(int i=0;i<arr.length;i++){ | ||
if(!map.containsKey(arr[i][1]))map.put(arr[i][1],0); | ||
} | ||
int total=0; | ||
for(int i=0;i<arr.length;i++){ | ||
if(n==0)break; | ||
if(map.get(arr[i][1])<u){ | ||
map.put(arr[i][1],map.get(arr[i][1])+1); | ||
total+=arr[i][0]; | ||
n--; | ||
} | ||
} | ||
return total; | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
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,46 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution { | ||
private int dir[][] = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,-1},{-1,1},{-1,-1},{1,1}}; | ||
|
||
public int shortestPathBinaryMatrix(int[][] grid) { | ||
|
||
int m = grid.length; | ||
int n = grid[0].length; | ||
|
||
if(grid[0][0]==1 || grid[m-1][n-1]==1) { | ||
return -1; | ||
} | ||
|
||
boolean[][] visited = new boolean[m][n]; | ||
visited[0][0] = true; | ||
Queue<int[]> queue = new LinkedList<>(); | ||
queue.add(new int[]{0,0}); | ||
|
||
int ans=0; | ||
while (!queue.isEmpty()) { | ||
int size = queue.size(); | ||
for(int i=0;i<size;i++) { | ||
int[] pop = queue.remove(); | ||
if(pop[0]==m-1 && pop[1]==n-1) { | ||
return ans+1; | ||
} | ||
for (int k=0;k<8;k++) { | ||
int nextX = dir[k][0]+pop[0]; | ||
int nextY = dir[k][1]+pop[1]; | ||
|
||
if(nextX>=0 && nextX<m && nextY>=0 && nextY<n && !visited[nextX][nextY] && grid[nextX][nextY]==0) { | ||
queue.add(new int[]{nextX,nextY}); | ||
visited[nextX][nextY]=true; | ||
} | ||
|
||
} | ||
} | ||
ans++; | ||
} | ||
|
||
return -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,38 @@ | ||
__________________________________________________________________________________________________ | ||
class Solution { | ||
public String shortestCommonSupersequence(String s1, String s2) { | ||
|
||
// find LCS. | ||
int m = s1.length(), n = s2.length(); | ||
int[][] dp = new int[m + 1][n + 1]; | ||
for (int i = 0; i < m; ++i) { | ||
for (int j = 0; j < n; ++j) { | ||
if (s1.charAt(i) == s2.charAt(j)) { | ||
dp[i + 1][j + 1] = 1 + dp[i][j]; | ||
}else { | ||
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]); | ||
} | ||
} | ||
} | ||
|
||
// Build result. | ||
StringBuilder sb = new StringBuilder(); | ||
int i = m - 1, j = n - 1; | ||
while (i >= 0 || j >= 0) { | ||
if (i < 0 ^ j < 0) { // only one string reaches left end. | ||
char c = i < 0 ? s2.charAt(j--) : s1.charAt(i--); // remaining chars in the other string. | ||
sb.append(c); | ||
}else if (s1.charAt(i) == s2.charAt(j)) { // common char in LCS. | ||
sb.append(s1.charAt(i)); // append the char of either s1 or s2. | ||
--i; --j; | ||
}else { // the char is not in LCS. | ||
char c = dp[i][j + 1] > dp[i + 1][j] ? s1.charAt(i--) : s2.charAt(j--); // the char corresponding to larger dp value. | ||
sb.append(c); | ||
} | ||
} | ||
return sb.reverse().toString(); | ||
} | ||
} | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
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,14 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution: | ||
def duplicateZeros(self, arr): | ||
L = len(arr) | ||
i = 0 | ||
while i<L: | ||
if arr[i] == 0: | ||
arr.insert(i+1,0) | ||
i+=1 | ||
del arr[-1] | ||
i+=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,16 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution: | ||
def largestValsFromLabels(self, values: List[int], labels: List[int], num_wanted: int, use_limit: int) -> int: | ||
count = collections.defaultdict(int) | ||
combine = sorted(zip(values, labels), reverse=True) | ||
result = length = 0 | ||
for v,l in combine: | ||
if count[l] < use_limit: | ||
count[l] += 1 | ||
result += v | ||
length += 1 | ||
if length == num_wanted:break | ||
return result | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |
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,23 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution: | ||
def shortestPathBinaryMatrix(self, A: List[List[int]]) -> int: | ||
if A[0][0]: return -1 | ||
dq = collections.deque([(0, 0)]) | ||
seen = {(0, 0)} | ||
step = 1 | ||
n = len(A) | ||
while dq: | ||
sz = len(dq) | ||
for _ in range(sz): | ||
x, y = dq.popleft() | ||
for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, 1), (-1, -1), (1, -1)): | ||
xx, yy = x + dx, y + dy | ||
if 0 <= xx < n and 0 <= yy < n and A[xx][yy] == 0 and (xx, yy) not in seen: | ||
seen.add((xx, yy)) | ||
if (xx, yy) == (n - 1, n - 1): return step + 1 | ||
dq.append((xx, yy)) | ||
step += 1 | ||
return -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,34 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
class Solution(object): | ||
def shortestCommonSupersequence(self, s1, s2): | ||
# find longest sub in s2 that is also sub in s1. | ||
#dp[i][j]=max:dp[i-1][j],dp[i][j-1], or dp[i-1][j-1]+s1[i-1] if s1[i]==s2[j] | ||
m,n=len(s1),len(s2) | ||
dp=[[""]*(n+1) for _ in range(m+1)] | ||
for i in range(1,m+1): | ||
for j in range(1,n+1): | ||
|
||
if s1[i-1]==s2[j-1]: | ||
dp[i][j]=dp[i-1][j-1]+s1[i-1] | ||
else: | ||
dp[i][j]=max(dp[i-1][j],dp[i][j-1],key=lambda x:len(x)) | ||
s=dp[m][n] | ||
|
||
#two pointer to insert | ||
i,j=0,0 | ||
res=[] | ||
for ch in s: | ||
newi=s1.find(ch,i) | ||
newj=s2.find(ch,j) | ||
|
||
res.append(s1[i:newi]) | ||
res.append(s2[j:newj]) | ||
res.append(ch) | ||
i,j=newi+1,newj+1 | ||
|
||
res.append(s1[i:]) | ||
res.append(s2[j:]) | ||
return "".join(res) | ||
__________________________________________________________________________________________________ | ||
|
||
__________________________________________________________________________________________________ |