Skip to content

Commit

Permalink
Merge pull request #12 from rajat19/test-aug-24
Browse files Browse the repository at this point in the history
Add questions
  • Loading branch information
rajat19 authored Aug 24, 2024
2 parents c7bce47 + ff544c4 commit 66dd87c
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 2 deletions.
23 changes: 23 additions & 0 deletions _includes/code/edit-distance/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int minDistance(string word1, string word2) {
int n1 = word1.length(), n2 = word2.length();
vector<vector<int>> dp(n1+1, vector<int>(n2+1, 0));
for (int i = 1; i <= n1; i++) {
dp[i][0] = i;
}
for (int j = 1; j <= n2; j++) {
dp[0][j] = j;
}
for(int i=1; i<=n1; i++) {
for(int j=1; j<=n2; j++) {
if (word1[i-1] != word2[j-1]) {
dp[i][j] = 1 + min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]));
} else {
dp[i][j] = dp[i-1][j-1];
}
}
}
return dp[n1][n2];
}
};
22 changes: 22 additions & 0 deletions _includes/code/longest-cycle-in-graph/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int longestCycle(int[] edges) {
int longestCycleLen = -1;
int timeStep = 1;
int[] nodeVisitedAtTime = new int[edges.length];

for (int currentNode = 0; currentNode < edges.length; ++currentNode) {
if (nodeVisitedAtTime[currentNode] > 0)
continue;
final int startTime = timeStep;
int u = currentNode;
while (u != -1 && nodeVisitedAtTime[u] == 0) {
nodeVisitedAtTime[u] = timeStep++;
u = edges[u];
}
if (u != -1 && nodeVisitedAtTime[u] >= startTime)
longestCycleLen = Math.max(longestCycleLen, timeStep - nodeVisitedAtTime[u]);
}

return longestCycleLen;
}
}
30 changes: 30 additions & 0 deletions _includes/code/minimum-cost-for-tickets/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
int[] days, costs, durations, memo;
public int mincostTickets(int[] days, int[] costs) {
this.days = days;
this.costs = costs;
this.durations = new int[]{1, 7, 30};
this.memo = new int[days.length];
Arrays.fill(memo, -1);
return dp(0);
}

private int dp(int i) {
if (i >= days.length) {
return 0;
}
if (memo[i] != -1) {
return memo[i];
}
int ans = Integer.MAX_VALUE;
int j = i;
for(int k=0; k<3; k++) {
while(j < days.length && days[j] < days[i] + durations[k]) {
j++;
}
ans = Math.min(ans, dp(j) + costs[k]);
}
memo[i] = ans;
return ans;
}
}
52 changes: 52 additions & 0 deletions _includes/code/sort-list/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode *slow = head, *fast = head;
while(fast->next != nullptr && fast->next->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
ListNode *next = slow->next;
slow->next = nullptr;
ListNode *first = sortList(head);
ListNode *second = sortList(next);
return merge(first, second);
}

ListNode* merge(ListNode* first, ListNode* second) {
ListNode *prev = new ListNode();
ListNode *head = prev;
while(first != nullptr || second != nullptr) {
if (first == nullptr) {
prev->next = second;
second = second->next;
}
else if (second == nullptr) {
prev->next = first;
first = first->next;
} else {
if (first->val < second->val) {
prev->next = first;
first = first->next;
} else {
prev->next = second;
second = second->next;
}
}
prev = prev->next;
prev->next = nullptr;
}
return head->next;
}
};
Binary file added assets/img/code/longest-cycle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion posts/_hard/edit-distance.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post
author: Rajat Srivastava
title: Edit Distance
topics: dynamic-programming string
langs: java
langs: java cpp
tc: O(nm)
sc: O(nm)
leetid: 72
Expand Down
40 changes: 40 additions & 0 deletions posts/_hard/longest-cycle-in-graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: post
author: Rajat Srivastava
title: Longest Cycle in Graph
topics: depth-first-search graph topological-sort
langs: java
tc: O(nm)
sc: O(nm)
leetid: 2360
---

You are given a directed graph of n nodes numbered from `0` to `n - 1`, where each node has at most one outgoing edge.

The graph is represented with a given **0-indexed** array edges of size `n`, indicating that there is a directed edge from node `i` to node `edges[i]`. If there is no outgoing edge from node `i`, then `edges[i] == -1`.

Return the *length of the longest cycle in the graph*. If no cycle exists, return `-1`.

A cycle is a path that starts and ends at the same node.

---

## Test Cases

**Example 1:**

![Screenshot](assets/img/code/longest-cycle.png)
```
Input: edges = [3,3,4,2,3]
Output: 3
Explanation:
The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.
The length of this cycle is 3, so 3 is returned.
```

**Example 2:**
```
Input: edges = [2,-1,3,1]
Output: -1
Explanation: There are no cycles in this graph.
```
47 changes: 47 additions & 0 deletions posts/_medium/minimum-cost-for-tickets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
layout: post
author: Rajat Srivastava
title: Minimum Cost for Tickets
topics: array dynamic-programming
langs: java
tc: O(N)
sc: O(N)
leetid: 983
---

You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array days. Each day is an integer from `1` to `365`.

Train tickets are sold in three different ways:

a **1-day** pass is sold for `costs[0]` dollars,
a **7-day** pass is sold for `costs[1]` dollars, and
a **30-day** pass is sold for `costs[2]` dollars.
The passes allow that many days of consecutive travel.

For example, if we get a 7-day pass on day `2`, then we can travel for 7 days: `2, 3, 4, 5, 6, 7, and 8`.
Return the _minimum number of dollars you need to travel every day in the given list of days._

---

## Test Cases

**Example 1:**
```
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation: For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total, you spent $11 and covered all the days of your travel.
```

**Example 2:**
```
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation: For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total, you spent $17 and covered all the days of your travel.
```
2 changes: 1 addition & 1 deletion posts/_medium/sort-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: post
author: Rajat Srivastava
title: Sort List
topics: divide-and-conquer linked-list sorting two-pointers
langs: java
langs: java cpp
tc: O(nlogn)
sc: O(1)
leetid: 148
Expand Down
2 changes: 2 additions & 0 deletions scripts/download/leetcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,7 @@ def generate_question(self):
tc = input('Time complexity: ')
sc = input('Space complexity: ')
languages = input('Available languages (default java): ')
if len(languages) == 0:
languages = 'java'
leetcode = Leetcode(ques, tc, sc, languages)
leetcode.generate_question()
56 changes: 56 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

usage() {
# Function: Print a help message.
echo "
Usage:
sh test.sh [ -p POST] [ -l LANGUAGE]
" 1>&2
}

while getopts p:l:h option
do
case "${option}"
in
p) POST=${OPTARG};;
l) LANGUAGE=${OPTARG};;
h | *) usage exit 1 ;;
esac
done

if [ "$LANGUAGE" ]
then
echo "Checking solution for $POST in $LANGUAGE language"
cd _includes/code/"$POST" || exit
case $LANGUAGE in
"c")
gcc solution.c -o solution.out
./solution.out
;;
"cpp")
g++ solution.cpp -o solution.out
./solution.out
;;
"go")
go build solution.go
;;
"java")
javac solution.java
java Solution
;;
"js")
node solution.js
;;
"py")
python3 solution.py
;;
"php")
php solution.php
;;
"rb")
ruby solution.rb
;;
*)
echo "Unknown language to test"
esac
fi

0 comments on commit 66dd87c

Please sign in to comment.