Skip to content

Commit 65890ce

Browse files
authored
Merge pull request #2629 from shivnandk/main
Added Solution of Dijkastra using set
2 parents 7e90b21 + 624a89e commit 65890ce

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: power-of-three
3+
title: Power of Three (LeetCode)
4+
sidebar_label: 0326-PowerOfThree
5+
---
6+
7+
## Problem Description
8+
9+
| Problem Statement | Solution Link |
10+
| :---------------- | :------------ |
11+
| [Power of Three](https://leetcode.com/problems/power-of-three/) | [Power of Three Solution on LeetCode](https://leetcode.com/problems/power-of-three/solutions/) |
12+
13+
## Problem Description
14+
15+
Given an integer `n`, return `true` if it is a power of three. Otherwise, return `false`.
16+
17+
An integer `n` is a power of three if there exists an integer `x` such that `n == 3^x`.
18+
19+
### Examples
20+
21+
#### Example 1:
22+
23+
- **Input:** n = 27
24+
- **Output:** true
25+
26+
#### Example 2:
27+
28+
- **Input:** n = 0
29+
- **Output:** false
30+
31+
#### Example 3:
32+
33+
- **Input:** n = 9
34+
- **Output:** true
35+
36+
#### Example 4:
37+
38+
- **Input:** n = 45
39+
- **Output:** false
40+
41+
### Constraints:
42+
43+
- $-2^{31} <= n <= 2^{31} - 1$
44+
45+
## Approach
46+
47+
We can solve this problem by iterating and multiplying a variable starting from 3 until it exceeds or equals `n`. If at any point it equals `n`, we return `true`. If we exceed `n` without finding an exact match, we return `false`.
48+
49+
## Solution Code
50+
51+
#### C++
52+
53+
```cpp
54+
class Solution {
55+
public:
56+
bool isPowerOfThree(int n) {
57+
if(n == 1) {
58+
return true;
59+
}
60+
for(long long i = 3; i <= n; i *= 3) {
61+
if(i == n) {
62+
return true;
63+
}
64+
}
65+
return false;
66+
}
67+
};
68+
```
69+
70+
#### Python
71+
72+
```python
73+
class Solution:
74+
def isPowerOfThree(self, n: int) -> bool:
75+
if n == 1:
76+
return True
77+
i = 3
78+
while i <= n:
79+
if i == n:
80+
return True
81+
i *= 3
82+
return False
83+
```
84+
85+
#### Java
86+
87+
```java
88+
class Solution {
89+
public boolean isPowerOfThree(int n) {
90+
if (n == 1) {
91+
return true;
92+
}
93+
for (long i = 3; i <= n; i *= 3) {
94+
if (i == n) {
95+
return true;
96+
}
97+
}
98+
return false;
99+
}
100+
}
101+
```
102+
103+
#### JavaScript
104+
105+
```javascript
106+
var isPowerOfThree = function(n) {
107+
if (n === 1) {
108+
return true;
109+
}
110+
for (let i = 3; i <= n; i *= 3) {
111+
if (i === n) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
};
117+
```
118+
119+
## Conclusion
120+
121+
The "Power of Three" problem can be solved by iteratively checking powers of three. The provided solutions in C++, Python, Java, and JavaScript efficiently implement this approach, ensuring correct results within the given constraints.

dsa/Algorithms/Graph Algorithms/01-Dijkstra's Algorithm.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def dijkstra(graph, start):
134134
#include <vector>
135135
#include <queue>
136136
#include <unordered_map>
137+
#include <set>
137138
using namespace std;
138139

139140
typedef pair<int, int> pii;
@@ -170,6 +171,40 @@ unordered_map<int, int> dijkstra(unordered_map<int, vector<pii>>& graph, int sta
170171

171172
return distances;
172173
}
174+
175+
void shortDijkstra(int src, int n, vector<vector<pii>>& adjList) {
176+
vector<int> dist(n, INT_MAX);
177+
set<pii> st;
178+
179+
dist[src] = 0;
180+
st.insert({0, src});
181+
182+
while (!st.empty()) {
183+
auto mini = *st.begin();
184+
185+
int nodedistance = mini.first;
186+
int node = mini.second;
187+
188+
st.erase(st.begin());
189+
190+
for (auto nbr : adjList[node]) {
191+
int dis = nodedistance + nbr.second;
192+
if (dis < dist[nbr.first]) {
193+
auto result = st.find(make_pair(dist[nbr.first], nbr.first));
194+
if (result != st.end()) {
195+
st.erase(result);
196+
}
197+
dist[nbr.first] = dis;
198+
st.insert(make_pair(dist[nbr.first], nbr.first));
199+
}
200+
}
201+
}
202+
cout << "Printing ans" << endl;
203+
for (auto i : dist) {
204+
cout << i << " ";
205+
}
206+
}
207+
173208
```
174209
175210
### Java

0 commit comments

Comments
 (0)