Sort by

recency

|

23 Discussions

|

  • + 0 comments

    PHP solution error "Time limit exceeded"

    code:

    function solve(array $h, int $limit, array $queries) {
        $res=[];
        for($k=0;$k<count($queries);$k++) {
            $count=0;
            for ($i=$queries[$k][0];$i<=$queries[$k][1];$i++) {
                for ($j=$queries[$k][1];$j>0;$j--) {
                    if ($i==$j) break;
                    if (abs($h[$i]-$h[$j])>$limit) continue;
                    $count++;
                }
            }
            $res[]=$count;
        }
        return $res;
    }
    
    #change run oprion to:
    
    $result = solve($h,$k, $queries);
    
  • + 0 comments

    for complete solution in java c++ and c programming search for programs.programmingoneonone.com on google

  • + 1 comment

    C++ doesn't include k in the solve function definition... Given code is incomplete.

  • + 0 comments

    Can anybody please explain solution?

  • + 0 comments

    A brute force solution, good to compare against a routine:

    // Complete the solve function below.
    static int[] solve(int[] h, int difference, int[][] queries) {
        List<int> ret = new List<int>();
        for(int query=0; query < queries.Length; query++)
        {
            int inicio = queries[query][0];
            int fim = queries[query][1];
            int total = 0;
            for(int iextertno = inicio; iextertno < fim; iextertno++)
            {
                for(int iinterno = iextertno+1; iinterno <= fim; iinterno++)
                {
                    int diff = Math.Abs(h[iextertno] - h[iinterno]);
                    if(diff <= difference)
                        total++;
                }
            }
            ret.Add(total);
        }
        return ret.ToArray();
    }