Skip to content

Instantly share code, notes, and snippets.

@smothiki
Created January 7, 2024 17:19
Show Gist options
  • Save smothiki/fc912bc58a5f87631ec93faa3860a027 to your computer and use it in GitHub Desktop.
Save smothiki/fc912bc58a5f87631ec93faa3860a027 to your computer and use it in GitHub Desktop.
all permutations of N dice roll
def combine(self, n: int, k: int) -> List[List[int]]:
combs = []
def backtrack(index, path):
if len(path) == k:
combs.append(list(path))
return
for i in range(index, n+1):
path.append(i)
backtrack(i, path)
path.pop()
backtrack(1, [])
return combs
Given an integer matrix, find the length of the longest path that have same values. The matrix has no boundary limits. (like, Google Maps - see edit for context)
From each cell, you can either move to two directions: horizontal or vertical. You may NOT move diagonally or move outside of the boundary.
nums = [
[1,1],
[5,5],
[5,5]
]
Return 4 ( Four 5's).
Thoughts
I used DFS to traverse the grid while keeping tracking of the max of longest path with same values. This solved the problem for a boundary contrained matrix grid. I got rejection for it. I wonder if its because I couldnt solve the problem for an infinite grid, but for a 33 min interview i thought i did decent. (45mins - 15 min intro + problem solving). Is there a better alternative?
public class LongestPath {
public int longestPath;
public LongestPath() {
longestPath = 0;
}
public int getLongestPath(int[][] arr) {
boolean[][] visited = new boolean[arr.length][arr[0].length];
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
dfs(arr, visited, row, col, arr[row][col], 0);
}
}
return longestPath;
}
public void dfs(int[][] arr, boolean[][] visited, int row, int col, int target, int path) {
if (row < 0 || col < 0 || row >= arr.length || col >= arr[0].length || visited[row][col] || arr[row][col] != target) {
longestPath = Math.max(path, longestPath);
return;
}
if (arr[row][col] == target) {
path++;
}
visited[row][col] = true;
dfs(arr, visited, row + 1, col, target, path);
dfs(arr, visited, row, col + 1, target, path);
dfs(arr, visited, row - 1, col, target, path);
dfs(arr, visited, row, col - 1, target, path);
visited[row][col] = false;
}
}
Never met this before, please check:
Given a sequence of timestamps & actions of a dasher's activity within a day, we would like to know the active time of the dasher. Idle time is defined as the dasher has NO delivery at hand. (That means all items have been dropped off at this moment and the dasher is just waiting for another pickup) Active time equals total time minus idle time. Below is an example. Dropoff can only happen after pickup. 12:00am means midnight and 12:00pm means noon. All the time is within a day.
Timestamp(12h) | Action
8:30am | pickup
9:10am | dropoff
10:20am| pickup
12:15pm| pickup
12:45pm| dropoff
2:25pm | dropoff
total time = 2:25pm-8:30am = 355 mins;
idle time = 10:20am-9:10am = 70 mins;
active time = total time-idle time = 355-70 = 285 mins;
static int getActiveTime(ArrayList<Activity> seq) {
int start = 0, timeWorking = 0, count =0;
Activity lastPickupActivity = null;
for (Activity a: seq) {
if (a.activity == 'p') {
count--;
// if lastPickupActivity is null, then you know that this will be your first activity, so "start the timer"
if (lastPickupActivity == null) {
lastPickupActivity = a;
start = timeInMinutes(a.time);
}
}
else {
count++;
// when count==0, then you know that your pickups have matched your dropoffs, so, "stop the timer" and calculate the new 'timeWorking'
if (count == 0){
timeWorking += timeInMinutes(a.time) - start;
// set lastPickupActivity back to null so that you can know when to start the timer again.
lastPickupActivity = null;
}
}
}
return timeWorking;
}
static int timeInMinutes(String time) {
int indexOfColon = time.indexOf(':');
int hour = Integer.parseInt(time.substring(0, indexOfColon)) * 60;
int min = Integer.parseInt(time.substring(indexOfColon+1, indexOfColon+3));
if (time.contains("pm"))
hour += (60*12);
return hour + min;
}
static class Activity{
String time;
char activity; // 'p' for pickup, 'd' for dropoff
Activity(String time, char activity) {
this.time = time;
this.activity = activity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment