Content-Length: 4813 | pFad | http://github.com/algorithm-visualizer/algorithms/pull/20.diff
thub.com diff --git a/Uncategorized/Shortest Unsorted Continuous Subarray/README.md b/Uncategorized/Shortest Unsorted Continuous Subarray/README.md new file mode 100644 index 00000000..26924a03 --- /dev/null +++ b/Uncategorized/Shortest Unsorted Continuous Subarray/README.md @@ -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 ), 4 loops are used +* **Space**: worst ), to hold min and max values + +## References +* [LeetCode](https://leetcode.com/articles/shortest-unsorted-continous-subarray/) \ No newline at end of file diff --git a/Uncategorized/Shortest Unsorted Continuous Subarray/code.js b/Uncategorized/Shortest Unsorted Continuous Subarray/code.js new file mode 100644 index 00000000..df0f12c8 --- /dev/null +++ b/Uncategorized/Shortest Unsorted Continuous Subarray/code.js @@ -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); \ No newline at end of fileFetched URL: http://github.com/algorithm-visualizer/algorithms/pull/20.diff
Alternative Proxies: