Skip to content

Commit 03dcd7a

Browse files
authored
IOS-26 Add solutions to 1004, 1051, 1456, 1493 problems (#36)
* IOS-26 Add 1456 problem solution 1456. Maximum Number of Vowels in a Substring of Given Length * IOS-26 Add 1004 problem solution 1004. Max Consecutive Ones III * IOS-26 Add 1493 problem solution 1493. Longest Subarray of 1's After Deleting One Element * IOS-26 Add 1051 problem soultion 1051. Height Checker
1 parent 66325b2 commit 03dcd7a

File tree

9 files changed

+411
-0
lines changed

9 files changed

+411
-0
lines changed

LeetCode.xcodeproj/project.pbxproj

+56
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666
498B5C502C1062F700B2DC79 /* StringCompression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498B5C4E2C1062F700B2DC79 /* StringCompression.swift */; };
6767
498B5C532C107BC600B2DC79 /* FindCommonCharacters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498B5C522C107BC600B2DC79 /* FindCommonCharacters.swift */; };
6868
498B5C542C107BC600B2DC79 /* FindCommonCharacters.swift in Sources */ = {isa = PBXBuildFile; fileRef = 498B5C522C107BC600B2DC79 /* FindCommonCharacters.swift */; };
69+
4992C5242C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C5232C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift */; };
70+
4992C5252C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C5232C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift */; };
71+
4992C5292C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C5282C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift */; };
72+
4992C52A2C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C5282C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift */; };
73+
4992C52D2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C52C2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift */; };
74+
4992C52E2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C52C2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift */; };
75+
4992C5312C170E9900E16FCF /* HeightChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C5302C170E9900E16FCF /* HeightChecker.swift */; };
76+
4992C5322C170E9900E16FCF /* HeightChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4992C5302C170E9900E16FCF /* HeightChecker.swift */; };
6977
499763162BFF8181009D6EF5 /* BenchTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495D40482BFC94DC00ECFC81 /* BenchTimer.swift */; };
7078
499763172BFF8181009D6EF5 /* TreeNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495D404C2BFC960B00ECFC81 /* TreeNode.swift */; };
7179
499763182BFF8181009D6EF5 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495D404E2BFC965F00ECFC81 /* Node.swift */; };
@@ -178,6 +186,10 @@
178186
498B5C4A2C1061DE00B2DC79 /* IncreasingTripletSubsequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IncreasingTripletSubsequence.swift; sourceTree = "<group>"; };
179187
498B5C4E2C1062F700B2DC79 /* StringCompression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringCompression.swift; sourceTree = "<group>"; };
180188
498B5C522C107BC600B2DC79 /* FindCommonCharacters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindCommonCharacters.swift; sourceTree = "<group>"; };
189+
4992C5232C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaximumNumberOfVowelsInSubstringOfGivenLength.swift; sourceTree = "<group>"; };
190+
4992C5282C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaxConsecutiveOnesIII.swift; sourceTree = "<group>"; };
191+
4992C52C2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongestSubarrayOf1sAfterDeletingOneElement.swift; sourceTree = "<group>"; };
192+
4992C5302C170E9900E16FCF /* HeightChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeightChecker.swift; sourceTree = "<group>"; };
181193
499762E92BFF8031009D6EF5 /* LeetCode75 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = LeetCode75; sourceTree = BUILT_PRODUCTS_DIR; };
182194
499762EB2BFF8031009D6EF5 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
183195
499762F42BFF809E009D6EF5 /* LeetCode75_UnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LeetCode75_UnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -272,6 +284,7 @@
272284
49EC4CBB2C06082900D68CF9 /* 876. Middle of the Linked List */,
273285
49EC4CC12C06096800D68CF9 /* 949. Largest Time for Given Digits */,
274286
498B5C512C107BB500B2DC79 /* 1002. Find Common Characters */,
287+
4992C52F2C170E8B00E16FCF /* 1051. Height Checker */,
275288
49EC4CC32C06096800D68CF9 /* 1480. Running Sum of 1d Array */,
276289
);
277290
path = Easy;
@@ -510,6 +523,38 @@
510523
path = "1002. Find Common Characters";
511524
sourceTree = "<group>";
512525
};
526+
4992C5222C14AB7300E16FCF /* 1456. Maximum Number of Vowels in a Substring of Given Length */ = {
527+
isa = PBXGroup;
528+
children = (
529+
4992C5232C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift */,
530+
);
531+
path = "1456. Maximum Number of Vowels in a Substring of Given Length";
532+
sourceTree = "<group>";
533+
};
534+
4992C5262C16FD9900E16FCF /* 1004. Max Consecutive Ones III */ = {
535+
isa = PBXGroup;
536+
children = (
537+
4992C5282C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift */,
538+
);
539+
path = "1004. Max Consecutive Ones III";
540+
sourceTree = "<group>";
541+
};
542+
4992C52B2C16FF0000E16FCF /* 1493. Longest Subarray of 1's After Deleting One Element */ = {
543+
isa = PBXGroup;
544+
children = (
545+
4992C52C2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift */,
546+
);
547+
path = "1493. Longest Subarray of 1's After Deleting One Element ";
548+
sourceTree = "<group>";
549+
};
550+
4992C52F2C170E8B00E16FCF /* 1051. Height Checker */ = {
551+
isa = PBXGroup;
552+
children = (
553+
4992C5302C170E9900E16FCF /* HeightChecker.swift */,
554+
);
555+
path = "1051. Height Checker";
556+
sourceTree = "<group>";
557+
};
513558
499762EA2BFF8031009D6EF5 /* LeetCode75 */ = {
514559
isa = PBXGroup;
515560
children = (
@@ -565,6 +610,9 @@
565610
49B703632C0F106C0005B3D4 /* 238. Product of Array Except Self */,
566611
498B5C492C1061BB00B2DC79 /* 334. Increasing Triplet Subsequence */,
567612
498B5C4D2C1062E800B2DC79 /* 443. String Compression */,
613+
4992C5262C16FD9900E16FCF /* 1004. Max Consecutive Ones III */,
614+
4992C5222C14AB7300E16FCF /* 1456. Maximum Number of Vowels in a Substring of Given Length */,
615+
4992C52B2C16FF0000E16FCF /* 1493. Longest Subarray of 1's After Deleting One Element */,
568616
49450F762C11828B0036A167 /* 1679. Max Number of K-Sum Pairs */,
569617
);
570618
path = Medium;
@@ -907,6 +955,7 @@
907955
49450F802C11AE370036A167 /* LongTestCases.swift in Sources */,
908956
49E202562C04E623005D75E9 /* NAryTreePreorderTraversal.swift in Sources */,
909957
49EC4CC42C06096900D68CF9 /* LargestTimeForGivenDigits.swift in Sources */,
958+
4992C5312C170E9900E16FCF /* HeightChecker.swift in Sources */,
910959
);
911960
runOnlyForDeploymentPostprocessing = 0;
912961
};
@@ -926,6 +975,7 @@
926975
49E202592C04E623005D75E9 /* LongestPalindrome.swift in Sources */,
927976
495D40FE2BFCBBBE00ECFC81 /* BenchTimer.swift in Sources */,
928977
495511A02C12FCBB004BCD04 /* ReplaceWords.swift in Sources */,
978+
4992C5322C170E9900E16FCF /* HeightChecker.swift in Sources */,
929979
49450F812C11AE370036A167 /* LongTestCases.swift in Sources */,
930980
495D40FF2BFCBBBE00ECFC81 /* TreeNode.swift in Sources */,
931981
490BB3652C0F441B0026F70C /* MaximizeDistanceToClosestPerson.swift in Sources */,
@@ -961,13 +1011,15 @@
9611011
49EC4CF62C06115C00D68CF9 /* MergeStringsAlternately.swift in Sources */,
9621012
49B703662C0F10830005B3D4 /* ProductOfArrayExceptSelf.swift in Sources */,
9631013
498B5C4F2C1062F700B2DC79 /* StringCompression.swift in Sources */,
1014+
4992C52D2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift in Sources */,
9641015
4955119B2C12FC87004BCD04 /* TrieNode.swift in Sources */,
9651016
49EC4CD62C060E0D00D68CF9 /* IsSubsequence.swift in Sources */,
9661017
4973991A2C0E0DFC003031AA /* SingleNumber.swift in Sources */,
9671018
498B5C4B2C1061DE00B2DC79 /* IncreasingTripletSubsequence.swift in Sources */,
9681019
49B331292C09AE6600BC47D8 /* NthTribonacciNumber.swift in Sources */,
9691020
499763192BFF8181009D6EF5 /* ListNode.swift in Sources */,
9701021
49EC4CEE2C06102E00D68CF9 /* GreatestCommonDivisorOfStrings.swift in Sources */,
1022+
4992C5292C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift in Sources */,
9711023
49EC4CEC2C06102E00D68CF9 /* KidsWithTheGreatestNumberOfCandies.swift in Sources */,
9721024
49DA51162C08B1D30062A064 /* GuessNumberHigherOrLower.swift in Sources */,
9731025
49450F782C1182A70036A167 /* MaxNumberOfKSumPairs.swift in Sources */,
@@ -978,6 +1030,7 @@
9781030
49450F742C117FE90036A167 /* ContainerWithMostWater.swift in Sources */,
9791031
490BD5822C0721AC003AE043 /* MaximumDepthOfBinaryTree.swift in Sources */,
9801032
49B703612C0EFAAF0005B3D4 /* ReverseWordsInString.swift in Sources */,
1033+
4992C5242C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift in Sources */,
9811034
49EC4CE22C060F1F00D68CF9 /* MaximumAverageSubarrayI.swift in Sources */,
9821035
);
9831036
runOnlyForDeploymentPostprocessing = 0;
@@ -1002,19 +1055,22 @@
10021055
49EC4CFB2C06115C00D68CF9 /* FindTheDifferenceOfTwoArrays.swift in Sources */,
10031056
4973991B2C0E0DFC003031AA /* SingleNumber.swift in Sources */,
10041057
490119312C087DD0009ADEC2 /* SearchInBinarySearchTree.swift in Sources */,
1058+
4992C52A2C16FDAC00E16FCF /* MaxConsecutiveOnesIII.swift in Sources */,
10051059
4997631D2BFF8181009D6EF5 /* ListNode.swift in Sources */,
10061060
49450F832C11AE370036A167 /* LongTestCases.swift in Sources */,
10071061
49EC4CF92C06115C00D68CF9 /* FindTheHighestAltitude.swift in Sources */,
10081062
4955119C2C12FC87004BCD04 /* TrieNode.swift in Sources */,
10091063
49EC4CCD2C060CAA00D68CF9 /* ReverseLinkedList.swift in Sources */,
10101064
4912C7AE2C09CBBA00DEB016 /* MinCostClimbingStairs.swift in Sources */,
1065+
4992C52E2C16FF2300E16FCF /* LongestSubarrayOf1sAfterDeletingOneElement.swift in Sources */,
10111066
490BD5832C0721AC003AE043 /* MaximumDepthOfBinaryTree.swift in Sources */,
10121067
490BD57F2C071DB7003AE043 /* NumberOfRecentCalls.swift in Sources */,
10131068
49DA51172C08B1D30062A064 /* GuessNumberHigherOrLower.swift in Sources */,
10141069
49B3312A2C09AE6600BC47D8 /* NthTribonacciNumber.swift in Sources */,
10151070
49450F792C1182A70036A167 /* MaxNumberOfKSumPairs.swift in Sources */,
10161071
497399172C0DBA1A003031AA /* CountingBits.swift in Sources */,
10171072
49EC4CF72C06115C00D68CF9 /* MergeStringsAlternately.swift in Sources */,
1073+
4992C5252C14AB8900E16FCF /* MaximumNumberOfVowelsInSubstringOfGivenLength.swift in Sources */,
10181074
498B5C4C2C1061DE00B2DC79 /* IncreasingTripletSubsequence.swift in Sources */,
10191075
49450F752C117FE90036A167 /* ContainerWithMostWater.swift in Sources */,
10201076
498B5C502C1062F700B2DC79 /* StringCompression.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// HeightChecker.swift
3+
// LeetCode
4+
//
5+
// Created by Artem Mayer on 10.06.2024.
6+
//
7+
8+
import Foundation
9+
10+
// MARK: - 1051. Height Checker
11+
12+
/// A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.
13+
///
14+
/// You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).
15+
///
16+
/// Return the number of indices where heights[i] != expected[i].
17+
///
18+
///
19+
/// **Example 1:**
20+
/// ```
21+
/// Input: heights = [1,1,4,2,1,3]
22+
/// Output: 3
23+
/// Explanation:
24+
/// heights: [1,1,4,2,1,3]
25+
/// expected: [1,1,1,2,3,4]
26+
/// Indices 2, 4, and 5 do not match.
27+
/// ```
28+
///
29+
/// **Example 2:**
30+
/// ```
31+
/// Input: heights = [5,1,2,3,4]
32+
/// Output: 5
33+
/// Explanation:
34+
/// heights: [5,1,2,3,4]
35+
/// expected: [1,2,3,4,5]
36+
/// All indices do not match.
37+
/// ```
38+
///
39+
/// **Example 3:**
40+
/// ```
41+
/// Input: heights = [1,2,3,4,5]
42+
/// Output: 0
43+
/// Explanation:
44+
/// heights: [1,2,3,4,5]
45+
/// expected: [1,2,3,4,5]
46+
/// All indices match.
47+
/// ```
48+
///
49+
/// **Constraints:**
50+
/// ```
51+
/// 1 <= heights.length <= 100
52+
/// 1 <= heights[i] <= 100
53+
/// ```
54+
final class HeightChecker_Solution {
55+
56+
func heightChecker(_ heights: [Int]) -> Int {
57+
heights.sorted()
58+
.enumerated()
59+
.reduce(into: 0, { if $1.element != heights[$1.offset] { $0 += 1 } })
60+
}
61+
62+
}

LeetCode100/main.swift

+7
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ final class Solution {
112112
return solution.commonChars(words)
113113
}
114114

115+
// MARK: - 1051. Height Checker
116+
117+
func heightChecker(_ heights: [Int]) -> Int {
118+
let solution = HeightChecker_Solution()
119+
return solution.heightChecker(heights)
120+
}
121+
115122
// MARK: - 1480. Running Sum of 1d Array
116123

117124
func runningSumOf1dArray(_ nums: [Int]) -> [Int] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// MaxConsecutiveOnesIII.swift
3+
// LeetCode
4+
//
5+
// Created by Artem Mayer on 10.06.2024.
6+
//
7+
8+
import Foundation
9+
10+
// MARK: - 1004. Max Consecutive Ones III
11+
12+
/// Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
13+
///
14+
///
15+
/// **Example 1:**
16+
/// ```
17+
/// Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
18+
/// Output: 6
19+
/// Explanation: [1,1,1,0,0,1,1,1,1,1,1]
20+
/// Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
21+
/// ```
22+
///
23+
/// **Example 2:**
24+
/// ```
25+
/// Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
26+
/// Output: 10
27+
/// Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
28+
/// Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
29+
/// ```
30+
///
31+
/// **Constraints:**
32+
/// ```
33+
/// 1 <= nums.length <= 10⁵
34+
/// nums[i] is either 0 or 1.
35+
/// 0 <= k <= nums.length
36+
/// ```
37+
final class MaxConsecutiveOnesIII_Solution {
38+
39+
func longestOnes(_ nums: [Int], _ k: Int) -> Int {
40+
var k = k
41+
var l = 0, r = 0
42+
43+
while r < nums.count {
44+
if nums[r] == 0 {
45+
k -= 1
46+
}
47+
48+
if k < 0 {
49+
k += 1 - nums[l]
50+
l += 1
51+
}
52+
53+
r += 1
54+
}
55+
56+
return r - l
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// MaximumNumberOfVowelsInSubstringOfGivenLength.swift
3+
// LeetCode
4+
//
5+
// Created by Artem Mayer on 08.06.2024.
6+
//
7+
8+
import Foundation
9+
10+
// MARK: - 1456. Maximum Number of Vowels in a Substring of Given Length
11+
12+
/// Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.
13+
///
14+
/// Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
15+
///
16+
///
17+
/// **Example 1:**
18+
/// ```
19+
/// Input: s = "abciiidef", k = 3
20+
/// Output: 3
21+
/// Explanation: The substring "iii" contains 3 vowel letters.
22+
/// ```
23+
///
24+
/// **Example 2:**
25+
/// ```
26+
/// Input: s = "aeiou", k = 2
27+
/// Output: 2
28+
/// Explanation: Any substring of length 2 contains 2 vowels.
29+
/// ```
30+
///
31+
/// **Example 3:**
32+
/// ```
33+
/// Input: s = "leetcode", k = 3
34+
/// Output: 2
35+
/// Explanation: "lee", "eet" and "ode" contain 2 vowels.
36+
/// ```
37+
///
38+
/// **Constraints:**
39+
/// ```
40+
/// 1 <= s.length <= 10⁵
41+
/// s consists of lowercase English letters.
42+
/// 1 <= k <= s.length
43+
/// ```
44+
final class MaximumNumberOfVowelsInSubstringOfGivenLength_Soluton {
45+
46+
func maxVowels(_ s: String, _ k: Int) -> Int {
47+
var res = s[s.startIndex..<s.index(s.startIndex, offsetBy: k)].utf8.reduce(0, { $0 + $1.vowelCount })
48+
49+
s.utf8.withContiguousStorageIfAvailable {
50+
var cur = res
51+
52+
for i in k..<$0.count {
53+
cur -= $0[i - k].vowelCount
54+
cur += $0[i].vowelCount
55+
res = max(res, cur)
56+
}
57+
}
58+
59+
return res
60+
}
61+
62+
}
63+
64+
extension String.UTF8View.Element {
65+
66+
/// 'a', 'e', 'i', 'o', and 'u''
67+
var vowelCount: Int {
68+
switch self {
69+
case 97, 101, 105, 111, 117: return 1
70+
default: return 0
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)