zoukankan      html  css  js  c++  java
  • Codility---StoneWall

    Task description

    You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by a zero-indexed array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall's left end and H[N−1] is the height of the wall's right end.

    The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular). Your task is to compute the minimum number of blocks needed to build the wall.

    Write a function:

    class Solution { public int solution(int[] H); }

    that, given a zero-indexed array H of N positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it.

    For example, given array H containing N = 9 integers:

    H[0] = 8 H[1] = 8 H[2] = 5 H[3] = 7 H[4] = 9 H[5] = 8 H[6] = 7 H[7] = 4 H[8] = 8

    the function should return 7. The figure shows one possible arrangement of seven blocks.

    Assume that:

    • N is an integer within the range [1..100,000];
    • each element of array H is an integer within the range [1..1,000,000,000].

    Complexity:

    • expected worst-case time complexity is O(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.

    Solution
     
    Programming language used: Java
    Total time used: 33 minutes
    Effective time used: 33 minutes
    Code: 16:12:13 UTC, java, final, score:  100
    // you can also use imports, for example:
    // import java.util.*;
    
    // you can write to stdout for debugging purposes, e.g.
    // System.out.println("this is a debug message");
    import java.util.Stack;
    class Solution {
        public int solution(int[] H) {
            // write your code in Java SE 8
            int blockct = 0;
            Stack<Integer> st = new Stack();
            int wallHeight = 0;
            for(int i=0;i<H.length;i++) {
                int currHeight = H[i];
                while(wallHeight > currHeight) {
                    wallHeight -= st.peek();
                    st.pop();
                }
                if(wallHeight < currHeight) {
                    st.push(currHeight - wallHeight);
                    wallHeight = currHeight;
                    blockct++;
                }
            }
            return blockct;
        }
    }


    https://codility.com/demo/results/trainingZ6BBNM-68A/
  • 相关阅读:
    【转】探秘Java中的String、StringBuilder以及StringBuffer
    【转】深入剖析Java中的装箱和拆箱
    谈谈我对多态的理解
    mysql组合索引之最左原则
    白衣浅谈各个集合的特性
    Linux 下的两个特殊的文件 -- /dev/null 和 /dev/zero 简介及对比
    内网穿透工具的原理与开发实战
    nohup命令说明-转载
    springboot 启动jar正确方式
    maven版本仲裁原则
  • 原文地址:https://www.cnblogs.com/samo/p/6864484.html
Copyright © 2011-2022 走看看