Coder Social home page Coder Social logo

array-3's Introduction

Array-3

//Time Complexity = O(N) //Space Complexity = O(1) class Solution { public int trap(int[] height) { if(height == null) return 0; int lw = 0; int l = 0; int rw = 0; int r = height.length - 1; int result = 0;

    while(l <= r){
        if(lw <= rw){
            if(lw > height[l]){
                result = result + (lw - height[l]);
            }else{
                lw = height[l];
            }
            l++;
        }
        else{
            if(rw > height[r]){
                result = result + (rw - height[r]);
            }else{
                rw = height[r];
            }
            r--; 
        } 
    }
    return result;
}

}

//Time Complexity = O(n) //Space Complexity = O(n)

class Solution { public int hIndex(int[] citations) { int n = citations.length; int[] arr = new int[n+1];

    for(int i = 0; i < n; i++){
        if(citations[i] >= n){
            arr[n] = arr[n] + 1; 
        }else{
            arr[citations[i]] += 1; 
        }
    }
    int sum = 0;
    for(int i = n; i >= 0; i--){
       sum = sum + arr[i]; 
       if(sum >= i) return i; 
    }
    
    return -1;
}

}

Problem3 Rotate Array by K Places(https://leetcode.com/problems/rotate-array/)

//Time Complexity = O(n) //Space Complexity = O(1) class Solution{ public void rotate(int[] nums, int k) { if(nums == null) return; int n = nums.length; if(k >= n) k = k % n; reverse(nums, 0, n - 1); reverse(nums, 0, k - 1); reverse(nums, k , n - 1); }

private void reverse(int[] nums, int start, int end){
    while(start < end){
        swap(nums,start,end); 
        start++;
        end--; 
    }
}

private void swap(int[] nums, int start, int end){
    int temp = nums[start];
    nums[start] = nums[end]; 
    nums[end] = temp; 
}

}

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.