`

Codility - Earliest Time Frog Crossing River

 
阅读更多

Question:

A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river. You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in minutes. The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X.

 

Solution:

public int solution(int X, int[] A) {
    boolean[] tiles = new boolean[X];
    int todo = X;

    for (int i = 0; i < A.length; i++) {
        int internalIndex = A[i] - 1;
        if (!tiles[internalIndex]) {
            todo--;
            tiles[internalIndex] = true;
        }

        if (todo == 0)
            return i;
    }

    return -1;
}

This algorithm only requires O(n) time, since it always keeps track of how many "holes" we still have to fill with leaves.

 

todo is the number of leaves still required to build the "bridge" of leaves. Whenever a leaf falls down, we first check whether there not already is a leaf at the position it falls down. If not, we decrement todo, fill the hole and go on. As soon as todo reaches 0, the entire river is covered ;) 

 

分享到:
评论

相关推荐

    EDF.zip_This Time For Good_com192edf_deadline_edf

    Earliest-deadline-first (EDF) is good for scheduling real-time tasks in order to meet timing constraint. Earliest-deadline-first (EDF) is real-time disk-scheduling algorithms that service realtime ...

    Hard_Real-Time_Computing_Systems

    4.1到4.7小节深入讨论了周期性任务调度方法,包括时间线调度、速率单调调度、最早截止时间优先以及具有约束截止时间的调度算法,同时对速率单调调度(Rate Monotonic, RM)和最早截止时间优先(Earliest Deadline ...

    Evaluation of Partitioning of Real-Time Workloads on Linux.pdf

    Evaluation of Partitioning of Real-Time Workloads on Linux.pdf 本文档对 Linux 操作系统上的实时工作负载分区进行了评估。实时系统是指能够在规定的时间范围内完成任务的系统,具有很高的时间效率和可靠性。...

    Hard Real-Time Computing Systems 3rd edition Giorgio C. Buttazzo

    综合上述内容,文档《Hard Real-Time Computing Systems, 3rd edition》是由Giorgio C. Buttazzo所著,主要围绕实时系统的概念、任务调度和算法进行了详细探讨。特别地,书中介绍了多种预测性调度算法及其应用场景,...

    An EDF-based Restricted-Migration Scheduling Algorithm for Multiprocessor Soft Real-Time Systems

    本文提出了一种基于最早截止时间优先(Earliest Deadline First, EDF)的受限迁移调度算法(EDF-fm),旨在解决多处理器软实时系统中的任务调度问题。软实时系统允许一定程度的任务延迟,而硬实时系统则要求所有任务...

    Concurrent-and-real-time-programming:加的斯大学关于并行和实时编程的实践

    7. **实时调度策略**:分析不同类型的实时调度算法,如Earliest Deadline First (EDF)和Priority Scheduling。 8. **异常处理和资源管理**:在并发和实时环境中如何正确地处理异常,以及如何优雅地关闭资源。 9. **...

    算法设计与分析中动态规划法

    在AOE网上,最早可能发生的事件时间(earliest)和最晚允许发生的事件时间(latest)对于识别关键活动至关重要。如果latest(j) - earliest(i)等于边,j&gt;的权重,那么边,j&gt;代表的活动就是关键活动。 解决关键路径问题...

    Real-Time-System:MCTE 4324实时系统每周分配

    实时系统(Real-Time System,简称RTS)是一种特殊类型的计算机系统,其主要特征在于它必须在规定的时间限制内完成特定的任务,这些时间限制是系统设计的关键要素。在MCTE 4324这门课程中,我们深入探讨了实时系统的...

    几种工期索赔方法的比较-副本.doc

    - **最早完成时间法(EFN - Earliest Finish Time Normal)**:通过计算延误事件后各活动最早完成时间的变化,确定索赔天数。 4. **实际与计划比较法(As-Planned vs. As-Built Method)**:通过对比实际施工进度...

    minix内核修改,增加实时进程和实时调度

    2 实现EDF(Earliest-Deadline-First)实时调度算法  EDF deadline越近的进程越先执行  实时调度 设置进程的优先级,保证进程的优先级高于其他用户进程,以保证它的实时性。但要考虑不会影响系统进程的执行。

    Real time scheduling theory-A historical perspective

    **EDF(Earliest Deadline First)算法**:基于截止时间最早的任务优先执行的原则,确保了关键任务能够按时完成。 - **特点**:简单易实现,适用于大多数实时系统。 - **局限性**:当任务截止时间分布不均匀时,可能...

    real time on windows

    - **调度策略**:实时系统需要高效的调度算法,如 Earliest Deadline First (EDF) 或者 Rate Monotonic (RM) 策略,确保优先处理具有严格截止期限的任务。 - **内存管理**:实时系统通常需要确定性和低延迟的内存...

    英语形容词比较级和最高级讲解.doc

    13. early - earlier - earliest 14. careful - more careful - most careful 15. exciting - more exciting - most exciting 16. busy - busier - busiest 17. great - greater - greatest 18. small - smaller - ...

    初中英语语法大全PPT学习教案.pptx

    以辅音字母+y结尾的词,将y变为i再加"-er"或"-est",如"early - earlier - earliest"。 对于部分双音节和多音节词,通常在词前加"more"或"most"来构成比较级和最高级,如"slowly - more slowly - most slowly"。...

    7-1 Build A Binary Search Tree.zip_13MV_6ST_Case File_Datastrctu

    Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project. Input Specification: Each input file contains one test case. Each case ...

    实时系统讲义 RealTime Lecture

    讲义中的"RealTime Section 1"可能介绍了实时系统的概念和分类,包括硬实时(hard real-time)和软实时(soft real-time)的区别。硬实时系统必须在规定时间内完成任务,否则可能导致严重后果;而软实时系统则允许一定...

    东北大学项目管理期末复习

    其中,EF represents the earliest finish time, ES represents the earliest start time, LS represents the latest start time, and LF represents the latest finish time. Float Time represents the total ...

    动态优先级调度算法的特点和实现.pdf

    1. 截止时间优先调度算法(EDF - Earliest Deadline First) EDF算法是一种基于任务截止时间的动态优先级调度策略。在实时系统中,任务的优先级由其绝对截止时间决定,即将要到期的任务具有更高的优先级。这使得...

Global site tag (gtag.js) - Google Analytics