Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I think that question could be solved in O(m + n).
- public List<Integer> itemCount(String s, List<Integer> start, List<Integer> end) {
- List<Integer> result = new ArrayList<>();
- int sum, currentSum, len, index;
- len = s.length();
- int count[] = new int[len];
- int left[] = new int[len];
- int right[] = new int[len];
- sum = currentSum = 0;
- boolean begin = false;
- for(int i = 0; i < len; i++) {
- if(s.charAt(i) == '|') {
- if(begin) {
- sum += currentSum;
- count[i] = sum;
- }
- currentSum = 0;
- begin = true;
- } else {
- currentSum++;
- }
- }
- index = -1;
- for(int i = 0; i < len; i++) {
- if(s.charAt(i) == '|') {
- index = i;
- }
- left[i] = index;
- }
- index = len;
- for(int i = len - 1; i >= 0; i--) {
- if(s.charAt(i) == '|') {
- index = i;
- }
- right[i] = index;
- }
- int rBoundary, lBoundary;
- for(int i = 0; i < start.size(); i++) {
- lBoundary = right[start.get(i) - 1];
- rBoundary = left[end.get(i) - 1];
- if(lBoundary < rBoundary) {
- result.add(count[rBoundary] - count[lBoundary]);
- } else {
- result.add(0);
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement