2Sigma OA prepare: Longest Chain

DP use HashMap:

根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新

 1 package twoSigma;
 2
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 import java.util.HashMap;
 6 import java.lang.StringBuilder;
 7
 8 public class LongestChain {
 9     public int findLongestChain(String[] words) {
10         if (words==null || words.length==0) return 0;
11         int longestChain = 0;
12         Arrays.sort(words, new Comparator<String>() {
13             public int compare(String str1, String str2) {
14                 return str1.length()-str2.length();
15             }
16         });
17         HashMap<String, Integer> map = new HashMap<String, Integer>();
18         for (String word : words) {
19             if (map.containsKey(word)) continue;
20             map.put(word, 1);
21             for (int i=0; i<word.length(); i++) {
22                 StringBuilder sb = new StringBuilder(word);
23                 sb.deleteCharAt(i);
24                 String after = sb.toString();
25                 if (map.containsKey(after) && map.get(after)+1 > map.get(word)) {
26                     map.put(word, map.get(after)+1);
27                 }
28             }
29             if (map.get(word) > longestChain) longestChain = map.get(word);
30         }
31         return longestChain;
32     }
33
34     /**
35      * @param args
36      */
37     public static void main(String[] args) {
38         // TODO Auto-generated method stub
39         LongestChain sol = new LongestChain();
40         //String[] words = new String[]{"6", "a", "b", "ba", "bca", "bda", "bdca"};
41         //String[] words = new String[]{"a", "a", "bc", "exf", "abc"};
42         String[] words = new String[]{"bc", "abc"};
43         int longestChain = sol.findLongestChain(words);
44         System.out.println(longestChain);
45     }
46
47 }
时间: 2024-12-28 06:42:18

2Sigma OA prepare: Longest Chain的相关文章

2Sigma OA prepare: Friends Circle

DFS & BFS: 关键在于构造graph 1 package twoSigma; 2 3 import java.util.ArrayList; 4 import java.util.HashSet; 5 import java.util.LinkedList; 6 import java.util.Queue; 7 8 public class FriendCircle1 { 9 ArrayList<ArrayList<Integer>> graph; 10 publ

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 这是一个简单版本of LC 424 Longest Repeating Character Replacement 又是Window, 又是Two Pointers Window还是采用每次都try to update left使得window valid, 每次都检查最大window 1 package

Twitter OA prepare: K-complementary pair

A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q) is called K-complementary in array A if 0 ≤ P, Q < N and A[P] + A[Q] = K. For example, consider array A such that: A[0] = 1 A[1] = 8 A[2]= -3 A[3] = 0 A[4]

Twitter OA prepare: Equilibrium index of an array

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A: A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0 3 is an equil

Twitter OA prepare: Flipping a bit

You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one move on the array: choose any two integers [L, R], and flip all the elements between (and including) the L-th and R-th bits. L and R represent the lef

UvaLive 6667 Longest Chain (分治求三元组LIS&amp;树状数组)

题目链接: here 题意: 和hdu4742类似.区别就是一部分三元组是直接给出的.还有一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by,az>bz. 思路: 思路也和hdu4742here类似.只是有几个比较棘手的问题.现在变成严格大于了.对于y还是很好办的.我们在排序y的时候可以使的标号大的排在前面这样就可以防止y和它一样的更新它了.感觉比较麻烦的是x一样怎么办.这个真没想出什么好办法.就只有x和mid+1的x不一样的建一个树状数

UvaLive 6667 Longest Chain (分治求三维LIS)

题目大意: 题目给出了定义的小于号,然后求出一个LIS... 思路分析: 这道题目的是一个严格递增的,和 Hdu 4742 类似.只不过Hdu的这道题是一个不递减的序列. 简单说一下Hdu 4742的做法. 首先我们可以想到的是一维的LIS,那么简单就是n. 然后二维的LIS,就是先排序一维,然后用求第二维的LIS. 现在问题扩展到三维.依然排序一维. 假设我们排序的是z. 然后记下排序后的id.现在已知,id小的z就小. 然后开始分治,类似线段树的递归.对于一个区间,我们将这个区间的所有元素取

Twitter OA prepare: even sum pairs

Write a function: class Solution { public int solution(int[] A); } that, given an array A consisting of N integers, returns the number of pairs (P, Q) such that 0 ≤ P < Q < N and (A[P] + A[Q]) is even. The function should return −1 if the number of

Twitter OA prepare: Anagram is A Palindrome

A string is a palindrome if it has exactly the same sequence of characters when traversed left-to-right as right-to-left. For example, the following strings are palindromes: "kayak" "codilitytilidoc" "neveroddoreven" A string