给定一个整数序列作为数组,我必须通过从数组中删除不超过一个元素来确定是否有可能获得严格增加的序列。示例
对于sequence = [1, 3, 2, 1]
,输出应为
almostIncreasingSequence(sequence) = false
为了获得严格增加的序列,此数组中没有一个元素可以删除。
对于sequence = [1, 3, 2]
,输出应为
almostIncreasingSequence(sequence) = true
我们可以从数组中删除3
以获得严格增加的序列[1, 2]
。或者,我们可以删除2
以获得严格增加的序列[1, 3].
如果可以从数组中删除一个元素以获得严格增加的序列,则该函数必须返回true
,否则返回false.
这是我已经尝试过的,但它并不适用于所有情况
function almostIncreasingSequence(sequence) {
for (var i = 0; i < sequence.length; i++) {
if (sequence[i] > sequence[i + 1]) {
sequence.splice(i, 1);
return true;
};
return false;
};
}
答案 0 :(得分:4)
function almostIncreasingSequence(sequence) {
var found = false;
for (var i=0;i<sequence.length;i++) {
if(sequence[i] <= sequence[i-1]) {
if(found) {
return false;
}
found = true;
if(i === 1 || i + 1 === sequence.length) {
continue;
}
else if (sequence[i] > sequence[i-2]) {
sequence[i-1] = sequence[i-2];
}
else if(sequence[i-1] >= sequence[i+1]) {
return false;
}
}
}
return true;
}
答案 1 :(得分:2)
这是我的解决方案。
首先我寻找一个递减序列。如果没有,则返回true 如果数组中有一个递减序列,我再创建两个数组,不包括递减序列中的两个项目,并检查这两个新数组。如果其中之一没有递减序列,则返回true,否则返回false
function indexOfFail(sequence) {
let index = null;
for (let i = 0; i < sequence.length -1; i++) {
if (sequence[i] >= sequence[i + 1]) {
index = i;
break;
}
}
return index;
}
function almostIncreasingSequence(sequence) {
let index = indexOfFail(sequence);
if (index == null) return true;
let tmp1 = sequence.slice(0);
tmp1.splice(index, 1); //remove failed item
if (indexOfFail(tmp1) == null) return true;
let tmp2 = sequence.slice(0);
tmp2.splice(index + 1, 1); //remove next item to failed item
if (indexOfFail(tmp2) == null) return true;
return false;
}
答案 2 :(得分:1)
这是我的回答
function almostIncreasingSequence(sequence) {
if (isIncreasingSequence(sequence)) {
return true;
}
for (var i = 0; i < sequence.length > 0; i++) {
var tmpSequence = sequence.slice(0); // copy original array
tmpSequence.splice(i, 1);
if (isIncreasingSequence(tmpSequence)) {
return true;
}
}
return false;
}
function isIncreasingSequence(sequence) {
for (var i = 0; i < sequence.length - 1; i++) {
if (sequence[i] >= sequence[i + 1]) {
return false;
}
}
return true;
}
almostIncreasingSequence([1, 3, 2, 1]); // false
almostIncreasingSequence([1, 3, 2]); // true
答案 3 :(得分:1)
如果有一个以上的数字小于之前的数字,我认为序列几乎没有增加:
function almostIncreasingSequence(sequence) {
var found = 0;
sequence.forEach((el, index) => {
var next= sequence[index + 1] || Infinity;
if (el >= next) {
found++;
}
});
return found <= 1;
}
console.log("1, 3, 2, 1: ", almostIncreasingSequence([1, 3, 2, 1]));
console.log("1, 3, 2: ", almostIncreasingSequence([1, 3, 2]));
console.log("1, 2, 5, 5, 5: ", almostIncreasingSequence([1, 2, 5, 5, 5]));
使用array.filter:
更简单,更简单
function almostIncreasingSequence(sequence) {
return sequence.filter((it, index) => it >= (sequence[index + 1] || Infinity)).length <= 1;
}
console.log("1, 3, 2, 1: ", almostIncreasingSequence([1, 3, 2, 1]));
console.log("1, 3, 2: ", almostIncreasingSequence([1, 3, 2]));
console.log("1, 2, 5, 5, 5: ", almostIncreasingSequence([1, 2, 5, 5, 5]));
console.log("1, 2, 4, 5, 5: ", almostIncreasingSequence([1, 2, 4, 5, 5]));
答案 4 :(得分:1)
我知道帖子很旧,但这里是解决问题的php
function almostIncreasingSequence($sequence){
$error = false;
$check_current_again = false;
for ($i = 0; $i + 1 < count($sequence); $i++) {
$next = $i + 1;
if($check_current_again){
$i = $i - 1;
}
$check_current_again = false;
if($sequence[$i] >= $sequence[$next]){
if($error){
return false;
}
$error = true;
if($i > 0 && $sequence[$i -1] >= $sequence[$i + 1]){
$check_current_again = true;
}
}
}
return true;
}
答案 5 :(得分:0)
boolean almostIncreasingSequence(int[] sequence) {
int n = sequence.length;
if (n == 2)
{
return true;
}
int count = 0;
boolean result = true;
for (int i = 0; i < sequence.length - 1; i++)
{
if (sequence[i] >= sequence[i+1])
{
count++;
if (i != 0 && sequence[i-1] >= sequence[i+1])
{
result = false;
}
if (i < n-2 && sequence[i] < sequence[i+2])
{
result = true;
}
if (i == n - 2)
{
result = true;
}
}
}
return (count < 2 && result);
}
答案 6 :(得分:0)
我的 Python3 答案:
O(n) 时间和 O(1) 空间
def almostIncreasingSequence(sequence):
counter = 0
for idx in range(1, len(sequence)):
# n is smaller or equal to n-1
if sequence[idx] <= sequence[idx-1]:
counter += 1
# checking if index to be checked is within bounds
if idx - 2 >= 0 and idx + 1 <= len(sequence)-1:
# n is smaller or equal to n-1 and n+1 is smaller or equal to n-1
print(sequence[idx], sequence[idx-2], sequence[idx+1], sequence[idx-1])
if sequence[idx] <= sequence[idx-2] and sequence[idx+1] <= sequence[idx-1]:
counter += 1
print(counter)
# return true if 1 or 0 in counter otherwise return false
return counter <= 1
答案 7 :(得分:0)
例如 1,2,[5,3],6
我们可以用2个指针来解决这个问题,p2一开始会比p1领先1个。一旦我们发现,例如,5 > 3,我们需要跳过每个并检查序列是否仍然有效,因此我们调用increasingSeq(),并通过(p1 -1)跳过5。然后我们通过 (p2 + 1) 跳过 3。
一旦我们跳过那些,我们就会再次开始比较序列。
function almostIncreasingSequence(sequence) {
if (sequence.length < 2) return true;
let p1 = 0;
let p2 = 1;
while(p2 < sequence.length) {
if (sequence[p1] >= sequence[p2]) {
const a = increasingSeq(sequence, p1 - 1, p2);
const b = increasingSeq(sequence, p1, p2 +1 )
return a || b
}
p1 = p2;
p2++
}
return true;
}
function increasingSeq(sequence, p1, p2) {
while(p2 < sequence.length) {
if (sequence[p1] >= sequence[p2]) return false;
p1 = p2;
p2++
}
return true;
}
答案 8 :(得分:0)
只是为了多样性。我就是这样解决的。
var datas = [
{ date: 'Fri 09, Apr', price: 239, currency: 'i' },
{ date: 'Fri 09, Apr', price: null, currency: null },
{ date: 'Fri 09, Apr', price: null, currency: null },
{ date: 'Mon 12, Apr', price: 239, currency: 'i' },
{ date: 'Mon 12, Apr', price: null, currency: null },
{ date: 'Wed 14, Apr', price: 339, currency: 'i' },
{ date: 'Wed 14, Apr', price: null, currency: null }
]
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: 'dummy@gmail.com',
pass: 'dummy'
}
});
let mailOptions = {
from: 'dummy@gmail.com',
to: 'dummy@gmail.com',
subject: 'Email subject',
text: datas // ( not working this way)
};
transporter.sendMail(mailOptions, function(err, data){
if (err) {
console.log('Error occurs');
} else { console.log('Email sent!');}
});
}
}
答案 9 :(得分:0)
这是一个非常脏的版本。 最近,我一直在尝试提供与语言无关的解决方案,以使我不再依赖于解决问题的功能,而是以后再学习我擅长的任何语言。
也许这是最肮脏的方法之一,但是时间复杂度是O(3N)= O(N)。 空间复杂度为O(2N)。
逻辑来自这样一个事实,您只需要检查一个缺少的元素,因此不需要做任何花哨的事情,只需保存该元素的索引并检查没有该元素的数组。 因为有时您可能会遇到另一个引起问题的元素(特别是在重复的情况下),所以您应该保存两个问题元素的索引。
再次,非常脏,但是它通过了所有测试用例和时间限制。
function almostIncreasingSequence(sequence) {
let wrongindex = 0;
let nextwrongindex = 0;
for(let i = 0; i < sequence.length - 1; i++){
if(sequence[i] >= sequence[i + 1]){
wrongindex = i;
nextwrongindex = i+1;
}
}
let newArr = [];
let newArr2 = [];
for(let i = 0; i < sequence.length; i++){
if(i != wrongindex){
newArr.push(sequence[i]);
}
}
for(let i = 0; i < sequence.length; i++){
if(i != nextwrongindex){
newArr2.push(sequence[i]);
}
}
let isincreasingcount = 0;;
for(let i = 0; i < newArr.length - 1; i++){
if(newArr[i] >= newArr[i+1]){
isincreasingcount++;
}
}
for(let i = 0; i < newArr2.length -1; i++){
if(newArr2[i] >= newArr2[i+1]){
isincreasingcount++;
}
}
if(isincreasingcount > 1){
return false;
}
return true;
}
答案 10 :(得分:0)
这是相同的python3实现。花了我一段时间。不太干净的代码
但是,请占用O(N)时间和O(1)空间,这样就可以了。
在这种情况下,我使用一种方法来维护计数器变量和指针
每当序列不以递增方式递增时,我都会将计数器增加1。
并保持指向该元素索引的指针,该索引不按顺序排列。
如果计数器的值大于1,则该序列在2点处中断,并且无法实现几乎增加的序列。如果序列已经到位,则计数器将为零。
当计数器为1时,棘手的部分到来了
在这里,我们利用我们保持的索引变量p
如果c == 1且索引为0或序列的长度,则破坏序列的元素位于开始或结尾,可以将其删除以获得递增的序列。
,如果不是这样。我们检查相邻的值,它们是否破坏任何顺序。 如果他们没有破坏订单,则返回True,否则返回False。希望我能提供帮助。原谅错误的缩进。我是StackOverflow上的代码编辑器的新手
def almostIncreasingSequence(sequence):
p = -1
c = 0
for i in range(1, len(sequence)):
if sequence[i-1] >= sequence[i]:
p = i
c += 1
if c > 1:
return False
if c == 0:
return True
if p == n -1 or p == 1:
return True
if sequence[p-1] < sequence[p+1] or sequence[p-2] < sequence[p]:
return True
return False
答案 11 :(得分:0)
static bool almostIncreasingSequence(int[] sequence)
{
int counter = 0;
if (sequence.Length <3)
return true;
int prevNo = sequence.Min() - 1;
for (int i = 0; i < sequence.Length; i++)
{
if (counter < 2)
{
if (prevNo < sequence[i])
prevNo = sequence[i];
else
{
if (i == 0 || i == sequence.Length - 1)
{
counter += 1;
}
else if (i > 1)
{
if (sequence[i] <= sequence[i - 2])
{
if (prevNo < sequence[i + 1])
{
counter += 1;
}
else
{
counter += 2;
}
}
else
{
prevNo = sequence[i];
counter = counter + 1;
}
}
// {
if (i < sequence.Length - 2)
{
if (sequence[i] < sequence[i + 1])
{
counter = counter + 1;
prevNo = sequence[i];
}
else
{
counter += 1;
}
}
// }
if (counter >= 2)
return false;
}
}
}
return true;
}
答案 12 :(得分:0)
算法:几乎递增的序列只能有一个小于其前一个数字的数字,并且不得等于其前一个数字之前的数字。另外,前一个数字不得等于当前数字之后的数字。
插图:考虑以下4个数字:
a, b, c, d
a和c不能彼此相等,b和d也不能相等。我们必须将此条件嵌套在(b > c)
条件之内。
实际的JavaScript代码:
const almostIncreasingSequence = seq => {
let decrementCount = 0;
for (let i = 1; i < seq.length - 1; i ++) {
if (seq[i] <= seq[i - 1]) {
decrementCount++;
if (seq[i] <= seq[i - 2] && seq[i + 1] <= seq[i - 1]) {
return false;
}
}
}
return decrementCount <= 1;
}
答案 13 :(得分:0)
function almostIncreasingSequence(seq) {
var bad=0
for(var i=1;i<seq.length;i++) if(seq[i]<=seq[i-1]) {
bad++
if(bad>1) return false
if(seq[i]<=seq[i-2]&&seq[i+1]<=seq[i-1]) return false
}
return true
}
&#13;
答案 14 :(得分:0)
Do until now() >= waittime
DoEvents
Loop
答案 15 :(得分:0)
解决问题:
function almostIncreasingSequence(sequence) {
var inc = true;
for (var i = 0; i < sequence.length; i++) {
if (sequence[i] >= sequence[i + 1]) {
if (inc) {
inc = false;
}
else {
return false;
}
}
};
return true;
}
console.log(almostIncreasingSequence([1, 3, 2, 1])); // false
console.log(almostIncreasingSequence([1, 3, 2])); // true
答案 16 :(得分:-1)
这是PHP中的解决方案
function almostIncreasingSequence($sequence) {
$foundOne = false;
for ($i = -1, $j = 0, $k = 1; $k < count($sequence); $k++) {
$deleteCurrent = false;
if ($sequence[$j] >= $sequence[$k])
{
if ($foundOne)
{
return false;
}
$foundOne = true;
if ($k > 1 && $sequence[$i] >= $sequence[$k])
{
$deleteCurrent = true;
}
}
if (!$foundOne)
{
$i = $j;
}
if (!$deleteCurrent)
{
$j = $k;
}
}
return true;
}