2Sigma OA prepare: Longest Chain

DP use HashMap:
根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新
package twoSigma; import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.lang.StringBuilder; public class LongestChain {
public int findLongestChain(String[] words) {
if (words==null || words.length==0) return 0;
int longestChain = 0;
Arrays.sort(words, new Comparator<String>() {
public int compare(String str1, String str2) {
return str1.length()-str2.length();
}
});
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String word : words) {
if (map.containsKey(word)) continue;
map.put(word, 1);
for (int i=0; i<word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(i);
String after = sb.toString();
if (map.containsKey(after) && map.get(after)+1 > map.get(word)) {
map.put(word, map.get(after)+1);
}
}
if (map.get(word) > longestChain) longestChain = map.get(word);
}
return longestChain;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LongestChain sol = new LongestChain();
//String[] words = new String[]{"6", "a", "b", "ba", "bca", "bda", "bdca"};
//String[] words = new String[]{"a", "a", "bc", "exf", "abc"};
String[] words = new String[]{"bc", "abc"};
int longestChain = sol.findLongestChain(words);
System.out.println(longestChain);
} }
2Sigma OA prepare: Longest Chain的更多相关文章
- 2Sigma OA prepare: Friends Circle
DFS & BFS: 关键在于构造graph package twoSigma; import java.util.ArrayList; import java.util.HashSet; i ...
- G面经Prepare: Longest All One Substring
give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping 这是一 ...
- Twitter OA prepare: Two Operations
准备T家OA,网上看的面经 最直接的方法,从target降到1,如果是奇数就减一,偶数就除2 public static void main(String[] args) { int a = shor ...
- UvaLive 6667 Longest Chain (分治求三元组LIS&树状数组)
题目链接: here 题意: 和hdu4742类似.差别就是一部分三元组是直接给出的.另一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by ...
- Twitter OA prepare: even sum pairs
思路:无非就是扫描一遍记录奇数和偶数各自的个数,比如为M和N,然后就是奇数里面选两个.偶数里面选两个,答案就是M(M-1)/2 + N(N-1)/2
- Twitter OA prepare: K-complementary pair
2sum的夹逼算法,需要sort一下.本身不难,但是tricky的地方在于允许同一个数组元素自己跟自己组成一个pair,比如上例中的[5, 5].而且数组本身就允许值相等的元素存在,在计算pair时, ...
- Twitter OA prepare: Anagram is A Palindrome
Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...
- Twitter OA prepare: Visit element of the array
分析:就是建立一个boolean array来记录array里面每个元素的访问情况,遇到访问过的元素就停止visiting,返回未访问的结点个数 public int visiting(int[] A ...
- Twitter OA prepare: Rational Sum
In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...
随机推荐
- asp.net mvc4 HTTP Error 403.14
asp.net mvc4项目部署到II&上时,出现HTTP Error 403.14 - Forbidden - The Web server is configured to not lis ...
- xargs
xargs的作用一般等同于大多数Unix shell中的反引号,但更加灵活易用,并可以正确处理输入中有空格等特殊字符的情况.对于经常产生大量输出的命令如find.locate和grep来说非常有用.
- AlertDialog使用时遇到问题
1.其内de子控件高宽度为0? AlertDialog.Builder builder = new AlertDialog.Builder(StoryActivity.this); View view ...
- *HDU3496 背包DP
Watch The Movie Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- 我爱记单词(iWords)之NABC by张恿
1) N (Need 需求) 我们组的项目是做一个英语学习助手,前人的基础上开发新的功能,修改原来功能的bug等等.之前的版本只提供了主动“看单词”和单词测试的功能,我们希望增加背单词的功能,以便更好 ...
- .net 设置导航的当前状态
1.静态地址共用母版页时,加当前页的状态(使用加参数的方法实现): a: main.Master为链接设参数 MenuId <li> <a <%=MenuId==?" ...
- Mysql权限
连接Oracle/Mysql数据库的配置 1.Oracle <context:property-placeholder location="jdbc.properties"/ ...
- iOS frame从导航栏下面开始
iOS7.0 之后,页面布局默认延伸到了手机界面的边缘;导航栏背景成透明颜色. 解决fram从导航栏下面开始方法: 第一种: 将导航栏改成不透明即可 if( ([[[UIDevice currentD ...
- Android课程---Activity中保存和恢复用户状态
onSaveInstanceState 保存 在暂停之后和保存之前调用 onRestoreInstanceState 恢复 再启动之后和显示之前调用 package com.example.chens ...
- IOS第15天(2,事件处理hitTest练习)
***hitTest 获取最合适的点 @implementation HMGreenView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEv ...