Here I would be sharing the code I wrote for the problems posted on LeetCode.
Most problems took time for me to write the working code & optimize that. So I am giving those here as I solve Hard/Medium problems on LeetCode. I think, like me, many people would be able to understand the problems & come up with possible solution quickly, but during implementation are lost in the middle. So check the code given here & get the idea & write your own code your own way...Cheers!
238. Product of Array Except Self(Medium)
class Solution {
public int[] productExceptSelf(int[] nums) {
int dpl[] = new int[nums.length];
dpl[0] = 1;
for (int i = 1; i < nums.length; i++) {
dpl[i] = dpl[i-1] * nums[i-1];
}
int right = 1;
for (int j = nums.length - 1; j >=0; j--) {
dpl[j] = dpl[j] * right;
right *= nums[j];
}
return dpl;
}
}
class Solution {
public int[] productExceptSelf(int[] nums) {
int dpl[] = new int[nums.length];
dpl[0] = 1;
for (int i = 1; i < nums.length; i++) {
dpl[i] = dpl[i-1] * nums[i-1];
}
int right = 1;
for (int j = nums.length - 1; j >=0; j--) {
dpl[j] = dpl[j] * right;
right *= nums[j];
}
return dpl;
}
}
442. Find All Duplicates in an Array(Medium)
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList();
int[] map = new int[nums.length + 1];
for (int e : nums) {
map[e]++;
}
for (int i = 1; i <= nums.length; i++) {
if (map[i] == 2) {
res.add(i);
}
}
return res;
}
}
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList();
int[] map = new int[nums.length + 1];
for (int e : nums) {
map[e]++;
}
for (int i = 1; i <= nums.length; i++) {
if (map[i] == 2) {
res.add(i);
}
}
return res;
}
}
262. Trips and Users(Hard)
select total.request_at Day,
case
when cancel.cancels is null
then 0.00
else round(cancel.cancels/total.total, 2)
end "Cancellation Rate"
from
(select filter.request_at,count(*) cancels from
(select t.client_id,t.status,t.request_at from trips t inner join users u
on u.Banned='No' and u.users_id = t.client_id and (status = 'cancelled_by_driver' or status = 'cancelled_by_client')
and t.request_at between '2013-10-01' and'2013-10-03') filter
group by filter.request_at) cancel right outer join
(select filter.request_at,count(*) Total from
(select t.client_id,t.status,t.request_at from trips t, users u where u.Banned='No' and u.users_id = t.client_id and t.request_at between '2013-10-01' and'2013-10-03') filter
group by filter.request_at) total
on total.request_at = cancel.request_at
order by total.request_at
select total.request_at Day,
case
when cancel.cancels is null
then 0.00
else round(cancel.cancels/total.total, 2)
end "Cancellation Rate"
from
(select filter.request_at,count(*) cancels from
(select t.client_id,t.status,t.request_at from trips t inner join users u
on u.Banned='No' and u.users_id = t.client_id and (status = 'cancelled_by_driver' or status = 'cancelled_by_client')
and t.request_at between '2013-10-01' and'2013-10-03') filter
group by filter.request_at) cancel right outer join
(select filter.request_at,count(*) Total from
(select t.client_id,t.status,t.request_at from trips t, users u where u.Banned='No' and u.users_id = t.client_id and t.request_at between '2013-10-01' and'2013-10-03') filter
group by filter.request_at) total
on total.request_at = cancel.request_at
order by total.request_at
1250. Check If It Is a Good Array(Hard)
import java.util.ArrayList;
class Solution {
public boolean isGoodArray(int[] nums) {
int x= nums[0],y;
for (int num : nums) {
while (num>0){
y=x%num;
x=num;
num=y;
}
if(x==1){
return true;
}
}
return false;
}
}
import java.util.ArrayList;
class Solution {
public boolean isGoodArray(int[] nums) {
int x= nums[0],y;
for (int num : nums) {
while (num>0){
y=x%num;
x=num;
num=y;
}
if(x==1){
return true;
}
}
return false;
}
}
239. Sliding Window Maximum(Hard)
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(k == 0) {
return new int[0];
} else if(k == 1)
return nums;
int len = nums.length, maxes = 0, i = 1;
for(; i < k; i++) {
if(nums[maxes] <= nums[i])
maxes = i;
}
nums[0] = nums[maxes];
for(; i < len; i++) {
if(maxes > i-k) {
if(nums[maxes] <= nums[i])
maxes = i;
nums[i-k+1] = nums[maxes];
} else {
maxes = i-k+1;
for(int m = maxes+1; m <= i; m++) {
if(nums[maxes] <= nums[m])
maxes = m;
}
nums[i-k+1] = nums[maxes];
}
}
return Arrays.copyOf(nums, len-k+1);
}
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(k == 0) {
return new int[0];
} else if(k == 1)
return nums;
int len = nums.length, maxes = 0, i = 1;
for(; i < k; i++) {
if(nums[maxes] <= nums[i])
maxes = i;
}
nums[0] = nums[maxes];
for(; i < len; i++) {
if(maxes > i-k) {
if(nums[maxes] <= nums[i])
maxes = i;
nums[i-k+1] = nums[maxes];
} else {
maxes = i-k+1;
for(int m = maxes+1; m <= i; m++) {
if(nums[maxes] <= nums[m])
maxes = m;
}
nums[i-k+1] = nums[maxes];
}
}
return Arrays.copyOf(nums, len-k+1);
}