注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
马上要Dropbox的一面了,发个您好! 本帖隐藏的内容需要积分高于 188 才可浏览 您当前积分为 0。 使用VIP即刻解锁阅读权限或查看其他获取积分的方式 游客,您好! 本帖隐藏的内容需要积分高于 188 才可浏览 您当前积分为 0。 VIP即刻解锁阅读权限 或 查看其他获取积分的方式 Unlock interview details and practice with AI Curated Interview Questions from Top Companies
还行吧。求各位给点大米。- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- public class GridIllumination {
- public GridIllumination()
- {
-
- }
-
- static class Point {
- public int x;
- public int y;
-
- public Point(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
-
- public boolean equals(Object o)
- {
- Point point = (Point)o;
-
- if(this.x==point.x&&this.y==point.y)
- return true;
-
- return false;
- }
-
- public int hashCode()
- {
- return this.x+this.y;
- }
- }
-
- public ArrayList<String> checkIllumination(int N, ArrayList<Integer[]>lamps, ArrayList<Integer[]> queries)
- {
- HashMap<Long, Integer> rows = new HashMap<Long, Integer>();
- HashMap<Long, Integer> cols = new HashMap<Long, Integer>();
- HashMap<Long, Integer> diags = new HashMap<Long, Integer>();
- HashMap<Long, Integer> otherDiags = new HashMap<Long, Integer>();
- HashSet<Point> lampSet = new HashSet<Point>();
-
- ArrayList<String> ans = new ArrayList<String>();
-
- for(int i = 0;i<lamps.size();++i)
- {
- Integer[] lamp = lamps.get(i);
- long row = lamp[0];
- long column = lamp[1];
-
- Point point = new Point(lamp[0], lamp[1]);
- lampSet.add(point);
-
- if(!rows.containsKey(row))
- {
- rows.put(row, 1);
- }
- else {
- rows.put(row, rows.get(row)+1);
- }
-
- if(!cols.containsKey(column))
- {
- cols.put(column, 1);
- }
- else {
- cols.put(column, cols.get(column)+1);
- }
-
- if(!diags.containsKey(row+column))
- {
- diags.put(row+column, 1);
- }
- else {
- diags.put(row+column, diags.get(row+column)+1);
- }
-
- if(!otherDiags.containsKey(row-column))
- {
- otherDiags.put(row-column, 1);
- }
- else {
- otherDiags.put(row-column, otherDiags.get(row-column)+1);
- }
- }
-
-
- for(int i = 0;i<queries.size();++i)
- {
- Integer[] query = queries.get(i);
- int x = query[0];
- int y = query[1];
- // turn light off
- for(long j = x-1;j<=x+1;++j)
- {
- for(long k = y-1;k<=y+1;++k)
- {
- if(j>0&&j<=N&&k>0&&k<=N) // valid position
- {
- if(lampSet.contains(new Point((int)j,(int)k)))
- {
- rows.put(j, rows.get(j)-1);
- cols.put(k, cols.get(k)-1);
- diags.put(j+k, diags.get(j+k)-1);
- otherDiags.put(j-k, otherDiags.get(j-k)-1);
- }
- }
- }
- }
-
- boolean dark= this.isDark(x, rows)&&this.isDark(y, cols)&&this.isDark(x+y, diags)&&this.isDark(x-y, otherDiags);
-
- if(dark)
- ans.add("DARK");
- else
- ans.add("LIGHT");
-
-
- // turn light on
- for(long j = x-1;j<=x+1;++j)
- {
- for(long k = y-1;k<=y+1;++k)
- {
- if(j>0&&j<=N&&k>0&&k<=N) // valid position
- {
- if(lampSet.contains(new Point((int)j,(int)k)))
- {
- rows.put(j, rows.get(j)+1);
- cols.put(k, cols.get(k)+1);
- diags.put(j+k, diags.get(j+k)+1);
- otherDiags.put(j-k, otherDiags.get(j-k)+1);
- }
- }
- }
- }
- }
-
- return ans;
- }
-
-
- public boolean isDark(long key, HashMap<Long, Integer> map)
- {
- // test if there is light on this row, column, diagnals
- if(map.containsKey(key))
- {
- int value = map.get(key);
- // test if the there is still some light on
- if(value>0)
- return false;
- }
-
- return true;
- }
-
-
- public static void main(String[] args) {
- GridIllumination illumination = new GridIllumination();
-
- // test case 1
- int N = 8;
- ArrayList<Integer[]> lamps = new ArrayList<Integer[]>();
- lamps.add(new Integer[]{1,6});
- lamps.add(new Integer[]{5,6});
- lamps.add(new Integer[]{7,3});
- lamps.add(new Integer[]{3,2});
-
- ArrayList<Integer[]> queries = new ArrayList<Integer[]>();
- queries.add(new Integer[]{4,4});
- queries.add(new Integer[]{6,6});
- queries.add(new Integer[]{8,1});
- queries.add(new Integer[]{3,2});
- queries.add(new Integer[]{2,3});
-
-
- ArrayList<String> answer = illumination.checkIllumination(N, lamps, queries);
-
- for(int i = 0;i<answer.size();++i)
- System.out.println(answer.get(i));
- }
- }
复制代码 |