由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这个题没见过大家可以很快做出来吗?
相关主题
[google面试]iterator访问Bloomberg phone interview 面经
微软onsite SDET 面经求指点一道G家Iterator的题目
上一道小题请教F最近超爱的面试题任务安排的follow up怎么做
问个java hashcode的题曼哈顿距离iterator随便写了一个 请大家帮挑毛病,謝謝
讨论一个题目Amazon first phone interview
报个A家OFFER,回馈版上G家SET面经新题求解
share int2roman and roman2int java version请教一个关于java comparator的问题
G的一道Onesite题java8 的Lambda 有没有人用
相关话题的讨论汇总
话题: charstream话题: override话题: character话题: public话题: int
进入JobHunting版参与讨论
1 (共1页)
y***n
发帖数: 1594
1
Given a string of lowercase characters, reorder them such that the same
characters are at least distance d from each other.
Input: { a, b, b }, distance = 2
Output: { b, a, b }
w********s
发帖数: 1570
2
不一定有解,有可能有多个解
你要所有解,还是一个就够了?
w********s
发帖数: 1570
3
瞄了一眼,第一想法是sort,然后用唯一出现字符去插

【在 y***n 的大作中提到】
: Given a string of lowercase characters, reorder them such that the same
: characters are at least distance d from each other.
: Input: { a, b, b }, distance = 2
: Output: { b, a, b }

y***n
发帖数: 1594
4
一个就够了,leetCode 看到了,觉得还是有点绕,不是很常规的思维。
l*********8
发帖数: 4642
5
第一次见到这个题目。
首先,把字母按照frequency降序排序.
S = {{2,b}, {1,a}}
然后,把字母依次放到以下位置:0, d, 2*d, ..., 1, 1+d, 1+2d, ....

【在 y***n 的大作中提到】
: Given a string of lowercase characters, reorder them such that the same
: characters are at least distance d from each other.
: Input: { a, b, b }, distance = 2
: Output: { b, a, b }

d*****y
发帖数: 1365
6
I think if you use a queue of length d, you can get an O(dn) algorithm. You
probably can improve it to O(nlogd) though.

【在 y***n 的大作中提到】
: Given a string of lowercase characters, reorder them such that the same
: characters are at least distance d from each other.
: Input: { a, b, b }, distance = 2
: Output: { b, a, b }

m*****n
发帖数: 204
7

The following is an O(nlgn) solution for string of size n.
It maintains a heap for eligible chars, preferring longer streams.
Ineligible chars are maintained in a candidate queue, and added back
to the working queue when their position constraint is removed.
public class MinDistance {
public String rearrange(String source, int distance) {
// TODO Check params.

// WorkingQueue of eligible streams. Longest stream at head,
// but BackupStream at tail.
PriorityQueue workingQueue = buildStreams(source.toLowerCase
());
// CandidateQueue of ineligible streams. Sorted by target position.
List candidateQueue = new LinkedList<>();

StringBuilder result = new StringBuilder();

while (result.length() < source.length()) {
int currPos = result.length();

// Resupply the working queue:
if (!candidateQueue.isEmpty()) {
CharStream candidate = candidateQueue.get(0);
if (candidate.getTargetPos() <= currPos) {
workingQueue.add(candidateQueue.remove(0));
}
}

if (workingQueue.isEmpty()) {
// No solution. Return null or throw
}

CharStream stream = workingQueue.poll();
result.append(stream.next());
if (stream.hasNext()) {
stream.setTargetPos(currPos + distance);
}
candidateQueue.add(stream);
}
return result.toString();
}
private PriorityQueue buildStreams(String source) {
Map map = new HashMap<>();
for (int pos = 0; pos < source.length(); pos++) {
Character c = source.charAt(pos);
AtomicInteger count = map.get(c);
if (count == null) {
count = new AtomicInteger();
map.put(c, count);
}
count.incrementAndGet();
}
PriorityQueue streams =
new PriorityQueue<>(source.length(), new LengthComparator());
List backupList = new ArrayList<>();
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue().get() == 1) {
backupList.add(entry.getKey());
} else {
streams.add(
new SingleCharStream(entry.getKey(), entry.getValue().get()));
}
}
if (backupList.size() > 0) {
streams.add(new BackupStream(backupList.iterator()));
}
return streams;
}

// Comparator for the working queue. BackupStream is at the tail.
private static class LengthComparator implements Comparator {
@Override
public int compare(CharStream o1, CharStream o2) {
if (o1 instanceof BackupStream) {
return 1;
} else if (o2 instanceof BackupStream) {
return -1;
}
SingleCharStream sc1 = (SingleCharStream) o1;
SingleCharStream sc2 = (SingleCharStream) o2;
return -Integer.compare(sc1.remaining, sc2.remaining);
}
}
private static interface CharStream extends Iterator {
int getTargetPos();
void setTargetPos(int targetPos);
}

private static class BackupStream implements CharStream {
private final Iterator myIterator;

BackupStream(Iterator it) {
this.myIterator = it;
}
@Override
public boolean hasNext() {
return myIterator.hasNext();
}
@Override
public Character next() {
return myIterator.next();
}
@Override
public void remove() {
myIterator.remove();
}

@Override
public int getTargetPos() {
return 0;
}
@Override
public void setTargetPos(int targetPos) {
// Do nothing.
}
}
private static class SingleCharStream implements CharStream {
private final char character;
private int remaining;
private int targetPos;
SingleCharStream(char c, int count) {
this.character = c;
this.remaining = count;
}
@Override
public boolean hasNext() {
return remaining > 0;
}
@Override
public Character next() {
// check hasNext()
remaining--;
return character;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}

@Override
public int getTargetPos() {
return targetPos;
}
@Override
public void setTargetPos(int targetPos) {
this.targetPos = targetPos;
}
}
}

【在 y***n 的大作中提到】
: Given a string of lowercase characters, reorder them such that the same
: characters are at least distance d from each other.
: Input: { a, b, b }, distance = 2
: Output: { b, a, b }

l*********8
发帖数: 4642
8
面试不可能写这么长吧

【在 m*****n 的大作中提到】
:
: The following is an O(nlgn) solution for string of size n.
: It maintains a heap for eligible chars, preferring longer streams.
: Ineligible chars are maintained in a candidate queue, and added back
: to the working queue when their position constraint is removed.
: public class MinDistance {
: public String rearrange(String source, int distance) {
: // TODO Check params.
:
: // WorkingQueue of eligible streams. Longest stream at head,

m*****n
发帖数: 204
9
Start with pseudo code for helper methods and classes.
Fill in details if you have time.

【在 l*********8 的大作中提到】
: 面试不可能写这么长吧
r****s
发帖数: 1025
10
这个应该很简单吧。
首先sort:cccaaaabbbbddddd
然后按长度排序:dddddaaaabbbbccc
从中间拆成两个array:dddddaaa abbbbccc
两个array merge:dadbdbdbdbdacacac
这里面有一个结论,就是如果最长的字母序列长度大于n/2+2,那么不可能有答案。很
简单,要间隔开n/2+2个字母,你需要n/2个其他字母。
相关主题
报个A家OFFER,回馈版上Bloomberg phone interview 面经
share int2roman and roman2int java version求指点一道G家Iterator的题目
G的一道Onesite题请教F最近超爱的面试题任务安排的follow up怎么做
进入JobHunting版参与讨论
l********1
发帖数: 24
11
是不是这道题,感觉是leetcode原题吧
http://leetcode.com/2010/05/here-is-another-google-phone-interv
l*********8
发帖数: 4642
12
就是这个题
f******n
发帖数: 279
13
mark
f*****e
发帖数: 2992
14
还真是;
Let N = total # of chars,
ceil(N / d) must >= max # of a specific char. 大于等于一定有解,小于一定无解。
比如bbccceeee, d = 2, N = 9, ceil(N/d)=5>4, 5 buckets with d slots.
%d == 0 : e e e e c
%d == 1 : c c b b
Final -> ececebebc

【在 l*********8 的大作中提到】
: 第一次见到这个题目。
: 首先,把字母按照frequency降序排序.
: S = {{2,b}, {1,a}}
: 然后,把字母依次放到以下位置:0, d, 2*d, ..., 1, 1+d, 1+2d, ....

j**********3
发帖数: 3211
15
晚点上我的答案
m*****k
发帖数: 731
16
Input: { a, a, b, b,c,c,d,d }, distance = 3
Output: { a,b,c,d,a,b,c,d}
你的解法貌似不会给出这个答案吧?

【在 r****s 的大作中提到】
: 这个应该很简单吧。
: 首先sort:cccaaaabbbbddddd
: 然后按长度排序:dddddaaaabbbbccc
: 从中间拆成两个array:dddddaaa abbbbccc
: 两个array merge:dadbdbdbdbdacacac
: 这里面有一个结论,就是如果最长的字母序列长度大于n/2+2,那么不可能有答案。很
: 简单,要间隔开n/2+2个字母,你需要n/2个其他字母。

1 (共1页)
进入JobHunting版参与讨论
相关主题
java8 的Lambda 有没有人用讨论一个题目
几道微软面试题报个A家OFFER,回馈版上
一个答案看不明白谁解释一下share int2roman and roman2int java version
讨论:这个题怎么解G的一道Onesite题
[google面试]iterator访问Bloomberg phone interview 面经
微软onsite SDET 面经求指点一道G家Iterator的题目
上一道小题请教F最近超爱的面试题任务安排的follow up怎么做
问个java hashcode的题曼哈顿距离iterator随便写了一个 请大家帮挑毛病,謝謝
相关话题的讨论汇总
话题: charstream话题: override话题: character话题: public话题: int