Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gridland Metro Hackerrank Java_Java_Algorithm - Fatal编程技术网

Gridland Metro Hackerrank Java

Gridland Metro Hackerrank Java,java,algorithm,Java,Algorithm,我正在Hackerrank[1]上解决此问题: Gridland的城市用m矩阵表示为n,其中行的编号从1到n,列的编号从1到m Gridland有一个火车轨道网络,总是沿着一排直线水平运行。换句话说,列车轨道的起点和终点是(r,c1)和(r,c2),其中r表示行号,c1表示起点列,c2表示列车轨道的终点列 Gridland市长正在对该市进行调查,以确定可以放置灯柱的位置数量。灯柱可以放置在任何未被火车轨道占用的单元中 给出Gridland及其k火车轨道的地图,找到并打印市长可以放置灯柱的单元格数

我正在Hackerrank[1]上解决此问题:

Gridland的城市用m矩阵表示为n,其中行的编号从1到n,列的编号从1到m

Gridland有一个火车轨道网络,总是沿着一排直线水平运行。换句话说,列车轨道的起点和终点是(r,c1)和(r,c2),其中r表示行号,c1表示起点列,c2表示列车轨道的终点列

Gridland市长正在对该市进行调查,以确定可以放置灯柱的位置数量。灯柱可以放置在任何未被火车轨道占用的单元中

给出Gridland及其k火车轨道的地图,找到并打印市长可以放置灯柱的单元格数量

注:一条列车轨道可能(或可能不)与同一行内的其他列车轨道重叠

输入格式

第一行包含三个空格分隔的整数,分别描述n(行数)、m(列数)和k(列车轨道数)的值。 k后续行的每一行i包含三个空格分隔的整数,分别描述定义列车轨道的r、c1、c2的值

打印一个整数,表示市长可以安装灯柱的单元数

以下是我的解决方案:

import java.io. * ;
import java.util. * ;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner input = new Scanner(System. in );
        int n = input.nextInt();
        int m = input.nextInt();
        int k = input.nextInt();
        int[][] arr = new int[n][m];
        int occupied = 0;
        for (int i = 0; i < n; i++) {
            Arrays.fill(arr[i], 0);
        }
        for (int i = 0; i < k; i++) {
            int r = input.nextInt() - 1;
            int c1 = input.nextInt() - 1;
            int c2 = input.nextInt() - 1;
            for (int j = c1; j <= c2; j++) {
                arr[r][j] = -1;
            }
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j] != -1) {
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}
import java.io.*;
导入java.util.*;
公共类解决方案{
公共静态void main(字符串[]args){
/*在此处输入代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/
扫描仪输入=新扫描仪(System.in);
int n=input.nextInt();
int m=input.nextInt();
int k=input.nextInt();
int[]arr=新的int[n][m];
int=0;
对于(int i=0;i对于(int j=c1;j问题中的约束是
1我使用HashMap编写了此解决方案,我维护的map Integer是行号,hashset是特定行访问的列集。维护此映射是为了检查是否有任何重叠轨迹。代码在小测试用例上给出了正确答案。但是,我在大多数测试用例中,它都会因为超时而失败。非常感谢对优化此代码的任何帮助

    import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        long n = sc.nextInt();
        long m = sc.nextInt();
        int k = sc.nextInt();        
        HashMap<Integer,HashSet> map = new HashMap<Integer,HashSet>(); 
        long count = n*m;
        for (int i =0;i<k;i++)
            {
            HashSet<Integer> set = new HashSet<Integer>();    
            int row = sc.nextInt();
            int c1 = sc.nextInt();
            int c2 = sc.nextInt();
            //System.out.println(row+" "+ c1+" "+c2);
            if (map.containsKey(row))
                {
                set = map.get(row);
                for (int j = c1;j<=c2;j++)
                {
                    if(set.add(j)) count--;
                }
                //if (set.add(c2)) count--;
                map.put(row,set);
            }
                else
                {
                    for (int p = c1;p<=c2;p++)
                    {
                        if (set.add(p)) count --;
                    }
                    //if (set.add(c2)) count--;

                    map.put(row,set);
                }
        }
        System.out.println(count);

    }
    }
import java.io.*;
导入java.util.*;
导入java.text.*;
导入java.math.*;
导入java.util.regex.*;
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
长n=sc.nextInt();
长m=sc.nextInt();
int k=sc.nextInt();
HashMap=newHashMap();
长计数=n*m;

对于(int i=0;i我不打算在这里提供解决方案,因为我认为这不符合HackerRank的精神。不过,我已经提供了一些提示,并指出了代码中的问题

Scanner input = new Scanner(System.in);
int n = input.nextInt();//ERROR: n ranges from 1 to 10E9, read in as a String
int m = input.nextInt();//ERROR: m ranges from 1 to 10E9, read in as String
int k = input.nextInt();
int[][] arr = new int[n][m];/*ERROR: The range of an int is 2E31-1 and large 
                                    integers will cause stack overflow anyway. 
                                    You'll need to consider a data structure 
                                    that's compatible w/ strings. Hint, consider 
                                    a hash table. */
int occupied = 0;
for (int i = 0; i < n; i++) {
    Arrays.fill(arr[i], 0);
}
for (int i = 0; i < k; i++) {
    int r = input.nextInt() - 1;//ERROR: r ranges from 1 to 10E9, read in as a String
    int c1 = input.nextInt() - 1;//ERROR: c1 ranges from 1 to 10E9, read in as a String
    int c2 = input.nextInt() - 1;//ERROR: c2 ranges from 1 to 10E9, read in as a String
    for (int j = c1; j <= c2; j++) {
        arr[r][j] = -1;
    }
}
/* NOTE: A train track may (or may not) overlap other train tracks within the 
     same row. Hence, your algorithm will need to account for track overlaps 
     and tracks that are in between of each other. For example, if a track 
     overlaps with another track in the same the row, the tracks should be 
     merged into a single track. If a smaller track is contained in a bigger 
     track, the smaller track should be ignored.*/
int count = 0;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
        if (arr[i][j] != -1) {
            count++;
        }
    }
}

System.out.println(count);
扫描仪输入=新扫描仪(System.in);
int n=input.nextInt();//错误:n的范围为1到10E9,以字符串形式读入
int m=input.nextInt();//错误:m的范围为1到10E9,以字符串形式读入
int k=input.nextInt();
int[][]arr=new int[n][m];/*错误:int的范围为2E31-1且较大
整数将导致堆栈溢出。
您需要考虑数据结构。
这是兼容的W/South.提示,考虑
哈希表*/
int=0;
对于(int i=0;i对于(int j=c1;j),我建议根据我的建议更改您的算法。祝您好运!