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. 【转载】wireshark抓包

    两种过滤器 捕捉过滤器:用于决定将什么样的信息记录在捕捉结果中.需要在开始捕捉前设置显示过滤器:在捕捉结果中进行详细查找.他们可以在得到捕捉结果后随意修改 1.捕捉过滤器   语法实例: tcp ds ...

  2. NSQ:分布式的实时消息平台

    NSQ是一个基于Go语言的分布式实时消息平台,它基于MIT开源协议发布,代码托管在GitHub,其当前最新版本是0.3.1版.NSQ可用于大规模系统中的实时消息服务,并且每天能够处理数亿级别的消息,其 ...

  3. LAMP环境搭建教程(原创)

    学习PHP脚本编程语言之前,必须先搭建并熟悉开发环境,开发环境有很多种,例如LAMP.WAMP.MAMP等.这里我介绍一下LAMP环境的搭建,即Linux.Apache.MySQL.PHP环境. 一. ...

  4. push to deploy

    1.server端 建立裸仓 $ mkdir ~/repo.git $ cd ~/repo.git $ git init --bare 脚本 $ vim ~/repo.git/hooks/post-r ...

  5. Introduction to Structured Data

    https://developers.google.com/search/docs/guides/intro-structured-data Structured data refers to kin ...

  6. nginx搭建http和rtmp协议的流媒体服务器

    nginx搭建http和rtmp协议的流媒体服务器 时间:2013-09-23 23:52来源:佚名 作者:本站 举报 点击:232次 实验目的:让Nginx支持flv和mp4格式文件,同时支持Rtm ...

  7. php 通过exec 创建git分支失败

    今天给我们自己的发布系统增加一个新建分支的功能,操作比较简单,但是使用php执行shell命令的时候总是无法push分支到远程,但是登陆服务器执行却是可以的 新建分支命令如下 git fetch -- ...

  8. 自动adsl拨号上网

    @echo offmode con cols=35 lines=6 & color 5Btitle 开机连接宽带--设置工具 QQ1009693258echo 请稍候...VER|FIND & ...

  9. Barricade---hdu5889(最短路+网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5889 题意:有n个点m条边,每条边的长度相同,我们可以默认为1,构成一个无向图:现在起点为1,终点为n ...

  10. Mac废纸篓 不能完全清空的有效解决方法

    摘自:http://www.neonan.com/articles/5582 对于苹果用户来说, 可能都会遇到过这样的情况(尽管这种情况是不常出现的!), Mac OS X系统突然出现一些顽固的文件家 ...