Skip to content

add Shortest Unsorted Continous Subarray #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Uncategorized/Shortest Unsorted Continuous Subarray/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Shortest Unsorted Continous Subarray
"Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length."

## Complexity
* **Time**: worst ![](https://latex.codecogs.com/svg.latex?O(N)), 4 loops are used
* **Space**: worst ![](https://latex.codecogs.com/svg.latex?O(1)), to hold min and max values

## References
* [LeetCode](https://leetcode.com/articles/shortest-unsorted-continous-subarray/)
144 changes: 144 additions & 0 deletions Uncategorized/Shortest Unsorted Continuous Subarray/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// import visualization libraries {
const { Tracer, LogTracer, Array1DTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
// }

// define tracer variables {
const tracer = new Array1DTracer('Sequence');
const D = [2, 6, 4, 8, 10, 9, 15];
tracer.set(D);
const logger = new LogTracer();
Layout.setRoot(new VerticalLayout([tracer, logger]));
Tracer.delay();
// }

function findUnsortedSubarray(nums) {
let min = Number.MAX_VALUE;
let max = Number.MIN_VALUE;
let flag = false;
// visualize {
let minIndex = -1;
let maxIndex = -1;
// }

for (let i = 1; i < nums.length; i++) {
// visualize {
tracer.deselect(i - 2, i - 1);
tracer.select(i - 1, i);
Tracer.delay();
// }

if (nums[i] < nums[i - 1]) {
flag = true;
}
if (flag) {
min = Math.min(min, nums[i]);
// visualize {
if (min === nums[i]) {
tracer.depatch(minIndex);
minIndex = i;
tracer.patch(i);
}
Tracer.delay();
// }
}
}

// visualize {
tracer.depatch(minIndex);
tracer.deselect(nums.length - 2);
tracer.deselect(nums.length - 1);
// }

// logger {
logger.println(`min = ${min}`);
Tracer.delay();
// }

flag = false;
for (let i = nums.length - 2; i >= 0; i--) {
// visualize {
tracer.deselect(i + 1, i + 2);
tracer.select(i, i + 1);
Tracer.delay();
// }

if (nums[i] > nums[i + 1]) {
flag = true;
}
if (flag) {
max = Math.max(max, nums[i]);
// visualize {
if (max === nums[i]) {
tracer.depatch(maxIndex);
maxIndex = i;
tracer.patch(i);
}
Tracer.delay();
// }
}
}

// visualize {
tracer.depatch(maxIndex);
tracer.deselect(0);
tracer.deselect(1);
Tracer.delay();
// }

// logger {
logger.println(`max = ${max}`);
// }

let l;
let r;
for (l = 0; l < nums.length; l++) {
// visualize {
tracer.deselect(l - 1);
tracer.select(l);
Tracer.delay();
// }

if (min < nums[l]) {
// visualize {
tracer.patch(l);
Tracer.delay();
// }
break;
}
}

for (r = nums.length - 1; r >= 0; r--) {
// visualize {
tracer.deselect(r + 1);
tracer.select(r);
Tracer.delay();
// }

if (max > nums[r]) {
// visualize {
tracer.patch(r);
Tracer.delay();
// }
break;
}
}

// visualize {
tracer.depatch(l);
tracer.depatch(r);
tracer.select(l, r);
Tracer.delay();
// }

const result = r - l < 0
? 0
: r - l + 1;

// logger {
logger.println(`result = ${result}`);
Tracer.delay();
// }

return result;
}
findUnsortedSubarray(D);