issues
search
2024-TEAM-05
/
algorithm-for-kakao
μΉ΄μΉ΄μ€ κΈ°μΆ λ¬Έμ κ°μ¦μπ£
0
stars
0
forks
source link
[λ°±μ€] μΌμ
#52
Open
hye-on
opened
3 weeks ago
hye-on
commented
3 weeks ago
π
μΌμ
hye-on
commented
2 weeks ago
π λκΈ ν νλ¦Ώ
Language : C++
μ±λ₯
μ½λ νμ΄
```cpp #include
#include
#include
using namespace std; //10:25 int n, k; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; cin >> k; vector
v; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { v[i - 1] = v[i] - v[i - 1]; } v[n - 1] = 0; sort(v.begin(), v.end()); int ans = 0; //2 3 0 1 2 // 0 1 2 2 3 - 3μ λΊ for (int i = 0; i < n - k+1; i++) ans += v[i]; cout << ans; return 0; } ```
μ½λ©νΈ
- μ²μμ μ΄λ €μ λλ° λΊ κ±°λ¦¬λ₯Ό μκ°νλκΉ μ’ κ°λ¨ν΄μ§ κ² κ°μ΅λλ€.
uijin-j
commented
2 weeks ago
π λκΈ ν νλ¦Ώ
Language : Java
μ±λ₯
μ½λ νμ΄
```java import java.io.*; import java.util.*; /** * 11:03 */ public class Main { /** * 1 3 66 7 9 * 3 6 7 8 10 12 14 15 18 20 */ public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); int k = Integer.parseInt(bf.readLine()); int[] sensors = new int[n]; StringTokenizer st = new StringTokenizer(bf.readLine()); for(int i = 0; i < n; ++i) { sensors[i] = Integer.parseInt(st.nextToken()); } Arrays.sort(sensors); int[] gaps = new int[n-1]; for(int i = 1; i < n; ++i) { gaps[i-1] = sensors[i] - sensors[i-1]; } Arrays.sort(gaps); int total = sensors[n-1] - sensors[0]; for(int i = n - 2; i >= 0; --i) { if(k == 1) break; total -= gaps[i]; k--; } System.out.println(total); } } ```
μ½λ©νΈ
- 그리λλ‘ νμμ΅λλ€! (μ 체 μΌμ λ²μμμ K-1κ°μ κ°μ λΊλ€)
π μΌμ