Skip to content

Commit 8ed511d

Browse files
committed
2 problems
1 parent b8dd7f9 commit 8ed511d

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
211211
#### [284. Peeking Iterator](https://github.com/hitzzc/go-leetcode/tree/master/peeking_iterator)
212212
#### [287. Find the Duplicate Number](https://github.com/hitzzc/go-leetcode/tree/master/find_the_duplicate_number)
213213
#### [290. Word Pattern](https://github.com/hitzzc/go-leetcode/tree/master/word_pattern)
214+
#### [292. Nim Game](https://github.com/hitzzc/go-leetcode/tree/master/nim_game)
215+
#### [295. Find Median from Data Stream](https://github.com/hitzzc/go-leetcode/tree/master/find_median_from_data_stream)
214216

215217

216218

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include <vector>
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Heap {
6+
private:
7+
vector<int> array;
8+
int (*compare) (int, int);
9+
void adjust(int idx) {
10+
int largest = idx;
11+
if (2*idx+1 < array.size() && compare(array[2*idx+1], array[idx]))
12+
largest = 2*idx+1;
13+
if (2*idx+2 < array.size() && compare(array[2*idx+2],array[largest]))
14+
largest = 2*idx+2;
15+
if (largest != idx) {
16+
int tmp = array[idx];
17+
array[idx] = array[largest];
18+
array[largest] = tmp;
19+
adjust(largest);
20+
}
21+
return;
22+
}
23+
public:
24+
Heap(int (*compare) (int, int)): compare(compare) {}
25+
void push(int val) {
26+
array.push_back(val);
27+
for (int i = array.size()/2 - 1; i >= 0; --i){
28+
adjust(i);
29+
}
30+
}
31+
void print() {
32+
for (auto val: array)
33+
cout << "print\t" << val << endl;
34+
}
35+
int pop() {
36+
int ret = array[0];
37+
array[0] = array.back();
38+
array.pop_back();
39+
adjust(0);
40+
return ret;
41+
}
42+
int empty() {
43+
return array.empty();
44+
}
45+
int size() {
46+
return array.size();
47+
}
48+
int top() {
49+
return array[0];
50+
}
51+
};
52+
53+
int max(int i, int j) {
54+
if (i > j) return true;
55+
return false;
56+
}
57+
58+
int min(int i, int j) {
59+
if (i < j) return true;
60+
return false;
61+
}
62+
63+
class MedianFinder {
64+
Heap max_heap;
65+
Heap min_heap;
66+
public:
67+
MedianFinder(): max_heap(Heap(max)), min_heap(Heap(min)){}
68+
// Adds a number into the data structure.
69+
void addNum(int num) {
70+
if (max_heap.empty() || num <= max_heap.top()) {
71+
max_heap.push(num);
72+
}else {
73+
min_heap.push(num);
74+
}
75+
if (max_heap.size() > min_heap.size()+1){
76+
int top = max_heap.pop();
77+
min_heap.push(top);
78+
}
79+
if (min_heap.size() > max_heap.size()) {
80+
int top = min_heap.pop();
81+
max_heap.push(top);
82+
}
83+
return;
84+
}
85+
86+
// Returns the median of current data stream
87+
double findMedian() {
88+
if (max_heap.size() == min_heap.size())
89+
return ((max_heap.top()+min_heap.top())/2.0);
90+
if (max_heap.size() > min_heap.size())
91+
return max_heap.top();
92+
return min_heap.top();
93+
}
94+
};
95+
96+
int main() {
97+
MedianFinder finder;
98+
finder.addNum(-1);
99+
finder.addNum(-2);
100+
finder.addNum(-3);
101+
finder.addNum(-4);
102+
finder.addNum(-5);
103+
return 0;
104+
}

Diff for: nim_game/nim_game.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool canWinNim(int n) {
4+
return !(n%4 == 0);
5+
}
6+
};

0 commit comments

Comments
 (0)