Another Trip to the Hospital, more free time to research code!
This week, I ended up in the hospital due to my kidney stone again. While this did slow down my physical coding, it didn’t slow down my learning! While I was sitting in the bed, I decided to research Java for an upcoming interview I have for a company out in Nashville, TN where my girlfriend and I would LOVE to relocate. They know my Java knowledge is limited, but that won’t stop me from trying to get ahead of the game and impress them in my first interview with what I have picked up from our first chat.
I did some Java LeetCode with my friend, who is a developer that focuses on Java. My learning definitely wasn’t completely there, but I did get to see how some of it works to gain familiarity before I start my intro courses to Java.
My first Leetcode was removing subsequent palindromes, which was my first look at Java so my understanding was somewhat lacking.
#Remove Palindrome Subsequences(JAVA)
class Solution {
public int removePalindromeSub(String s) {
String rev = new StringBuilder(s).reverse().toString();
if(rev.equals(s)){
return 1;
}
return 2;
}
}
In this problem, we created a new string, where we reversed it, and if the reversed portion was equal to the initial string, then we returned 1, otherwise we returned 2, which was the condition to the problem.
My next Leetcode was finding the minimum value for a positive sum.
#Min Value for Positive(JAVA)
class Solution {
public int minStartValue(int[] nums) {
int min = 0;
int sum = 0;
for(int num: nums) {
sum += num;
min = Math.min(min, sum);
}
return 1 - min;
}
}
In this, it is exactly as I explained before. We set the min to 0, and sum to 0, and we looped through the different numbers to find all possibilities to match our “min” of 0, meaning all of which are positive.
Next, was finding the maximum balls in a box. This one I definitely got lost in, but that was my friends goal in this, was to overwhelm me.
class Solution {
public int countBalls(int lowLimit, int highLimit) {
Map<Integer, Integer> map = new HashMap<>();
int res = 0;
for (int i = lowLimit; i<= highLimit; i++) {
int d = digitSum(i);
map.put(d, map.getOrDefault(d, 0) + 1);
res = Math.max(res, map.get(d));
}
return res;
}
public int digitSum(int num) {
int sum = 0;
while (num != 0) {
sum += (num % 10);
num /= 10;
}
return sum;
}
}
In this problem, we mapped through each integer, but that is where I got slightly lost in it. I do understand the math functions at the bottom, but without understanding the original countBalls, it is hard to explain. He said that once I take my intro classes, to come back and try to explain this better to show myself how I have progressed. I personally, enjoy this type of learning.
Last, but not least, we did the Sum of 1d Arrays, which we added each input of the array, and saved each output each time, so for the given array [1,2,3,4] our output would be [1,3,6,10], representing [1, 1+2, 1+2+3, 1+2+3+4]. This was a relatively simple one as we just iterated through each number in the array, and added them together and returned them.
class Solution {
public int[] runningSum(int[] nums) {
for (int i = 1; i < nums.length; i++) {
nums[i] = nums[i] + nums[i - 1];
}
return nums;
}
}