注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 MeowMoew 于 2025-2-11 13:12 编辑
整理电脑翻到了半年前的面试,发上来求点米
SDE2岗位总共面了四轮,最后的结果是排序掉了,很遗憾没能抱上巨硬大腿-baidu 1point3acres
round1
Jump game 2, Greedy, 我用dfs写的,也给过了
followup:如何改进-空间换时间- // https://leetcode.com/problems/jump-game-ii/
- // written during interview, can't promise the correctness, just for draft reference
- var minJump int = math.MaxInt
- // Additionally: use map to store calcuated value -> o(n) time complexity
- var indexToMinJump = make(map[int]int). 1point 3 acres
- func jump(nums []int) int {. Χ
- // // special cases
- // // 1. len = 0 2. len>=1, nums[0] == 0
- // if len(nums) == 0 {
- // return 0
- // }
- search(nums, 0, 0)
- return minJump
- }. 1point3acres.com
- func search(nums []int, index, count int) {
- // index valid
- if index < 0 || index >= len(nums) {
- return-baidu 1point3acres
- }
- // end condition.--
- if index == len(nums)-1 {
- if count < minJump {
- minJump = count
- }. 1point 3 acres
- return
- }
- // jump. Χ
- times := nums[index]
- count++
- for i := times; i >= 1; i-- {. 1point3acres
- search(nums, index+i, count)
- }
- }
复制代码 round2 主要是聊项目,然后做了个简单的小题,问一共能想出多少种解法,最优解是什么- // https://leetcode.com/problems/sort-colors
- // written during interview, can't promise the correctness, just for draft reference. Χ
- . .и
- // sort.google и
- // [2,0,2,1,1,0] -> [0,0,1,1,2,2]
- // Method 1: classify through counting for each number, then appending
- func sortColors (nums []int){
- numToCount := make([]int, 3)
- for _, num := range nums {
- numToCount[num]++
- }
- . 1point 3 acres
- index := 0
- for i, v := range numToCount {
- for j:=0; j<v; j++ {
- nums[index] = i
- index++
- }
- }
- }
- // sort
- // [2,0,2,1,1,0] -> [0,0,1,1,2,2]
- // Method 2: two pointers, simulating quick sort, each time group one color, need to go through the process two times
. 1point3acres.com - func sortColors (nums []int){
- // input validation ?. .и
- if len(nums) < 2 {.
- return
- }
- // sort
- left, right := 0, 1
- pivot := nums[0]
- . ----
- //end condition: right - > end . Χ
- for right < len(nums) {
- for nums[left]==pivot {
- left++
- }
-
- for nums[right] != pivot {
- right++
- }. 1point3acres.com
- . ----
- // switch. 1point 3 acres
- nums[left], nums[right] = nums[right], nums[left]. From 1point 3acres bbs
- }
- // iteration times control? 3->2
- sortColors(nums[left:])
- }.
- // Method 3: iterate with two pointers, left:=0, right := len(nums), then iterate, if nums[i] == 0, switch with left, if nunms[i] == 2, switch with right
复制代码
一周后收到thank u |