Skip to content

Commit 57b23de

Browse files
committed
🚀 26-Sep-2020
1 parent c192444 commit 57b23de

File tree

5 files changed

+365
-0
lines changed

5 files changed

+365
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.
2+
3+
The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.
4+
5+
6+
7+
Example 1:
8+
9+
Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
10+
Output: 2
11+
Explanation:
12+
For arr1[0]=4 we have:
13+
|4-10|=6 > d=2
14+
|4-9|=5 > d=2
15+
|4-1|=3 > d=2
16+
|4-8|=4 > d=2
17+
For arr1[1]=5 we have:
18+
|5-10|=5 > d=2
19+
|5-9|=4 > d=2
20+
|5-1|=4 > d=2
21+
|5-8|=3 > d=2
22+
For arr1[2]=8 we have:
23+
|8-10|=2 <= d=2
24+
|8-9|=1 <= d=2
25+
|8-1|=7 > d=2
26+
|8-8|=0 <= d=2
27+
Example 2:
28+
29+
Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
30+
Output: 2
31+
Example 3:
32+
33+
Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
34+
Output: 1
35+
36+
37+
Constraints:
38+
39+
1 <= arr1.length, arr2.length <= 500
40+
-10^3 <= arr1[i], arr2[j] <= 10^3
41+
0 <= d <= 100
42+
43+
44+
45+
46+
47+
class Solution {
48+
public:
49+
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
50+
int res=0;
51+
52+
for(auto x: arr1){
53+
bool flag=true;
54+
for(auto y: arr2){
55+
if(abs(x-y)<=d){
56+
flag=false; break;
57+
}
58+
}
59+
if(flag) res++;
60+
}
61+
62+
return res;
63+
}
64+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.
2+
3+
You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.
4+
5+
Example 1:
6+
7+
Input: [1,4], 2
8+
Output: 4
9+
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.
10+
This poisoned status will last 2 seconds until the end of time point 2.
11+
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
12+
So you finally need to output 4.
13+
14+
15+
Example 2:
16+
17+
Input: [1,2], 2
18+
Output: 3
19+
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned.
20+
This poisoned status will last 2 seconds until the end of time point 2.
21+
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
22+
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
23+
So you finally need to output 3.
24+
25+
26+
Note:
27+
28+
You may assume the length of given time series array won't exceed 10000.
29+
You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.
30+
31+
32+
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
41+
int n=timeSeries.size();
42+
43+
if(n==0) return 0;
44+
45+
int res=0;
46+
47+
res+=duration;
48+
49+
for(int i=1;i<n;i++){
50+
if(timeSeries[i]-timeSeries[i-1]>=duration)
51+
res+=duration;
52+
else res+=timeSeries[i]-timeSeries[i-1];
53+
}
54+
55+
return res;
56+
}
57+
};
58+
59+
60+
61+
62+
63+
64+
65+
class Solution {
66+
public:
67+
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
68+
int n=timeSeries.size();
69+
if(n==0) return 0;
70+
int res=duration;
71+
for(int i=1;i<n;i++)
72+
res+=min(timeSeries[i]-timeSeries[i-1], duration);
73+
74+
return res;
75+
}
76+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.
2+
3+
You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.
4+
5+
Example 1:
6+
7+
Input: [1,4], 2
8+
Output: 4
9+
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.
10+
This poisoned status will last 2 seconds until the end of time point 2.
11+
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
12+
So you finally need to output 4.
13+
14+
15+
Example 2:
16+
17+
Input: [1,2], 2
18+
Output: 3
19+
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned.
20+
This poisoned status will last 2 seconds until the end of time point 2.
21+
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
22+
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
23+
So you finally need to output 3.
24+
25+
26+
Note:
27+
28+
You may assume the length of given time series array won't exceed 10000.
29+
You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.
30+
31+
32+
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
41+
int n=timeSeries.size();
42+
43+
if(n==0) return 0;
44+
45+
int res=0;
46+
47+
res+=duration;
48+
49+
for(int i=1;i<n;i++){
50+
if(timeSeries[i]-timeSeries[i-1]>=duration)
51+
res+=duration;
52+
else res+=timeSeries[i]-timeSeries[i-1];
53+
}
54+
55+
return res;
56+
}
57+
};
58+
59+
60+
61+
62+
63+
64+
65+
class Solution {
66+
public:
67+
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
68+
int n=timeSeries.size();
69+
if(n==0) return 0;
70+
int res=duration;
71+
for(int i=1;i<n;i++)
72+
res+=min(timeSeries[i]-timeSeries[i-1], duration);
73+
74+
return res;
75+
}
76+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
You are given an array A of strings.
2+
3+
A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.
4+
5+
Two strings S and T are special-equivalent if after any number of moves onto S, S == T.
6+
7+
For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" that swap S[0] and S[2], then S[1] and S[3].
8+
9+
Now, a group of special-equivalent strings from A is a non-empty subset of A such that:
10+
11+
Every pair of strings in the group are special equivalent, and;
12+
The group is the largest size possible (ie., there isn't a string S not in the group such that S is special equivalent to every string in the group)
13+
Return the number of groups of special-equivalent strings from A.
14+
15+
16+
Example 1:
17+
18+
Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
19+
Output: 3
20+
Explanation:
21+
One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these.
22+
23+
The other two groups are ["xyzz", "zzxy"] and ["zzyx"]. Note that in particular, "zzxy" is not special equivalent to "zzyx".
24+
Example 2:
25+
26+
Input: ["abc","acb","bac","bca","cab","cba"]
27+
Output: 3
28+
29+
30+
Note:
31+
32+
1 <= A.length <= 1000
33+
1 <= A[i].length <= 20
34+
All A[i] have the same length.
35+
All A[i] consist of only lowercase letters.
36+
37+
38+
39+
40+
41+
class Solution {
42+
public:
43+
int numSpecialEquivGroups(vector<string>& A) {
44+
set<pair<string, string> > s;
45+
46+
for(auto str: A){
47+
pair<string, string> p;
48+
for(int i=0;i<str.length();i++){
49+
if(i%2==0) p.first+=str[i];
50+
else p.second+=str[i];
51+
}
52+
sort(p.first.begin(), p.first.end());
53+
sort(p.second.begin(), p.second.end());
54+
s.insert(p);
55+
}
56+
return s.size();
57+
}
58+
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.
2+
3+
The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.
4+
5+
Return the number of pawns the rook can capture in one move.
6+
7+
8+
9+
Example 1:
10+
11+
12+
13+
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
14+
Output: 3
15+
Explanation:
16+
In this example the rook is able to capture all the pawns.
17+
Example 2:
18+
19+
20+
21+
Input: [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
22+
Output: 0
23+
Explanation:
24+
Bishops are blocking the rook to capture any pawn.
25+
Example 3:
26+
27+
28+
29+
Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
30+
Output: 3
31+
Explanation:
32+
The rook can capture the pawns at positions b5, d6 and f5.
33+
34+
35+
Note:
36+
37+
board.length == board[i].length == 8
38+
board[i][j] is either 'R', '.', 'B', or 'p'
39+
There is exactly one cell with board[i][j] == 'R'
40+
41+
42+
43+
44+
45+
class Solution {
46+
public:
47+
int numRookCaptures(vector<vector<char>>& board) {
48+
int cnt=0;
49+
50+
int n=board.size();
51+
52+
int rx, ry;
53+
54+
for(int i=0;i<n;i++)
55+
for(int j=0;j<n;j++)
56+
if(board[i][j]=='R'){
57+
rx=i;
58+
ry=j;
59+
}
60+
61+
for(int j=ry;j>=0;j--){
62+
if(board[rx][j]=='B') break;
63+
if(board[rx][j]=='p'){
64+
cnt++; break;
65+
}
66+
}
67+
68+
for(int j=ry;j<n;j++){
69+
if(board[rx][j]=='B') break;
70+
if(board[rx][j]=='p'){
71+
cnt++; break;
72+
}
73+
}
74+
75+
for(int i=rx;i>=0;i--){
76+
if(board[i][ry]=='B') break;
77+
if(board[i][ry]=='p'){
78+
cnt++; break;
79+
}
80+
}
81+
82+
for(int i=rx;i<n;i++){
83+
if(board[i][ry]=='B') break;
84+
if(board[i][ry]=='p'){
85+
cnt++; break;
86+
}
87+
}
88+
89+
return cnt;
90+
}
91+
};

0 commit comments

Comments
 (0)