📣 独立日限时特惠: VIP通行证立减$68
查看: 952| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Leetcode Contest 8/25/2018

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
892. Surface Area of 3D Shapes
On a N * N grid, we place some 1 * 1 * 1 cubes.
Each value v = grid[j] represents a tower of v cubes placed on top of grid cell (i, j).
Return the total surface area of the resulting shapes.

  1. class Solution {
  2.     public int surfaceArea(int[][] grid) {
  3.         if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
  4.         int R = grid.length;
  5.         int C = grid[0].length;
  6.         int area = 0;
  7.         for (int r = 0; r < R; r++) {
  8.             for (int c = 0; c < C; c++) {
  9.                 int h = grid[r][c];
  10.                 if (h > 0) {
  11.                     area += (h * 4 + 2);
  12.                     area -= overlapped(r, c, R, C, grid);
  13.                 }
  14.             }
  15.         }
  16.         return area;
  17.     }
  18.    
  19.     public int height(int r, int c, int R, int C, int[][] grid) {
  20.         if (r >= 0 && r < R && c >= 0 && c < C) {
  21.             return grid[r][c];
  22.         } else {
  23.             return 0;
  24.         }
  25.     }
  26.    
  27.     public int overlapped(int r, int c, int R, int C, int[][] grid) {
  28.         int h = grid[r][c];
  29.         return Math.min(h, height(r + 1, c, R, C, grid)) +
  30.             Math.min(h, height(r -1, c, R, C, grid)) +
  31.             Math.min(h, height(r, c + 1, R, C, grid)) +
  32.             Math.min(h, height(r, c - 1, R, C, grid));
  33.     }
  34. }
复制代码


893. Groups of Special-Equivalent Strings
You are given an array A of strings.
Two strings S and T are [i]special-equivalent if after any number of moves, S == T.
A move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S with S[j].
Now, a [i]group of special-equivalent strings from A is a non-empty subset of A such that any string not in A is not special-equivalent with any string in A.
Return the number of groups of special-equivalent strings from A.
  1. import java.util.Arrays;

  2. class Solution {
  3.     public int numSpecialEquivGroups(String[] A) {
  4.         if (A == null || A.length == 0) return 0;
  5.         Set<String> set = new HashSet<String>();
  6.         for (String s: A) {
  7.             set.add(min(s));
  8.         }
  9.         return set.size();
  10.     }
  11.    
  12.     public String min(String s) {
  13.         if (s == null) return null;
  14.         if (s.length() == 0) return "";
  15.         int len = s.length();
  16.         char[] even = new char[(len +1) / 2];
  17.         for (int i = 0; i < len; i += 2) even[i / 2] = s.charAt(i);
  18.         char[] odd = new char[len / 2];
  19.         for (int i = 1; i < len; i += 2) odd[i / 2] = s.charAt(i);
  20.         Arrays.sort(even);
  21.         Arrays.sort(odd);
  22.         char[] result = new char[len];
  23.         for (int i = 0; i < len; i += 2) result[i] = even[i / 2];
  24.         for (int i = 1; i < len; i += 2) result[i] = odd[i / 2];
  25.         return new String(result);
  26.     }
  27. }
复制代码


894. All Possible Full Binary Trees
A full binary tree is a binary tree where each node has exactly 0 or 2 children.
Return a list of all possible full binary trees with N nodes.  Each element of the answer is the root node of one possible tree.
Each node of each tree in the answer must have node.val = 0.
You may return the final list of trees in any order.
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     static Map<Integer, List<TreeNode>> dic = new HashMap<>();
  12.    
  13.    
  14.     static private  void buildDic(int N) {
  15.         List<TreeNode> one = new ArrayList<TreeNode>();
  16.         one.add(newTreeNode());
  17.         dic.put(1, one);
  18.         for (int n = 1; n <= N; n += 2) {
  19.             if (dic.containsKey(n)) continue;
  20.             List<TreeNode> list = new ArrayList<>();
  21.             for (int l = 1; l <= n - 2; l += 2) {
  22.                 int r = n - l - 1;
  23.                 // System.out.println("" + l + ", " + r);
  24.                 for (TreeNode left : dic.get(l)) {
  25.                     for (TreeNode right: dic.get(r)) {
  26.                         TreeNode root = newTreeNode();
  27.                         root.left = copy(left);
  28.                         root.right = copy(right);
  29.                         list.add(root);
  30.                     }
  31.                 }
  32.             }
  33.             dic.put(n, list);
  34.         }
  35.     }
  36.    
  37.     static private TreeNode newTreeNode() { return new TreeNode(0); }
  38.    
  39.     public List<TreeNode> allPossibleFBT(int N) {
  40.         if (N <= 0 || N % 2 == 0) return new ArrayList<TreeNode>();
  41.         buildDic(N);
  42.         return dic.get(N);
  43.     }
  44.    
  45.     static public TreeNode copy(TreeNode origin) {
  46.         if (origin == null) return null;
  47.         TreeNode copied = new TreeNode(origin.val);
  48.         copied.left = copy(origin.left);
  49.         copied.right = copy(origin.right);
  50.         return copied;
  51.     }
  52. }
复制代码

895. Maximum Frequency Stack
Implement FreqStack, a class which simulates the operation of a stack-like data structure.
FreqStack has two functions:
  • push(int x), which pushes an integer x onto the stack.
  • pop(), which removes and returns the most frequent element in the stack.
    • If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.

  1. class FreqStack {
  2.    
  3.     static class Node implements Comparable {
  4.         int val;
  5.         int freq;
  6.         int order;
  7.         public Node(int val, int freq, int order) {
  8.             this.val = val;
  9.             this.freq = freq;
  10.             this.order = order;
  11.         }
  12.         @Override
  13.         public int compareTo(Object o) {
  14.             Node that = (Node) o;
  15.             if (this.freq != that.freq) return that.freq - this.freq;
  16.             return that.order - this.order;
  17.         }
  18.     }
  19.    
  20.     Map<Integer, Integer> freqs;
  21.     PriorityQueue<Node> pq;
  22.     int order = 0;
  23.    
  24.     public FreqStack() {
  25.         freqs = new HashMap<>();
  26.         pq = new PriorityQueue<>();
  27.     }
  28.    
  29.     public void push(int x) {
  30.         order += 1;
  31.         int freq = freqs.getOrDefault(x, 0) + 1;
  32.         freqs.put(x, freq);
  33.         pq.offer(new Node(x, freq, order));
  34.     }
  35.    
  36.     public int pop() {
  37.         Node node = pq.poll();
  38.         if (node.freq == 1) {
  39.             freqs.remove(node.val);
  40.         } else {
  41.             freqs.put(node.val, node.freq - 1);
  42.         }
  43.         return node.val;
  44.     }
  45. }

  46. /**
  47. * Your FreqStack object will be instantiated and called as such:
  48. * FreqStack obj = new FreqStack();
  49. * obj.push(x);
  50. * int param_2 = obj.pop();
  51. */
复制代码


上一篇:intern面试可以用STL吗?
下一篇:求大神分享刷题经验!!
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表