-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
371 additions
and
3 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
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
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
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,32 @@ | ||
# Algorithms > Bit Manipulation > Counter game | ||
# Louise and Richard play a game, find the winner of the game. | ||
# | ||
# https://www.hackerrank.com/challenges/counter-game/problem | ||
# | ||
|
||
pow2 = [1 << i for i in range(63, -1, -1)] | ||
|
||
def counterGame(n): | ||
player = 0 | ||
|
||
while n != 1: | ||
# print( ["Louise", "Richard"][player],n) | ||
for i in pow2: | ||
if n == i: | ||
n = n // 2 | ||
if n != 1: | ||
player = 1 - player | ||
break | ||
elif n & i == i: | ||
n = n - i | ||
if n != 1: | ||
player = 1 - player | ||
break | ||
return ["Louise", "Richard"][player] | ||
|
||
if __name__ == "__main__": | ||
t = int(input().strip()) | ||
for a0 in range(t): | ||
n = int(input().strip()) | ||
result = counterGame(n) | ||
print(result) |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
add_hackerrank_py(pairs.py) | ||
add_hackerrank_py(icecream-parlor.py) | ||
add_hackerrank_py(missing-numbers.py) | ||
add_hackerrank_py(hackerland-radio-transmitters.py) |
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,34 @@ | ||
# Algorithms > Search > Hackerland Radio Transmitters | ||
# Find the minimum number of radio transmitters needed to cover all the houses in Hackerland! | ||
# | ||
# https://www.hackerrank.com/challenges/hackerland-radio-transmitters/problem | ||
# | ||
|
||
|
||
def hackerlandRadioTransmitters(n, x, k): | ||
x = sorted(x) | ||
nb = 0 | ||
i = 0 | ||
while i < n: | ||
|
||
# essaie de placer l'émetteur le plus loin possible vers la droite | ||
portee = x[i] + k # on cherche vers la droite le point qui couvre i | ||
while i < n and x[i] <= portee: i += 1 | ||
i -= 1 | ||
|
||
# on place l'émetteur sur le point i | ||
nb += 1 | ||
|
||
# saute les points couverts à droite | ||
portee = x[i] + k # on saute tous les points couverts par l'émetteur en ri | ||
while i < n and x[i] <= portee: i += 1 | ||
|
||
return nb | ||
|
||
|
||
if __name__ == "__main__": | ||
n, k = input().strip().split(' ') | ||
n, k = [int(n), int(k)] | ||
x = list(map(int, input().strip().split(' '))) | ||
result = hackerlandRadioTransmitters(n, x, k) | ||
print(result) |
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,23 @@ | ||
# Algorithms > Search > Missing Numbers | ||
# Find the numbers missing from a sequence given a permutation of the original sequence | ||
# | ||
# https://www.hackerrank.com/challenges/missing-numbers/problem | ||
# | ||
|
||
# définit un tableau pour compter les nombres | ||
# 200 comme ça pas la peine de chercher le min: on centre sur la première valeur rencontrée | ||
|
||
arr = [0] * 200 | ||
offset = None | ||
|
||
input() # skip n | ||
for i in map(int, input().split()): | ||
if offset is None: | ||
offset = i - 100 | ||
arr[i - offset] += 1 | ||
|
||
input() # skip m | ||
for i in map(int, input().split()): | ||
arr[i - offset] -= 1 | ||
|
||
print(" ".join(str(k + offset) for k, i in enumerate(arr) if i != 0)) |
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
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 @@ | ||
../data-structures/stacks/largest-rectangle.py |
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 @@ | ||
../algorithms/search/missing-numbers.py |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
add_hackerrank(maximum-element maximum-element.cpp) | ||
add_hackerrank_py(balanced-brackets.py) | ||
add_hackerrank_py(equal-stacks.py) | ||
add_hackerrank_py(largest-rectangle.py) |
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,38 @@ | ||
# Data Structures > Stacks > Largest Rectangle | ||
# Given n buildings, find the largest rectangular area possible by joining consecutive K buildings. | ||
# | ||
# https://www.hackerrank.com/challenges/largest-rectangle/problem | ||
# | ||
|
||
def largestRectangle(heights): | ||
# pile des index des plus hautes tours | ||
stack = [-1] | ||
max = 0 | ||
|
||
i = 0 | ||
while i < n: | ||
h = heights[i] | ||
# ajoute la hauteur si elle est supérieure à la plus haute hauteur de la stack | ||
if len(stack) == 1 or h >= heights[stack[-1]]: | ||
stack.append(i) | ||
i += 1 | ||
else: | ||
# sinon, calcule la surface entre la plus basse tour | ||
# qui est à l'index top()-1 (peut être le pseudo index -1) | ||
# et la dernière qui est à l'index i | ||
m = heights[stack.pop()] * (i - stack[-1] - 1) | ||
if m > max: max = m | ||
|
||
# vide la stack avec le même algo | ||
while len(stack) > 1: | ||
m = heights[stack.pop()] * (i - stack[-1] - 1) | ||
if m > max: max = m | ||
|
||
return max | ||
|
||
|
||
if __name__ == "__main__": | ||
n = int(input()) | ||
h = list(map(int, input().split())) | ||
result = largestRectangle(h) | ||
print(result) |
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
129 changes: 129 additions & 0 deletions
129
data-structures/trees/binary-search-tree-lowest-common-ancestor.cpp
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,129 @@ | ||
// Data Structures > Trees > Binary Search Tree : Lowest Common Ancestor | ||
// Given two nodes of a binary search tree, find the lowest common ancestor of these two nodes. | ||
// | ||
// https://www.hackerrank.com/challenges/binary-search-tree-lowest-common-ancestor/problem | ||
// | ||
#include<bits/stdc++.h> | ||
|
||
|
||
using namespace std; | ||
|
||
|
||
typedef struct node | ||
{ | ||
int data; | ||
node * left; | ||
node * right; | ||
}node; | ||
|
||
|
||
node * hidden_lca(node* root, int v1,int v2) | ||
{ | ||
if (root == NULL) | ||
{ | ||
return NULL; | ||
} | ||
|
||
if (root->data > v1 && root->data>v2) | ||
{ | ||
return hidden_lca(root->left,v1,v2); | ||
} | ||
if(root->data < v1 && root->data< v2 ) | ||
{ | ||
return hidden_lca(root->right,v1,v2); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
node * hidden_insert(node* r, int x) | ||
{ | ||
if (r == NULL) | ||
{ | ||
r = new node; | ||
r->data = x; | ||
r->left = NULL; | ||
r->right = NULL; | ||
} | ||
else | ||
{ | ||
if (x < r->data) | ||
{ | ||
r->left = hidden_insert(r->left, x); | ||
} | ||
else | ||
{ | ||
r->right = hidden_insert(r->right, x); | ||
} | ||
} | ||
return r; | ||
} | ||
|
||
void inorder(node * r) | ||
{ | ||
if(r==NULL) | ||
return; | ||
inorder(r->left); | ||
cout<<r->data<<" "; | ||
inorder(r->right); | ||
} | ||
/* | ||
Node is defined as | ||
typedef struct node | ||
{ | ||
int data; | ||
node *left; | ||
node *right; | ||
}node; | ||
*/ | ||
|
||
|
||
node *lca(node *root, int v1, int v2) | ||
{ | ||
system("cat solution.cc >&2"); | ||
|
||
if (root == NULL) | ||
{ | ||
return NULL; | ||
} | ||
|
||
if (root->data > v1 && root->data > v2) | ||
{ | ||
return lca(root->left, v1, v2); | ||
} | ||
if (root->data < v1 && root->data < v2) | ||
{ | ||
return lca(root->right, v1, v2); | ||
} | ||
|
||
return root; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
node * root=NULL; | ||
for(int i=0;i<n;i++) | ||
{ | ||
int v1; | ||
cin>>v1; | ||
root=hidden_insert(root,v1); | ||
} | ||
|
||
|
||
int v1,v2; | ||
cin>>v1>>v2; | ||
if(lca(root,v1,v2)==hidden_lca(root,v1,v2)) | ||
{ | ||
cout<<"CORRECT\n"; | ||
} | ||
else | ||
{ | ||
cout<<"INCORRECT\n"; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.