Skip to content

Commit 4b7c413

Browse files
authored
Added tasks 155-221
1 parent 073885f commit 4b7c413

File tree

21 files changed

+787
-0
lines changed

21 files changed

+787
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design
2+
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
3+
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2024_09_10_Time_0_ms_(100.00%)_Space_5.9_MB_(71.62%)
4+
5+
pub struct MinStack {
6+
current_node: Option<Box<Node>>,
7+
}
8+
9+
/**
10+
* `&self` means the method takes an immutable reference.
11+
* If you need a mutable reference, change it to `&mut self` instead.
12+
*/
13+
struct Node {
14+
min: i32,
15+
data: i32,
16+
next_node: Option<Box<Node>>,
17+
previous_node: Option<Box<Node>>,
18+
}
19+
20+
impl Node {
21+
fn new(min: i32, data: i32, previous_node: Option<Box<Node>>, next_node: Option<Box<Node>>) -> Self {
22+
Node {
23+
min,
24+
data,
25+
next_node,
26+
previous_node,
27+
}
28+
}
29+
}
30+
31+
impl MinStack {
32+
pub fn new() -> Self {
33+
MinStack { current_node: None }
34+
}
35+
36+
pub fn push(&mut self, val: i32) {
37+
match &self.current_node {
38+
None => {
39+
self.current_node = Some(Box::new(Node::new(val, val, None, None)));
40+
}
41+
Some(current) => {
42+
let min = std::cmp::min(current.min, val);
43+
let previous_node = self.current_node.take();
44+
self.current_node = Some(Box::new(Node::new(min, val, previous_node, None)));
45+
}
46+
}
47+
}
48+
49+
pub fn pop(&mut self) {
50+
if let Some(current) = self.current_node.take() {
51+
self.current_node = current.previous_node;
52+
}
53+
}
54+
55+
pub fn top(&self) -> i32 {
56+
if let Some(current) = &self.current_node {
57+
return current.data;
58+
}
59+
panic!("Stack is empty!");
60+
}
61+
62+
pub fn get_min(&self) -> i32 {
63+
if let Some(current) = &self.current_node {
64+
return current.min;
65+
}
66+
panic!("Stack is empty!");
67+
}
68+
}
69+
70+
/**
71+
* Your MinStack object will be instantiated and called as such:
72+
* let obj = MinStack::new();
73+
* obj.push(val);
74+
* obj.pop();
75+
* let ret_3: i32 = obj.top();
76+
* let ret_4: i32 = obj.get_min();
77+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
155\. Min Stack
2+
3+
Easy
4+
5+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
6+
7+
Implement the `MinStack` class:
8+
9+
* `MinStack()` initializes the stack object.
10+
* `void push(int val)` pushes the element `val` onto the stack.
11+
* `void pop()` removes the element on the top of the stack.
12+
* `int top()` gets the top element of the stack.
13+
* `int getMin()` retrieves the minimum element in the stack.
14+
15+
**Example 1:**
16+
17+
**Input**
18+
19+
["MinStack","push","push","push","getMin","pop","top","getMin"]
20+
[[],[-2],[0],[-3],[],[],[],[]]
21+
22+
**Output:** [null,null,null,null,-3,null,0,-2]
23+
24+
**Explanation:**
25+
26+
MinStack minStack = new MinStack();
27+
minStack.push(-2);
28+
minStack.push(0);
29+
minStack.push(-3);
30+
minStack.getMin(); // return -3
31+
minStack.pop();
32+
minStack.top(); // return 0
33+
minStack.getMin(); // return -2
34+
35+
**Constraints:**
36+
37+
* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code>
38+
* Methods `pop`, `top` and `getMin` operations will always be called on **non-empty** stacks.
39+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `push`, `pop`, `top`, and `getMin`.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
2+
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
3+
// #Big_O_Time_O(n)_Space_O(1) #2024_09_10_Time_1_ms_(82.64%)_Space_2.5_MB_(18.91%)
4+
5+
impl Solution {
6+
pub fn majority_element(arr: Vec<i32>) -> i32 {
7+
let mut count = 1;
8+
let mut majority = arr[0];
9+
10+
// For Potential Majority Element
11+
for &num in arr.iter().skip(1) {
12+
if num == majority {
13+
count += 1;
14+
} else {
15+
if count > 1 {
16+
count -= 1;
17+
} else {
18+
majority = num;
19+
}
20+
}
21+
}
22+
23+
// For Confirmation
24+
count = 0;
25+
for &num in arr.iter() {
26+
if num == majority {
27+
count += 1;
28+
}
29+
}
30+
31+
if count >= (arr.len() / 2) + 1 {
32+
majority
33+
} else {
34+
-1
35+
}
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
169\. Majority Element
2+
3+
Easy
4+
5+
Given an array `nums` of size `n`, return _the majority element_.
6+
7+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,2,3]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [2,2,1,1,1,2,2]
18+
19+
**Output:** 2
20+
21+
**Constraints:**
22+
23+
* `n == nums.length`
24+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
25+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
26+
27+
**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
2+
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
3+
// #2024_09_10_Time_3_ms_(96.69%)_Space_3.6_MB_(76.46%)
4+
5+
impl Solution {
6+
pub fn rotate(nums: &mut Vec<i32>, k: i32) {
7+
let mut rotated = vec![0; nums.len()];
8+
let k_fixed = k as usize % nums.len();
9+
10+
for (i, item) in nums.iter().enumerate() {
11+
match nums.get(i+k_fixed) {
12+
Some(n) => rotated[i+k_fixed] = *item,
13+
None => rotated[(i+k_fixed)-nums.len()] = *item,
14+
}
15+
}
16+
nums.copy_from_slice(&rotated)
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
189\. Rotate Array
2+
3+
Medium
4+
5+
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3,4,5,6,7], k = 3
10+
11+
**Output:** [5,6,7,1,2,3,4]
12+
13+
**Explanation:**
14+
15+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
16+
17+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
18+
19+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [-1,-100,3,99], k = 2
24+
25+
**Output:** [3,99,-1,-100]
26+
27+
**Explanation:**
28+
29+
rotate 1 steps to the right: [99,-1,-100,3]
30+
31+
rotate 2 steps to the right: [3,99,-1,-100]
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
37+
* <code>0 <= k <= 10<sup>5</sup></code>
38+
39+
**Follow up:**
40+
41+
* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem.
42+
* Could you do it in-place with `O(1)` extra space?
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2+
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3
3+
// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
4+
// #2024_09_10_Time_0_ms_(100.00%)_Space_2.1_MB_(31.95%)
5+
6+
impl Solution {
7+
pub fn rob(nums: Vec<i32>) -> i32 {
8+
if nums.is_empty() {
9+
return 0;
10+
}
11+
if nums.len() == 1 {
12+
return nums[0];
13+
}
14+
if nums.len() == 2 {
15+
return std::cmp::max(nums[0], nums[1]);
16+
}
17+
18+
let mut profit = vec![0; nums.len()];
19+
profit[0] = nums[0];
20+
profit[1] = std::cmp::max(nums[0], nums[1]);
21+
22+
for i in 2..nums.len() {
23+
profit[i] = std::cmp::max(profit[i - 1], nums[i] + profit[i - 2]);
24+
}
25+
26+
profit[nums.len() - 1]
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
198\. House Robber
2+
3+
Medium
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
6+
7+
Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3,1]
12+
13+
**Output:** 4
14+
15+
**Explanation:** Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [2,7,9,3,1]
20+
21+
**Output:** 12
22+
23+
**Explanation:** Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
24+
25+
**Constraints:**
26+
27+
* `1 <= nums.length <= 100`
28+
* `0 <= nums[i] <= 400`
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Depth_First_Search
2+
// #Breadth_First_Search #Matrix #Union_Find
3+
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
4+
// #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph
5+
// #Big_O_Time_O(M*N)_Space_O(M*N) #2024_09_10_Time_7_ms_(86.79%)_Space_9.1_MB_(42.14%)
6+
7+
impl Solution {
8+
pub fn num_islands(grid: Vec<Vec<char>>) -> i32 {
9+
let mut num_of_islands: i32 = 0;
10+
let mut current_index: i32 = 1;
11+
let height = grid.len();
12+
let width = grid[0].len();
13+
let mut part_of_island = vec![0; width * height];
14+
let mut pos;
15+
16+
for current in 0..height * width {
17+
let x = current / width;
18+
let y = current % width;
19+
20+
if grid[x][y] == '1' {
21+
let left = if y > 0 {
22+
part_of_island[current - 1]
23+
} else {
24+
0
25+
};
26+
27+
let up = if x > 0 {
28+
part_of_island[current - width]
29+
} else {
30+
0
31+
};
32+
33+
if up > 1 {
34+
if left > 1 && left != up {
35+
for i in 0..width {
36+
pos = part_of_island[current - i];
37+
if pos == left {
38+
part_of_island[current - i] = up;
39+
}
40+
}
41+
num_of_islands -= 1;
42+
}
43+
part_of_island[current] = up;
44+
continue;
45+
} else if left > 1 {
46+
part_of_island[current] = left;
47+
continue;
48+
}
49+
50+
current_index += 1;
51+
num_of_islands += 1;
52+
part_of_island[current] = current_index;
53+
}
54+
}
55+
56+
num_of_islands
57+
}
58+
}
59+

0 commit comments

Comments
 (0)