/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 126ms 13.746 MiB
#2 Wrong Answer 127ms 13.602 MiB

Code

function findStaircaseSegment(n, k, heights) {
    for (let i = 0; i < n; i++) {
        // Try all segments with lengths from 2 to k, starting at index i
        for (let length = 2; length <= k && i + length <= n; length++) {
            // Extract the segment and sort it
            const segment = heights.slice(i, i + length);
            const sortedSegment = [...segment].sort((a, b) => a - b);
            
            // Check if sorted segment is non-decreasing
            let isNonDecreasing = true;
            for (let j = 1; j < sortedSegment.length; j++) {
                if (sortedSegment[j] < sortedSegment[j - 1]) {
                    isNonDecreasing = false;
                    break;
                }
            }
            
            if (isNonDecreasing) {
                // Output the result as a 1-based index range
                return `YES\n${i + 1} ${i + length}`;
            }
        }
    }
    
    // If no such segment was found, return "NO"
    return "NO";
}

// Sample input
const n = 5;
const k = 2;
const heights = [1, 2, 4, 3, 5];

console.log(findStaircaseSegment(n, k, heights));

Information

Submit By
Type
Submission
Problem
P1120 Stairway to the Skyline
Contest
Brain Booster #7
Language
JavaScript (Node.js v18.19.1)
Submit At
2024-11-05 14:50:07
Judged At
2024-11-05 14:50:07
Judged By
Score
0
Total Time
127ms
Peak Memory
13.746 MiB