Skip to content

Commit ee9d31e

Browse files
committed
Added one solution & modified 2 solutions
1 parent 743bba8 commit ee9d31e

File tree

3 files changed

+83
-50
lines changed

3 files changed

+83
-50
lines changed

Easy/Day of the Week.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
int[] m = {0,31,28,31,30,31,30,31,31,30,31,30,31};
3+
String[] res = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
4+
public String dayOfTheWeek(int day, int month, int year) {
5+
int days = years(year);
6+
if(isLeap(year))
7+
m[2] = 29;
8+
for(int i=0; i < month; i++){
9+
days += m[i];
10+
}
11+
days += day-1;
12+
return res[days%7];
13+
}
14+
private int years(int y){
15+
int count = 0;
16+
for(int i=1971; i < y; i++){
17+
if(isLeap(i))
18+
count += 366;
19+
else
20+
count += 365;
21+
}
22+
return count;
23+
}
24+
private boolean isLeap(int y){
25+
if(y % 4 != 0) return false;
26+
else if(y%100 != 0) return true;
27+
else if(y % 400 != 0) return false;
28+
else return true;
29+
}
30+
}

Easy/Fibonacci Numbers.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
class Solution {
2-
int[] arr;
3-
public int fib(int N) {
4-
arr = new int[N + 1];
5-
return fibHelper(N);
2+
public int fib(int N) {
3+
Integer[] memo = new Integer[N + 1];
4+
return getFib(memo, N);
5+
}
6+
7+
private int getFib(Integer[] memo, int N) {
8+
if (memo[N] != null) {
9+
return memo[N];
610
}
7-
8-
private int fibHelper(int N) {
9-
if (N == 0) {
10-
return 0;
11-
}
12-
13-
if (N == 1) {
14-
arr[1] = 1;
15-
return 1;
16-
}
17-
18-
if (arr[N] != 0) {
19-
return arr[N];
20-
}
21-
22-
arr[N] = fibHelper(N - 1) + fibHelper(N - 2);
23-
24-
return arr[N];
11+
else if (N == 0) {
12+
return 0;
2513
}
14+
else if (N == 1) {
15+
return 1;
16+
}
17+
memo[N] = getFib(memo, N - 1) + getFib(memo, N - 2);
18+
return memo[N];
19+
}
2620
}

Easy/Implement Queue using Stacks.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
11
class MyQueue {
22

3-
/** Initialize your data structure here. */
4-
Stack<Integer> oldStack = new Stack<>();
5-
Stack<Integer> newStack = new Stack<>();
6-
7-
/** Push element x to the back of queue. */
8-
public void push(int x) {
9-
while (!newStack.isEmpty()) {
10-
oldStack.push(newStack.pop());
11-
}
12-
oldStack.push(x);
3+
/** Initialize your data structure here. */
4+
Stack<Integer> stack;
5+
Stack<Integer> revStack;
6+
public MyQueue() {
7+
stack = new Stack<>();
8+
revStack = new Stack<>();
9+
}
10+
11+
/** Push element x to the back of queue. */
12+
public void push(int x) {
13+
stack.push(x);
14+
}
15+
16+
/** Removes the element from in front of queue and returns that element. */
17+
public int pop() {
18+
while (!stack.isEmpty()) {
19+
revStack.push(stack.pop());
1320
}
14-
15-
/** Removes the element from in front of queue and returns that element. */
16-
public int pop() {
17-
while (!oldStack.isEmpty()) {
18-
newStack.push(oldStack.pop());
19-
}
20-
return newStack.pop();
21+
int num = revStack.pop();
22+
while (!revStack.isEmpty()) {
23+
stack.push(revStack.pop());
2124
}
22-
23-
/** Get the front element. */
24-
public int peek() {
25-
while (!oldStack.isEmpty()) {
26-
newStack.push(oldStack.pop());
27-
}
28-
return newStack.peek();
25+
return num;
26+
}
27+
28+
/** Get the front element. */
29+
public int peek() {
30+
while (!stack.isEmpty()) {
31+
revStack.push(stack.pop());
2932
}
30-
31-
/** Returns whether the queue is empty. */
32-
public boolean empty() {
33-
return oldStack.isEmpty() && newStack.isEmpty();
33+
int num = revStack.peek();
34+
while (!revStack.isEmpty()) {
35+
stack.push(revStack.pop());
3436
}
37+
return num;
38+
}
39+
40+
/** Returns whether the queue is empty. */
41+
public boolean empty() {
42+
return stack.isEmpty();
43+
}
3544
}
3645

3746
/**

0 commit comments

Comments
 (0)