G面经prepare: X-Straight
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的更多相关文章
- 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 ...
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- 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 ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- 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 ...
随机推荐
- P1443 马的遍历
同样是一个bfs水题... #include <bits/stdc++.h> using namespace std; typedef pair<int, int> St; S ...
- 告诉你Hadoop是什么
Hadoop是什么?Hadoop是一个开发和运行处理大规模数据的软件平台,是Appach的一个用java语言实现开源软件框架,实现在大量计算机组成的集群中对海量数据进行分布式计算. Hadoop框架中 ...
- composer 272解决
composer global require "fxp/composer-asset-plugin:~1.0.3" ...
- C# DateTime类型和时间戳 互相转换
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...
- Charles辅助调试接口
http://blog.sina.com.cn/s/blog_6ae8b50d0102w7tw.html
- 转: PE rva to raw 虚拟偏移地址和文件物理偏移地址
+---------+---------+---------+---------+---------+---------+| 段名称 虚拟地址 虚拟大小 物理地址 物理大小 标志 |+-------- ...
- 集合类(Objective-C & Swift)
内容提要: 本文前两部分讲了Cocoa的集合类和Swift的集合类,其中Cocoa提供的集合类包括NSArray.NSMutableArray.NSDictionary.NSMutableDictio ...
- ViewModel在MVC3中的应用:实现多字段表格的部分更新
假设我们有这样一张用户表: public class F_users { [Key] [Display(Name="用户名:")] [Required(ErrorMessage=& ...
- Average Cost (AVCO) Method
http://accountingexplained.com/financial/inventories/avco-method Average Cost (AVCO) Method Aver ...
- LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...