诚征大神求解codility上的测试题,智力和能力大考验!(我是被打击惨了,看看大神们如何吧)

歪着看世界 2014-10-03 04:01:05
有机会在codility上做了两个测试题,用php解。考虑到这个题目主要是算法题目,C语言几乎是很多现代语言的始祖,大神汇聚,所以也发这个版来,请这里的大神们挑战跳帧。

当时我了两个程序,当时感觉还行,但出来结果一看,0分。诚征大神想出更好的solution.
啥都不说了,先上题目:
A non-empty zero-indexed array A consisting of N integers is given.

You can perform a single swap operation in array A. This operation takes two indices I and J, such that 0 ≤ I ≤ J < N, and exchanges the values of A[I] and A[J].

The goal is to check whether array A can be sorted into non-decreasing order by performing only one swap operation.

For example, consider array A such that:

A[0] = 1
A[1] = 3
A[2] = 5
A[3] = 3
A[4] = 7
After exchanging the values A[2] and A[3] we obtain an array [1, 3, 3, 5, 7], which is sorted in non-decreasing order.

Write a function:

function solution($A);

that, given a non-empty zero-indexed array A consisting of N integers, returns true if the array can be sorted into non-decreasing order by one swap operation or false otherwise.

For example, given:

A[0] = 1
A[1] = 3
A[2] = 5
A[3] = 3
A[4] = 7
the function should return true, as explained above.

On the other hand, for the following array:

A[0] = 1
A[1] = 3
A[2] = 5
A[3] = 3
A[4] = 4
the function should return false, as there is no single swap operation that sorts the array.

Assume that:

N is an integer within the range [1..100,000];
each element of array A is an integer within the range [1..1,000,000,000].
Complexity:

expected worst-case time complexity is O(N*log(N));
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.


修改了我原先程序中的错误后,我的程序如下

function solution($A) {
// write your code in PHP5.3
$arr = array();
$len = count($A);
$check = false;

for ($i=0;$i<$len-1;$i++)
{
for ($j=$i+1;$j<$len;$j++)
{
$arr = $A;
$temp = $arr[$j];
$arr[$j] = $arr[$i];
$arr[$i] = $temp;

$no_decrease = true;
for ($k=1;$k< $len;$k++)
{
if ($arr[$k]<$arr[$k-1])
{
$no_decrease = false;
}
}

if ($no_decrease == true)
{
$check = true;
}
}
}

return $check;

}



我验证了,结果正确,但时间复杂度不满足要求。大神们,你们有什么更好的算法么?
...全文
807 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
歪着看世界 2014-10-03
  • 打赏
  • 举报
回复
算法时间复杂度肯定超过O(N),我怀疑你可能题意要求弄错了。 参考一下我提供链接的帖子。 100分给你了,兄弟。
geyewei 2014-10-03
  • 打赏
  • 举报
回复
算法复杂度是O(N) 例如 {8} :True {8, 1 } :True {8, 1, 2}:False {8, 1, 2, 1,9}:true {8, 1, 2, 1, 2, 9}:False
    Private Function solution(ByVal arr As Integer()) As Boolean
        Dim swapCount As Integer = 0

        For big As Integer = 0 To arr.Length - 2
            If arr(big) > arr(big + 1) Then
                If swapCount > 0 Then
                    '交换过一次了,不能再交换了。
                    Return False
                End If

                '查找能够交换的数字
                Dim blnFoundSmall As Boolean = False
                For small As Integer = arr.Length - 1 To big + 1 Step -1
                    If arr(small) < arr(big) _
                    AndAlso arr(big) >= arr(small - 1) AndAlso (small = arr.Length-1 OrElse arr(big) <= arr(small + 1)) _
                    AndAlso arr(small) <= arr(big + 1) AndAlso (big = 0 OrElse arr(small) >= arr(big - 1)) Then
                        blnFoundSmall = True '找到了
                        swap(arr, big, small) '交换
                        swapCount += 1 '交换次数加1
                        Exit For
                    End If
                Next

                If blnFoundSmall = False Then
                    '找不到的话不能交换。
                    Return False
                End If
            End If
        Next

        Return True
    End Function

    Private Sub swap(ByRef rArr As Integer(), ByVal IdxA As Integer, ByVal IdxB As Integer)
        Dim temp = rArr(IdxA)
        rArr(IdxA) = rArr(IdxB)
        rArr(IdxB) = temp
    End Sub
歪着看世界 2014-10-03
  • 打赏
  • 举报
回复
我在php版上也开了类似的帖子,有大神参与讨论。 如果这里有大神感兴趣的话,不妨参考帖子http://bbs.csdn.net/topics/390899317?page=1#post-398290923 解不出来,痛苦啊!

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧