Skip to content

Commit eaacec0

Browse files
committed
🚀 18-Sep-2020
1 parent 953a4d7 commit eaacec0

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
2+
3+
"G": go straight 1 unit;
4+
"L": turn 90 degrees to the left;
5+
"R": turn 90 degress to the right.
6+
The robot performs the instructions given in order, and repeats them forever.
7+
8+
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
9+
10+
11+
12+
Example 1:
13+
14+
Input: "GGLLGG"
15+
Output: true
16+
Explanation:
17+
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
18+
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
19+
Example 2:
20+
21+
Input: "GG"
22+
Output: false
23+
Explanation:
24+
The robot moves north indefinitely.
25+
Example 3:
26+
27+
Input: "GL"
28+
Output: true
29+
Explanation:
30+
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
31+
32+
33+
Note:
34+
35+
1 <= instructions.length <= 100
36+
instructions[i] is in {'G', 'L', 'R'}
37+
Hide Hint #1
38+
Calculate the final vector of how the robot travels after executing all instructions once - it consists of a change in position plus a change in direction.
39+
Hide Hint #2
40+
The robot stays in the circle iff (looking at the final vector) it changes direction (ie. doesn't stay pointing north), or it moves 0.
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
class Solution {
53+
public:
54+
bool isRobotBounded(string instructions) {
55+
int n=instructions.length();
56+
int dir=0; // 0-N, 1-E, 2-S, 3-W
57+
int x=0, y=0;
58+
for(int i=0;i<n;i++){
59+
if(instructions[i]=='G'){
60+
if(dir==0) y++;
61+
else if(dir==1) x--;
62+
else if(dir==2) y--;
63+
else x++;
64+
} else if(instructions[i]=='L'){
65+
if(dir==0) dir=1;
66+
else if(dir==1) dir=2;
67+
else if(dir==2) dir=3;
68+
else dir=0;
69+
} else if(instructions[i]=='R'){
70+
if(dir==0) dir=3;
71+
else if(dir==1) dir=0;
72+
else if(dir==2) dir=1;
73+
else dir=2;
74+
}
75+
}
76+
77+
78+
if(x==0 && y==0) return true;
79+
else if(dir!=0) return true;
80+
else return false;
81+
}
82+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].
2+
3+
Return the array in the form [x1,y1,x2,y2,...,xn,yn].
4+
5+
6+
7+
Example 1:
8+
9+
Input: nums = [2,5,1,3,4,7], n = 3
10+
Output: [2,3,5,4,1,7]
11+
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
12+
Example 2:
13+
14+
Input: nums = [1,2,3,4,4,3,2,1], n = 4
15+
Output: [1,4,2,3,3,2,4,1]
16+
Example 3:
17+
18+
Input: nums = [1,1,2,2], n = 2
19+
Output: [1,2,1,2]
20+
21+
22+
Constraints:
23+
24+
1 <= n <= 500
25+
nums.length == 2n
26+
1 <= nums[i] <= 10^3
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
class Solution {
39+
public:
40+
vector<int> shuffle(vector<int>& nums, int n) {
41+
vector<int> res;
42+
for(int i=0;i<n;i++){
43+
res.push_back(nums[i]);
44+
res.push_back(nums[n+i]);
45+
}
46+
return res;
47+
}
48+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
2+
3+
"G": go straight 1 unit;
4+
"L": turn 90 degrees to the left;
5+
"R": turn 90 degress to the right.
6+
The robot performs the instructions given in order, and repeats them forever.
7+
8+
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
9+
10+
11+
12+
Example 1:
13+
14+
Input: "GGLLGG"
15+
Output: true
16+
Explanation:
17+
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
18+
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
19+
Example 2:
20+
21+
Input: "GG"
22+
Output: false
23+
Explanation:
24+
The robot moves north indefinitely.
25+
Example 3:
26+
27+
Input: "GL"
28+
Output: true
29+
Explanation:
30+
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
31+
32+
33+
Note:
34+
35+
1 <= instructions.length <= 100
36+
instructions[i] is in {'G', 'L', 'R'}
37+
Hide Hint #1
38+
Calculate the final vector of how the robot travels after executing all instructions once - it consists of a change in position plus a change in direction.
39+
Hide Hint #2
40+
The robot stays in the circle iff (looking at the final vector) it changes direction (ie. doesn't stay pointing north), or it moves 0.
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
class Solution {
53+
public:
54+
bool isRobotBounded(string instructions) {
55+
int n=instructions.length();
56+
int dir=0; // 0-N, 1-E, 2-S, 3-W
57+
int x=0, y=0;
58+
for(int i=0;i<n;i++){
59+
if(instructions[i]=='G'){
60+
if(dir==0) y++;
61+
else if(dir==1) x--;
62+
else if(dir==2) y--;
63+
else x++;
64+
} else if(instructions[i]=='L'){
65+
if(dir==0) dir=1;
66+
else if(dir==1) dir=2;
67+
else if(dir==2) dir=3;
68+
else dir=0;
69+
} else if(instructions[i]=='R'){
70+
if(dir==0) dir=3;
71+
else if(dir==1) dir=0;
72+
else if(dir==2) dir=1;
73+
else dir=2;
74+
}
75+
}
76+
77+
78+
if(x==0 && y==0) return true;
79+
else if(dir!=0) return true;
80+
else return false;
81+
}
82+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Say you have an array for which the ith element is the price of a given stock on day i.
2+
3+
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
4+
5+
Note that you cannot sell a stock before you buy one.
6+
7+
Example 1:
8+
9+
Input: [7,1,5,3,6,4]
10+
Output: 5
11+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
12+
Not 7-1 = 6, as selling price needs to be larger than buying price.
13+
Example 2:
14+
15+
Input: [7,6,4,3,1]
16+
Output: 0
17+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
18+
19+
20+
21+
22+
23+
24+
class Solution {
25+
public:
26+
int maxProfit(vector<int>& prices) {
27+
int n=prices.size();
28+
if(n==0) return 0;
29+
int res=0;
30+
int mini=prices[0];
31+
for(int i=1;i<n;i++){
32+
if(prices[i]<mini){
33+
mini=prices[i];
34+
} else {
35+
res=max(res, prices[i]-mini);
36+
}
37+
}
38+
return res;
39+
}
40+
};

0 commit comments

Comments
 (0)