📣 独立日限时特惠: VIP通行证立减$68
123
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

狗家新鲜五轮昂塞

 
🔗
johnnywsd 2020-12-31 02:37:15 | 只看该作者
全局:
做了下第二题,欢迎指正。求米
  1. # coding: utf-8

  2. """
  3. Given an array containing only 0 and 1s, find the best subarray to perform a single flip operation.
  4. The goal is that: after the flip, number of 1s should  be the maximum.
  5. Flip operation means that you will convert all 0 to 1, and all 1 to 0 in that array.
  6. You can skip flipping if you think it is not worth doing the flip.
  7. Return the maximum number of 1s after you do the flip.

  8. 队友还是用的prefix sum再找区间最大的差值,面试官说可以O(1)空间,作为homework。

  9. Follow-up: what if it is a 2D array?</div>
  10. """


  11. class Solution:
  12.     def max_flip(self, bits):
  13.         dp = [0] * len(bits)
  14.         for i, b in enumerate(bits):
  15.             val = 1 if b == '1' else -1
  16.             prev = dp[i - 1] if i > 0 else 0
  17.             dp[i] = min(prev + val, val)
  18.         orig_one_count = sum(1 for b in bits if b == '1')
  19.         res = orig_one_count + max(-min(dp), 0)
  20.         return res

  21.     def max_flip_follow_up(self, bits):
  22.         # O(1) space. rolling array. dp[i] only relies on the prev one.
  23.         dp = [0] * 2
  24.         min_dp = 0
  25.         for i, b in enumerate(bits):
  26.             val = 1 if b == '1' else -1
  27.             prev = dp[(i - 1) % 2] if i > 0 else 0
  28.             dp[i % 2] = min(prev + val, val)
  29.             min_dp = min(min_dp, dp[i % 2])
  30.         orig_one_count = sum(1 for b in bits if b == '1')
  31.         res = orig_one_count + max(-min_dp, 0)
  32.         return res
  33.    

  34. import unittest
  35. class Tests(unittest.TestCase):
  36.     def test1(self):
  37.         bits = '0001000'
  38.         sol = Solution()
  39.         actual = sol.max_flip(bits)
  40.         self.assertEqual(actual, 6)
  41.         actual2 = sol.max_flip_follow_up(bits)
  42.         self.assertEqual(actual2, 6)

  43.     def test2(self):
  44.         bits = '1001001'
  45.         sol = Solution()
  46.         actual = sol.max_flip(bits)
  47.         self.assertEqual(actual, 6)
  48.         actual2 = sol.max_flip_follow_up(bits)
  49.         self.assertEqual(actual2, 6)

  50.     def test3(self):
  51.         bits = '1111111'
  52.         sol = Solution()
  53.         actual = sol.max_flip(bits)
  54.         self.assertEqual(actual, 7)
  55.         actual2 = sol.max_flip_follow_up(bits)
  56.         self.assertEqual(actual2, 7)

  57.     def test4(self):
  58.         bits = '1010101'
  59.         sol = Solution()
  60.         actual = sol.max_flip(bits)
  61.         self.assertEqual(actual, 5)
  62.         actual2 = sol.max_flip_follow_up(bits)
  63.         self.assertEqual(actual2, 5)

  64. unittest.main(verbosity=2)
复制代码
回复

使用道具 举报

🔗
Lucy Cao 2021-1-5 08:24:20 | 只看该作者
全局:
sqsigc 发表于 2020-11-22 03:59
第二轮,考虑flip 0的收益是1,flip 1的收益是-1,问题转化为【求一个element是1或者-1的array的max subarr ...

同意!follow up 2d array请问有啥思路么?
回复

使用道具 举报

🔗
user2198M 2021-2-23 00:41:11 | 只看该作者
全局:
huntingzhu 发表于 2020-12-29 15:48
还是没理解第一题什么意思。如果给定
[1‘abc’][2‘54321’][1‘abcdef’][1‘re’]
max_size_over_aa ...

我猜应该是max_size_overall是对字符数而言,而不是message数量
回复

使用道具 举报

🔗
copper1820 2021-3-21 08:05:04 | 只看该作者
全局:
sqsigc 发表于 2020-11-22 03:59
第二轮,考虑flip 0的收益是1,flip 1的收益是-1,问题转化为【求一个element是1或者-1的array的max subarr ...

真的牛逼!
回复

使用道具 举报

🔗
dylanethan 2021-4-18 06:15:10 | 只看该作者
全局:
本帖最后由 dylanethan 于 2021-4-17 17:18 编辑

第三题可以转化成 trie tree? then bfs??
回复

使用道具 举报

🔗
qingli.tamu 2022-1-18 10:19:04 | 只看该作者
全局:
第二题不是义领领思吗
回复

使用道具 举报

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

本版积分规则

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