Coder Social home page Coder Social logo

dfs-3's Introduction

DFS-3

//Time Complexity = O(5 * num of digits) //Space Complexity = O(num of digits)

class Solution { int result; HashMap<Integer, Integer> map; public int confusingNumberII(int n) { if(n < 1) { return 0; }

    map = new HashMap<>();
    map.put(0,0);
    map.put(1,1);
    map.put(6,9);
    map.put(8,8);
    map.put(9,6);

    dfs(n, 0);
    return result;
}

private void dfs(int n, long current) {

    if(current > n) {
        return;
    }

    if(isConfusing(current)) {
        result++;
    }
    for(Integer i : map.keySet()) {
        long next = current * 10 + i;
        if(next != 0) {
            dfs(n,next);
        }
    }
}
private boolean isConfusing(long num) {
    long reverse = 0;
    long number = num;

    while(num > 0) {
        reverse = reverse * 10 + map.get((int)num % 10);
        num = num / 10;
    }
    return reverse != number;
}

}

//Time Complexity = O(4^N) //Space Complexity = O(N)

class Solution { public boolean makesquare(int[] matchsticks) { if(matchsticks == null || matchsticks.length == 0) { return false; } int sum = 0; for(Integer i : matchsticks) { sum += i; } if(sum % 4 != 0) { return false; } int side = sum / 4;

    return backtrack(matchsticks,0, new int[4], side);
}

private boolean backtrack(int[] matchsticks, int index, int[] square,int side) {

    if(square[0] == side && square[1] == side && square[2] == side && square[3] == side) {
        return true;
    }


    for(int i = 0; i < 4; i++) {
        int current = matchsticks[index];
        if(current + square[i] <= side) {
            square[i] = square[i] + current;
            if(backtrack(matchsticks,index + 1, square, side)) {
                return true;
            }
            square[i] = square[i] - current;
        }
    }
    return false;
}

}

dfs-3's People

Contributors

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