Code Monkey home page Code Monkey logo

kata's Introduction

kata's People

Contributors

actions-user avatar xmchxup avatar

Stargazers

 avatar

kata's Issues

LeetCode 54. Spiral Matrix

模拟遍历右下左上,当遍历完所有元素结束。

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        res = []
        m, n = len(matrix), len(matrix[0])
        visited = [[False for _ in range(n)] for _ in range(m)]
        dirs = [[1, 0], [0, 1], [-1, 0], [0, -1]]
        x, y = 0, 0
        dir_idx = 0

        def is_valid_coordinate(x, y):
            return not (x < 0 or x >= n or y < 0 or y >= m or visited[y][x])

        while len(res) < m * n:
            res.append(matrix[y][x])
            visited[y][x] = True
            delta_x, delta_y = dirs[dir_idx]
            if not is_valid_coordinate(x + delta_x, y + delta_y):
                dir_idx = (dir_idx + 1) % 4
                delta_x, delta_y = dirs[dir_idx]
            x, y = x + delta_x, y + delta_y

        return res

Leetcode: Kth Largest Element in an Array

from typing import List
import random


class Solution:
    def findKthLargestV2(self, nums: List[int], k: int) -> int:
        pivot = random.choice(nums)
        left = [x for x in nums if x < pivot]
        mid = [x for x in nums if x == pivot]
        right = [x for x in nums if x > pivot]

        if k <= len(right):
            return self.findKthLargestV2(right, k)
        elif k > len(right) + len(mid):
            return self.findKthLargestV2(left, k - (len(right) + len(mid)))
        return mid[0]

    def findKthLargest(self, nums: List[int], k: int) -> int:
        def quickSelect(lo: int, hi: int, k: int):
            if lo >= hi:
                return nums[lo]
            x = nums[lo]
            i, j = lo - 1, hi + 1
            while i < j:
                i += 1
                while i <= j:
                    if nums[i] >= x:
                        break
                    i += 1
                j -= 1
                while j >= i:
                    if nums[j] <= x:
                        break
                    j -= 1
                if i < j:
                    nums[i], nums[j] = nums[j], nums[i]
            largeCnt = hi - j
            if k <= largeCnt:
                return quickSelect(j + 1, hi, k)
            else:
                return quickSelect(lo, j, k - largeCnt)

        return quickSelect(0, len(nums) - 1, k)

Leetcode: 62. Unique Paths

dp[i][j] = 到达i,j位置的唯一路径方案
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]

class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp = [[1 for _ in range(n)] for _ in range(m)]
        for i in range(1, m):
            for j in range(1, n):
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
        return dp[m - 1][n - 1]

LeetCode: 455. Assign Cookies

将两个数组排序,从大到小遍历尽可能的满足Big小孩的需要。

from typing import List
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        res = 0
        i, j = len(g) - 1, len(s) - 1
        while i >= 0 and j >= 0:
            if s[j] >= g[i]:
                res += 1
                j -= 1
                i -= 1
            else:
                i -= 1
        return res

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.