Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path.
Return True if the path crosses itself at any point, that is, if at any time you are on a location you’ve previously visited. Return False otherwise.
Example 1:
Input: path = "NES"
Output: false
Explanation: Notice that the path doesn't cross any point more than once.
Example 2:
Input: path = "NESWW"
Output: true
Explanation: Notice that the path visits the origin twice.
Constraints:
1 <= path.length <= 10^4
path will only consist of characters in {'N', 'S', 'E', 'W}
Given the integer n representing the number of courses at some university labeled from 1 to n, and the array dependencies where dependencies[i] = [xi, yi] represents a prerequisite relationship, that is, the course xi must be taken before the course yi. Also, you are given the integer k.
In one semester you can take at mostk courses as long as you have taken all the prerequisites for the courses you are taking.
Return the minimum number of semesters to take all courses. It is guaranteed that you can take all courses in some way.
Example 1:
Input: n = 4, dependencies = [[2,1],[3,1],[1,4]], k = 2
Output: 3
Explanation: The figure above represents the given graph. In this case we can take courses 2 and 3 in the first semester, then take course 1 in the second semester and finally take course 4 in the third semester.
Example 2:
Input: n = 5, dependencies = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output: 4
Explanation: The figure above represents the given graph. In this case one optimal way to take all courses is: take courses 2 and 3 in the first semester and take course 4 in the second semester, then take course 1 in the third semester and finally take course 5 in the fourth semester.
Example 3:
Input: n = 11, dependencies = [], k = 2
Output: 6
Constraints:
1 <= n <= 15
1 <= k <= n
0 <= dependencies.length <= n * (n-1) / 2
dependencies[i].length == 2
1 <= xi, yi <= n
xi != yi
All prerequisite relationships are distinct, that is, dependencies[i] != dependencies[j].
The given graph is a directed acyclic graph.
Solution: DP/ Bitmask
NOTE: This is a NP problem, any polynomial-time algorithm is incorrect otherwise P = NP.
Variant 1: dp[m] := whether state m is reachable, where m is the bitmask of courses studied. For each semester, we enumerate all possible states from 0 to 2^n – 1, if that state is reachable, then we choose c (c <= k) courses from n and check whether we can study those courses. If we can study those courses, we have a new reachable state, we set dp[m | courses] = true.
Time complexity: O(n*2^n*2^n) = O(n*n^4) <– This will be much smaller in practice. and can be reduced to O(n*3^n). Space complexity: O(2^n)
vector<int>deps(n);// deps[i] = dependency mask for course i.
for(constauto&d:dependencies)
deps[d[1]-1]|=1<<(d[0]-1);
// dp(i)[s] := reachable(s) at semester i.
vector<int>dp(S);
dp[0]=1;
for(intd=1;d<=n;++d){// at most n semesters.
vector<int>tmp(S);// start a new semesters.
for(ints=0;s<S;++s){
if(!dp[s])continue;// not a reachable state.
intmask=0;
for(inti=0;i<n;++i)
if(!(s&(1<<i))&&(s&deps[i])==deps[i])
mask|=(1<<i);
// Prunning, take all.
if(__builtin_popcount(mask)<=k){
tmp[s|mask]=1;
}else{
// Try all subsets.
for(intc=mask;c;c=(c-1)&mask)
if(__builtin_popcount(c)<=k){
tmp[s|c]=1;
}
}
if(tmp.back())returnd;
}
dp.swap(tmp);
}
return-1;
}
};
Variant 2: dp[m] := min semesters to reach state m. dp[m | c] = min{dp[m | c], dp[m] + 1}, if we can reach m | c from m. This allows us to get rid of enumerate n semesters. Time complexity: O(2^n*2^n) <– This will be much smaller in practice. and can be reduced to O(3^n). Space complexity: O(2^n)
Given a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1’s in the resulting array.
Return 0 if there is no such subarray.
Example 1:
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
Example 4:
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
Example 5:
Input: nums = [0,0,0]
Output: 0
Constraints:
1 <= nums.length <= 10^5
nums[i] is either 0 or 1.
Solution 1: DP
Preprocess: l[i] := longest 1s from left side ends with nums[i], l[i] = nums[i] + nums[i] * l[i – 1] r[i] := longest 1s from right side ends with nums[i], r[i] = nums[i] + nums[i] * r[i + 1]
Use each node as a bridge (ignored), the total number of consecutive 1s = l[i – 1] + r[i + 1].
ans = max{l[i-1] + r[i +1]} Time complexity: O(n) Space complexity: O(n)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Author: Huahua
classSolution{
public:
intlongestSubarray(vector<int>&nums){
constintn=nums.size();
vector<int>l(n);
vector<int>r(n);
for(inti=0;i<n;++i)
l[i]=(i>0?l[i-1]*nums[i]:0)+nums[i];
for(inti=n-1;i>=0;--i)
r[i]=(i<n-1?r[i+1]*nums[i]:0)+nums[i];
intans=0;
for(inti=0;i<n;++i)
ans=max(ans,(i>0?l[i-1]:0)+
(i<n-1?r[i+1]:0));
returnans;
}
};
Solution 2: DP
dp[i][0] := longest subarray ends with nums[i] has no ones. dp[i][0] := longest subarray ends with nums[i] has 1 one. if nums[i] == 1: dp[i][0] = dp[i – 1][0] + 1 dp[i][1] = dp[i – 1][1] + 1 if nums[i] == 0: dp[i][0] = 0 dp[i][1] = dp[i – 1][0] + 1 Time complexity: O(n) Space complexity: O(n) -> O(1)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Author: Huahua
classSolution{
public:
intlongestSubarray(vector<int>&nums){
constintn=nums.size();
// dp[i][0] := longest subarray ends with nums[i-1] has no zeros.
// dp[i][0] := longest subarray ends with nums[i-1] has 1 zero.
vector<vector<int>>dp(n+1,vector<int>(2));
intans=0;
for(inti=1;i<=n;++i){
if(nums[i-1]==1){
dp[i][0]=dp[i-1][0]+1;
dp[i][1]=dp[i-1][1]+1;
}else{
dp[i][0]=0;
dp[i][1]=dp[i-1][0]+1;
}
ans=max({ans,dp[i][0]-1,dp[i][1]-1});
}
returnans;
}
};
Solution 3: Sliding Window
Maintain a sliding window l ~ r s.t sum(num[l~r]) >= r – l. There can be at most one 0 in the window. ans = max{r – l} for all valid windows.
Given an array of unique integers salary where salary[i] is the salary of the employee i.
Return the average salary of employees excluding the minimum and maximum salary.
Example 1:
Input: salary = [4000,3000,1000,2000]
Output: 2500.00000
Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000)/2= 2500
Example 2:
Input: salary = [1000,2000,3000]
Output: 2000.00000
Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000)/1= 2000