Google 面试题 #3:文本换行行数 & 双栏最优列宽 – 一亩三分地 – 谷歌面经 – 面试辅助 – 代面试

3次阅读
没有评论

Given an English-language text and the width of a page, find how many lines the text would take on that page.

Given a text that you’d like to place into a two-column table on a fixed-width page, find the best column width that minimizes the overall table height.

题 1(行数统计):给定英文文本与页面宽度,按“单词换行(不拆词)”规则计算需要多少行。做法:贪心装填单词,放不下就换行。时间 O(n)。

题 2(两栏最优列宽):固定总页宽与双栏布局,选择每栏列宽使表格总高度最小。做法:对给定列宽 w 计算单词换行得到总行数 L(w),双栏高度约为 ceil(L(w)/2)H(w)w 非增,因此 取可行范围内最大列宽 (或在离散像素宽度上二分 / 枚举)即可最小化高度;若有 栏距 / 边距 限制,要满足 2*w + gutter ≤ pageWidth

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 OpenAI、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0