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

面经求玉米大米红薯土豆

🔗
matieee 2015-11-6 16:40:02 | 只看该作者
全局:
用k+1的min heap怎么样?
像Sort a nearly sorted (or K sorted) array ,
http://www.geeksforgeeks.org/nearly-sorted-algorithm/
回复

使用道具 举报

🔗
fatalme 2015-11-6 17:18:21 | 只看该作者
全局:
  1. class charComparator implements Comparator<int[]> {
  2.   
  3.   int k = 0;
  4.   
  5.   public charComparator(int k){
  6.     this.k = k;
  7.   }

  8.   @Override
  9.   public int compare(int[] o1, int[] o2) {
  10.     if(o1[k] != o2[k]){
  11.       return o1[k] - o2[k];
  12.     }
  13.     return o1[1-k] - o2[1-k];
  14.   }
  15.   
  16. }

  17. public class Solution {
  18.   
  19.   public String smallest(String s, int k){
  20.     StringBuffer sb = new StringBuffer();
  21.     PriorityQueue<int[]> pqc = new PriorityQueue<>(k, new charComparator(0));
  22.     PriorityQueue<int[]> pqi = new PriorityQueue<>(k, new charComparator(1));
  23.     int i = 0;
  24.     for(i = 0; i < Math.min(s.length(), k); ++i){
  25.       int[] a = new int[]{(int)s.charAt(i), i};
  26.       pqc.add(a);
  27.       pqi.add(a);
  28.     }
  29.     for(int j = i; j < i + s.length(); ++j){
  30.       if(j < s.length()){
  31.         int[] a = new int[]{(int)s.charAt(j), j};
  32.         pqc.add(a);
  33.         pqi.add(a);
  34.       }
  35.       if(pqi.peek()[1] + k == j - i){
  36.         sb.append(s.charAt(pqi.peek()[1]));
  37.         pqc.remove(pqi.poll());
  38.       }else{
  39.         sb.append((char)pqc.peek()[0]);
  40.         pqi.remove(pqc.poll());
  41.       }
  42.     }
  43.     return sb.toString();
  44.   }
  45. }
复制代码
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
回复

使用道具 举报

🔗
Mr.Sagemaker 2015-11-6 22:53:25 | 只看该作者
全局:
fatalme 发表于 2015-11-6 17:18
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点 ...

学习了!第一次看到的pq和comparator这样用法,给大神跪了。
回复

使用道具 举报

🔗
guoqinlong 2015-11-6 23:27:07 | 只看该作者
全局:
fatalme 发表于 2015-11-6 17:18
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点 ...

好厉害的算法,学习了!
另外问一下,38行,pqi.peek()[1]是不是应该是pqi.peek()[0]?因为数组0元素代表的是字符,1代表的是下标
回复

使用道具 举报

🔗
matieee 2015-11-7 00:19:05 | 只看该作者
全局:
public String minPermutation(int k, String s){
        if(s == null || s.length() < k || k == 0 ) return "";
            char[] str = s.toCharArray();
            PriorityQueue<Character> minHeap = new PriorityQueue<Character>(k+1);
            int index = 0;
            for(int i = 0; i<s.length();i++){
                if(minHeap.size()<k+1){
                    minHeap.add(str[i]);
                }
                if(minHeap.size() == k+1){
                    str[index++] = minHeap.remove();
                }
            }
            while(index < s.length()){
                str[index++] = minHeap.remove();
            }
            return new String(str);
    }
回复

使用道具 举报

全局:
fatalme 发表于 2015-11-6 17:18
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点 ...

感谢分享思路!!
回复

使用道具 举报

🔗
Mr.Sagemaker 2015-11-7 01:31:42 | 只看该作者
全局:
matieee 发表于 2015-11-7 00:19
public String minPermutation(int k, String s){
        if(s == null || s.length() < k || k == 0 ) r ...

你这里的代码和我在 “沙发” 的代码是一样的,犯了一个致命错误,看漏了那个条件, "每个字母只能换到距离k以内的地方".. 你可以试试

test case = "fdcbaabcdefadbcdaea", k = 2

正确:[c, b, a, a, b, c, d, e, f, a, d, b, c, d, a, e, a]
错误:[c, b, a, a, b, c, d, d, e, a, d, b, c, d, a, e, a, f, f]

第一个f应该被检查到out of range之后就扔掉的
回复

使用道具 举报

🔗
RagingSword 2015-11-7 01:47:31 | 只看该作者
全局:
一亩三分地严打"顶""好贴""收藏了"之类的垃圾回复帖!被警告三次,系统会自动封杀ID!

想支持楼主,请点击帖子下方的"好苗""分享""收藏"键,酌情给楼主加大米(系统不扣你自己的分)。
积分不够看不了帖子,请参考论坛导航里的"帮助","新手提纲"里有攒积分指南
回复

使用道具 举报

🔗
RagingSword 2015-11-7 01:48:14 | 只看该作者
全局:
我也上一个算法
  1. public class Solution {

  2.     public static void main(String[] args){
  3.         String s = "bcdaea";
  4.         int k = 2;
  5.         System.out.println(new Solution().smallest(s, k));
  6.     }


  7.     public String smallest(String s, int k){
  8.         StringBuffer sb = new StringBuffer();
  9.         PriorityQueue<Element> pq = new PriorityQueue<Element>(k, new Comparator<Element>() {
  10.             @Override
  11.             public int compare(Element o1, Element o2) {
  12.                 if(o1.c != o2.c){
  13.                     return o1.c - o2.c;
  14.                 }
  15.                 return o1.index - o2.index;
  16.             }
  17.         });
  18.         boolean[] visited = new boolean[s.length()];
  19.         HashMap<Integer, Element> hm = new HashMap<Integer, Element>();
  20.         int i = 0;
  21.         for(i = 0; i < Math.min(s.length(), k); ++i){
  22.             Element e = new Element(s.charAt(i), i);
  23.             hm.put(i, e);
  24.             pq.add(e);
  25.         }
  26.         for(int j = i; j < i + s.length(); ++j){
  27.             if(j < s.length()){
  28.                 Element e = new Element(s.charAt(j), j);
  29.                 hm.put(j, e);
  30.                 pq.add(e);
  31.             }
  32.             if(j - i - k >= 0 && !visited[j-i-k]){
  33.                 sb.append(hm.get(j-i-k).c);
  34.                 pq.remove(hm.get(j-i-k));
  35.                 visited[j-i-k] = true;
  36.             }else{
  37.                 Element e = pq.poll();
  38.                 visited[e.index] = true;
  39.                 sb.append(e.c);
  40.             }
  41.         }
  42.         return sb.toString();
  43.     }

  44.     static class Element{
  45.         char c;
  46.         int index;
  47.         public Element(char c, int index){
  48.             this.c = c;
  49.             this.index = index;
  50.         }
  51.     }
  52. }
复制代码
回复

使用道具 举报

🔗
matieee 2015-11-7 12:46:12 | 只看该作者
全局:
marthew777 发表于 2015-11-7 01:31
你这里的代码和我在 “沙发” 的代码是一样的,犯了一个致命错误,看漏了那个条件, "每个字母只能换到距 ...

的确:) 学习了。
回复

使用道具 举报

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

本版积分规则

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