JavaScript 程序:检查是否可以通过旋转数组使其递增或递减
数组的旋转是指将数组视为循环数组,并将数组的元素向左或向右旋转一个索引,每次旋转一个索引,并且一端的一个元素可能取另一端的值。递增数组表示每个元素都大于或等于其前一个元素,递减数组表示每个元素都小于或等于其前一个元素。
在这个问题中,我们给定一个数组,我们可以向左或向右旋转数组,我们必须找到是否在某些旋转(可能是零)之后我们可以使数组递增或递减。
朴素方法
在这种方法中,我们将旋转数组,并在每次旋转时检查当前数组是否递增或递减。
示例
在下面的示例中,我们检查是否可以通过旋转给定数组使其递增或递减。以下是输入和预期输出。
输入:arr = [3, 4, 5, 6, 1, 2]
预期输出:是
输入:arr = [ 5, 1, 6, 2, 5, 3 ]
预期输出:否
示例
// function to rotate the given array
function rotate(arr){
var l = 0;
var r = arr.length-1;
while(l < r){
arr[l] += arr[r];
arr[r] = arr[l]-arr[r];
arr[l] = arr[l]-arr[r];
l++;
}
return arr;
}
// function to check if the given array is increasing or not
function increasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
for(var i = 1; i < len; i++){
if(arr[i] < arr[i-1]){
return false;
}
}
return true;
}
// function to check if the given array is decreasing or not
function decreasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
for(var i = 1; i < len; i++){
if(arr[i] > arr[i-1]){
return false;
}
}
return true;
}
// function to check whether the given array can become
// increasing or decreasing after certain rotations
function check(arr){
var k = arr.length
while(k--){
if(increasing(arr) || decreasing(arr)){
return true;
}
arr = rotate(arr);
}
return false;
}
// defining the arr's
var arr1 = [3, 4, 5, 6, 1, 2]
var arr2 = [5, 1, 6, 2, 5, 3]
console.log("The given array is: ");
console.log(arr1)
if(check(arr1) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
console.log("The given array is: ");
console.log(arr2)
if(check(arr2) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
输出
The given array is: [ 3, 4, 5, 6, 1, 2 ] Yes, after some rotations given array can be transformed into an increasing or decreasing array The given array is: [ 5, 1, 6, 2, 5, 3 ] No, after some rotations given array cannot be transformed into an increasing or decreasing array
上述代码的时间复杂度为 O(N*N),空间复杂度为 O(1)。
高效方法
在前面的数组中,我们检查了每次旋转数组是否递增或递减,在这种方法中,我们将部分检查数组是否递增或递减。
示例
// function to check if the given array is increasing or not
function increasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
var i = 0;
for(var i = 1; i < len; i++){
if(arr[i] < arr[i-1]){
break;
}
}
if(i == len) return true;
i++;
for(; i< len; i++){
if(arr[i] < arr[i-1]){
return false;
}
}
return arr[len-1] <= arr[0];
}
// function to check if the given array is decreasing or not
function decreasing(arr){
// getting the size of array
var len = arr.length
// traversing over the array
var i = 0;
for(var i = 1; i < len; i++){
if(arr[i] > arr[i-1]){
break;
}
}
if(i == len) return true;
i++;
for(; i< len; i++){
if(arr[i] > arr[i-1]){
return false;
}
}
return arr[len-1] >= arr[0];
}
// function to check whether the given array can become increasing or decreasing after certain rotations
function check(arr){
if(increasing(arr) || decreasing(arr)){
return true;
}
else{
return false;
}
}
// defining the arr's
var arr1 = [3, 4, 7, 6, 1, 2]
var arr2 = [5, 1, 6, 2, 5, 3]
console.log("The given array is: ");
console.log(arr1)
if(check(arr1) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
console.log("The given array is: ");
console.log(arr2)
if(check(arr2) == true){
console.log("Yes, after some rotations given array can be transformed into an increasing or decreasing array");
}
else{
console.log("No, after some rotations given array cannot be transformed into an increasing or decreasing array");
}
输出
The given array is: [ 3, 4, 7, 6, 1, 2 ] No, after some rotations given array cannot be transformed into an increasing or decreasing array The given array is: [ 5, 1, 6, 2, 5, 3 ] No, after some rotations given array cannot be transformed into an increasing or decreasing array
上述代码的时间复杂度为 O(N),空间复杂度为 O(1)。
结论
在本教程中,我们实现了一个 JavaScript 程序,用于检查是否可以通过旋转给定数组使其递增或递减。我们实现了两种方法,时间复杂度分别为 O(N*N) 和 O(N),并且两种方法的空间复杂度均为 O(1)。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP