12
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

Uber OA 感恩吐血大放送 求米

 
🔗
剑心 2021-1-30 02:18:47 | 只看该作者
全局:
第四题晚上做出来了...做到要吐.
回复

使用道具 举报

🔗
longjia 2021-1-30 09:22:06 | 只看该作者
全局:
第三题 看了10分钟才明白问什么
回复

使用道具 举报

🔗
HenryXPHB 2021-4-4 04:21:04 | 只看该作者
全局:
workworkhard 发表于 2020-12-14 16:41
想问楼主第一题怎么做的

1-D 小dp 当然可以用一个variable解决 dp[num] = dp[num-1] + count(num)
回复

使用道具 举报

🔗
yliaoshuaige 2022-11-9 10:51:46 | 只看该作者
全局:
  1. def sol(prices, algo, k):
  2.     ret = rev = 0
  3.     for i in range(len(prices)):
  4.         r = prices[i] if algo[i] == 1 else -prices[i]
  5.         rev += r

  6.     for start in range(len(prices)-k+1):
  7.         tmp = rev
  8.         for j in range(start, start+k):
  9.             if not algo[j]:
  10.                 tmp += 2 * prices[j]
  11.         ret = max(ret, tmp)
  12.     return ret
复制代码
第三题我这样写然后TLE 只过了13/20。有什么更好的解法吗
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-VX5KM  2023-2-2 11:53:40
第三題sliding window 可以解吧?
麻煩不吝指正

  1. func max(a, b int) int {
  2.         if a > b {
  3.                 return a
  4.         }
  5.         return b
  6. }
  7. func buySell(prices []int, algo []int, k int) int {
  8.         //sliding window
  9.         diff := 0
  10.         total := 0
  11.         for i := range prices {
  12.                 if algo[i] == 0 {
  13.                         total -= prices[i]
  14.                 } else {
  15.                         total += prices[i]
  16.                 }
  17.         }
  18.         //init
  19.         for i := 0; i < k; i++ {
  20.                 if algo[i] == 0 {
  21.                         diff += 2 * prices[i]
  22.                 }
  23.         }
  24.         max_diff := diff
  25.         for i := k; i < len(prices); i++ {
  26.                 if algo[i] == 0 {
  27.                         diff += 2 * prices[i]
  28.                 }
  29.                 if algo[i-k] == 0 {
  30.                         diff -= 2 * prices[i-k]
  31.                 }
  32.                 max_diff = max(max_diff, diff)
  33.         }
  34.         return total + max_diff
  35. }

  36. func main() {
  37.         prices, algo, k := []int{2, 4, 1, 5, 2, 6, 7}, []int{0, 1, 0, 0, 1, 0, 0}, 4
  38.         res := buySell(prices, algo, k) // 21
  39. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表