Giter VIP home page Giter VIP logo

cybercode's Introduction

CyberCode

Coding challenges in Transformers/Cybertron setting. Mostly from leetcode.

55. Jump Game

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

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

Example 1:

Input: nums = [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2:

Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Constraints: 1 <= nums.length <= $10^4$ 0 <= nums[i] <= $10^5$

class Solution:
    def canJump(self, nums: List[int]) -> bool:
        # index=0
        # for i in range(len(nums)):
        #     if i+nums[i]>=index:
        #         index=i+nums[i]
        #     if i >= index:
        #         break
        # return index>=len(nums)-1
        """
        greedy algorithm
        KEEP: max index possible
        BREAK: cannot reach the index
        max index: just add up the index and the according number in the list
        cannot reach: if current index cannot be reached by current max index 
        """
        cubes=0
        for cube in nums:
            if cubes<0:
                return False
            elif cube>cubes:
                cubes=cube
            cubes-=1
        return True
        """
        energon replacement strategy
        set energon cubes to 0
        traverse the whole engergon supplies:
        replace with new supply if more cubes than current
        each step cost one cube
        if cubes<0, stasis return false
        if successfully traversed list, return True
        """

45. Jump Game II

You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].

Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:

0 <= j <= nums[i] and i + j < n Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

Example 1:

Input: nums = [2,3,1,1,4] Output: 2 Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2:

Input: nums = [2,3,0,1,4] Output: 2

Constraints:

1 <= nums.length <= 10^4 0 <= nums[i] <= 1000 It's guaranteed that you can reach nums[n - 1].

class Solution:
    def jump(self, nums: List[int]) -> int:
        reach, step, last = 0,0,0
        for i in range(len(nums)-1):
            reach=max(reach,i+nums[i])
            if reach>=len(nums)-1:
                step+=1
                break
            if i == last:
                last=reach
                step+=1
        return step
        """
        Q: how to reach the last energon mine with least steps
        A: find the farthest reach of mines within each mines reachable range
        (Because it's bound that the last one shall be reached)
        Keep: 
        reach: farthest reach of mines within each energon mine's range,
        step: number of steps
        last: end point of each energon mine's reachable range
        Update:
        reach: max(i+nums[i])
        step: if reach the furthest mine of each step, +1
        or if reach the last mine, +1 and BREAK
        """

cybercode's People

Contributors

lina8g avatar

Watchers

 avatar

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.