Day-6 Leetcode Coding Journey: Mastering Problem-Solving Techniques

·

1 min read

Leetcode #54 Spiral Matrix

📌 Problem Statement

Given an m x n matrix, return all elements of the matrix in spiral order.

Constraints

  • m==matrix.length

  • n==matrix[i].length

  • 1≤m,n≤10

  • −100≤matrix[i][j]≤100

🚀 Code Implementation

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        result = []
        while matrix:
            result=result+matrix.pop(0)  
            if matrix and matrix[0]:
                for row in matrix:
                    result.append(row.pop())
            if matrix:
                result=result+matrix.pop()[::-1]
            if matrix and matrix[0]:
                for row in matrix[::-1]:
                    result.append(row.pop(0))  
        return result

Leetcode #55 Jump Game

📌 Problem Statement

You are given an integer array nums where each element represents the maximum jump length from that position.

Return true if you can reach the last index, or false otherwise.

Constraints

  • 1≤nums.length≤10^4

  • 0≤nums[i]≤10^5

🚀 Code Implementation

class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        max_reach = 0
        for i,jump in enumerate(nums):
            if i>max_reach:  
                return False
            max_reach=max(max_reach,i+jump) 
            if max_reach>=len(nums)-1:  
                return True
        return False
Â