-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,43 @@ | ||
<script type="text/javascript"> | ||
//fetch("{{include.url}}") | ||
fetch("https://raw.githubusercontent.com/anoopkini/{{include.repo}}/main/{{include.filepath}}") | ||
.then(response => response.text()) | ||
.then(data => document.getElementById('code').textContent = data) | ||
|
||
function toggleCodeWrapper(){ | ||
document.getElementById("code-wrapper").style.display="block"; | ||
document.getElementById("code-toggle").style.display="none"; | ||
} | ||
</script> | ||
<div class="language-{{include.highlight}} highlighter-rouge"> | ||
<div class="highlight"><pre class="highlight"> | ||
<a id="ghLink" href="https://github.com/anoopkini/{{include.repo}}/blob/main/{{include.filepath}}" target="_blank" style="float:right;padding:5px;margin:2px;background: #fff;font-size: x-small;border-radius: 5px;">GitHub</a><a id="ghLink" href="{{include.leetcodeurl}}" target="_blank" style="float:right;padding:5px;margin:2px;background: #fff;font-size: x-small;border-radius: 5px;">Leetcode</a> | ||
<code><div id="code"></div></code></pre> | ||
<style> | ||
.ghLink, .lcLink { | ||
float:right; | ||
padding:5px; | ||
background-color: #fff; | ||
font-size: x-small; | ||
border-radius: 5px; | ||
margin:2px; | ||
} | ||
#code-toggle{ | ||
border:1px solid #333; | ||
border-radius: 2px; | ||
padding:5px; | ||
border-bottom: 5px solid #333; | ||
border-right: 5px solid #333; | ||
text-decoration: none; | ||
cursor: pointer; | ||
} | ||
|
||
#code-wrapper{ | ||
display: none; | ||
} | ||
#code-toggle:active{ | ||
border-bottom: 2px solid #333; | ||
border-right: 2px solid #333; | ||
} | ||
</style> | ||
<span id="code-toggle" onclick="toggleCodeWrapper()">Show code</span> | ||
<div id="code-wrapper" class="language-{{include.highlight}} highlighter-rouge"> | ||
<div class="highlight"> | ||
<pre class="highlight"><a class="ghLink" href="https://github.com/anoopkini/{{include.repo}}/blob/main/{{include.filepath}}" target="_blank">GitHub</a><a class="lcLink" href="{{include.lcurl}}" target="_blank">Leetcode</a><code><div id="code"></div></code></pre> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
--- | ||
title: 217. Contains Duplicates | ||
layout: post | ||
code_url: https://raw.githubusercontent.com/anoopkini/lc-problems/main/217-contains-duplicate.py | ||
#permalink: /217-contains-duplicates/ | ||
category: DataStructures | ||
--- | ||
|
||
{% include ghCodeBlock.html highlight="python" repo="lc-problems" filepath="217-contains-duplicate.py" leetcodeurl="https://leetcode.com/problems/contains-duplicate/" %} | ||
**Description:** *Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.* | ||
|
||
*Example 1:* | ||
|
||
Input : `nums = [1,2,3,1]` | ||
Output: `true` | ||
|
||
*Example 2:* | ||
|
||
Input : `nums = [1,2,3,4]` | ||
Output: `false` | ||
|
||
*Example 3:* | ||
|
||
Input: `nums = [1,1,1,3,3,4,3,2,4,2]` | ||
Output: `true` | ||
|
||
**Constraints:** | ||
|
||
- 1 <= nums.length <= 10<sup>5</sup> | ||
- -10<sup>9</sup> <= nums[i] <= 10<sup>9</sup> | ||
|
||
## Solution | ||
|
||
### *Brute Force*: | ||
|
||
- Compare every element of the array with other elements. Return true if two elements are same else return false. | ||
- Time: O(n<sup>2</sup>) | ||
- Space: O(1) -> no additional memory | ||
|
||
``` python | ||
for i in range(len(nums)-1): | ||
for j in range(i+1, len(nums)): | ||
if nums[i] == nums[j]: | ||
return True | ||
return False | ||
``` | ||
|
||
### *Revised Solution*: | ||
- Sort the array. Return true if two adjacent elements are same, else return false. | ||
- Time: O(n.log(n)) -> single loop of the array + time complexity of the sorting algo | ||
- Space: O(1) -> No additional memory | ||
|
||
``` python | ||
for i in range(len(sorted_nums)-1): | ||
for j in range(i+1, len(sorted_nums)): | ||
if sorted_nums[i] == sorted_nums[j]: | ||
return True | ||
return False | ||
``` | ||
|
||
### *Optimal Solution*: | ||
- As you are looping through the array add it to a hash set. | ||
- Time: O(n) -> single loop of the array | ||
- Space: O(n) -> space occupied by hastset | ||
|
||
{% include ghCodeBlock.html highlight="python" repo="lc-problems" filepath="217-contains-duplicate.py" lcurl="https://leetcode.com/problems/contains-duplicate/" %} |