-
-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* uploading first time * added few changes in ReadMe.md * corrected one solution file link Co-authored-by: Gourav Rusiya <[email protected]>
- Loading branch information
1 parent
1c6dc62
commit dfaa959
Showing
6 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters