Code Monkey home page Code Monkey logo

java_leetcode's Introduction

Java_LEETCode

Programs/Algorithms during this journey

Maximum sub Array using kadane's algorithm

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2:

Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3:

Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.\

Algorithm:

current_sum=overallsum=a[0] i=1--->n current_sum=max(a[i],current_sum+a[i]) overallsum=max(overallsum,current_sum) return overallsum

soultion:

class Solution { public int maxSubArray(int[] nums) { int sum,currentSum; sum=currentSum=nums[0]; for(int i=1;i<nums.length;i++) { currentSum=Math.max(nums[i],currentSum+nums[i]); sum=Math.max(currentSum,sum); } return sum; } }

java_leetcode's People

Contributors

manepullavamsi 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.