Define “X-Straight” as X cards with consecutive numbers (X >= 3). Determine if the deck can be fully divided into sets of “X-Straight”.
Example: 1, 2, 3, 4, 4, 5, 6 -> True

Backtracking:

 package Straight;
import java.util.*; public class Solution2 {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
public boolean determine(int[] arr) {
for (int elem : arr) {
if (!map.containsKey(elem)) {
map.put(elem, 1);
}
else map.put(elem, map.get(elem)+1);
}
return helper(arr, 0);
} public boolean helper(int[] arr, int pos) {
if (pos == arr.length) return true;
int cur = arr[pos];
for (int k=3; k<map.keySet().size(); k++) {
HashMap<Integer, Integer> copy = new HashMap<Integer, Integer>(map);
for (int i=cur; i<cur+k; i++) {
if (!map.containsKey(i)) return false;
if (map.get(i) == 0) return false;
map.put(i, map.get(i)-1);
}
while (pos<arr.length && map.get(arr[pos]) == 0) pos++;
if (helper(arr, pos))
return true;
map = copy;
}
return false; } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution2 sol = new Solution2();
boolean res = sol.determine(new int[]{1,2,3,2,3,4,8,9,10,6});
if (res) System.out.println("true");
else System.out.println("false");
} }

G面经prepare: X-Straight的更多相关文章

  1. G面经prepare: Straight Partition of A Deck of Cards

    Define “Straight” as 5 cards with consecutive numbers. Determine if the deck can be fully divided in ...

  2. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  3. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  4. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  5. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  6. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  7. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  8. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

  9. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

随机推荐

  1. w-BIG TABLE-view+where-small table

    w-BIG TABLE DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_month; DELIMITER /w/ CREATE ...

  2. P1351 联合权值

    #include <bits/stdc++.h> using namespace std; const int maxn = 200005; vector<int> son[m ...

  3. C#打开一个文本文件并读写

    OpenFileDialog OFD = new OpenFileDialog(); OFD.Title = "打开第一个文本文件"; OFD.FileName = "* ...

  4. 【Demo】HTML5 拍照上传

    本文主要讲解 手机浏览器 如何拍照 为什么会有这个需求 最近做一个项目要用到拍照然后上传照片,但是网页拍照一般都是用Flash做的,而我们主要是H5页面,如果在微信里面有权限就可以通过JSSDK调起摄 ...

  5. 设置MyEclipse开发项目时使用的JDK

    安装好MyEclipse之后,在MyEclipse中开发项目时,默认使用的是MyEclipse是自带的JDK,如下图所示: 如果我们需要使用自己安装好的JDK,那么就需要在MyEclipse中重新设置 ...

  6. 我的第一个chrome扩展(3)——继续读样例

    1.操作用户正在浏览的界面 http://www.ituring.com.cn/article/60212 问题:1.google未定义ID,用name为何无法找到? 2.如何让整个按钮一起动?原函数 ...

  7. C#中String 和string 的区别

    C#中同时存在String与string MSDN中对string的说明: string is an alias for String in the .NET Framework.string是Str ...

  8. .NET对象与Windows句柄(二):句柄分类和.NET句柄泄露的例子

    上一篇文章介绍了句柄的基本概念,也描述了C#中创建文件句柄的过程.我们已经知道句柄代表Windows内部对象,文件对象就是其中一种,但显然系统中还有更多其它类型的对象.本文将简单介绍Windows对象 ...

  9. (leetcode) countandsay

    class Solution { public: string calcuate(string s) { string result; char pre = s[0]; int cnt = 1; fo ...

  10. linux boot logo rotate

    开机logo旋转方法. 参考链接 https://www.kernel.org/doc/Documentation/fb/fbcon.txt https://community.nxp.com/thr ...