楼主: Myron2017
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖子

🔗
 楼主| Myron2017 2021-2-25 13:51:10 | 只看该作者
全局:
516. Longest Palindromic Subsequence

经典的 DP 题目,还是非常需要理解的。参考了huahua 的解法 https://www.youtube.com/watch?v=OZX1nqaQ_9M,当然还有这个视频的解答, https://www.bilibili.com/video/BV1po4y1R738。 第二个视频还介绍了 Python @lru_cache() 高级了,直接暴力记忆函数不同参数调用的次数。

空间未优化版本



空间优化版本



  1. class Solution:
  2.     def longestPalindromeSubseq(self, s: str) -> int:
  3.         n = len(s)
  4.         # Processing edge cases
  5.         if n == 1: return 1
  6.         if n == 2:
  7.             if s[0] == s[1]: return 2
  8.             else: return 1
  9.         """
  10.         dp_x[i]:max Palindromic Subsequence starts from i with length x
  11.         
  12.         in detail:
  13.         dp0:
  14.             current_len substring,starts from length 2 substring, to be updated
  15.         dp1:
  16.             current_len-1 substring, starts from length 1 substring, so initialized all to 1
  17.         dp2:
  18.             current_len-2 substring, starts from length 2 substring, so initialized all to 0         
  19.         """     
  20.         dp0 = [0] * n
  21.         dp1 = [1] * n
  22.         dp2 = [0] * n
  23.         
  24.         for ll in range(2, n+1): # substring len 2 to n
  25.             for i in range(0, n-ll+1):
  26.                 j = i + ll - 1
  27.                 if s[i] == s[j]:
  28.                     dp0[i] = dp2[i+1] + 2
  29.                 else:
  30.                     dp0[i] = max(dp1[i+1], dp1[i])
  31.             dp2 = dp1.copy()
  32.             dp1 = dp0.copy()
  33.         return dp0[0]

复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-26 06:36:51 | 只看该作者
全局:
581 Shortest Unsorted Continuous Subarray (Python)

O(nlogn)



单调栈可以做到 O(N),题目的答案有更加全面的解释, https://leetcode.com/problems/sh ... -subarray/solution/

Approach 4: Using Stack
Algorithm

The idea behind this approach is also based on selective sorting. We need to determine the correct position of the minimum and the maximum element in the unsorted subarray to determine the boundaries of the required unsorted subarray.

To do so, in this implementation, we make use of a stackstack. We traverse over the numsnums array starting from the beginning. As we go on facing elements in ascending order(a rising slope), we keep on pushing the elements' indices over the stackstack. This is done because such elements are in the correct sorted order(as it seems till now). As soon as we encounter a falling slope, i.e. an element nums[j]nums[j] which is smaller than the element on the top of the stackstack, we know that nums[j]nums[j] isn't at its correct position.

In order to determine the correct position of nums[j]nums[j], we keep on popping the elemnents from the top of the stackstack until we reach the stage where the element(corresponding to the index) on the top of the stackstack is lesser than nums[j]nums[j]. Let's say the popping stops when the index on stackstack's top is kk. Now, nums[j]nums[j] has found its correct position. It needs to lie at an index k + 1k+1.

We follow the same process while traversing over the whole array, and determine the value of minimum such kk. This marks the left boundary of the unsorted subarray.

Similarly, to find the right boundary of the unsorted subarray, we traverse over the numsnums array backwards. This time we keep on pushing the elements if we see a falling slope. As soon as we find a rising slope, we trace forwards now and determine the larger element's correct position. We do so for the complete array and thus, determine the right boundary.

We can look at the figure below for reference. We can observe that the slopes directly indicate the relative ordering. We can also observe that point bb needs to lie just after index 0 marking the left boundary and the point aa needs to lie just before index 7 marking the right boundary of the unsorted subarray.


image.png (50.81 KB, 下载次数: 0)

image.png
回复

使用道具 举报

🔗
mambahang 2021-2-26 08:33:57 | 只看该作者
全局:
楼主一直在坚持。想问下楼主是在上学还是已经工作。上岸了吗?
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-26 21:35:29 | 只看该作者
全局:
946. Validate Stack Sequences
Greedy
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-28 12:27:41 | 只看该作者
全局:
LeetCode 29. Divide Two Integers (Python)

还是很经典的题目,有两点,一个是 log N 的优化,一个是边界条件处理,我个人不太喜欢 bitwise 操作,当然还是理解为什么用的。感觉还是可以的。

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-3-1 08:53:02 | 只看该作者
全局:
895. Maximum Frequency Stack

多重 stack 来解决,一个 dict 记录 freq 一个 维持插入的 order。

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-3-1 20:27:05 | 只看该作者
全局:
575. Distribute Candies

Easy

其实是抽屉原理
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-3-3 05:24:27 | 只看该作者
全局:
645. Set Mismatch

Using Constant Space to record the showing of the array

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-3-4 03:08:39 | 只看该作者
全局:
LeetCode 268. Missing Number  Easy 题
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-3-4 07:38:49 | 只看该作者
全局:
235        Lowest Common Ancestor of a Binary Search Tree
236        Lowest Common Ancestor of a Binary Tree

这个两题需要放到一起,学习下如何做递归的基本方法。

推荐这个视频,讲解的真好!

https://www.youtube.com/watch?v= ... upszWcA&index=2

通过 Base Case, 空 + root==p + root == q 来解决这个问题 ,然后递归求解,答案在左子树或者右子树的情况,

左右子树的返回值,都不空,就是 root 自己;
一个空,一个不空,答案是那个不空。
都空就没找到 LCA,当然这个答案不合法,因为题目保证了存在。



回复

使用道具 举报

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

本版积分规则

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