Skip to content

Commit 3f87403

Browse files
committed
optimise solutions
1 parent 228ede7 commit 3f87403

File tree

21 files changed

+603
-228
lines changed

21 files changed

+603
-228
lines changed

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/dynamic_programming/_53.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@ public int maxSubArray(int[] nums) {
3131

3232
return max;
3333
}
34+
35+
public int maxSubArray2(int[] nums) {
36+
int n = nums.length;
37+
int currSum = nums[0], maxSum = nums[0];
38+
39+
for (int i = 1; i < n; ++i) {
40+
currSum = Math.max(nums[i], currSum + nums[i]);
41+
maxSum = Math.max(maxSum, currSum);
42+
}
43+
return maxSum;
44+
}
3445
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_204.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public int countPrimes(int n) {
1616
boolean[] notPrime = new boolean[n];
1717
int count = 0;
1818
for (int i = 2; i < n; i++) {
19-
if (notPrime[i] == false) {
19+
if (!notPrime[i]) {
2020
count++;
2121
for (int j = 2; i * j < n; j++) {
2222
notPrime[i * j] = true;

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_412.java

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hit.basmath.interview.top_interview_questions.easy_collection.math;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.List;
56

67
/**
@@ -36,24 +37,100 @@
3637
*/
3738
public class _412 {
3839
public List<String> fizzBuzz(int n) {
39-
List<String> ret = new ArrayList<String>(n);
40-
for (int i = 1, fizz = 0, buzz = 0; i <= n; i++) {
41-
fizz++;
42-
buzz++;
43-
if (fizz == 3 && buzz == 5) {
44-
ret.add("FizzBuzz");
45-
fizz = 0;
46-
buzz = 0;
47-
} else if (fizz == 3) {
48-
ret.add("Fizz");
49-
fizz = 0;
50-
} else if (buzz == 5) {
51-
ret.add("Buzz");
52-
buzz = 0;
40+
// ans list
41+
List<String> ans = new ArrayList<String>();
42+
43+
for (int num = 1; num <= n; num++) {
44+
45+
boolean divisibleBy3 = (num % 3 == 0);
46+
boolean divisibleBy5 = (num % 5 == 0);
47+
48+
if (divisibleBy3 && divisibleBy5) {
49+
// Divides by both 3 and 5, add FizzBuzz
50+
ans.add("FizzBuzz");
51+
} else if (divisibleBy3) {
52+
// Divides by 3, add Fizz
53+
ans.add("Fizz");
54+
} else if (divisibleBy5) {
55+
// Divides by 5, add Buzz
56+
ans.add("Buzz");
5357
} else {
54-
ret.add(String.valueOf(i));
58+
// Not divisible by 3 or 5, add the number
59+
ans.add(Integer.toString(num));
60+
}
61+
}
62+
63+
return ans;
64+
}
65+
66+
public List<String> fizzBuzz2(int n) {
67+
// ans list
68+
List<String> ans = new ArrayList<String>();
69+
70+
for (int num = 1; num <= n; num++) {
71+
72+
boolean divisibleBy3 = (num % 3 == 0);
73+
boolean divisibleBy5 = (num % 5 == 0);
74+
75+
String numAnsStr = "";
76+
77+
if (divisibleBy3) {
78+
// Divides by 3, add Fizz
79+
numAnsStr += "Fizz";
80+
}
81+
82+
if (divisibleBy5) {
83+
// Divides by 5, add Buzz
84+
numAnsStr += "Buzz";
85+
}
86+
87+
if (numAnsStr.equals("")) {
88+
// Not divisible by 3 or 5, add the number
89+
numAnsStr += Integer.toString(num);
5590
}
91+
92+
// Append the current answer str to the ans list
93+
ans.add(numAnsStr);
5694
}
57-
return ret;
95+
96+
return ans;
97+
}
98+
99+
public List<String> fizzBuzz3(int n) {
100+
// ans list
101+
List<String> ans = new ArrayList<String>();
102+
103+
// Hash map to store all fizzbuzz mappings.
104+
HashMap<Integer, String> fizzBizzDict =
105+
new HashMap<Integer, String>() {
106+
private static final long serialVersionUID = 7966206953841139705L;
107+
{
108+
put(3, "Fizz");
109+
put(5, "Buzz");
110+
}
111+
};
112+
113+
for (int num = 1; num <= n; num++) {
114+
StringBuilder numAnsStr = new StringBuilder();
115+
116+
for (Integer key : fizzBizzDict.keySet()) {
117+
118+
// If the num is divisible by key,
119+
// then add the corresponding string mapping to current numAnsStr
120+
if (num % key == 0) {
121+
numAnsStr.append(fizzBizzDict.get(key));
122+
}
123+
}
124+
125+
if (numAnsStr.toString().equals("")) {
126+
// Not divisible by 3 or 5, add the number
127+
numAnsStr.append(Integer.toString(num));
128+
}
129+
130+
// Append the current answer str to the ans list
131+
ans.add(numAnsStr.toString());
132+
}
133+
134+
return ans;
58135
}
59136
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/array_and_strings/_238.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,36 @@
1818
*/
1919
public class _238 {
2020
public int[] productExceptSelf(int[] nums) {
21-
int n = nums.length;
22-
int[] res = new int[n];
23-
res[0] = 1;
24-
for (int i = 1; i < n; i++) {
25-
res[i] = res[i - 1] * nums[i - 1];
21+
// The length of the input array
22+
int length = nums.length;
23+
24+
// Final answer array to be returned
25+
int[] answer = new int[length];
26+
27+
// answer[i] contains the product of all the elements to the left
28+
// Note: for the element at index '0', there are no elements to the left,
29+
// so the answer[0] would be 1
30+
answer[0] = 1;
31+
for (int i = 1; i < length; i++) {
32+
33+
// answer[i - 1] already contains the product of elements to the left of 'i - 1'
34+
// Simply multiplying it with nums[i - 1] would give the product of all
35+
// elements to the left of index 'i'
36+
answer[i] = nums[i - 1] * answer[i - 1];
2637
}
27-
int right = 1;
28-
for (int i = n - 1; i >= 0; i--) {
29-
res[i] *= right;
30-
right *= nums[i];
38+
39+
// R contains the product of all the elements to the right
40+
// Note: for the element at index 'length - 1', there are no elements to the right,
41+
// so the R would be 1
42+
int R = 1;
43+
for (int i = length - 1; i >= 0; i--) {
44+
45+
// For the index 'i', R would contain the
46+
// product of all elements to the right. We update R accordingly
47+
answer[i] = answer[i] * R;
48+
R *= nums[i];
3149
}
32-
return res;
50+
51+
return answer;
3352
}
3453
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/array_and_strings/_239.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,45 @@
3333
* Could you solve it in linear time?
3434
*/
3535
public class _239 {
36+
3637
public int[] maxSlidingWindow(int[] nums, int k) {
37-
if (nums == null || k <= 0) {
38-
return new int[0];
38+
int n = nums.length;
39+
if (n * k == 0) return new int[0];
40+
41+
int [] output = new int[n - k + 1];
42+
for (int i = 0; i < n - k + 1; i++) {
43+
int max = Integer.MIN_VALUE;
44+
for(int j = i; j < i + k; j++)
45+
max = Math.max(max, nums[j]);
46+
output[i] = max;
3947
}
48+
return output;
49+
}
50+
51+
public int[] maxSlidingWindow2(int[] nums, int k) {
4052
int n = nums.length;
41-
int[] r = new int[n - k + 1];
42-
int ri = 0;
43-
// store index
44-
Deque<Integer> q = new ArrayDeque<>();
45-
for (int i = 0; i < nums.length; i++) {
46-
// remove numbers out of range k
47-
while (!q.isEmpty() && q.peek() < i - k + 1) {
48-
q.poll();
49-
}
50-
// remove smaller numbers in k range as they are useless
51-
while (!q.isEmpty() && nums[q.peekLast()] < nums[i]) {
52-
q.pollLast();
53-
}
54-
// q contains index... r contains content
55-
q.offer(i);
56-
if (i >= k - 1) {
57-
r[ri++] = nums[q.peek()];
58-
}
53+
if (n * k == 0) return new int[0];
54+
if (k == 1) return nums;
55+
56+
int [] left = new int[n];
57+
left[0] = nums[0];
58+
int [] right = new int[n];
59+
right[n - 1] = nums[n - 1];
60+
for (int i = 1; i < n; i++) {
61+
// from left to right
62+
if (i % k == 0) left[i] = nums[i]; // block_start
63+
else left[i] = Math.max(left[i - 1], nums[i]);
64+
65+
// from right to left
66+
int j = n - i - 1;
67+
if ((j + 1) % k == 0) right[j] = nums[j]; // block_end
68+
else right[j] = Math.max(right[j + 1], nums[j]);
5969
}
60-
return r;
70+
71+
int [] output = new int[n - k + 1];
72+
for (int i = 0; i < n - k + 1; i++)
73+
output[i] = Math.max(left[i + k - 1], right[i]);
74+
75+
return output;
6176
}
6277
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/backtracking/_131.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,30 @@
2121
* ]
2222
*/
2323
public class _131 {
24-
private List<List<String>> resultLst;
25-
private ArrayList<String> currLst;
24+
private List<List<String>> resultList;
25+
private ArrayList<String> currList;
2626

2727
public List<List<String>> partition(String s) {
28-
resultLst = new ArrayList<List<String>>();
29-
currLst = new ArrayList<String>();
28+
resultList = new ArrayList<List<String>>();
29+
currList = new ArrayList<String>();
3030
backTrack(s, 0);
31-
return resultLst;
31+
return resultList;
3232
}
3333

3434
private void backTrack(String s, int l) {
35-
if (currLst.size() > 0 //the initial str could be palindrome
35+
if (currList.size() > 0 //the initial str could be palindrome
3636
&& l >= s.length()) {
37-
List<String> r = (ArrayList<String>) currLst.clone();
38-
resultLst.add(r);
37+
List<String> r = (ArrayList<String>) currList.clone();
38+
resultList.add(r);
3939
}
4040
for (int i = l; i < s.length(); i++) {
4141
if (isPalindrome(s, l, i)) {
4242
if (l == i)
43-
currLst.add(Character.toString(s.charAt(i)));
43+
currList.add(Character.toString(s.charAt(i)));
4444
else
45-
currLst.add(s.substring(l, i + 1));
45+
currList.add(s.substring(l, i + 1));
4646
backTrack(s, i + 1);
47-
currLst.remove(currLst.size() - 1);
47+
currList.remove(currList.size() - 1);
4848
}
4949
}
5050
}

0 commit comments

Comments
 (0)