强化班ladder

@2017.06.04

Follow up in Code Interview

  • 401 - kth-smallest-number-in-sorted-matrix

    • 排序数组,即每一行每一列都是排好序的
    • 类似于之前做的那道题,对这种排序数组,是从左上角往右下角看的。
    • 每次pop一个最小值直到找到kth,所以用堆。
    • 重刷呀
    • --- heap堆 & matrix矩阵 ---
    • from heapq import *
      class Solution:
          # @param matrix: a matrix of integers
          # @param k: an integer
          # @return: the kth smallest number in the matrix
          def kthSmallest(self, matrix, k):
              # write your code here
              if not matrix or not matrix[0] or k <= 0:
                  return
              rowNum, colNum = len(matrix), len(matrix[0])
              if rowNum * colNum < k:
                  return
              kth, heap, visited = 0, [], set([(0, 0)])
              heappush(heap, (matrix[0][0], 0, 0))
              xPath, yPath = [1, 0], [0, 1]  # go right or go down to get the next one
              while kth < k - 1 and heap:
                  val, row, col = heappop(heap)
                  kth += 1
                  for i in range(2):
                      x = row + xPath[i]
                      y = col + yPath[i]
                      if x < rowNum and y < colNum and (x, y) not in visited:
                          heappush(heap, (matrix[x][y], x, y))
                          visited.add((x, y))
              return heappop(heap)[0]
  • 406 - minimum-size-subarray-sum
    • 窗口型指针问题。最好还是用 for i in range(): while j 的模板
    • 因为i,j都是一直向右移动(不会回退),所以时间复杂度是O(2 * n)的
    • 并不是所有subarray sum的题都是用prefixSum前缀和
    • 还要注意这道题如果array里面有负数,就不能实现这种O(n)的指针移动啦,具体可以再思考下。
    • --- TwoPointers两根指针 ---
    • btw,看到chanllenge里有说可以试着实现O(nlogn)的算法,我想了可以利用prefixSum + BinarySearch的方式实现。也就是对每一个前缀和为x的位置,去二分查找其前面的最右边的小于 x - s的位置,即可。
    • 重刷呀
    • class Solution:
           # @param nums: a list of integers
           # @param s: an integer
           # @return: an integer representing the minimum size of subarray
          def minimumSize(self, nums, s):
              # write your code here
              if not nums or s <= 0:
                  return -1
              j, subSum, length = 0, 0, sys.maxint
              for i in range(len(nums)):
                  while j < len(nums) and subSum < s:
                      subSum += nums[j]
                      j += 1
                  if subSum >= s and j - i < length:
                      length = j - i
                  subSum -= nums[i]
              return -1 if length == sys.maxint else length
  • 384 - longest-substring-without-repeating-characters
    • 这个题也属于窗口类指针问题。
    • 因为只有256个ascii字符,所以可以直接用charAt数组,而不用hash。
    • 这题我没用for while的模板,因为j >= len(s)就可以直接退出了。
    • --- TwoPointers两根指针 ---
    • class Solution:
          # @param s: a string
          # @return: an integer
          def lengthOfLongestSubstring(self, s):
              # write your code here
              if not s:
                  return 0
              cnt, i, j, ans = 0, 0, 0, 1
              charAt = [0] * 256
              while j < len(s):
                  while j < len(s) and not charAt[ord(s[j])]:
                      cnt += 1
                      charAt[ord(s[j])] += 1
                      j += 1
                  ans = max(ans, j - i)
                  charAt[ord(s[i])] -= 1
                  i += 1
              return ans
  • 386 - longest-substring-with-at-most-k-distinct-characters
    • 重刷呀(必须)
    • 在(s[j] in hashTable or distinctCount < k)这里犯了错,我第一遍的时候写成了while j < len(s) and distinctCount <= k,这导致我需要这样更新ans:ans = max(ans, j - i - 1) if distinctCount > k else max(ans, j - i). 这就是因为不知道退出while的时候到底是因为distinctCount > k or j >= len(s),所以ans要分情况计算,这种情况很难想出来,所以倒不如用第一种写法。
    • --- TwoPointers两根指针 & HashTable哈希表 ---
    • class Solution:
          # @param s : A string
          # @return : An integer
          def lengthOfLongestSubstringKDistinct(self, s, k):
              # write your code here
              if not s or k <= 0:
                  return 0
              i, j, distinctCount, ans = 0, 0, 0, 1
              hashTable = {}
              while j < len(s):
                  while j < len(s) and (s[j] in hashTable or distinctCount < k):
                      if s[j] in hashTable:
                          hashTable[s[j]] += 1
                          j += 1
                      else:
                          hashTable[s[j]] = 1
                          j += 1
                          distinctCount += 1
                  ans = max(ans, j - i)
                  hashTable[s[i]] -= 1
                  if not hashTable[s[i]]:
                      hashTable.pop(s[i])
                      distinctCount -= 1
                  i += 1
              return ans
  • 465 - kth-smallest-sum-in-two-sorted-arrays
    • 本质上跟401是一样的,你可以吧这两个数组的组合看成是一个矩阵呀。
    • --- TwoPoniters两根指针 ---
    • from heapq import *
      class Solution:
          # @param {int[]} A an integer arrays sorted in ascending order
          # @param {int[]} B an integer arrays sorted in ascending order
          # @param {int} k an integer
          # @return {int} an integer
          def kthSmallestSum(self, A, B, k):
              # Write your code here
              if not A or not B or k <= 0 or k > len(A) * len(B):
                  return
              heap = [(A[0] + B[0], 0, 0)]
              APath, BPath = [0, 1], [1, 0]
              visited = set((0, 0))
              kth = 0
              while heap and kth < k - 1:
                  val, indA, indB = heappop(heap)
                  kth += 1
                  for i in range(2):
                      newA, newB = indA + APath[i], indB + BPath[i]
                      if newA < len(A) and newB < len(B) and (newA, newB) not in visited:
                          heappush(heap, (A[newA] + B[newB], newA, newB))
                          visited.add((newA, newB))
              return heappop(heap)[0]
时间: 2024-10-21 13:20:41

强化班ladder的相关文章

九章算法强化班ladder题目梳理

1 - Follow up in Code Interview kth-smallest-number-in-sorted-matrix Find the kth smallest number in at row and column sorted matrix. [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9],] 注意点: Comparator<Pair>的实现 PriorityQueue poll add 1 import java.util.*; 2 3 class

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

Java Word Ladder(字梯)

问题: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:

18. Word Ladder &amp;&amp; Word Ladder II

Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example,

Leetcode 127. Word Ladder

思路: 1 class Solution(object): 2 def __init__(self): 3 return 4 5 def ladderLength(self, beginWord, endWord, wordList): 6 """ 7 :type beginWord: str 8 :type endWord: str 9 :type wordList: List[str] 10 :rtype: int 11 """ 12 fro

【LeetCode】Word Ladder 字符串

题目:Word Ladder <span style="font-size:18px;">/**LeetCode word ladder * 题目:给定一个起始单词和一个终结单词以及一个字典,要求每次变换一个字符,成为字典中新的词,直到变为最后的词,要求其最短路径 * 思路:利用队列,先弹出第一个词,分别将词中每一个字符替换直到找到一个字典中存在的词,加入队列,直到匹配的词是最后一个,此时终止 * 如果没有这样的路径,则返回0 */ package javaTrain; i

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given:

leetcode第一刷_Word Ladder II

注:本文仅供技术探讨, 研究,测试使用. 这个漏洞是2014年2月4日被发现的, 因为该组件试用范围非常广, 所以该漏洞的影响也非常巨大.通过特制的包含畸形header的http请求,可以导致使用该组件的应用程序进入无限循环从而耗尽CPU等资源并最终崩溃. 最近因为在修补struts1的可操纵classLoader的漏洞(struts2也有该漏洞, 不在本文讨论范围), 所以我就在我建立的struts1的项目上直接做测试,怎么创建struts1的项目不在本文讨论范围之列你可以在这里下载strut

LeetCode OJ - Word Ladder 2

我发现在leetcode上做题,当我出现TLE问题时,往往是代码有漏洞,有些条件没有考虑到,这道题又验证了我这一想法. 这道题是在上一道的基础上进一步把所有可能得转换序列给出. 同样的先是BFS,与此同时需要一个hashMap记录下每个节点,和他所有父节点的对应关系,然后通过DFS,回溯所有可能的路径. 下面是AC代码. 1 /** 2 * Given two words (start and end), and a dictionary, find all shortest transform