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 ...
随机推荐
- spring容器IOC创建对象<二>
问题?spring是如何创建对象的?什么时候创建对象?有几种创建方式?测试对象是单例的还是多例的 ?对象的初始化和销毁? 下面的四大模块IOC的内容了!需要深刻理解 SpringIOC定义:把对象的创 ...
- css层叠选择
首先声明一下CSS三大特性——继承.优先级和层叠.继承即子类元素继承父类的样式,比如font-size,font-weight等f开头的css样式以及text-align,text-indent等t开 ...
- Bluetooth HCI介绍
目录 1. HCI功能 2. HCI Packet 1. HCI Command 2. HCI Event 3. HCI Data 3. HCI传输层 HCI, 主机控制接口(Host Control ...
- 【转】java正则表达式
在Sun的Java JDK 1.40版本中,Java自带了支持正则表达式的包,本文就抛砖引玉地介绍了如何使用java.util.regex包. 可粗略估计一下,除了偶尔用Linux的外,其他Linu ...
- python对象数据的读写权限
面向对象的编程语言在写大型程序的的时候,往往比面向过程的语言用起来更方便,安全.其中原因之一在于:类机制. 类,对众多的数据进行分类,封装,让一个数据对象成为一个完整的个体,贴近现实生活,高度抽象化. ...
- key_value 类型配置文件的解析
#include <iostream> #include <string> #include <stdint.h> #include <map> #in ...
- CSS控制"标题前增加小图标或编号"
---题目前加图片--- p:before { content:url(xxx/xx.png); }//所有p的最前都有一个图标 p.a:after { content:url(xxx/xx.png) ...
- Magento去掉价格的小数点
magento的默认情况,价格后面是有小数点的,我们来看下如何正确的来去掉小数点. 1.复制如下路径的文件 app/code/core/Mage/Directory/Model/Currency.ph ...
- JavaScript实现在页面上的文本框中输入小写字母自动变为大写字母
<script language="javascript" type="text/javascript"> $(function () { $(&q ...
- oracle dump event
一.Memory Dumps 1).Global Area ALTER SESSION SET EVENTS 'immediate trace name global_area level n'; 1 ...