Interview Questions: Analysis of Algorithms

GPT-4o 生成保研面试英文自我介绍:3个提示词模板与5个润色要点 本文详细介绍了如何利用GPT-4o生成保研面试英文自我介绍,提供3个精准提示词模板(30秒精简版、1分钟标准版、2分钟完整版)和5个关键润色要点,帮助申请者突出个人亮点、优化表达结构,提升面试表现。 阅读详情

Analysis of Algorithms

3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to n 2 n^2 n2 in the worst case. You may assume that you can sort the n integers in time proportional to n 2 n^2 n2 or better.

利用two pointer的思想进行处理,复杂度为 O ( N 2 ) O(N^2) O(N2)

public int threeSum(int[] a) {
    int count = 0;
    // 需要先进行排序处理
    Arrays.sort(a);
    // 循环中保证a[i]<a[j]<a[k]
    for (int i = 0; i < a.length - 2; i++) {
        int j = i + 1;
        int k = a.length - 1;
        // j和k从两头向中间移动
        while (j < k) {
            int sum = a[i] + a[j] + a[k];
            if (sum == 0) {
                count++;
                // 处理三元组元素相同的情况
                if (a[j] == a[j + 1]) {
                    j++;
                } else {
                    k--;
                }
            } else if (sum < 0) {
                j++;
            } else {
                k--;
            }
        }
    }
    return count;
}

Search in a bitonic array. An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of nn distinct integer values, determines whether a given integer is in the array.

  • Standard version: Use ∼3lgn compares in the worst case.
  • Signing bonus: Use ∼2lgn compares in the worst case (and prove that no algorithm can guarantee to perform fewer than ∼2lgn compares in the worst case).

~3lgn: 1lgn用于二分查找最大值,划分左右区间;1lgn用于二分查找左区间;1lgn用于二分查找右区间

~2lgn: 节省1lgn关键在于不找最大值,直接依据不同情况来划分左右区间并进行查找。

  1. a[mid]>a[mid-1],中间元素在左边递增区间时:
    示意图
    如果key>a[mid],说明key只可能在mid右边,那么问题就变为在一个更小的抛物线上找key,递归进行处理即可;如果key<a[mid],那么key既可能在mid左边,也可能在mid右边,若在左边只要对左半边进行二分查找,若在右边也可以直接对右半部分的抛物线进行二分查找,原因在于key<a[mid],那么key只可能在横向红线的下方出现,即使key在右半部分也只会在右半部分的递减区域出现。

  2. a[mid]>a[mid+1],中间元素在右边递减区域时:与上述情况正好相反。

public class Bitonic {
    // 处理抛物线部分
    public boolean searchBitonic(int[] a, int left, int right, int key) {
        int mid = (left + right) / 2;
        // 直接找到key返回
        if (key == a[mid]) {
            return true;
        }
        if (a[mid] > a[mid - 1]) {		// mid在左侧递增部分
            if (key > a[mid]) {		// 递归处理右部分抛物线
                return searchBitonic(a, mid + 1, right, key);
            } else {    // 对左右两部分进行二分查找
                return searchLeft(a, left, mid - 1, key) || searchRight(a, mid + 1, right, key);
            }
        } else if (a[mid] > a[mid + 1]) {		// mid在右侧递减部分
            if (key > a[mid]) {		// 递归处理左部分抛物线
                return searchBitonic(a, left, mid - 1, key);
            } else {		// 对左右两部分进行二分查找
                return searchRight(a, mid + 1, right, key) || searchLeft(a, left, mid - 1, key);
            }
        }
    }

    // 递增部分二分查找
    private boolean searchLeft(int[] a, int left, int right, int key) {
        while (left <= right) {
            int mid = (left + right) / 2;
            if (a[mid] == key) {
                return true;
            }
            if (a[mid] < key) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return false;
    }

    // 递减部分二分查找
    private boolean searchRight(int[] a, int left, int right, int key) {
        while (left <= right) {
            int mid = (left + right) / 2;
            if (a[mid] == key) {
                return true;
            }
            if (a[mid] < key) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return false;
    }
}

Egg drop. Suppose that you have an nn-story building (with floors 1 through nn) and plenty of eggs. An egg breaks if it is dropped from floor TT or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of TT given the following limitations on the number of eggs and tosses:

  • Version 0: 1 egg, ≤T tosses.
  • Version 1: ∼1lgn eggs and ∼1lgn tosses.
  • Version 2: ∼lgT eggs and ∼2lgT tosses.
  • Version 3: 2 eggs and ∼ 2 n 2\sqrt{n} 2n ​ tosses.
  • Version 4: 2 eggs and ≤ c T c\sqrt{T} cT ​ tosses for some fixed constant c.

以下分析转载自 https://www.cnblogs.com/evasean/p/7208986.html

version 0: 拿着一个鸡蛋从1~n依次扔就可以,到floor T会碎,故复杂度为≤T。

version 1: 采用二分查找,首先从n/2层开始扔: if(鸡蛋碎) 从(n/2)/2层开始扔;else 从n/2+(n/2)/2层开始扔。二分方法需要lgn个鸡蛋尝试lgn次。

version 2: 依次从1, 2, 4, 8, 16, 32,… 2 k 2^k 2k开始扔,如果鸡蛋在 2 k 2^k 2k碎了,那么 2 k − 1 ≤ T ≤ 2 k 2^{k-1}≤T≤2^k 2k−1≤T≤2k,这时已经使用了 lgT 次步,接下来在 [ 2 k − 1 + 1 , 2 k ) [2^{k-1}+1,2^{k}) [2k−1+1,2k)区间进行version 1的二分查找方法,需要花费lgT步。这两种操作加起来总共花费2lgT步。

version 3: 将0~n层楼分成 [ 1 , n − 1 ] [1, \sqrt{n}-1] [1,n ​−1], [ n , 2 n − 1 ] [\sqrt{n}, 2\sqrt{n}-1] [n ​,2n ​−1], [ 2 n , 3 n − 1 ] [2\sqrt{n},3\sqrt{n}-1] [2n ​,3n ​−1]… [ k n , ( k + 1 ) n − 1 ] [k\sqrt{n}, (k+1)\sqrt{n}-1] [kn ​,(k+1)n ​−1]…个区间,用一个鸡蛋分布从1开始在各个区间的起始楼层扔,如果在 k n k\sqrt{n} kn ​层碎了,那就从 ( k − 1 ) n + 1 (k-1)\sqrt{n}+1 (k−1)n ​+1开始逐层扔。第一步区间选择用了 n \sqrt{n} n ​的复杂度,第二步区间内部扔鸡蛋用了 n \sqrt{n} n ​的复杂度,总共用了 2 n 2\sqrt{n} 2n ​。

version 4: 尝试从1, 4, 9, 16, 25,… ( k − 1 ) 2 (k-1)^2 (k−1)2, k 2 k^2 k2…楼层扔鸡蛋,加入鸡蛋在楼层 k 2 k^2 k2碎了,意味着 ( k − 1 ) 2 ≤ T ≤ k 2 (k-1)^2 ≤T≤ k^2 (k−1)2≤T≤k2,这一步尝试了 T \sqrt{T} T ​次 ( k = T ) (k=\sqrt{T}) (k=T ​)。接着从楼层 ( k − 1 ) 2 + 1 (k-1)^2+1 (k−1)2+1开始逐层扔,最多尝试至 k 2 − 1 k^2-1 k2−1结束,这一步需要尝试 k 2 − 1 − ( k − 1 ) 2 − 1 + 1 = 2 T − 2 k^2-1-(k-1)^2-1+1=2\sqrt{T}-2 k2−1−(k−1)2−1+1=2T ​−2次。总共用了 3 T − 2 3\sqrt{T}-2 3T ​−2次。


参考

Bitonic search - iteye_12150
Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题 - evasean

interview questions 非常有用的面试问题描述,详细的解答,手把手教你如何面试 立即下载

相关推荐

手把手教你用Vivado的STARTUPE3原语驱动S25FL256S Flash(附完整Verilog代码)

本文详细介绍了如何利用Xilinx UltraScale+系列FPGA的STARTUPE3原语驱动S25FL256S SPI Flash,包括原语配置、SPI协议实现、状态机设计及调试技巧。通过完整的Verilog代码示例和性能优化建议,帮助开发者高效完成Flash通信解决方案的开发与集成。

weixin_30660027的博客 460

面试时最经常被问到的问题(Frenquently asked interview questions)之Algorithms & Coding篇

 Algorithms & Coding Questions & Answers1、Write a function to print the Fibonacci numbers.-- int fib ( int n ){if (n == 0) return 1;else return fib(n-1) + fib(n-2);} -- int fibo (int n){in

我的专栏 2201

Direct-LiNGAM算法理解

现在是大数据的时代,数据即是财富。那么如何从数据中发现因果关系呢?这是我们促进我们更加高效利用数据的一个问题?本文介绍了一种方法,可以在符合条件的观察数据中恢复出数据背后的因果结构,帮助我们更好理解数据的产生机制。

小小程序猿的博客 4591

19 Essential Algorithm Interview Questions

What are Divide and Conquer algorithms? Describe how they work. Can you give any common examples of the types of problems where this approach might be used? Hide answer ...

weixin_30641999的博客 320

使用ST-LINK烧录stm32教程说明

使用ST-LINK烧录stm32

qq_34426854的博客 1万+

GEO数据挖掘实战:用R语言从GSE42872数据集中找到关键差异基因(附完整代码)

本文详细介绍了使用R语言从GSE42872数据集中挖掘关键差异基因的完整流程,包括数据下载、探针ID转换、差异表达分析和功能富集分析。通过实战代码演示,帮助生信研究者掌握GEO数据挖掘的核心技术,快速识别疾病相关基因并理解其生物学功能。

weixin_42625070的博客 365

算法工程师面试问题及资料超详细合集(多家公司算法岗面经/代码实战/网课/竞赛等)

这里是算法江湖,传授AI武林秘籍。

sh_0001的博客 8057

OpenCV函数用法之calibrateCamera

参考链接:opencv中标定函数calibrateCamera_ychl87的专栏-优快云博客 世界坐标系的三维点投影到成像坐标系中的二维点的投影公式如下: 其中(X,Y,Z)为世界坐标系中的三维点; (u,v)为成像面坐标系中的二维点; A为相机的内参数矩阵:(cx,cy)为主光轴点,一般为图像的中心;fx和fy为焦距; [R|t]为相机的外参数矩阵:R为旋转矩阵,t为位移矩阵; 上述公式的简单推理过程如下 考虑到镜头畸变 其中,k1,k2,k3,k4,k5和k6为径向畸变,p1和.

tiger&sheep的博客 3万+

TOP 30 INTERVIEW QUESTIONS & ANSWERS! (Job Interview PASS GUARANTEED!)

TOP 30 INTERVIEW QUESTIONS & ANSWERS! (Job Interview PASS GUARANTEED!)

weixin_45428910的博客 1879

IDEA集成selenium自动化测试(Java+Selenuim+testNG+Maven)

文章目录写在前面01 | 新建Maven项目02 | idea集成Selenium03 | 写在前面 因为软件测试的需求和博主比较经常使用Java,于是,想在Idea中集成selenuim,用Java编写测试代码,因此有了这篇文章来记录一下过程。 资源指路: java使用selenium实现模拟浏览器操作API大全 模拟登录 TestNG入门教程 感谢上面博客的大佬。 01 | 新建Maven项目 不使用骨架创建,直接点击左侧Maven,然后一路next即可。 02 | idea集成Sele

aLinxi的博客 8279

基于Python的Booking酒店数据爬虫实战:最新技术与完整代码解析

本文将详细介绍如何使用Python构建一个高效的Booking酒店数据爬虫,涵盖从基础爬取到高级反反爬技术的完整实现。我们将使用最新的Python爬虫技术栈,包括requests-html、Playwright、智能代理轮换等技术,并提供完整的可运行代码示例。文章将深入探讨爬虫的各个组成部分,包括数据提取、存储、反反爬策略以及性能优化等方面。关键词:Python爬虫、Booking数据采集、反反爬技术、Playwright、数据存储。

2201_76125261的博客 936

Android Interview Questions & Answers

Describe the APK format.The APK file is compressed the AndroidManifest.xml file, application code (.dex files), resource files, and other files. A project is compiled into a single .apk file.What is an action?A description of something that an Intent sende

蓝蓝的天 5197

“快速掌握Vivado FPGA开发流程“——详解Vivado开发流程及实例

快速掌握Vivado FPGA开发流程”——详解Vivado开发流程及实例FPGA技术在现代电子领域中应用越来越广泛,而Vivado作为业界最流行的FPGA设计工具之一,也受到越来越多人的关注和使用。那么如何快速掌握Vivado FPGA开发流程呢?本文将详细介绍Vivado开发流程,并通过实例来加深您的理解。首先,我们需要了解Vivado开发流程的主要步骤:创建工程、设计电路、约束电路、生成比特流。接下来,让我们一步步了解这些步骤。

2301_78484069的博客 731

[princeton/Algotithm I/week1](5)Interview Questions: Analysis of Algorithms

Interview Questions: Analysis of Algorithms 《算法4》这本书提供的TwoSumFast解法为NlogN,ThreeSumFast解法为N2logN,根据课后练习,要实现3Sum复杂度为N2,建议先把2Sum复杂度实现为N。同时教材提示用排好序的数组可以实现复杂度N。没有发现排好序的数组对复杂度降至N有太大帮助,于是在网上搜索了下大家的做法。网上的大...

gdtop的个人笔记 436

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

手把手部署Ollama:在本地免费运行Qwen3等主流大模型

本教程详细记录了我从零开始本地部署Ollama并运行Qwen3大模型的完整实操过程。涵盖环境准备、分步安装、模型下载、API调用及服务器部署方案,特别针对内存不足、下载缓慢等常见问题提供已验证的解决方案。你可在个人电脑上免费搭建隐私安全的本地AI环境,为后续智能体应用奠定模型基础。

我是慎独玩家,无人见处,自有尺度! 823

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Interview Questions: Union–Find (ungraded)

最近在学coursera上的算法课程。下面是课后习题的答案。 1. Social network connectivity.Given a social network containingnnmembers and a log file containingmmtimestamps at which times pairs of members formed friendships...

Geek_sun的博客 1435

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time

题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better. 分析:

evasean 1012

INTERVIEW QUESTIONS

四面技术,一面HR。 一面: 1.1、把你最近一个项目的流程图画出来并说明每个模块都是干嘛用的你负责哪些模块。 答:笔者画了流程图,讲解了每个模块的功能及他们之间的关系,以及每个模块用到的哪些新技术。(感觉答得一般,口才不好) 1.2、说一下ThreadLocal 笔者有段时间研究过这个ThreadLocal,但是并未真正用过,之前也没有复习,然后就直接说没用过了。 见:https:/...

qq_35689573的博客 387

Coursera Algorithms week1 Interview Questions: Egg drop

Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise.

evasean 826

top interview questions 4

总结下,从奇虎的两次测试来看,数组的变相二分查找,以及利用map与数组,结合dp,可以有效的解决很多问题。给定字符串,输出包含偶数个字符的子串的个数public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(

Marlborough 439

Interview questions

.NET Interview Questions :http://blogs.crsw.com/mark/archive/2004/03/16/255.aspxhttp://blogs.crsw.com/mark/articles/254.aspxhttp://blogs.crsw.com/mark/articles/252.aspxTechInterviews.com - interview q...

weixin_33671935的博客 192

Interview Questions

1.self-introduction2.whats your favourite course?3.最失败的事4.why are you the best person for the job?5.how do you balance work and study?6.whats your greatest strength?7.whats your greates

summer_grass的专栏 589

interview-question

Android面试经验总结

JasonZhang 742

关于招聘的英语(2)

You are very punctual.Have u brought your resume with u?Why did u quit?I will try u out.我要考你一下。Have u had much exp...

cixi4543的博客 542

8 Puzzle

8 Puzzle Board.java 照着API写即可。注意每次调用twin()都要返回相同的值,即twin()只能交换两个固定位置的数字。 import edu.princeton.cs.algs4.StdRandom; import java.util.ArrayList; import java.util.List; public class Board { private f...

Mapoos 900
上一篇: 软连接与硬连接
下一篇: Interview Questions: Union-Find
绿色小光头
博客等级 码龄10年 10粉丝 301原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值