在下次下雨前要把湖里的水排出去,算是未雨绸缪。用到的数据结构Map, 有序Set
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.next(); line = line.substring(1, line.length()-1); List<Integer> rainList = new ArrayList<>(); TreeSet<Integer> zeroIdxSet = new TreeSet<>(); List<Integer> ansList = new ArrayList<>(); int idx = 0; while(idx < line.length()) { int num = 0; while(idx < line.length() && Character.isDigit(line.charAt(idx))) { num = num * 10 + line.charAt(idx++) - '0'; } if (num == 0) { zeroIdxSet.add(rainList.size()); ansList.add(1); // 待填值 } else { ansList.add(-1); } rainList.add(num); ++idx; } Map<Integer, Integer> rainMap = new HashMap<>(); for (int i = 0; i < rainList.size(); ++i) { if (rainList.get(i) != 0) { if (rainMap.containsKey(rainList.get(i))) { int date = rainMap.get(rainList.get(i)); Integer index = zeroIdxSet.higher(date); if (index == null || index > i) { System.out.println("[]"); // 来不及排水 return; } else { ansList.set(index, rainList.get(i)); } zeroIdxSet.remove(index); rainMap.put(rainList.get(i), i); } else { rainMap.put(rainList.get(i), i); // 记录第i天rains[i]下雨了 } } } StringBuilder cache = new StringBuilder("["); for (int num: ansList) { cache.append(num).append(","); } if (ansList.size() > 0) { cache.setCharAt(cache.length() -1, ']'); } else { cache.append("]"); } System.out.println(cache); } }
————————————————————————————
1 题 SQL 查询 select demand_id, cnt from (select demand_id, count(id) cnt from Task group by(demand_id)) table1 where cnt > 1;2 滑动窗口
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); List<Integer> list = new ArrayList<>(); String line = sc.next(); int idx = 0, k = 0; while (idx < line.length()) { int num = 0; while(Character.isDigit(line.charAt(idx))) { num = num * 10 + line.charAt(idx++) - '0'; } list.add(num); if (line.charAt(idx) == ':') { while(++idx < line.length() && Character.isDigit(line.charAt(idx))) { k = k * 10 + line.charAt(idx) -'0'; } break; } else { ++idx; } } if (list.size() <= k) { System.out.println("0.00%"); } else { double sum = 0; for (int i = 0; i < k; ++i) { sum += list.get(i); } double delta = 0.0; for (int i = k; i < list.size(); ++i) { double oldSum = sum; sum += list.get(i) - list.get(i-k); delta = Math.max(delta, (sum - oldSum) / oldSum); } System.out.printf("%.2f%%%n", delta * 100); } } }
全部评论
(7) 回帖