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. smarty3.0中文手册文档API及使用指南

    1.安装Smarty3.0一.什么是smarty?smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分离,使用的程序员改变 ...

  2. Nginx_Lua

    http://www.ttlsa.com/nginx/nginx-lua/ 1.1. 介绍ngx_lua – 把lua语言嵌入nginx中,使其支持lua来快速开发基于nginx下的业务逻辑该模块不在 ...

  3. mysqli_multi_query($link, $wsql)

    if (mysqli_multi_query($link, $wsql)) { do { if ($result = mysqli_store_result($link)) { mysqli_free ...

  4. android 定位的四种方式

    [原文]  开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...

  5. comms.nottingham.ac.uk/learningtechnology

    http://comms.nottingham.ac.uk/learningtechnology/

  6. php curl多线程抓取网页

    PHP 利用 Curl Functions 可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,受限于php语言本身不支持多线程,所以开发爬虫程序效率并不高,这时候往往需 要借助Cu ...

  7. C++ 字符串操作常见函数

    //字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...

  8. TEST指令

    In the x86 assembly language, the TEST instruction performs a bitwise AND on two operands. The flags ...

  9. UITableView & UITableViewController

    内容概要: 本文先讲解了UITableView概述,然后主要从应用方面讲解了UITableViewController(包括add.delete.move单元cell的操作,以及UITableView ...

  10. 【C++】字符串的大小写转换

    #include<string> #include<algorithm> #include<iostream> using namespace std; int m ...