Skip to content

Commit

Permalink
uploading first time (#13)
Browse files Browse the repository at this point in the history
* uploading first time

* added few changes in ReadMe.md

* corrected one solution file link

Co-authored-by: Gourav Rusiya <[email protected]>
  • Loading branch information
Rudra407 and GouravRusiya30 authored Jul 22, 2020
1 parent 1c6dc62 commit dfaa959
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
25 changes: 25 additions & 0 deletions C++/Count-Primes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:

int countPrimes(int n) {

vector<bool> arr(n+1,0);
arr[0]=true;
arr[1]=true;
for(int i=2;i*i<=n;i++){
if(arr[i]==false){
for(int j = i*i;j<=n;j+=i){
arr[j]=true;
}
}
}

int count = 0;
for(int i=2;i<n;i++){
if(!arr[i]){
count++;
}
}
return count;
}
};
25 changes: 25 additions & 0 deletions C++/Excel-Sheet-Column-Title.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Runtime - 0ms
* Memory - 5.8
* LOGIC - simple maths
*
*/

class Solution {
public:
string convertToTitle(int n) {
string s="";
while(n>26){
int rem = n%26;
n=n/26;
if(rem==0){
rem = 26;
n--;
}
s.push_back('A'+(rem-1));
}
s.push_back('A'+n-1);
reverse(s.begin(),s.end());
return s;
}
};
16 changes: 16 additions & 0 deletions C++/Reverse-Integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int reverse(int x) {
int rev = 0;
while (x!=0) {
int pop = x % 10;
x /= 10;
// to check that it may not overflow integer range
if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return rev;
}
};

109 changes: 109 additions & 0 deletions C++/Sudoku-Solver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Runtime - 56ms
* Memory - 6.8
* LOGIC - described in functions
*
*/
class Solution {
public:

// to check whether num is valid in that row
bool isRowSafe(vector<vector<char>>& board, int row, char num)
{
for(int col = 0;col<9;col++)
{
if(board[row][col] == num)
return true;
}
return false;
}

// to check whether num is valid in that column
bool isColumnSafe(vector<vector<char>>& board, int col , char num)
{
for(int row = 0; row<9; row++)
{
if(board[row][col] == num)
return true;
}
return false;
}

// to check whether num is valid in that 3*3 matrix grid
bool isBoxSafe(vector<vector<char>>& board, int row1, int col1 , char num)
{
for(int row = 0;row<3;row++)
{
for(int col = 0;col<3;col++)
{
if(board[row1 + row][col1 + col] == num)
return true;
}
}

return false;
}

bool isSafe(vector<vector<char>>& board, int row, int col , char num)
{
return !isRowSafe(board,row,num) && !isColumnSafe(board, col, num) && !isBoxSafe(board, row-row%3,col-col%3,num) && board[row][col] == '.';
}


bool findUnassignedLocation(vector<vector<char>>& board, int &row, int &col)
{
for(row = 0;row<9;row++)
{
for(col = 0;col<9;col++)
{
if(board[row][col] == '.')
return true;

}
}
return false;
}



bool solve(vector<vector<char>>& board)
{
int row,col;
if(!findUnassignedLocation(board, row, col))
return true;

for(char i = '1' ; i<='9';i++)
{
if(isSafe(board,row,col,i))
{
board[row][col] = i;

if(solve(board))
return true;

board[row][col] = '.';
}
}

return false;
}

/* A utility function to print grid */
void printGrid(vector<vector<char>>& board)
{
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++)
cout << board[row][col] << " ";
cout << endl;
}
}


void solveSudoku(vector<vector<char>>& board) {

solve(board);
printGrid(board);

}
};

91 changes: 91 additions & 0 deletions Java/LRU-Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* LRU Cache
*
* Logic : maintain doubly linked List for insertion at head and deletion from tail and maintain hashmap of key as key and value as node. Node consists of key, value, prev and next pointers.
* Runtime: 16 ms
* Memory Usage: 47.9 MB
*/

class LRUCache {

final Node head = new Node();
final Node tail = new Node();
int capacity;
Map<Integer, Node> map;

public LRUCache(int capacity) {
map = new HashMap(capacity);
this.capacity = capacity;
head.next = tail;
tail.prev = head;
}

public int get(int key) {
int result = -1;
Node node = map.get(key);
if(node!=null)
{
remove(node);
add(node);
result = node.val;
}
return result;
}

public void put(int key, int value) {
Node node = map.get(key);
if(node!=null)
{
remove(node);
node.val = value;
add(node);
}
else{
if(map.size() == capacity)
{
map.remove(tail.prev.key);
remove(tail.prev);
}
Node new_node = new Node();

new_node.key = key;
new_node.val = value;
map.put(key, new_node);
add(new_node);
}
}

public void add(Node node)
{
Node head_next = head.next;
node.next = head_next;
head_next.prev = node;
head.next = node;
node.prev = head;

}

public void remove(Node node)
{
Node next_node = node.next;
Node prev_node = node.prev;

next_node.prev = prev_node;
prev_node.next = next_node;

}

class Node{
int key;
int val;
Node prev;
Node next;
}
}

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
</div>
<br/>


## String

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
Expand Down Expand Up @@ -128,13 +129,30 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| --- | ------------------------------------------------------------- | --------------------------------- | ------ | ------ | ---------- | --- | ------------- |
| 001 | [Two Sum](https://leetcode.com/problems/two-sum/)| [Java](./Java/two-sum.java) <br> [Python](./Python/1_TwoSum.py)|_O(N)_|_O(N)_|Easy|||
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [Java](./Java/valid-anagram.java) | _O(n)_ | _O(1)_ | Easy | | Unicode chars|
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Java](./Java/LRU-Cache.java) | | | Medium | | |

<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
<br/>

## Math

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
| --- | ---------------------------------------------------------------- | --------------------------------------- | ---------- | ------ | ---------- | --------- | ---- |
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | |
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) |[C++](./C++/Excel-Sheet-Column-Title.cpp)| _O(n)_ | _O(n)_ | Easy | String| |
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) |[C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |


<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
<br/>


## Two Pointer

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
Expand All @@ -147,6 +165,20 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
</div>
<br/>


## BackTracking

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
| 037 | [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) | [C++](./C++/Sudoku-Solver.cpp) | _O(9^(n*n))_ | _O(n*n)_ | Hard | Hash Table | |

<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
<br/>


## Binary Search

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
Expand Down Expand Up @@ -186,6 +218,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| [Lokendra Bohra](https://github.com/lokendra1704/) <br> <img src="https://github.com/lokendra1704.png" width="100" height="100"> | India | Python | [Leetcode](https://t.co/u0OByxhcHA) <br> [Hackerrank](https://www.hackerrank.com/lokendra17) |
| [Yuri Spiridonov](https://github.com/YuriSpiridonov) <br> <img src="https://github.com/YuriSpiridonov.png" width="100" height="100"> | Russia | Python | [Twitter](https://twitter.com/YuriSpiridonov)<br>[Leetcode](https://leetcode.com/yurispiridonov/)<br>[Hackerrank](https://www.hackerrank.com/YuriSpiridonov) |
| [Naveen Kashyap](https://github.com/naveenkash) <br> <img src="https://github.com/naveenkash.png" width="100" height="100"> | India | Javascript | [Twitter](https://twitter.com/naveen_kashyapp)<br>[Leetcode](https://leetcode.com/naveenkash/) |
| [Rudra Mishra](https://github.com/Rudra407) <br> <img src="https://github.com/Rudra407.png" width="100" height="100"> | India | C++ | [Twitter](https://twitter.com/ruDra_Mishra407)<br>[Leetcode](https://leetcode.com/rudramishra/) |
| [Sachin Singh Negi](https://github.com/sachinnegi) <br> <img src="https://github.com/sachinnegi.png" width="100" height="100"> | India | Python | [Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688) | |
<br/>
<div align="right">
Expand Down

0 comments on commit dfaa959

Please sign in to comment.