在每个查询的2D数组中查找给定范围内的最大元素

时间:2016-06-07 06:22:28

标签: algorithm data-structures

给定2D数组,找到与每个查询对应的给定子矩形的元素最大值。 例如 对于3 * 4的数组

1 2 3 7

13 6 34 7

12 5 7 8

例如: 在(0,0)和(1,1)之间的子阵列中的MAX是13

(1,1)和(2,2)之间的子阵列中的MAX是34

1 个答案:

答案 0 :(得分:1)

我相信你看起来像这样:

public class test {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    LinkedHashMap<Integer, Integer> sorting = new LinkedHashMap<Integer, Integer>();
    sorting.put(10, 100);
    sorting.put(1, 100);
    sorting.put(20, 200);
    sorting.put(40, 100);
    sorting.put(30, 100);
    sorting.put(50, 200);

    for (Entry<Integer, Integer> entry : sorting.entrySet()) {
         Integer key = entry.getKey();
         Integer value = entry.getValue();
         System.out.println("LINKED UNSORTED === key: "+ key + '\t' + "Value: " + value);
          // do stuff
        }


    Comparator<Integer> comparator = new ValueCom();
    TreeMap<Integer, Integer> sortedMap =new TreeMap<Integer, Integer>(comparator);
    sortedMap.putAll(sorting);      


    for (Entry<Integer, Integer> entry : sortedMap.entrySet()) {
         Integer key = entry.getKey();
         Integer value = entry.getValue();
         System.out.println("SORTED   === key: "+ key + '\t' + "Value: " + value);
          // do stuff
        }

}

}


class ValueCom implements Comparator<Integer> {
public int compare(Integer keyA, Integer keyB){

return keyA.compareTo(keyB);
}

}