Sign in

Reverse bits of a given 32 bits unsigned integer.

Since Java doesn’t have unsigned numbers, this task becomes a way more interesting. The simplest way would be to operate with bits, I’ve added a bunch of comments to the code to make it clear

public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
// shift to the left, i.e. ...001 is changed into ...010
res <<= 1;
// n&1 - keep 1 at the end of n if it ends with 1
// | if n ends with 1, then res is also ends with 1
res = res | (n & 1);
n >>= 1; // shift n to the right for 1 position
}
return res;

}
}

Not so hard for people familiar with streaming processing: You are given an array of integers nums, there is a sliding window of size kwhich is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time…


To my surprise, brute force solution worked for this task. The idea to combine all possible topping costs in a form of set and then combine topping costs with base costs and simply select the closest one.

class Solution {
// [4,5]
// 0,4,5,4*4…

According to leetcode tags Jump Game VI can land your dream job at either AQR Capital Management or Uber:)

My first attempt was to simply apply dynamic programming and dp[i] has the max score possible to achieve on that step. To initialize dp[i], we calculate: max{dp[i-k]+nums[i], dp[i-k+1]+nums[i],…dp[i-1]+nums[i]}

In code it…


I wasn’t able to come up with better idea than brute force, surprisingly it worked. The idea of solution for this task is the foloowing:

  1. From every empty piece of land, let’s try to reach all building and calculate a distance.
  2. When we calculating a distance, I imagine that I…

Indeed interesting task!

Photo by Capturing the human heart. on Unsplash

The solution would be starting with city 1 and assigning this city province 1, go recursevely to all cities this city is connected to and assign province to all of them.

class Solution {
public int findCircleNum(int[][] isConnected) {
int[] clusters = new int[isConnected.length+1];
Arrays.fill(clusters, -1);
int nextCluster = 0;
for(int i = 0; i < isConnected.length; i++) {
if(clusters[i+1]==-1){
++nextCluster;
visit(isConnected, clusters, i, nextCluster);
}

}
return nextCluster;
}

void visit(int[][] isConnected, int[] clusters, int i, int nextCluster) {
clusters[i+1]=nextCluster;
for(int j = 0; j < isConnected[0].length; j++) {
if(isConnected[i][j]==0) continue;
if(clusters[j+1]==-1) visit(isConnected, clusters, j, nextCluster);
}
}
}

Excellent performance lesson I’ve got today from leetcode challenge: I’ve created correct algo to solve 1770. Maximum Score from Performing Multiplication Operations during context but wasn’t able to get it to pass Time Limit during the context, no score for me today :(

My solution was based on simple backtracking…


Another kind of leetcode tasks without proper description: when I read the minimum area of a rectangle formed from these points my imagination built a rectangle that covers all these points. However, in reality, the task meant minimum rectangle that can be formed from any of these points!

After struggling…


One of the biggest mysteries in the world: how leetcode assigns a level to the task — please comment if you have an insight.

This one is a classic example of such question: there are a lot of similar tasks with Medium level. …


This is a kind of task that real leetcoder has a hard time to deal with :) Instead of academic algorithms this is more focused on understanding how real computers work. Meet The Dining Philosophers https://leetcode.com/problems/the-dining-philosophers/. The actual problem is very old, solved in real OSs and described in Operating…

Kostya

Java, start-up, hiking, photos, bicycle,journey

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store