C_and_CPP 板


LINE

说是心得嘛... 当灌水也行XD 这是 Codility 的 demo sample, 提供一个满分解答。 (Codility 是程设训练平台,某些公司会用这个平台来面试) ==================== This is a demo task. Write a function: int solution(vector<int> &A); that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [-1, -3], the function should return 1. Write an efficient algorithm for the following assumptions: N is an integer within the range [1..100,000]; each element of array A is an integer within the range [-1,000,000..1,000,000] 给分的重点在於运算速度要快、对於上下限的处理要明确、 Compile 连 warning 都不能。时间复杂度在 O(N^2) 以上的都不及格。 限时30分钟。 ==================== 范例码在下面 ==================== int solution(vector<int> &A) { // write your code in C++14 (g++ 6.2.0) if (A.size() > 100000) { return 100001; } if (A.empty()) { return 1; } bool ba[A.size()] = {false}; for (unsigned int ix = 0; ix < A.size(); ix++) { if (A[ix] < 1) { continue; } else { if ((unsigned int)A[ix] > A.size()) { continue; } } ba[A[ix]-1] = true; } for (unsigned int ix = 0; ix < A.size(); ix++) { if (ba[ix] == false) { return ix+1; } } return A.size() + 1; } --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 27.147.15.203
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1535983690.A.ED1.html
1F:推 fatrabitree: 第一个if题目有提吗 09/03 22:20
2F:推 a21802: 如果只是要最小正整数 从1开始跑发现不在A里直接跳出不是 09/03 22:23
3F:→ a21802: 更好吗 09/03 22:23
4F:→ a21802: 还是我漏了什麽 09/03 22:24
5F:→ a21802: 我的想法会造成双层回圈 请无视 09/03 22:32
6F:推 alan23273850: 总觉得开阵列解的方法有点弱,有没有不用阵列的方 09/04 09:54
7F:→ alan23273850: 法? 09/04 09:54
如果对 STL 熟悉的话,可以这样写,不用开 array,也是满分解法 int solution(vector<int> &A) { int result = 1; bool found = false; sort(A.begin(), A.end()); do { found = binary_search(A.begin(), A.end(), result); if (found) { result++; } } while (found); return result; } 不过我个人不太喜欢这解法。
8F:→ sarafciel: 空间复杂度O(1)的话就快排後扫一遍吧 会慢一点就是 09/04 10:41
9F:→ adrianshum: 严格来说这个的空间复杂度也是O(1) 吧,只是 constant 09/04 12:25
10F:→ adrianshum: term 很大而已 09/04 12:25
11F:→ adrianshum: 对不起,请无视,我看错code 每次都allocate bool[100 09/04 12:29
12F:→ adrianshum: 000] 了 09/04 12:29
※ 编辑: babelism (61.218.44.76), 09/04/2018 17:00:48
13F:推 oToToT: 不是该用size_t吗OAO 09/04 18:52
14F:推 oToToT: 顺便问一下原来现在可以直接宣告bool A[(non constant)]? 09/04 18:55
15F:推 alan23273850: 原po後来回的那篇STL解法时间应该是O(nlgn)吧 09/05 06:50
16F:→ sarafciel: C99的话VLA已经是标准了 C++目前还是不行的样子 09/05 10:25
17F:推 alan23273850: cutekid 大大刚刚有水球我说排序过後不用那个 do-wh 09/05 15:25
18F:→ alan23273850: ile 回圈,可以 linear time 扫过去,请各位检查看 09/05 15:26
19F:→ alan23273850: 看是不是如此,不过还是 O(nlgn) 就是 09/05 15:26
20F:→ sarafciel: 是的 直接回圈扫一遍就好了 原PO这样写其实二分搜寻没 09/05 19:34
21F:→ sarafciel: 有发挥到 时间复杂度的瓶颈在sort上 所以依然是O(nlgn) 09/05 19:37
22F:推 alvinpon: http://codepad.org/nIN4qy0c 09/09 10:37







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP