GithubHelp home page GithubHelp logo

greedy-5's Introduction

Greedy-5

//Time - Complexity = O( M * N ) //Space - Complexity = O(1)

class Solution { public boolean isMatch(String s, String p) { if(s.equals(p) || p.equals("*")) { return true; } int slength = s.length(); int plength = p.length();

// boolean[][] dp = new boolean[plength + 1][slength + 1]; // dp[0][0] = true;

// for(int i = 1; i < dp.length;i++) { // for(int j = 0;j < dp[0].length; j++) { // if(p.charAt(i-1) == '*') { // dp[i][j] = dp[i-1][j]; // if(j > 0) { // dp[i][j] = dp[i][j] || dp[i][j-1]; // } // } else if(j>0 && (p.charAt(i-1) == s.charAt(j-1) || p.charAt(i-1) == '?')){ // dp[i][j] = dp[i-1][j-1]; // } // } // } // return dp[plength][slength];

    int smem = -1;
    int pmem = -1;

    int sp = 0;
    int pp = 0;

    while(sp < slength) {
        if(pp < plength && (s.charAt(sp) == p.charAt(pp) || p.charAt(pp) == '?')) {
            pp++;
            sp++;
        } else if (pp < plength && p.charAt(pp) == '*') {
            smem = sp;
            pmem = pp;
            pp++;
        } else if(pmem == -1 ) {
            return false;
        } else {
            smem++;
            sp = smem;
            pp = pmem +1;
        }
    }

    while(pp < plength) {
        if(p.charAt(pp) != '*') {
            return false;
        }
        pp++;
    }
    return true;
}

}

Problem2: Bikes in a Campus (https://leetcode.com/problems/campus-bikes/)

//Time - Complexity = O(MN) //Space - Complexity = O(MN)

class Solution { public int[] assignBikes(int[][] workers, int[][] bikes) { if(workers == null || workers.length == 0 || bikes == null || bikes.length == 0) { return new int[0]; }

    Map<Integer, List<int[]>> map = new HashMap<>();
    int wlen = workers.length;
    int blen = bikes.length;
    int[] result = new int[wlen];
    int max = 2001;
    int min = 0;

    for(int i = 0 ; i < wlen; i++) {
        for(int j = 0 ; j < blen; j++) {
            int dist = manhattanDistance(workers[i],bikes[j]);

            max = Math.max(dist,max);
            min = Math.min(dist,min);

            if(!map.containsKey(dist)) {
                map.put(dist, new ArrayList<>());
            }
            map.get(dist).add(new int[]{i,j});
        }
    }
    int count = 0;
    boolean[] assignedWorkers = new boolean[workers.length];
    boolean[] assignedBikes = new boolean[bikes.length];

    for(int dist = min; dist <= max; dist++) {

        if(map.containsKey(dist)) {
            List<int[]> distList = map.get(dist);
            for(int[] pair : distList) {
                int worker = pair[0];
                int bike = pair[1];

                if(!assignedBikes[bike] && !assignedWorkers[worker]) {
                    count++;
                    result[worker] = bike;
                    assignedBikes[bike] = true;
                    assignedWorkers[worker] = true;
                }
                if(count == wlen) {
                    return result;
                }
            }
        }
    }
    return result;





}

private int manhattanDistance(int[] worker,int[] bikes) {
    return Math.abs(worker[0] - bikes[0]) + Math.abs(worker[1] - bikes[1]);
}

}

greedy-5's People

Contributors

dasaniaditya avatar super30admin 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.